python工具——Mimesis的简单使用教程

2021-01-16 19:51:42 IT技术网 互联网
浏览

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

以上就是python工具——Mimesis的简单使用教程的详细内容,更多关于python Mimesis的使用教程的资料请关注IT技术网其它相关文章!

您可能感兴趣的文章:

  • Python脚本调试工具安装过程
  • 详解python使用金山词霸的翻译功能(调试工具断点的使用)
  • python 实现全球IP归属地查询工具
  • python可视化 matplotlib画图使用colorbar工具自定义颜色
  • Python代码覆盖率统计工具coverage.py用法详解
  • python 实用工具状态机transitions
  • Python大批量搜索引擎图像爬虫工具详解
  • python如何编写类似nmap的扫描工具
  • Python Pandas数据分析工具用法实例
  • python性能测试工具locust的使用
  • Python+unittest+requests+excel实现接口自动化测试框架
  • python 实现ping测试延迟的两种方法