Quickstart¶
Basic Usage¶
The typical process for using Mimesis involves importing the necessary provider, locale, and enums (if required). Next, create a provider instance and invoke the desired method with the appropriate parameters.
Consider the following example:
from mimesis import Person
from mimesis.locales import Locale
from mimesis.enums import Gender
person = Person(Locale.EN)
person.full_name(gender=Gender.FEMALE)
# Output: 'Antonetta Garrison'
person.full_name(gender=Gender.MALE)
# Output: 'Jordon Hall'
What did the code above do?
First, we imported the
Personprovider from mimesis. An instance of this class will serve as our provider of personal data.We imported the
Localeobject, which provides locale codes and must be used as a parameter for locale-dependent data providers.We imported the
Genderobject from themimesis.enumsmodule, which we use as a parameter forfull_name().Next, we generate a random female full name.
The same as above, but for a male.
Creating objects¶
If your app requires data in one particular language, it’s preferable to
use the Generic() class, which gives access to all providers through a
single object, rather than through multiple separate providers.
Using Generic() lets you get rid of several extra lines of
code.
Incorrect:
from mimesis import Person, Datetime, Text, Code
from mimesis.locales import Locale
person = Person(Locale.RU)
datetime = Datetime(Locale.RU)
text = Text(Locale.RU)
code = Code(Locale.RU)
Correct:
from mimesis import Generic
from mimesis.locales import Locale
generic = Generic(locale=Locale.EN)
generic.person.username()
# Output: 'sherley3354'
generic.datetime.date()
# Output: '14-05-2007'
Still correct:
from mimesis import Person
from mimesis.locales import Locale
p_en = Person(Locale.EN)
p_sv = Person(Locale.SV)
Also correct:
from mimesis import Person
from mimesis.locales import Locale
person = Person(Locale.EN)
with person.override_locale(Locale.SV):
pass
Importing individual providers may be useful if you only need access to the data provided by that specific class.
However, if you require access to a broader range of data, it is recommended to use the Generic() class instead.
This will enable you to access data from all available providers within the library.
What’s next?¶
See Data Providers for a list of all available providers.
See Structured Data Generation for generating structured data.
See Locales for a list of all available locales.
See API for a list of all available methods, providers, and enums.