Svarog
Svarog allow to create object from non typed data. All it need is annotated __init__ method:
>>> from svarog import forge
... class A:
... def __init__(self, a: int, b: str):
... self._a = a
... self._b = b
... def __repr__(self):
... return f'A(a={self._a}, b="{self._b}")'
>>> forge(A, {"a": 1, "b": "3"})
A(a=1, b="3")
More complicated types as Sequence, Mapping, Optional are possible
>>> class A:
... def __init__(self, b: Sequence[int]):
... self._b = b
... def __repr__(self):
... return f'A(b={self._b})'
>>> forge(A, {"b": "3213"})
A(b=[3, 2, 1, 3])
You can use forward refs:
>>> class WithRef:
... def __init__(self, child: Optional['WithRef']):
... self._child = child
... def __repr__(self):
... return f"WithRef({self._child!r})"
>>> forge(WithRef(WithRef(WithRef())))
WithRef(WithRef(WithRef(None)))
Objects are forged recursively:
>>> @dataclass
... class A:
... b: 'B'
... c: 'C'
... @dataclass
... class B:
... number: int
... @dataclass
... class C:
... string: str
>>> forge(A, {'b': {'number': 42}, 'c': {'string': 'the-string'}})
A(b=B(number=42), c=C(string='the-string'))
You can register own forge for your classes:
>>> class FooType(Enum):
... LOREM = "lorem"
... IPSUM = "ipsum"
...
... class FooParams:
... types: ClassVar[Mapping[FooType, "FooParams"]] = {}
... def __init_subclass__(cls, type: FooType):
... cls.types[type] = cls
...
... @classmethod
... def for_type(cls, type):
... return cls.types[type]
...
... @dataclass
... class LoremFooParams(FooParams, type=FooType.LOREM):
... lorem: str
...
... @dataclass
... class IpsumFooParams(FooParams, type=FooType.IPSUM):
... ipsum: int
...
... @dataclass
... class Foo:
... type: FooType
... params: FooParams
...
... @classmethod
... def forge(cls, _, data, forge):
... foo_type = forge(FooType, data["type"])
... return Foo(
... type=forge(FooType, foo_type),
... params=forge(FooParams.for_type(foo_type), data["params"])
... )
...
>>> register_forge(Foo, Foo.forge)
>>> forge(Foo, {"type": "lorem", "params": {"lorem": "foo-bar"}})
Foo(type=<FooType.LOREM: 'lorem'>, params=LoremFooParams(lorem='foo-bar'))
>>> forge(Foo, {"type": "ipsum", "params": {"ipsum": 42}})
Foo(type=<FooType.IPSUM: 'ipsum'>, params=IpsumFooParams(ipsum=42))
Support for CamelCase to snake_case convertion:
>>> class Snake:
... lorem_ipsum: int
>>> forge = Svarog(snake_case=True).forge
>>> forge(Snake, {"LoremIpsum": 42})
Snake(lorem_ipsum=42)
Free software: MIT license
Documentation: https://svarog.readthedocs.io.
Features
Converts unstructured data into structured recursively
Works with dataclasses
Works with Sequence, Mapping, Optional
Special conventers for types can be registered with
Credits
Some parts of this code, and concept borrowed from cattrs project
This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.
Metadata
Release files for svarog 0.3.2
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 |
|---|---|---|---|---|
| svarog-0.3.2.tar.gz | 8.0 kB | Without provenance | Details for svarog-0.3.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| svarog-0.3.2-py3-none-any.whl | Python 3 | none | any | Details for svarog-0.3.2-py3-none-any.whl |
Total release size: 17.2 kB
Release files / svarog-0.3.2.tar.gz
| Download URL | svarog-0.3.2.tar.gz |
|---|---|
| Size | 8.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d65c6a4f364f9135539e6b93348bc08c668534cc5feb21641c69bcd52ffdc56d
|
|
BLAKE2b-256 checksum How to use checksums |
8598a3ca1de0c32ba1476ce4f7b6d4f20b93aae3a1681918bac97dbde111a994
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.5.1 CPython/3.10.11 Linux/5.15.0-1037-azure
|
Release files / svarog-0.3.2-py3-none-any.whl
| Download URL | svarog-0.3.2-py3-none-any.whl |
|---|---|
| Size | 9.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
747cb845ec13604e7697a11051b7861266c1ac7629eb0572ebe6fdf9d8503d4c
|
|
BLAKE2b-256 checksum How to use checksums |
a2019e6fb11ae3236baaea1d3e26b0fb2ec6c06c41ba571ed7f75218cd8ec641
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.5.1 CPython/3.10.11 Linux/5.15.0-1037-azure
|
Security for svarog 0.3.2
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.