python工具——Mimesis的簡(jiǎn)單使用教程
Mimesis是一個(gè)用于Python的高性能偽數(shù)據(jù)生成器, 支持多種不同的語(yǔ)言
可以用來生成各種測(cè)試數(shù)據(jù)、假的 API 、任意結(jié)構(gòu)的 JSON 、XML 數(shù)據(jù)
安裝
pip install mimesis
示例
from mimesis import Person
person = Person('zh')
print(f'name: {person.surname() + "" + person.name()}')
print(f'sex: {person.sex()}')
print(f'academic degree: {person.academic_degree()}')
print(f'occupation: {person.occupation()}')
email = person.email(domains=['126.com'])
print(f'email: {email}')
phone = person.telephone(mask='132-8###-5##3')
print(f'telephone: {phone}')
結(jié)果

查看 Person 對(duì)象里面都有什么數(shù)據(jù)
from mimesis import Person
from pprint import pprint
person = Person('zh')
pprint(vars(person))
數(shù)據(jù)結(jié)構(gòu)
{'_data': {'academic_degree': ['學(xué)士', '研究生', '博士'],
'gender': ['男性', '女性'],
'language': ['南非語(yǔ)',
……
'中文',
'祖魯語(yǔ)'],
'names': {'female': ['朵雯',
……
'若未'],
'male': ['彥龍',
……
'清妍']},
'nationality': ['阿爾及利亞',
……
'南喬治亞島和南桑威奇群島'],
'occupation': ['民意代表',
……
'職業(yè)運(yùn)動(dòng)員'],
'political_views': ['社會(huì)主義', '民主', '共産'],
'sexuality': ['異性戀', '同性戀', '雙性戀', '無性戀'],
'surnames': ['趙',
……
'司空'],
'telephone_fmt': ['+86 ###-########'],
'title': {'female': {'academic': ['工學(xué)碩士',
……
'教授'],
'typical': ['小姐', '女士']},
'male': {'academic': ['工學(xué)碩士',
……
'教授'],
'typical': ['先生']}},
'university': ['北京大學(xué)',
……
'新疆工業(yè)職業(yè)技術(shù)學(xué)'],
'views_on': ['負(fù)面', '正面', '中立'],
'worldview': ['無神論', '不可知論', '自然神論', '泛神論', '儒教']},
'_data_dir': WindowsPath('D:/Python37/lib/site-packages/mimesis/data'),
'_datafile': 'person.json',
'_store': {'age': 0},
'locale': 'zh',
'random': <mimesis.random.Random object at 0x0000000002A41EA8>,
'seed': None}
除了Person ,還有 food、 address、transport、Business 等對(duì)象提供的相應(yīng)假數(shù)據(jù)
生成json數(shù)據(jù)
eg:
data.py
from mimesis.schema import Field,Schema
from mimesis.enums import Gender
_ = Field('zh')
schema = Schema(schema=lambda: {
'id': _('uuid'),
'name': _('person.name'),
'version': _('version', pre_release=True),
'timestamp': _('timestamp', posix=False),
'owner': {
'email': _('person.email', domains=['test.com'], key=str.lower),
'token': _('token_hex'),
'creator': _('full_name', gender=Gender.FEMALE)
},
'address': {
'country': _('address.country'),
'province': _('address.province'),
'city': _('address.city')
}
})
使用FastAPI
from fastapi import FastAPI
from data import schema
app = FastAPI()
@app.get("/")
def home():
# 生成數(shù)據(jù)
testData = schema.create(iterations=2)
return testData
運(yùn)行
uvicorn main:app
訪問http://127.0.0.1:8000/
結(jié)果
[
{
"id": "aebd4f31-3dfe-4c9d-a3e9-ef3a0b88007a",
"name": "江燕",
"version": "1.8.3-rc.1",
"timestamp": "2020-05-08T22:25:47Z",
"owner": {
"email": "boobies1874@test.com",
"token": "136bfa9e7771842dae3758de2cf5997f0fcfd39b56b6063f11e6123638e7d951",
"creator": "襲韻 歐"
},
"address": {
"country": "中華民國(guó)",
"province": "青海省",
"city": "開封市"
}
},
{
"id": "69ed6ad2-5430-4627-ab36-73c2df4a9ca2",
"name": "綿恩",
"version": "4.3.4-alpha.2",
"timestamp": "2001-11-12T15:29:39Z",
"owner": {
"email": "awatch1835@test.com",
"token": "b352bcc3c446650c2682bfc09a068acc4d0b60583cbb0e241f7fd44d02e43d89",
"creator": "樂軒 烏"
},
"address": {
"country": "中華民國(guó)",
"province": "陜西省",
"city": "黃石市"
}
}
]
文檔 https://mimesis.readthedocs.io/api.html
以上就是python工具——Mimesis的簡(jiǎn)單使用教程的詳細(xì)內(nèi)容,更多關(guān)于python Mimesis的使用教程的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Python腳本調(diào)試工具安裝過程
- 詳解python使用金山詞霸的翻譯功能(調(diào)試工具斷點(diǎn)的使用)
- python 實(shí)現(xiàn)全球IP歸屬地查詢工具
- python可視化 matplotlib畫圖使用colorbar工具自定義顏色
- Python代碼覆蓋率統(tǒng)計(jì)工具coverage.py用法詳解
- python 實(shí)用工具狀態(tài)機(jī)transitions
- Python大批量搜索引擎圖像爬蟲工具詳解
- python如何編寫類似nmap的掃描工具
- Python Pandas數(shù)據(jù)分析工具用法實(shí)例
- python性能測(cè)試工具locust的使用
- Python+unittest+requests+excel實(shí)現(xiàn)接口自動(dòng)化測(cè)試框架
- python 實(shí)現(xiàn)ping測(cè)試延遲的兩種方法
相關(guān)文章
Python Faker批量生成測(cè)試數(shù)據(jù)的實(shí)現(xiàn)
本文主要介紹了Python Faker批量生成測(cè)試數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
Python報(bào)錯(cuò):TypeError:?‘xxx‘?object?is?not?subscriptable解決
這篇文章主要給大家介紹了關(guān)于Python報(bào)錯(cuò):TypeError:?‘xxx‘?object?is?not?subscriptable的解決辦法,TypeError是Python中的一種錯(cuò)誤,表示操作或函數(shù)應(yīng)用于不合適類型的對(duì)象時(shí)發(fā)生,文中將解決辦法介紹的非常詳細(xì),需要的朋友可以參考下2024-08-08
python?opencv的imread方法無法讀取圖片問題
這篇文章主要介紹了python?opencv的imread方法無法讀取圖片問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
Python實(shí)現(xiàn)半角轉(zhuǎn)全角的方法示例
本文介紹了使用Python實(shí)現(xiàn)半角字符到全角字符的轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
Python關(guān)于拓?fù)渑判蛑R(shí)點(diǎn)講解
在本篇文章里小編給大家分享了一篇關(guān)于Python關(guān)于拓?fù)渑判蛑R(shí)點(diǎn)講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-01-01
Python中的標(biāo)簽編碼和獨(dú)熱編碼示例詳解
標(biāo)簽編碼是一種用于將分類列轉(zhuǎn)換為數(shù)值列的技術(shù),以便它們可以通過僅采用數(shù)值數(shù)據(jù)的機(jī)器學(xué)習(xí)模型進(jìn)行擬合,這篇文章主要介紹了Python中的標(biāo)簽編碼和獨(dú)熱編碼,需要的朋友可以參考下2023-07-07

