CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-schematics

Python Data Structures for Humans - a library for data validation and transformation using structured models

Pending
Overview
Eval results
Files

models.mddocs/

Model System

Core model definition and data management functionality in Schematics. The Model class serves as the foundation for defining structured data with validation, providing an ORM-like interface without database coupling.

Capabilities

Model Definition

Define structured data models using field declarations with metaclass-based field registration and inheritance.

class Model:
    """
    Base class for defining structured data models with validation.
    
    The Model class uses a metaclass (ModelMeta) to register fields and
    handle inheritance, providing a Django-like model definition experience.
    """
    
    def __init__(self, raw_data=None, trusted_data=None, deserialize_mapping=None,
                 init=True, partial=True, strict=True, validate=False, app_data=None,
                 lazy=False, **kwargs):
        """
        Initialize model instance with optional data.
        
        Args:
            raw_data (dict, optional): Initial data to populate the model
            trusted_data (dict, optional): Pre-validated data
            deserialize_mapping (dict, optional): Field name mapping
            init (bool): Whether to initialize field values
            partial (bool): Whether to allow partial data
            strict (bool): Whether to enforce strict validation
            validate (bool): Whether to validate on initialization
            app_data (dict, optional): Application-specific data
            lazy (bool): Whether to defer field initialization
            **kwargs: Additional initialization options
        """

Data Import and Validation

Import raw data into models with automatic type conversion and comprehensive validation.

def import_data(self, raw_data, recursive=False, **kwargs):
    """
    Import and convert raw data into the model.
    
    Args:
        raw_data (dict): Raw data to import
        recursive (bool): Whether to recursively import nested models
        **kwargs: Additional import options
        
    Returns:
        None
        
    Raises:
        ConversionError: If data conversion fails
        ValidationError: If validation fails
    """

def validate(self, partial=False, convert=True, app_data=None, **kwargs):
    """
    Validate all model data according to field rules.
    
    Args:
        partial (bool): Whether to allow partial validation
        convert (bool): Whether to convert data during validation
        app_data (dict, optional): Application-specific validation data
        **kwargs: Additional validation options
        
    Returns:
        None
        
    Raises:
        ValidationError: If any field validation fails
        DataError: If multiple validation errors occur
    """

Data Export and Serialization

Export model data in various formats with field filtering and level control.

def export(self, field_converter=None, role=None, app_data=None, **kwargs):
    """
    Export model data with field filtering and conversion.
    
    Args:
        field_converter (callable, optional): Custom field conversion function
        role (str, optional): Role-based field filtering
        app_data (dict, optional): Application-specific export data
        **kwargs: Additional export options including fields, exclude, etc.
        
    Returns:
        dict: Exported data
    """

def to_native(self):
    """
    Convert model to native Python objects.
    
    Returns:
        dict: Model data as native Python types
    """
    
def to_primitive(self):
    """
    Convert model to JSON-serializable primitives.
    
    Returns:
        dict: Model data as JSON-serializable types
    """

def serialize(self):
    """
    Serialize model to JSON string.
    
    Returns:
        str: JSON representation of model data
    """

Data Access Methods

Access model data using dictionary-like and object-like interfaces.

def get(self, key, default=None):
    """
    Get field value with optional default.
    
    Args:
        key (str): Field name
        default: Default value if field is undefined
        
    Returns:
        Field value or default
    """

def keys(self):
    """
    Get all field names.
    
    Returns:
        dict_keys: Field names
    """
    
def items(self):
    """
    Get field name-value pairs.
    
    Returns:
        dict_items: Field name-value pairs
    """
    
def values(self):
    """
    Get all field values.
    
    Returns:
        dict_values: Field values
    """

def atoms(self):
    """
    Iterate over field names and native values.
    
    Yields:
        tuple: (field_name, native_value) pairs
    """

Mock Data Generation

Generate mock data for testing and development.

def get_mock_object(self, **overrides):
    """
    Generate mock instance with realistic test data.
    
    Args:
        **overrides: Field values to override in mock data
        
    Returns:
        Model: New model instance with mock data
        
    Raises:
        MockCreationError: If mock generation fails
    """

Model Metaclass

The ModelMeta metaclass handles field registration and model class creation.

class ModelMeta(type):
    """
    Metaclass for Model classes that registers fields and handles inheritance.
    
    Automatically discovers field declarations and creates the _fields
    class attribute mapping field names to field instances.
    """

Field Descriptor

Internal descriptor class for field access on model instances.

class FieldDescriptor:
    """
    Descriptor that provides field access on model instances.
    
    Returns field type objects when accessed on model classes,
    and field values when accessed on model instances.
    """
    
    def __init__(self, name):
        """
        Initialize field descriptor.
        
        Args:
            name (str): Field name
        """

Usage Examples

Basic Model Definition

from schematics.models import Model
from schematics.types import StringType, IntType, BooleanType

class Product(Model):
    name = StringType(required=True, max_length=100)
    price = IntType(min_value=0, required=True)
    in_stock = BooleanType(default=True)
    
# Usage
product_data = {'name': 'Widget', 'price': 1999}
product = Product(product_data)
product.validate()
print(product.to_primitive())  # {'name': 'Widget', 'price': 1999, 'in_stock': True}

Data Import and Validation

# Import from various sources
raw_data = {'name': 'Test Product', 'price': '29.99', 'in_stock': 'true'}

product = Product()
product.import_data(raw_data)  # Converts strings to appropriate types
product.validate()  # Validates all fields

# Access converted data
print(product.name)      # 'Test Product'
print(product.price)     # 2999 (converted from string)
print(product.in_stock)  # True (converted from string)

Export with Filtering

product = Product({'name': 'Widget', 'price': 1999, 'in_stock': False})

# Export specific fields only
public_data = product.export(fields=['name', 'price'])
# {'name': 'Widget', 'price': 1999}

# Export excluding sensitive fields  
safe_data = product.export(exclude=['price'])
# {'name': 'Widget', 'in_stock': False}

Install with Tessl CLI

npx tessl i tessl/pypi-schematics

docs

basic-types.md

compound-types.md

contrib-modules.md

dynamic-fields.md

exceptions.md

index.md

models.md

network-types.md

utilities.md

tile.json