LoginSignup
1
3

More than 5 years have passed since last update.

mimesis使ってみた

Last updated at Posted at 2017-08-29

mimesis

テストデータ作成ライブラリ。
聞きなれない単語だけど、「擬態」とかそういう意味があるらしい。
fakerとかも有名だけど、fakerよりも高速らしい。

install

pip install mimesis

usage

プロバイダーからテストデータを作成するのが基本。
プロバイダーには、人に関するもの(Personal)、住所に関するもの(Address)、食べ物に関するもの(Food)など結構たくさん種類がある。

基本的な使い方

from mimesis import Personal

# プロバイダにロケールを指定してインスタンス作成
personal = Personal(locale='ja')

# プロバイダからデータを取得
print(personal.full_name(gender='male'))

Genericを使う

Genericは複数のプロバイダを使用するときに、いちいちプロバイダごとにロケール指定してインスタンスを作成するのが面倒なときに使える。

from mimesis import Generic

generic = Generic(locale='ja')

# Genericを通してプロバイダからテストデータを作成する
print(generic.personal.full_name())

Schemaを使う

Schemaを使うとlistのオブジェクトとしてテストデータを作成してくれる。
Schema.loadメソッドに欲しいデータの形式を指定して使用する。

from mimesis.schema import Schema

schema = Schema('ja')
data = schema.load(schema={
    "name": "personal.full_name",
    "email": "personal.email"
}).create(itertions=2)
結果
[{'name': '秋葉 高木', 'email': 'lorriane_2938@yandex.com'}, {'name': 'ちはる 市川', 'email': 'delisa_7119@gmail.com'}]

まとめ

その他、いろんなプロバイダがあるので普通のテストデータ作るには機能としては十分かと。
ただ、本家サイトのドキュメントが貧弱なんでフル機能使おうと思ったらコード読まないといけないかも。
テキストに関するプロバイダText.sentenceとか、なぜか歴史系の文章返してきて面白い・・・

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3