Mimesis是一个用于Python的高性能伪数据生成器, 支持多种不同的语言
可以用来生成各种测试数据、假的 API 、任意结构的 JSON 、XML 数据
安装
pip install mimesis
eg:
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}')
结果
查看 Person 对象里面都有什么数据
from mimesis import Person from pprint import pprint person = Person('zh') pprint(vars(person))
数据结构
{'_data': {'academic_degree': ['学士', '研究生', '博士'], 'gender': ['男性', '女性'], 'language': ['南非语', …… '中文', '祖鲁语'], 'names': {'female': ['朵雯', …… '若未'], 'male': ['彦龙', …… '清妍']}, 'nationality': ['阿尔及利亚', …… '南乔治亚岛和南桑威奇群岛'], 'occupation': ['民意代表', …… '职业运动员'], 'political_views': ['社会主義', '民主', '共産'], 'sexuality': ['异性恋', '同性恋', '双性恋', '无性恋'], 'surnames': ['赵', …… '司空'], 'telephone_fmt': ['+86 ###-########'], 'title': {'female': {'academic': ['工学硕士', …… '教授'], 'typical': ['小姐', '女士']}, 'male': {'academic': ['工学硕士', …… '教授'], 'typical': ['先生']}}, 'university': ['北京大学', …… '新疆工业职业技术学'], '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 等对象提供的相应假数据
生成json数据
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(FastAPI)
from fastapi import FastAPI from data import schema app = FastAPI() @app.get("/") def home(): # 生成数据 testData = schema.create(iterations=2) return testData
运行
uvicorn main:app
访问http://127.0.0.1:8000/
结果
[ { "id": "aebd4f31-3dfe-4c9d-a3e9-ef3a0b88007a", "name": "江燕", "version": "1.8.3-rc.1", "timestamp": "2020-05-08T22:25:47Z", "owner": { "email": "[email protected]", "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": "[email protected]", "token": "b352bcc3c446650c2682bfc09a068acc4d0b60583cbb0e241f7fd44d02e43d89", "creator": "乐轩 乌" }, "address": { "country": "中華民國", "province": "陕西省", "city": "黄石市" } } ]
文档 IT虾米网
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/20434.html