python工具——Mimesis的簡單使用教程
Mimesis是一個用于Python的高性能偽數(shù)據(jù)生成器, 支持多種不同的語言
可以用來生成各種測試數(shù)據(jù)、假的 API 、任意結(jié)構(gòu)的 JSON 、XML 數(shù)據(jù)
安裝pip install mimesis示例
from mimesis import Personperson = 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é)果
from mimesis import Personfrom pprint import pprintperson = Person(’zh’)pprint(vars(person))數(shù)據(jù)結(jié)構(gòu)
{’_data’: {’academic_degree’: [’學(xué)士’, ’研究生’, ’博士’], ’gender’: [’男性’, ’女性’], ’language’: [’南非語’, …… ’中文’, ’祖魯語’], ’names’: {’female’: [’朵雯’,……’若未’], ’male’: [’彥龍’, …… ’清妍’]}, ’nationality’: [’阿爾及利亞’, …… ’南喬治亞島和南桑威奇群島’], ’occupation’: [’民意代表’, …… ’職業(yè)運(yùn)動員’], ’political_views’: [’社會主?’, ’民主’, ’共?’], ’sexuality’: [’異性戀’, ’同性戀’, ’雙性戀’, ’無性戀’], ’surnames’: [’趙’, …… ’司空’], ’telephone_fmt’: [’+86 ###-########’], ’title’: {’female’: {’academic’: [’工學(xué)碩士’, …… ’教授’],’typical’: [’小姐’, ’女士’]}, ’male’: {’academic’: [’工學(xué)碩士’, …… ’教授’], ’typical’: [’先生’]}}, ’university’: [’北京大學(xué)’, …… ’新疆工業(yè)職業(yè)技術(shù)學(xué)’], ’views_on’: [’?面’, ’正面’, ’中立’], ’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 等對象提供的相應(yīng)假數(shù)據(jù)
生成json數(shù)據(jù)eg:
data.py
from mimesis.schema import Field,Schemafrom 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 FastAPIfrom data import schemaapp = 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': '中?民??, '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': '中?民??, 'province': '陜西省', 'city': '黃石市' } }]
文檔 https://mimesis.readthedocs.io/api.html
以上就是python工具——Mimesis的簡單使用教程的詳細(xì)內(nèi)容,更多關(guān)于python Mimesis的使用教程的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. javascript設(shè)計(jì)模式 ? 建造者模式原理與應(yīng)用實(shí)例分析2. Docker 部署 Prometheus的安裝詳細(xì)教程3. Ajax引擎 ajax請求步驟詳細(xì)代碼4. IntelliJ IDEA設(shè)置條件斷點(diǎn)的方法步驟5. JavaScript Tab菜單實(shí)現(xiàn)過程解析6. ThinkPHP5 通過ajax插入圖片并實(shí)時顯示(完整代碼)7. Python使用oslo.vmware管理ESXI虛擬機(jī)的示例參考8. 解析使用useDark(),發(fā)現(xiàn)transition 動畫失效9. javascript xml xsl取值及數(shù)據(jù)修改第1/2頁10. ASP中實(shí)現(xiàn)字符部位類似.NET里String對象的PadLeft和PadRight函數(shù)
