marshmallow-pyfields¶
Automatic generation of marshmallow
schemas from classes using pyfields
.
marshmallow-pyfields
extends marshmallow-dataclass
to help you generate marshmallow schemas for your classes using pyfields
.
Installing¶
> pip install marshmallow-pyfields
Usage¶
Usage is the same than marshmallow-dataclass
:
from pyfields import field, autoclass
from typing import List, Optional
from marshmallow_pyfields import get_schema
@autoclass
class Building:
# field metadata is used to instantiate the marshmallow field
height: float = field(validators={"should be positive": lambda x: x >= 0})
name: str = "anonymous"
@autoclass
class City:
name: Optional[str]
buildings: List[Building] = []
CitySchema = get_schema(City)
city = CitySchema().load(
{"name": "Paris", "buildings": [{"name": "Eiffel Tower", "height": 324}]}
)
assert repr(city) == "City(name='Paris', buildings=[Building(height=324.0, name='Eiffel Tower')])"
city_dict = CitySchema().dump(city)
assert city_dict == {'name': 'Paris', 'buildings': [{'name': 'Eiffel Tower', 'height': 324.0}]}
Main features / benefits¶
- Generate
marshmallow
schemas from classes usingpyfields
, so as to benefit from the wholemarshmallow
ecosystem.
See Also¶
- marshmallow of course
- marshmallow-dataclass
- the great marshmallow ecosystem
Others¶
Do you like this library ? You might also like my other python libraries
Want to contribute ?¶
Details on the github page: https://github.com/smarie/python-marshmallow-pyfields