schemantic


Nameschemantic JSON
Version 2.0.3 PyPI version JSON
download
home_pageNone
SummaryManipulate model schemas utilizing homologous, grouped, or cultured paradigms
upload_time2024-11-20 16:52:56
maintainerNone
docs_urlNone
authorcaniko
requires_python<4.0,>=3.10
licenseBSD-4
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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
```python
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:

```python
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:

```python
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.

```python
my_homolog.schema()
```
Output:
```yaml
"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.
```python
my_schema.dump("my/path/schema.yaml")
# There is also toml support
my_schema.dump("my/path/schema.toml")
```

### `.parse()`
```yaml
"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"}
```
```python
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
```python
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`:
```python
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
```shell
pip install schemantic
```

For `toml` or `yaml` dumping and parsing
```shell
pip install schemantic[toml]
pip install schemantic[yaml]
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "schemantic",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.10",
    "maintainer_email": null,
    "keywords": null,
    "author": "caniko",
    "author_email": "python@rotas.mozmail.com",
    "download_url": "https://files.pythonhosted.org/packages/a2/9d/a8074de05965421106ac1d210fd8371370fb02bf9af3aa511ebfbc0fe97e/schemantic-2.0.3.tar.gz",
    "platform": null,
    "description": "# Schemantic\n\nDo 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!\n\nSupports `pydantic.BaseModel` and any Python class and `dataclass`/`pydantic.dataclass`!\n\n## Classes of schemas\n\n### Homologue\n```python\nfrom pydantic import BaseModel\nfrom schemantic import HomologueSchemer\n\nclass Thief(BaseModel):\n    stolen_goods: int\n    steals_only: str\n\nmy_homolog = HomologueSchemer.from_originating_type(Thief, instance_name_to_pre_definition={\"copycat\": {}, \"pink_panther\": {}})\n```\n\n`HomologueSchemer` also accepts `instance_name_getter` parameter, which is a dictionary of instance names to pre-defined values.\n\n### Grouped\nYou can manage multiple schemas as a group:\n\n```python\nfrom pydantic import BaseModel\nfrom schemantic import GroupSchemer\n\nclass Baker(BaseModel):\n    baked_goods: int\n    citizen_of: str\n\nclass Cop(BaseModel):\n    years_of_service: int\n    citizen_of: str\n\ngroup_schema = GroupSchemer.from_originating_types([Baker, Cop])\n```\n\n### Culture\nYou can also manage multiple types of schemas under one culture:\n\n```python\nfrom ordered_set import OrderedSet\nfrom schemantic import CultureSchemer\n\nCultureSchemer(source_schemas=OrderedSet([homolog_schema, group_schema]))\n```\n\n## Methods\n`HomologueSchemer`, `GroupSchemer`, and `CultureSchemer` have the following methods.\n\n### `.schema()`\nCreates a BaseModel derived class instance, which represents the schema of the origin class/model.\n\n```python\nmy_homolog.schema()\n```\nOutput:\n```yaml\n\"class_name\": \"Thief\"\n\"common\": {\n    steals_only: \"jewelry\"\n}\n\"copycat\": {}\n\"pink_panther\": {}\n\"required\": [\"stolen_goods\", \"steals_only\"]\n\"field_to_info\": {\"stolen_goods\": \"integer\"}\n```\n\n### `.dump()`\nDump the dictionary from `.schema()` to a yaml or toml file.\n```python\nmy_schema.dump(\"my/path/schema.yaml\")\n# There is also toml support\nmy_schema.dump(\"my/path/schema.toml\")\n```\n\n### `.parse()`\n```yaml\n\"class_name\": \"Thief\"\n\"common\": {\n    steals_only: \"jewelry\"\n}\n\"copycat\": {\n    stolen_goods: 10\n}\n\"pink_panther\": {\n    stolen_goods: 14\n}\n\"required\": [\"stolen_goods\", \"steals_only\"]\n\"field_to_info\": {\"stolen_goods\": \"integer\"}\n```\n```python\nparsed = my_homolog.parse_schema(\"my/path/schema.yaml\")\n\n# parsed[\"copycat\"].stolen_goods == 10\n# parsed[\"pink_panther\"].stolen_goods == 14\n\n# Both -> steals_only == \"jewelry\"\n```\n\n## Class configuration\nUse `schemantic.project` module to control schemantic processing from the origin class/model side.\n\nClasses and dataclasses\n```python\nfrom dataclasses import dataclass\nfrom typing import Optional, Union\n\nfrom schemantic.project import SchemanticProjectMixin\n\n\n@dataclass\nclass TestDataclass(SchemanticProjectMixin):\n    must_be: int\n    we: str = \"n\"\n\n    n: None = None\n    age: Optional[int] = None\n    new_age: Union[int, str, None] = None\n\n    exclude_me: Optional[int] = None\n    _exclude_me_too: Optional[float] = None\n\n    @classmethod\n    @property\n    def fields_to_exclude_from_single_schema(cls) -> set[str]:\n        upstream = super().fields_to_exclude_from_single_schema\n        upstream.update((\"exclude_me\",))\n        return upstream\n```\n\nThis will exclude `exclude_me` (defined in `fields_to_exclude_from_single_schema`) and `_exclude_me_too` (private).\n\nSame for `pydantic.BaseModel`:\n```python\nfrom typing import Optional, Union\n\nfrom pydantic import BaseModel, computed_field\n\nfrom schemantic.project import SchemanticProjectModelMixin\n\n\nclass TestModel(SchemanticProjectModelMixin, BaseModel):\n    must_be: int\n    we: str = \"n\"\n\n    n: None = None\n    age: Optional[int] = None\n    new_age: Union[int, str, None] = None\n\n    exclude_me: Optional[int] = None\n    _exclude_me_too: Optional[float] = None\n\n    @classmethod\n    def fields_to_exclude_from_single_schema(cls) -> set[str]:\n        upstream = super().fields_to_exclude_from_single_schema\n        upstream.update((\"exclude_me\",))\n        return upstream\n```\n\n### Install\n```shell\npip install schemantic\n```\n\nFor `toml` or `yaml` dumping and parsing\n```shell\npip install schemantic[toml]\npip install schemantic[yaml]\n```\n",
    "bugtrack_url": null,
    "license": "BSD-4",
    "summary": "Manipulate model schemas utilizing homologous, grouped, or cultured paradigms",
    "version": "2.0.3",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "dd6c47e55b6b19721611ff57a2d4abbc6a931d6f3d85127340130f56afdc8d1a",
                "md5": "30a038e5d8842df81f44bd5d13951bcb",
                "sha256": "cbf13532a263596ce619bfc18d5c1f2328d850b3ca695c8601a9231d7e5edeca"
            },
            "downloads": -1,
            "filename": "schemantic-2.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "30a038e5d8842df81f44bd5d13951bcb",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.10",
            "size": 17429,
            "upload_time": "2024-11-20T16:52:55",
            "upload_time_iso_8601": "2024-11-20T16:52:55.176032Z",
            "url": "https://files.pythonhosted.org/packages/dd/6c/47e55b6b19721611ff57a2d4abbc6a931d6f3d85127340130f56afdc8d1a/schemantic-2.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a29da8074de05965421106ac1d210fd8371370fb02bf9af3aa511ebfbc0fe97e",
                "md5": "594a83eb298abddca3d0de15d508aa29",
                "sha256": "878f730c5699a965aec8fefd170a633a739efe9efb409e7f91e9a0954a274d42"
            },
            "downloads": -1,
            "filename": "schemantic-2.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "594a83eb298abddca3d0de15d508aa29",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.10",
            "size": 14087,
            "upload_time": "2024-11-20T16:52:56",
            "upload_time_iso_8601": "2024-11-20T16:52:56.387611Z",
            "url": "https://files.pythonhosted.org/packages/a2/9d/a8074de05965421106ac1d210fd8371370fb02bf9af3aa511ebfbc0fe97e/schemantic-2.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-20 16:52:56",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "schemantic"
}
        
Elapsed time: 0.56149s