python工具——Mimesis的簡(jiǎn)單使用教程
Mimesis是一個(gè)用于Python的高性能偽數(shù)據(jù)生成器, 支持多種不同的語(yǔ)言
可以用來(lái)生成各種測(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': ['異性戀', '同性戀', '雙性戀', '無(wú)性戀'],
'surnames': ['趙',
……
'司空'],
'telephone_fmt': ['+86 ###-########'],
'title': {'female': {'academic': ['工學(xué)碩士',
……
'教授'],
'typical': ['小姐', '女士']},
'male': {'academic': ['工學(xué)碩士',
……
'教授'],
'typical': ['先生']}},
'university': ['北京大學(xué)',
……
'新疆工業(yè)職業(yè)技術(shù)學(xué)'],
'views_on': ['負(fù)面', '正面', '中立'],
'worldview': ['無(wú)神論', '不可知論', '自然神論', '泛神論', '儒教']},
'_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
訪(fǎng)問(wèn)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": "開(kāi)封市"
}
},
{
"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": "樂(lè)軒 烏"
},
"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)試工具安裝過(guò)程
- 詳解python使用金山詞霸的翻譯功能(調(diào)試工具斷點(diǎn)的使用)
- python 實(shí)現(xiàn)全球IP歸屬地查詢(xún)工具
- python可視化 matplotlib畫(huà)圖使用colorbar工具自定義顏色
- Python代碼覆蓋率統(tǒng)計(jì)工具coverage.py用法詳解
- python 實(shí)用工具狀態(tài)機(jī)transitions
- Python大批量搜索引擎圖像爬蟲(chóng)工具詳解
- python如何編寫(xiě)類(lèi)似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完成 Saga 分布式事務(wù)
這篇文章主要介紹了如何利用 Python 完成一個(gè) Saga 的分布式事務(wù),需要的朋友可以參考下面文章具體的內(nèi)容2021-09-09
python實(shí)現(xiàn)從網(wǎng)絡(luò)下載文件并獲得文件大小及類(lèi)型的方法
這篇文章主要介紹了python實(shí)現(xiàn)從網(wǎng)絡(luò)下載文件并獲得文件大小及類(lèi)型的方法,涉及Python操作網(wǎng)絡(luò)文件的相關(guān)技巧,需要的朋友可以參考下2015-04-04
python實(shí)現(xiàn)將列表中各個(gè)值快速賦值給多個(gè)變量
這篇文章主要介紹了python實(shí)現(xiàn)將列表中各個(gè)值快速賦值給多個(gè)變量,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
pycharm配置SSH遠(yuǎn)程連接服務(wù)器詳細(xì)步驟(0基礎(chǔ)詳細(xì)版)
PyCharm是一款流行的Python集成開(kāi)發(fā)環(huán)境(IDE),提供了遠(yuǎn)程連接云服務(wù)器的功能,使得開(kāi)發(fā)者可以更加便捷地進(jìn)行遠(yuǎn)程開(kāi)發(fā)和調(diào)試,這篇文章主要給大家介紹了關(guān)于pycharm配置SSH遠(yuǎn)程連接服務(wù)器的詳細(xì)步驟,需要的朋友可以參考下2024-07-07
python里glob模塊知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于python里glob模塊知識(shí)點(diǎn)總結(jié),有需要的朋友們可以參考下。2021-01-01
Python實(shí)現(xiàn)二叉排序樹(shù)與平衡二叉樹(shù)的示例代碼
樹(shù)表查詢(xún)即借助具有特殊性質(zhì)的樹(shù)數(shù)據(jù)結(jié)構(gòu)進(jìn)行關(guān)鍵字查找,本文所涉及到的特殊結(jié)構(gòu)性質(zhì)的樹(shù)包括:二叉排序樹(shù)、平衡二叉樹(shù)。文中詳細(xì)介紹了二者的實(shí)現(xiàn)代碼,需要的可以參考一下2022-04-04
python 計(jì)算平均平方誤差(MSE)的實(shí)例
今天小編就為大家分享一篇python 計(jì)算平均平方誤差的實(shí)例 (MSE),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06

