bfh¶
BFH: a library for mapping schemas to other schemas.
-
class
bfh.
Schema
(*args, **kwargs)¶ A base class for defining your schemas:
Declare the shape of an object you expect to handle.
Just inherit this and add some fields:
class Animal(Schema): name = UnicodeField() type = UnicodeField() legs = IntegerField() noise = UnicodeField()
-
serialize
(implicit_nulls=False)¶ Represent this schema as a dictionary.
Handy for dumping to json or some other further serialization format, or splatting into an object constructor.
Parameters: implicit_nulls (bool) – drop any keys whose value is nullish Returns: dict
-
validate
()¶ Validate the values in the schema.
Returns: True Raises: Invalid
-
-
class
bfh.
Mapping
¶ A base class for defining your mappings:
Declare a transformation from one shape to another shape.
Just inherit this and add some fields:
class DogToAnimal(Mapping): source_schema = Dog target_schema = Animal name = Get('dogname') type = Const('dog') legs = Const(4) noise = Const('woof!')
The action happens when you get an instance of your mapping:
dog_to_animal_map = DogToAnimal()
Then apply it to an object. You get a Schema instance:
my_animal = dog_to_animal_map.apply(my_dog) type(my_animal) # __main__.Animal
... which is full of values:
my_animal.serialize() # {"name": "Fido", "type": "dog", "legs": 4, "noise": "woof!}
-
apply
(blob)¶ Take the mapping and push a blob through it.
Parameters: blob (dict or Schema) – the thing to transform Returns: instance of self.target_schema (if declared) or GenericSchema
-