AleksanderGondek / mimesis

Python library, which helps generate mock data in different languages for various purposes. These data can be especially useful at various stages of software development and testing.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool


Build Status codecov PyPI version

Mimesis is a fast and easy to use library for Python, which helps generate mock data for a variety of purposes in a variety of languages (see "Locales"). This data can be particularly useful during software development and testing. The library was written with the use of tools from the standard Python library, and therefore, it does not have any side dependencies.

Documentation

Mimesis is very simple to use, and the below examples should help you get started. Complete documentation for Mimesis is available here.

Installation

To install mimesis, simply:

~ pip install mimesis

Also you can install it manually:

(env) ➜  python3 setup.py install

Basic Usage

As we said above, this library is really easy to use:

>>> import mimesis
>>> person = mimesis.Personal(locale='en')

>>> person.full_name(gender='female')
'Antonetta Garrison'

>>> person.occupation()
'Backend Developer'

Locales

You can specify a locale when creating providers and they will return data that is appropriate for the language or country associated with that locale:

>>> from mimesis import Personal

>>> de = Personal('de')
>>> ic = Personal('is')

>>> de.full_name()
'Sabrina Gutermuth'

>>> ic.full_name()
'Rósa Þórlindsdóttir'

Mimesis currently includes support for 30 different locales. See details for more information.

Flag Code Name Native name
1 🇨🇿 cs Czech Česky
2 🇩🇰 da Danish Dansk
3 🇩🇪 de German Deutsch
4 🇦🇹 de-at Austrian German Deutsch
5 🇨🇭 de-ch Swiss German Deutsch
6 🇺🇸 en English English
7 🇦🇺 en-au Australian English English
8 🇨🇦 en-ca Canadian English English
9 🇬🇧 en-gb British English English
10 🇪🇸 es Spanish Español
11 🇲🇽 es-mx Mexican Spanish Español
12 🇮🇷 fa Farsi فارسی
13 🇫🇮 fi Finnish Suomi
14 🇫🇷 fr French Français
15 🇭🇺 hu Hungarian Magyar
16 🇮🇸 is Icelandic Íslenska
17 🇮🇹 it Italian Italiano
18 🇯🇵 ja Japanese 日本語
19 🇰🇷 ko Korean 한국어
20 🇳🇱 nl Dutch Nederlands
21 🇧🇪 nl-be Belgium Dutch Nederlands
22 🇳🇴 no Norwegian Norsk
23 🇵🇱 pl Polish Polski
24 🇵🇹 pt Portuguese Português
25 🇧🇷 pt-br Brazilian Portuguese Português Brasileiro
26 🇷🇺 ru Russian Русский
27 🇸🇪 sv Swedish Svenska
28 🇹🇷 tr Turkish Türkçe
29 🇺🇦 uk Ukrainian Український
30 🇨🇳 zh Chinese 汉语

When you only need to generate data for a single locale, use the Generic() provider, and you can access all providers of Mimesis from one object.

>>> import mimesis
>>> g = mimesis.Generic('es')

>>> g.datetime.month()
'Agosto'

>>> g.food.fruit()
'Limón'

Advantages

Mimesis offers a number of advantages over other similar libraries, such as Faker:

  • Performance. Mimesis is significantly faster than other similar libraries.
  • Completeness. Mimesis strives to provide many detailed providers that offer a variety of data generators.
  • Simplicity. Mimesis does not require any modules other than the Python standard library.

See here for an example of how we compare performance with other libraries.

Integration with Web Application Frameworks

You can use Mimesis during development and testing of applications built on a variety of frameworks. Here is an example of integration with a Flask application:

class Patient(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    full_name = db.Column(db.String(100))
    blood_type = db.Column(db.String(64))

    def __init__(self, **kwargs):
        super(Patient, self).__init__(**kwargs)

    @staticmethod
    def populate(count=500, locale=None):
        import mimesis

        person =  mimesis.Personal(locale=locale)

        for _ in range(count):
            patient = Patient(
                full_name=person.full_name('female'),
                blood_type=person.blood_type(),
            )

            db.session.add(patient)
            try:
                db.session.commit()
            except IntegrityError:
                db.session.rollback()

Just run shell mode and do following:

>>> Patient().populate(count=1000, locale='en')

Generate data by schema

Mimesis support generating data by schema:

>>> from mimesis.schema import Schema
>>> schema = Schema('en')

>>> schema.load(schema={
...     "id": "cryptographic.uuid",
...     "name": "text.word",
...     "version": "development.version",
...     "owner": {
...         "email": "personal.email",
...         "token": "cryptographic.token",
...         "creator": "personal.full_name"
...     }
... }).create(iterations=2)

>>> # or you can load data from json file:
>>> schema.load(path='schema.json').create(iterations=2)

Result:

[
  {
    "id": "790cce21-5f75-2652-2ee2-f9d90a26c43d",
    "name": "container",
    "owner": {
      "email": "anjelica8481@outlook.com",
      "token": "0bf924125640c46aad2a860f40ec4b7f33a516c497957abd70375c548ed56978",
      "creator": "Ileen Ellis"
    },
    "version": "4.11.6"
  },
  ...
]

Custom Providers

You also can add custom provider to Generic(), using add_provider() method:

>>> import mimesis
>>> generic = mimesis.Generic('en')

>>> class SomeProvider(object):
...     class Meta:
...         name = "some_provider"
...
...     def hello(self):
...         return "Hello!"

>>> class Another(object):
...     def bye(self):
...         return "Bye!"

>>> generic.add_provider(SomeProvider)
>>> generic.add_provider(Another)

>>> generic.some_provider.hi()
'Hello!'

>>> generic.another.bye()
'Bye!'

or multiple custom providers using method add_providers():

>>> generic.add_providers(SomeProvider, Another)

Builtins specific data providers

Some countries have data types specific to that country. For example social security numbers (SSN) in the United States of America (en), and cadastro de pessoas físicas (CPF) in Brazil (pt-br). If you would like to use these country-specific providers, then you must import them explicitly:

>>> from mimesis import Generic
>>> from mimesis.builtins import BrazilSpecProvider

>>> generic = Generic('pt-br')
>>> generic.add_provider(BrazilSpecProvider)
>>> generic.brazil_provider.cpf()
'696.441.186-00'

Decorators

If your locale belongs to the family of Cyrillic languages, but you need latinized locale-specific data, then you can use special decorator which help you romanize your data. At this moment it's works only for Russian and Ukrainian:

>>> from mimesis.decorators import romanized

>>> @romanized('ru')
... def russian_name():
...     return 'Вероника Денисова'

>>> russian_name()
'Veronika Denisova'

Disclaimer

The authors assume no responsibility for how you use this library data generated by it. This library is designed only for developers with good intentions. Do not use the data generated with Mimesis for illegal purposes.

Contributing

Your contributions are always welcome! Please take a look at the contribution guidelines first. Here you can look at list of our contributors.

License

Mimesis is licensed under the MIT License. See LICENSE for more information.

About

Python library, which helps generate mock data in different languages for various purposes. These data can be especially useful at various stages of software development and testing.

License:MIT License


Languages

Language:Python 99.6%Language:Makefile 0.4%