Svarog allow to create object from non typed data
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file svarog-0.3.2.tar.gz
.
File metadata
- Download URL: svarog-0.3.2.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.10.11 Linux/5.15.0-1037-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d65c6a4f364f9135539e6b93348bc08c668534cc5feb21641c69bcd52ffdc56d |
|
MD5 | 5fcf180e616041c38402e59ca77d2674 |
|
BLAKE2b-256 | 8598a3ca1de0c32ba1476ce4f7b6d4f20b93aae3a1681918bac97dbde111a994 |
File details
Details for the file svarog-0.3.2-py3-none-any.whl
.
File metadata
- Download URL: svarog-0.3.2-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.10.11 Linux/5.15.0-1037-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 747cb845ec13604e7697a11051b7861266c1ac7629eb0572ebe6fdf9d8503d4c |
|
MD5 | 3ea6b60b9eb6930caff2d166cf7cf953 |
|
BLAKE2b-256 | a2019e6fb11ae3236baaea1d3e26b0fb2ec6c06c41ba571ed7f75218cd8ec641 |