Skip to main content

Class models with validation and serialization

Project description

Inspired by Marshmallow Models and by ORM libraries.

The Toasted Marshmallow Models package makes it easy to define serializable classes based on the ultra fast serialization that Toasted Marshmallow provides.

Installing toastedmarshmallow-models

pip install toastedmarshmallow-models

Using Toasted Marshmallow Models

Using Toasted Marshmallow Models in an existing class requires your class to inherit the Model class and specify the relevant Marshmallow fields. For example:

from marshmallow import fields
from toastedmarshmallow_models import Model

class Entity(Model):  # Inherit Model
    # Define Marshmallow fields
    id = fields.Integer()
    name = fields.String()

    def __init__(self, id, name):
        self.id = id
        self.name = name

How it works

The Toasted Marshmallow Models package makes it easy to dump and load models.

Dump methods:

entity = Entity(id=1, name='John Doe')

print(entity.to_dict())
# {"id": 1, "name": "John"}

print(entity.to_json())
# '{"id": 1, "name": "John"}'

Load Methods:

entity = Entity.from_dict({"id": 1, "name": "John"}) # creates an Entity instance

entity = Entity.from_json('{"id": 1, "name": "John"}') # creates an Entity instance

Features

Validation:

entity = Entity(id='i-am-not-a-valid-int', name='John Doe')

entity.validate()  # throws marshmallow.ValidationError if not valid

Get validation errors:

entity = Entity(id='i-am-not-a-valid-int', name='John Doe')

entity.get_validation_errors()  # returns dict(id=['Not a valid integer.'])

Nested Models:

class ChildEntity(Model):
    name = fields.String()

    def __init__(self, name: str):
        self.name = name


class ParentEntity(Model):
    name = fields.String()
    # Use NestedModel to define parent-child relationships
    children = fields.Nested(NestedModel(ChildEntity), many=True)

    def __init__(self, name: str, children: List[ChildEntity]):
        self.children = children
        self.name = name

Self Referencing Model:

class Employee(Model):
    name = fields.String()
    # Use SelfReferencingModel to define self-referencing relationships
    subordinates = fields.Nested(SelfReferencingModel('Employee'), many=True, allow_none=True)

    def __init__(self, name: str, subordinates: List['Employee'] = None):
        self.subordinates = subordinates
        self.name = name

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

toastedmarshmallow-models-1.0.4.tar.gz (3.6 kB view details)

Uploaded Source

File details

Details for the file toastedmarshmallow-models-1.0.4.tar.gz.

File metadata

File hashes

Hashes for toastedmarshmallow-models-1.0.4.tar.gz
Algorithm Hash digest
SHA256 0cc66724d10a52a5a178d1321a767c190412a6de703d6fee0bc4cc6f05f7f01b
MD5 d74d5ad70b02679001e51584eade72bc
BLAKE2b-256 a91fca3bf352e5e07c44b90cd7cd0a0416c947a2dadeb7559c9dd572d27af2b3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page