• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

python 搞代码 4年前 (2022-01-09) 71次浏览 已收录 0个评论
文章目录[隐藏]

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<b style="color:transparent">本文来源gao@!dai!ma.com搞$$代^@码网*</b>_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

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python工具——Mimesis的简单使用教程
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址