[PYTHON] Introducing the TestData creation library Mimesis

Introduction

There was a situation where it would be nice if there was a large amount of (like) corporate data, and a library called Mimesis was convenient, so I will introduce it.

Target

Front engineer, anyone who wants to create test data, someone who likes Python python3.6 or higher must be installed

goal

Installation-until you can output simple JSON test data

What is Mimesis

Test data creation library Faker seems to be famous, but it seems that there is no corporate data creation Mimesis can create corporate data, and it's a strange Home Page It seems that you can also create data such as IP address, so I tried to create data using this library Mimesis Official

Installation

Install Mimesis with pip only

$ pip install Mimesis

I always think that pip is a very good guy (copype)

Getting Started Easy to use

main.py


import mimesis

#Set test data to Japanese
g = mimesis.Generic('ja')

#Company name and legal entity type
print("{0} {1}".format(g.business.company_type(), g.business.company()))
# homePage
print(g.internet.home_page())

Launch & browse

At the terminal

$ python main.py 
Denka Seiken Co., Ltd.
https://bantay.moscow

Isn't it a company name that seems to be somehow?

Output as JSON data

I will create JSON test data because it is a big deal (Because it's more convenient whether you import Swagger or Import)

Source

The source from earlier

main.py


import json
from datetime import date, datetime
import mimesis

#Align date and number formats(It is convenient to have this when outputting JSON)
def format_default(obj):
    if isinstance(obj, datetime) or isinstance(obj, date):
        return obj.isoformat()
    if isinstance(obj, decimal.Decimal):
        #If there is no fractional part, return with Int
        if float(obj).is_integer():
            return int(obj)
        else:
            return float(obj)
    raise TypeError

#Prepare Dict so that multiple items can be set
data = []
g = mimesis.Generic('ja')

#Generate multiple items and set them in Dict(Change here for the required number
for idx, x in enumerate(range(0, 2)):
    ins_data = {}
    ins_data['company_id'] = idx  # Id
    ins_data['company_name'] = "Co., Ltd.{0}".format(g.business.company())  #company name
    ins_data['foundation_date'] = g.datetime.formatted_date("%Y/%m")  #Date of establishment
    ins_data['postal_code'] = g.address.postal_code()  #Postal code
    ins_data['state'] = g.address.state()  #Prefectures
    ins_data['city'] = g.address.city()  #~city
    ins_data['street'] = g.address.street_name()  #address
    ins_data['home_page'] = g.internet.home_page()  # homepage
    data.append(ins_data)  #Added to Dict

#Format Dict as JSON and output it
print(json.dumps(data, default=format_default, indent=2, ensure_ascii=False))
$ python main.py 
[
  {
    "company_id": 0,
    "company_name": "Mitsubishi UFJ Financial Group, Inc.",
    "foundation_date": "2003/08",
    "postal_code": "559-9285",
    "state": "Akita",
    "city": "Matsuyama City",
    "street": "Meguro",
    "home_page": "https://trachytes.frl"
  },
  {
    "company_id": 1,
    "company_name": "Daimaru Inc.",
    "foundation_date": "2004/02",
    "postal_code": "564-0918",
    "state": "Fukushima Prefecture",
    "city": "Ota City",
    "street": "Kanamecho",
    "home_page": "https://arvin.tui"
  }
]

How about it, it feels fun just to look at it.

The rest is like this

$ ptyhon main.py > testData.json

Test data (JSON) is created and used for Swagger and Front Mock data.

bonus

Last but not least, I will introduce what kind of data can be created. (Because there are other interesting things that you don't know how to use

Random id, amount, etc.

Address country, city, etc.

Business company, company_type, etc.

Datetime days, hours, etc.

Food drink, vegetable, etc. (This is interesting, isn't it?

Person first_name, email, blood_type, etc.

Text alphabet, answer, etc.

Development os, version, etc.

File file_name, mime_type, etc.

Hardware cpu, screen_size, etc.

Internet home_page, http_method, ip_v4, etc.

Numbers complex_number, integers (start = 0, end = 10, n = 10), etc.

Path home, root, etc.

Path home, root, etc.

Recommended Posts

Introducing the TestData creation library Mimesis
Introducing the color map extension library palettable
[Python] How to use the graph creation library Altair
Introducing configure of self-made library
I tried the changefinder library!