本篇文章给大家分享的是有关Mimesis工具怎么在python项目中使用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
Mimesis是一个用于Python的高性能伪数据生成器, 支持多种不同的语言
可以用来生成各种测试数据、假的 API 、任意结构的 JSON 、XML 数据
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}')
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 等对象提供的相应假数据
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():
# 生成数据
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": "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": "awatch2835@test.com",
"token": "b352bcc3c446650c2682bfc09a068acc4d0b60583cbb0e241f7fd44d02e43d89",
"creator": "乐轩 乌"
},
"address": {
"country": "中華民國",
"province": "陕西省",
"city": "黄石市"
}
}
]
以上就是Mimesis工具怎么在python项目中使用,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。