Mimesis

repository·master·Indexed 26 days ago

https://github.com/lk-geimfari/mimesis

A high-performance Python library for generating realistic fake data across 47 different locales. Designed for populating test databases, mocking APIs, and creating sample datasets, Mimesis provides localized providers (Address, Finance, Person, etc.), universal providers (Internet, Numeric, Science, etc.), and schema-based generation. It is optimized for speed, significantly outperforming Faker in benchmarks, and offers integration with factory_boy.

Tokens
18.7K
Snippets
52
Records
133
Agent score
89%

What's inside mimesis

  1. Overview of Mimesis Fake Data Generator

    master

    Mimesis is a Python library designed to generate fake but realistic data across multiple languages and locales. It is suitable for populating test databases, mocking API responses, generating JSON/XML fixtures, creating sample datasets, and anonymizing production data.

    Supported data types include:

    • Names
    • Addresses
    • Dates
    • Phone numbers
    • Emails
    • Financial data
    • And many other value types.
  2. Overview of Mimesis capabilities

    master

    Mimesis is a high-performance Python data generator used to populate databases, create complex JSON/XML files, anonymize production data, and generate Pandas dataframes.

    Key capabilities include:

    • Multilingual support: 47 different locales.
    • Extensibility: Support for custom data providers and custom field handlers.
    • Schema-based generation: Effortlessly produce complex data structures.
    • Relational data: Support for generating related datasets with foreign keys and nested schemas using mimesis.builder.SchemaBuilder.
    • Performance: Optimized for speed in Python environments.
    • Type Safety: Fully typed for excellent IDE autocompletion support.
  3. Performance comparison between Mimesis and Faker

    master

    Mimesis is designed for high-performance data generation and consistently outperforms Faker in speed across all tested operations. In benchmarks conducted on an Apple M1 Pro, Mimesis demonstrated a 100% win rate across 47 tested operations, providing an average speedup of approximately 23×.

    Key performance characteristics include:

    • Text, Address, Internet, and Generic providers: Typically 20–30× faster.
    • Finance and company-related data: Up to ~140× faster.
    • Complex operations (e.g., user profiles, bulk generation): ~25× faster, moving workloads from millisecond-level execution (Faker) to microsecond-level execution (Mimesis).

    In summary, Mimesis operates at the nanosecond–microsecond scale, whereas Faker frequently operates at the microsecond–millisecond scale.

  4. Performance comparison: Mimesis vs Faker

    master

    Mimesis is significantly faster than Faker across all tested providers. In benchmark tests involving 47 operations, Mimesis outperformed Faker in 100% of cases, achieving an overall speedup of approximately 23.18x.

    Key performance highlights include:

    • Person Provider: ~19.57x speedup.
    • Address Provider: ~26.76x speedup.
    • Internet Provider: ~17.95x speedup.
    • Text Provider: ~28.07x speedup.
    • Finance Provider: ~67.79x speedup.
    • Complex Operations (e.g., generate_100_names): ~25.03x speedup.
  5. Understand Data Providers and Locales

    master

    Mimesis uses data providers to generate various types of data (food, people, addresses, etc.). Providers are categorized into two types:

    1. Locale-dependent providers: Offer data specific to a country/locale (e.g., Person). If no locale is specified, Locale.EN is used by default.
    2. Locale-independent providers: Offer universal data (e.g., Code).

    Warning: Data providers are heavy objects because they load JSON data into memory. Avoid constructing an excessive number of provider instances.

    Note: Attempting to pass a locale argument to a locale-independent provider will raise a TypeError.

  6. Understand Mimesis core concepts

    master

    To use Mimesis effectively, familiarize yourself with these core architectural terms:

    • provider: A class containing various data generators.
    • field: A string representing a specific method within a data provider.
    • fieldset: A collection or list of fields.
    • locale: Represents country-specific data for locale-dependent providers (see mimesis.enums.Locale).
    • localized provider: A provider that relies on external JSON files for locale-specific data.
    • universal provider: A provider with no external dependencies that works across any locale.
    • key function: A callable used to transform a generated field's result via the key parameter (see mimesis.keys).
  7. Use the Generic provider for multi-provider access

    master

    The Generic provider provides a single interface to access all Mimesis providers for a specific locale. It automatically handles the distinction between locale-dependent and locale-independent providers.

    from mimesis import Generic
    from mimesis.locales import Locale
    g = Generic(locale=Locale.ES)
    
    g.datetime.month()
    # Output: 'Agosto'
    
    g.code.imei()
    # Output: '353918052107063'
    
    g.food.fruit()
    # Output: 'Limón'
  8. Reproduce Mimesis benchmarks

    master

    To reproduce or refresh the performance and memory benchmarks for Mimesis, use the benchmark scripts located in the benchmarks directory of the repository.

    Note that absolute timings and memory peaks will vary depending on your hardware and Python interpreter, but the relative performance gains (speedup and memory efficiency) are the primary metrics of interest.

  9. Specify a locale when creating providers

    master

    Mimesis supports multiple locales to generate data in different languages and for different countries. The default locale is English (United States) (Locale.EN). To use a specific locale, import Locale from mimesis.locales and pass it to the provider's constructor using the locale argument.

    from mimesis import Address
    from mimesis.locales import Locale
    
    de = Address(locale=Locale.DE)
    ru = Address(locale=Locale.RU)
    
    de.region()
    # Output: 'Brandenburg'
    
    ru.federal_subject()
    # Output: 'Алтайский край'
  10. Create a Custom Provider by subclassing BaseProvider

    master

    To implement domain-specific methods, subclass BaseProvider.

    Requirements:

    1. Inherit from BaseProvider.
    2. Define a nested Meta class containing at least a name attribute (this is the attribute name used to access the provider via Generic, e.g., generic.some_provider).
    3. Decide on registration via Meta.auto_register.

    Registration Options (Meta.auto_register):

    • False (Recommended for apps/tests): You must manually attach the provider to Generic instances using add_provider() or add_providers(). This prevents leaking providers into unrelated objects.
    • True (Default, for plugins/libraries): The provider is automatically registered in the ProviderRegistry at class definition time and becomes available to all new Generic and Field objects created after import.
    from mimesis.providers.base import BaseProvider
    
    class SomeProvider(BaseProvider):
        class Meta:
            name = "some_provider"
            auto_register = False
    
        @staticmethod
        def hello() -> str:
            return "Hello!"
  11. Override locale for Generic providers

    master

    You can also use override_locale with mimesis.Generic instances to temporarily change the locale for specific sub-providers (e.g., generic.text).

    from mimesis import Generic
    from mimesis.locales import Locale
    
    generic = Generic(locale=Locale.EN)
    generic.text.word()
    # Output: 'anyone'
    
    with generic.text.override_locale(Locale.FR):
        generic.text.word()
    # Output: 'mieux'
    
    generic.text.word()
    # Output: 'responsibilities'
  12. Configure custom field handlers in factories

    master

    To define custom logic for fields that aren't standard Mimesis providers, define a field_handlers list within a Params inner class inside your factory. Each handler is a tuple containing the field name and a callable (e.g., a lambda) that accepts rand (the Mimesis instance) and **kwargs.

    import factory
    from mimesis.plugins.factory import FactoryField
    
    class FactoryWithCustomFieldHandlers(factory.Factory):
        class Meta(object):
            model = Guest
    
        class Params(object):
            field_handlers = [
                ("num", lambda rand, **kwargs: rand.randint(1, 99)),
                ("nick", lambda rand, **kwargs: rand.choice(["john", "alice"])),
            ]
    
        age = FactoryField("num")
        nickname = FactoryField("nick")