Skip to main content

Models for conversion and validation of rich data structures.

Project description

stereotype

readthedocs.org codecov.io

Stereotype is a performance-focused Python 3.8+ library for providing a structure for your data and validating it. The models allow fast & easy conversion between primitive data and well-typed Python classes.

Stereotype is heavily influenced by the beauty of dataclasses and versatility of Schematics, while having much better performance - both in terms of CPU usage and memory footprint. While it wasn't an influence, it is somewhat similar to Pydantic, but also beats it in benchmarks and provides easier validation.

Stereotype supports Python 3.8 and above (future support for older versions of Python is highly unlikely) and has 100% test coverage.

Features

  • Fields
    • All JSON atomic types - bool, int, float, str, Optional[*]
    • Compound fields - List[*] of any type or a Dict[*, *] of atomic types to any type
    • Model nesting - Model subclass fields, including recursive definitions
    • Dynamic model fields - Model subclass fields resolved using a string type key
    • Free-form fields using Any
    • Calculated serializable fields - a property present also in serialized data
    • Schematics compatibility field, custom fields can be defined
  • Validation
    • Basic built-in validation helpers for most fields
    • Custom field validator callbacks
    • Custom Model instance validation methods
    • Validation separate from conversion, multiple validation errors reported with paths
  • Conversion & serialization
    • Optional field defaults using atomic values or callables
    • Renaming or disabling fields for purposes of input/output/both
    • Optional hiding of None values from output
    • Serialization roles using field blacklists or whitelists, with inheritance or overriding

Documentation

Full documentation of stereotype

Brief usage example

from typing import Optional, List
from stereotype import Model, StrField, FloatField


class Movie(Model):
    name: str
    genre: str = StrField(choices=("Comedy", "Action", "Family", "Drama"))
    ratings: Optional[float] = FloatField(min_value=1, max_value=10, default=None)
    cast: List[CastMember] = []


class CastMember(Model):
    name: str


movie = Movie({"name": "Monty Python and the Holy Grail", "genre": "Comedy", "ratings": 8.2})
movie.validate()
movie.cast.append(CastMember({"name": "John Cleese"}))
print(movie.serialize())

See the Tutorial for more examples with detailed explanations.

Issues & contributing

Please see the Contribution guide

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

stereotype-1.5.4.tar.gz (40.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stereotype-1.5.4-py3-none-any.whl (31.1 kB view details)

Uploaded Python 3

File details

Details for the file stereotype-1.5.4.tar.gz.

File metadata

  • Download URL: stereotype-1.5.4.tar.gz
  • Upload date:
  • Size: 40.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for stereotype-1.5.4.tar.gz
Algorithm Hash digest
SHA256 358602f0859b6207c1706dd7bc19997b95a01728c1ed84f2c43a3692c5c55854
MD5 73299711cb11c6fb4cfdd51fdb96dbd7
BLAKE2b-256 29504bd826d65e2fa73bd6dba079fae4dd5f3596dfc457c68070fa67b8ac4930

See more details on using hashes here.

File details

Details for the file stereotype-1.5.4-py3-none-any.whl.

File metadata

  • Download URL: stereotype-1.5.4-py3-none-any.whl
  • Upload date:
  • Size: 31.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for stereotype-1.5.4-py3-none-any.whl
Algorithm Hash digest
SHA256 8697c907b6d72996e92197fe2a9c045f9c09474d2a9462c103bac7c6cbb88227
MD5 330f5615a7bea5c2542b188ffe59a266
BLAKE2b-256 dd806df593088537d8e7a2d497e3f3db65aab560e02e9fc9cf9a093dbb2e3c1c

See more details on using hashes here.

Supported by

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