python工具——Mimesis的简单使用教程
作者:慕尘 发布时间:2021-08-06 07:27:23
标签:python,工具,Mimesis,教程
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}')
结果
查看 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
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": "awatch1835@test.com",
"token": "b352bcc3c446650c2682bfc09a068acc4d0b60583cbb0e241f7fd44d02e43d89",
"creator": "乐轩 乌"
},
"address": {
"country": "中華民國",
"province": "陕西省",
"city": "黄石市"
}
}
]
文档 https://mimesis.readthedocs.io/api.html
来源:https://www.cnblogs.com/baby123/p/14270661.html
0
投稿
猜你喜欢
- 网上广泛流传的取 object 的绝对位置的做法是:var getAbsPos = function(pTar
- 前言本文通过使用 cpu 版本的 tensorflow 2.4 ,介绍三种方式进行加载和预处理图片数据。这里我们要确保 tensorflow
- 本文为大家分享了Navicat for SQLite导入csv中文数据的具体方法,供大家参考,具体内容如下1.用Notepad++打开csv
- 通常情况下,当我们要生成一个随机字符串时,总是先创建一个字符池,然后用一个循环和mt_rand()或rand()生成php随机数,从字符池中
- Python全局锁(1)全局锁导致的问题全局锁的英文简称是GIL,全称是Global Interpreter Lock(全局解释器锁),来源
- pyd文件生成安装easycython库pip install easycythontest.pydef test(): pri
- Django根据已有数据库表反向生成models类一. 创建一个Django项目django-admin startproject ‘xxx
- 我们先看一下相关数据结构的知识。 在学习线性表的时候,曾有这样一个例题。 已知一个存储整数的顺序表La,试构造顺序表Lb,要求顺序表Lb中只
- 前言你知道a**3是什么意思吗?你知道怎么打印模块路径吗?你知道reduce()函数怎么用和列表怎么拼接字符串吗?然而你可能全都知道但是还是
- 本文实例讲述了Python导出DBF文件到Excel的方法。分享给大家供大家参考。具体如下:from dbfpy import dbffro
- mouseDown事件和mouseUp事件 大家知道,mouseDown事件和mouseUp事件的组合就是click事件,但是如果在链接上按
- 本文实例讲述了Python3.5编程实现修改IIS WEB.CONFIG的方法。分享给大家供大家参考,具体如下:#!/usr/bin/env
- MySQL创建用户并授权及撤销用户权限运行环境:MySQL5.0一、创建用户命令:CREATE USER 'username'
- pycharm 2020.1.2激活工具下载以及破解方法 免费可用至2089年需要提前安装好IDEA,官网下载地址:https://www.
- 解决vscode终端输出中文乱码问题,强推方法二,少走几年弯路解决网上终端chcp65001仍然无效,或者vscode由utf-8改为GBK
- 1)按笔画排序 select * from Table order by nlssort(columnName,'NLS_SORT=
- 之前编写Python更多的是使用pycharm作为编译器进行开发,但是个人感觉用起来比较笨重,而且还收费的,需要进行破解才能使用。后来发现v
- 这篇文章主要介绍了Python for循环通过序列索引迭代过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价
- 一、同步原理基于Mysql的binlog日志订阅:binlog日志是Mysql用来记录数据实时的变化Mysql数据同步到ES中分为两种,分别
- MD5(Message-Digest Algorithm 5) 模块用于计算信息密文(信息摘要),得出一个128位的密文。sha模块跟md5