CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-mimesis

Mimesis is a robust fake data generator for Python that produces realistic fake data across 35 different locales with high performance and multilingual support.

Pending
Overview
Eval results
Files

schema.mddocs/

Schema-Based Data Generation

High-level tools for generating structured data with export capabilities, enabling bulk data creation for testing and development with schema-driven approaches.

Capabilities

Field Generation

class Field:
    """Single field data generation with provider method access."""
    
    def __init__(self, locale: Locale = Locale.DEFAULT):
        """Initialize Field with locale."""
    
    def __call__(self, provider_method: str, *args, **kwargs) -> Any:
        """
        Generate data using provider method.
        
        Parameters:
        - provider_method (str): Method path like 'person.full_name'
        - *args, **kwargs: Arguments for the provider method
        
        Returns:
        Generated data value
        
        Usage:
        ```python
        field = Field(Locale.EN)
        name = field('person.full_name')
        email = field('internet.email')
        age = field('person.age', minimum=18, maximum=65)
        ```
        """
    
    def register_handler(self, field_name: str, field_handler: callable) -> None:
        """Register custom field handler."""
    
    def handle(self, field_name: str = None):
        """Decorator for registering field handlers."""
    
    def reseed(self, seed: int) -> None:
        """Reseed random generator."""

Fieldset Generation

class Fieldset(Field):
    """Multiple field data generation."""
    
    def __call__(self, provider_method: str, *args, **kwargs) -> list[Any]:
        """Generate list of values using provider method."""

Schema Generation and Export

class Schema:
    """Schema-based bulk data generation with export capabilities."""
    
    def __init__(self, schema: callable, iterations: int = 1):
        """
        Initialize Schema with schema function.
        
        Parameters:
        - schema (callable): Function returning data structure template
        - iterations (int): Number of records to generate
        """
    
    def create(self) -> list[dict]:
        """
        Generate list of schema instances.
        
        Returns:
        list[dict]: Generated data records
        """
    
    def to_csv(self, file_path: str, **kwargs) -> None:
        """
        Export generated data to CSV file.
        
        Parameters:
        - file_path (str): Output CSV file path
        - **kwargs: Additional CSV writer arguments
        """
    
    def to_json(self, file_path: str, **kwargs) -> None:
        """
        Export generated data to JSON file.
        
        Parameters:
        - file_path (str): Output JSON file path
        - **kwargs: Additional JSON arguments
        """
    
    def to_pickle(self, file_path: str, **kwargs) -> None:
        """
        Export generated data to pickle file.
        
        Parameters:
        - file_path (str): Output pickle file path
        - **kwargs: Additional pickle arguments
        """
    
    def __iter__(self):
        """Iterator support for schema instances."""
    
    def __next__(self):
        """Next method for iterator."""

Usage Examples

Basic Field Usage

from mimesis import Field
from mimesis.locales import Locale

field = Field(Locale.EN)

# Generate individual fields
name = field('person.full_name')
email = field('internet.email')
age = field('person.age', minimum=21, maximum=65)
city = field('address.city')

Schema Definition and Generation

from mimesis import Field, Schema
from mimesis.locales import Locale

# Define schema function
field = Field(Locale.EN)

def user_schema():
    return {
        'id': field('increment'),
        'name': field('person.full_name'),
        'email': field('internet.email'),
        'age': field('person.age', minimum=18, maximum=65),
        'address': {
            'street': field('address.address'),
            'city': field('address.city'),
            'state': field('address.state'),
            'postal_code': field('address.postal_code')
        },
        'profile': {
            'occupation': field('person.occupation'),
            'company': field('finance.company'),
            'salary': field('finance.price', minimum=30000, maximum=150000)
        }
    }

# Create schema and generate data
schema = Schema(schema=user_schema, iterations=100)
users = schema.create()  # List of 100 user records

Data Export

from mimesis import Field, Schema

field = Field()
schema = Schema(
    schema=lambda: {
        'name': field('person.full_name'),
        'email': field('internet.email'),
        'company': field('finance.company')
    },
    iterations=1000
)

# Export to different formats
schema.to_csv('users.csv')
schema.to_json('users.json', indent=2)
schema.to_pickle('users.pkl')

Custom Field Handlers

from mimesis import Field
import random

field = Field()

# Register custom handler
@field.handle('custom_score')
def custom_score_handler():
    return random.randint(0, 100)

# Alternative registration
def status_handler():
    return random.choice(['active', 'inactive', 'pending'])

field.register_handler('status', status_handler)

# Use custom handlers in schema
def record_schema():
    return {
        'name': field('person.full_name'),
        'score': field('custom_score'),
        'status': field('status')
    }

Complex Nested Schema

from mimesis import Field, Schema
from mimesis.locales import Locale

field = Field(Locale.EN)

def complex_schema():
    return {
        'user': {
            'id': field('increment'),
            'personal': {
                'name': field('person.full_name'),
                'email': field('internet.email'),
                'birthdate': field('datetime.date', start=1980, end=2000),
                'contacts': [
                    field('person.phone_number') for _ in range(2)
                ]
            },
            'address': {
                'primary': {
                    'street': field('address.address'),
                    'city': field('address.city'),
                    'coordinates': field('address.coordinates')
                },
                'billing': {
                    'street': field('address.address'),
                    'city': field('address.city')
                }
            },
            'preferences': {
                'languages': [field('person.language') for _ in range(3)],
                'timezone': field('datetime.timezone')
            }
        },
        'metadata': {
            'created_at': field('datetime.datetime'),
            'user_agent': field('internet.user_agent'),
            'ip_address': field('internet.ip_v4')
        }
    }

schema = Schema(schema=complex_schema, iterations=50)
complex_data = schema.create()

Fieldset for Multiple Values

from mimesis import Fieldset

# Generate multiple values at once
fieldset = Fieldset()
names = fieldset('person.full_name', quantity=10)  # List of 10 names
emails = fieldset('internet.email', quantity=5)    # List of 5 emails

Iterator Pattern

from mimesis import Field, Schema

field = Field()
schema = Schema(
    schema=lambda: {
        'id': field('increment'),
        'name': field('person.full_name')
    },
    iterations=1000
)

# Use as iterator for memory efficiency
for record in schema:
    # Process one record at a time
    print(record['name'])
    if record['id'] > 10:
        break

Install with Tessl CLI

npx tessl i tessl/pypi-mimesis

docs

configuration.md

core-providers.md

datetime.md

financial-data.md

index.md

internet-data.md

location-data.md

personal-data.md

schema.md

specialized-providers.md

text-content.md

tile.json