Skip to main content

Manipulate model schemas utilizing homologous, grouped, or cultured paradigms

Project description

Schemantic

Create schemas from models or classes with homologous, grouped, or cultured paradigms.

Best with pydantic.BaseModel instances, but works with any Python class and dataclass/pydantic.dataclass!

Classes of schemas

Homolog

from pydantic import BaseModel
from ordered_set import OrderedSet
from schemantic.schema import HomologSchema

class Thief(BaseModel):
    stolen_goods: int
    steals_only: str

my_homolog = HomologSchema.from_model(Thief, instance_names=OrderedSet(["copycat", "pink_panther"]))

Grouped

You can manage multiple schemas as a group:

from pydantic import BaseModel
from schemantic.schema import GroupSchema

class Baker(BaseModel):
    baked_goods: int
    citizen_of: str

class Cop(BaseModel):
    years_of_service: int
    citizen_of: str

group_schema = GroupSchema.from_originating_types([Baker, Cop], )

Culture

You can also manage multiple types of schemas under one culture:

from ordered_set import OrderedSet
from schemantic.schema import CultureSchema

CultureSchema(source_schemas=OrderedSet([homolog_schema, group_schema]))

Methods

HomologSchema, GroupSchema, and CultureSchema have the following methods.

.schema()

Creates a dictionary, which represents the schema of the origin class/model.

my_schema.schema()

Gives:

"class_name": "Thief"
"common": {
    steals_only: "jewelry"
}
"copycat": {
    stolen_goods: 10
}
"pink_panther": {
    stolen_goods: 14
}
"required": ["stolen_goods", "steals_only"]
"field_to_info": {"stolen_goods": "integer"}

.dump()

Dump the dictionary from .schema() to a yaml or toml file.

my_schema.dump("my/path/schema.yaml")
# There is also toml support
my_schema.dump("my/path/schema.toml")

.parse()

parsed = my_homolog.parse_schema("my/path/schema.yaml")

# parsed["copycat"].stolen_goods == 10
# parsed["pink_panther"].stolen_goods == 14

# Both -> steals_only == "jewelry"

Class configuration

Use schemantic.project module to control schemantic processing from the origin class/model side.

Classes and dataclasses

from dataclasses import dataclass
from typing import Optional, Union

from schemantic.project import SchemanticProjectMixin


@dataclass
class TestDataclass(SchemanticProjectMixin):
    must_be: int
    we: str = "n"

    n: None = None
    age: Optional[int] = None
    new_age: Union[int, str, None] = None

    exclude_me: Optional[int] = None
    _exclude_me_too: Optional[float] = None

    @classmethod
    @property
    def fields_to_exclude_from_single_schema(cls) -> set[str]:
        upstream = super().fields_to_exclude_from_single_schema
        upstream.update(("exclude_me",))
        return upstream

This will exclude exclude_me (defined in fields_to_exclude_from_single_schema) and _exclude_me_too (private).

Same for pydantic.BaseModel:

from typing import Optional, Union

from pydantic import BaseModel, computed_field

from schemantic.project import SchemanticProjectModelMixin


class TestModel(SchemanticProjectModelMixin, BaseModel):
    must_be: int
    we: str = "n"

    n: None = None
    age: Optional[int] = None
    new_age: Union[int, str, None] = None

    exclude_me: Optional[int] = None
    _exclude_me_too: Optional[float] = None

    @classmethod  # type: ignore[misc]
    @computed_field(return_type=set[str])
    @property
    def fields_to_exclude_from_single_schema(cls) -> set[str]:
        upstream = super().fields_to_exclude_from_single_schema
        upstream.update(("exclude_me",))
        return upstream

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

schemantic-0.1.0.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

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

schemantic-0.1.0-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file schemantic-0.1.0.tar.gz.

File metadata

  • Download URL: schemantic-0.1.0.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.3 Linux/6.4.4-zen1-1-zen

File hashes

Hashes for schemantic-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f18c63cccfbce6c318e5a5a43ba38bbc8f5cea42659f43ca5a4a063a8a25ff75
MD5 0c7a5550d130cbf1e97d09f1b04c92f6
BLAKE2b-256 692e589f8351e61da05a6ed4c4fe0c85d26e2d93412a0ee5bab962e3e120ab99

See more details on using hashes here.

File details

Details for the file schemantic-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: schemantic-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.3 Linux/6.4.4-zen1-1-zen

File hashes

Hashes for schemantic-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1449135fc7b4dc6c39a655a9b406f649c2fabe97d987439d57d5ab999310ebab
MD5 4d38d287cd90e850fee32ae2ced25d18
BLAKE2b-256 e10c8185be1e3124b857cbb8576057a387a96015c588c30948f3e91d39ddf9f3

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