Schemantic
Do you have configurations stored in files? You have come to the right place! Create schemas from models or classes with homologous, grouped, or cultured paradigms. Batteries for loading and dumping included!
Supports pydantic.BaseModel and any Python class and dataclass/pydantic.dataclass!
Classes of schemas
Homologue
from pydantic import BaseModel
from schemantic import HomologueSchemer
class Thief(BaseModel):
stolen_goods: int
steals_only: str
my_homolog = HomologueSchemer.from_originating_type(Thief, instance_name_to_pre_definition={"copycat": {}, "pink_panther": {}})
HomologueSchemer also accepts instance_name_getter parameter, which is a dictionary of instance names to pre-defined values.
Grouped
You can manage multiple schemas as a group:
from pydantic import BaseModel
from schemantic import GroupSchemer
class Baker(BaseModel):
baked_goods: int
citizen_of: str
class Cop(BaseModel):
years_of_service: int
citizen_of: str
group_schema = GroupSchemer.from_originating_types([Baker, Cop])
Culture
You can also manage multiple types of schemas under one culture:
from ordered_set import OrderedSet
from schemantic import CultureSchemer
CultureSchemer(source_schemas=OrderedSet([homolog_schema, group_schema]))
Methods
HomologueSchemer, GroupSchemer, and CultureSchemer have the following methods.
.schema()
Creates a BaseModel derived class instance, which represents the schema of the origin class/model.
my_homolog.schema()
Output:
"class_name": "Thief"
"common": {
steals_only: "jewelry"
}
"copycat": {}
"pink_panther": {}
"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()
"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"}
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
def fields_to_exclude_from_single_schema(cls) -> set[str]:
upstream = super().fields_to_exclude_from_single_schema
upstream.update(("exclude_me",))
return upstream
Install
pip install schemantic
For toml or yaml dumping and parsing
pip install schemantic[toml]
pip install schemantic[yaml]
Metadata
Release files for schemantic 2.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| schemantic-2.0.3.tar.gz | 14.1 kB | Without provenance | Details for schemantic-2.0.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| schemantic-2.0.3-py3-none-any.whl | Python 3 | none | any | Details for schemantic-2.0.3-py3-none-any.whl |
Total release size: 31.5 kB
Release files / schemantic-2.0.3.tar.gz
| Download URL | schemantic-2.0.3.tar.gz |
|---|---|
| Size | 14.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
878f730c5699a965aec8fefd170a633a739efe9efb409e7f91e9a0954a274d42
|
|
BLAKE2b-256 checksum How to use checksums |
a29da8074de05965421106ac1d210fd8371370fb02bf9af3aa511ebfbc0fe97e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.4 CPython/3.12.7 Linux/6.6.59
|
Release files / schemantic-2.0.3-py3-none-any.whl
| Download URL | schemantic-2.0.3-py3-none-any.whl |
|---|---|
| Size | 17.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cbf13532a263596ce619bfc18d5c1f2328d850b3ca695c8601a9231d7e5edeca
|
|
BLAKE2b-256 checksum How to use checksums |
dd6c47e55b6b19721611ff57a2d4abbc6a931d6f3d85127340130f56afdc8d1a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.4 CPython/3.12.7 Linux/6.6.59
|
Security for schemantic 2.0.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.