python工具——Mimesis的簡單使用教程
Mimesis是一個用于Python的高性能偽數(shù)據(jù)生成器, 支持多種不同的語言
可以用來生成各種測試數(shù)據(jù)、假的 API 、任意結(jié)構的 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 對象里面都有什么數(shù)據(jù)
from mimesis import Person from pprint import pprint person = Person('zh') pprint(vars(person))
數(shù)據(jù)結(jié)構
{'_data': {'academic_degree': ['學士', '研究生', '博士'], 'gender': ['男性', '女性'], 'language': ['南非語', …… '中文', '祖魯語'], 'names': {'female': ['朵雯', …… '若未'], 'male': ['彥龍', …… '清妍']}, 'nationality': ['阿爾及利亞', …… '南喬治亞島和南桑威奇群島'], 'occupation': ['民意代表', …… '職業(yè)運動員'], 'political_views': ['社會主義', '民主', '共産'], 'sexuality': ['異性戀', '同性戀', '雙性戀', '無性戀'], 'surnames': ['趙', …… '司空'], 'telephone_fmt': ['+86 ###-########'], 'title': {'female': {'academic': ['工學碩士', …… '教授'], 'typical': ['小姐', '女士']}, 'male': {'academic': ['工學碩士', …… '教授'], 'typical': ['先生']}}, 'university': ['北京大學', …… '新疆工業(yè)職業(yè)技術學'], '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 等對象提供的相應假數(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
運行
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的簡單使用教程的詳細內(nèi)容,更多關于python Mimesis的使用教程的資料請關注腳本之家其它相關文章!
- Python腳本調(diào)試工具安裝過程
- 詳解python使用金山詞霸的翻譯功能(調(diào)試工具斷點的使用)
- python 實現(xiàn)全球IP歸屬地查詢工具
- python可視化 matplotlib畫圖使用colorbar工具自定義顏色
- Python代碼覆蓋率統(tǒng)計工具coverage.py用法詳解
- python 實用工具狀態(tài)機transitions
- Python大批量搜索引擎圖像爬蟲工具詳解
- python如何編寫類似nmap的掃描工具
- Python Pandas數(shù)據(jù)分析工具用法實例
- python性能測試工具locust的使用
- Python+unittest+requests+excel實現(xiàn)接口自動化測試框架
- python 實現(xiàn)ping測試延遲的兩種方法
相關文章
python實現(xiàn)從網(wǎng)絡下載文件并獲得文件大小及類型的方法
這篇文章主要介紹了python實現(xiàn)從網(wǎng)絡下載文件并獲得文件大小及類型的方法,涉及Python操作網(wǎng)絡文件的相關技巧,需要的朋友可以參考下2015-04-04python實現(xiàn)將列表中各個值快速賦值給多個變量
這篇文章主要介紹了python實現(xiàn)將列表中各個值快速賦值給多個變量,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04pycharm配置SSH遠程連接服務器詳細步驟(0基礎詳細版)
PyCharm是一款流行的Python集成開發(fā)環(huán)境(IDE),提供了遠程連接云服務器的功能,使得開發(fā)者可以更加便捷地進行遠程開發(fā)和調(diào)試,這篇文章主要給大家介紹了關于pycharm配置SSH遠程連接服務器的詳細步驟,需要的朋友可以參考下2024-07-07Python實現(xiàn)二叉排序樹與平衡二叉樹的示例代碼
樹表查詢即借助具有特殊性質(zhì)的樹數(shù)據(jù)結(jié)構進行關鍵字查找,本文所涉及到的特殊結(jié)構性質(zhì)的樹包括:二叉排序樹、平衡二叉樹。文中詳細介紹了二者的實現(xiàn)代碼,需要的可以參考一下2022-04-04