bfh.transformations¶
Transformations that can go on Mappings.
They are not used alone, but rather declared on a mapping. They may be combined in various ways:
class MyMapping(Mapping):
id = Concat('source', ':', Str(Get('id')))
name = Get('name')
squared = Do(lambda x: x*x, Get('value'))
When you apply a mapping to an object, the transformations filter and munge that object into the form of the output schema.
- You can Get the value from a field and pass it somewhere.
- You can use All to pass the whole input object somewhere.
- You can coerce a value into a type with Bool, Int, Num, or Str.
- You can Concat some values together into one value.
- You can use Many to group some values into a list.
- You can pass a constant value with Const no matter what the input object
- You can Do arbitrary functions on input.
- You can nest mappings inside others with Submapping and ManySubmap
-
class
bfh.transformations.
All
(strict=False)¶ Get the whole darn source object
Parameters: strict (bool) – If the object is a schema, drop any extra keys that don’t appear in the schema. Returns: If strict == False and called on a Schema instance, a GenericSchema Otherwise just the object.
-
class
bfh.transformations.
Bool
(*args, **kwargs)¶ Coerce input to a boolean
-
class
bfh.transformations.
Concat
(*args, **kwargs)¶ Concat some strings into a single string.
Parameters: - *args – strings to concatenate
- strict (bool, default False) – if not strict, ignore None
-
class
bfh.transformations.
Const
(*args, **kwargs)¶ Return a constant value.
-
class
bfh.transformations.
Do
(*args, **kwargs)¶ Do an arbitrary thing to something.
Example:
class MyMapping(Mapping): squared = Do(lambda x: x*x, Get('value')) MyMapping().apply({'value': 2}).serialize() # {'squared': 4}
Parameters: *args – the first positional arg to the constructor should be a callable. this callable is applied to the input generated by any transformations passed in the subsequent args
-
class
bfh.transformations.
Get
(*args, **kwargs)¶ Gets a value from a dict or object
Example:
value = Get("my_thing")({"my_thing": "get this"}) # value == "get this"
Fetch values from complex objects by passing multiple arguments:
value = Get("a", "b")({"a": {"b": 1}}) # value == 1
Parameters: - *args – a series of names to follow into object and subobject.
- required (bool, default False) – error if names missing
Returns: fetched value or None if required
Raises: Missing if required is false
-
class
bfh.transformations.
Int
(*args, **kwargs)¶ Coerce input to an integer
-
class
bfh.transformations.
Many
(subtrans, *args, **kwargs)¶ Construct an array from a series of values.
Parameters: subtrans (Transformation) – transformation to apply to all input items Returns: results of applying subtransformation to each item in the input
-
class
bfh.transformations.
ManySubmap
(submapping_class, *args)¶ Map an array of complex objects onto a subschema.
Returns: results of applying submapping to each item in the input
-
class
bfh.transformations.
Num
(*args, **kwargs)¶ Coerce input to a floating point number
-
class
bfh.transformations.
Str
(*args, **kwargs)¶ Coerce input to a unicode string
-
class
bfh.transformations.
Submapping
(submapping_class, *args)¶ Map a complex subobject into a subschema.
Parameters: submapping_class (bfh.Mapping) – a mapping Returns: result of applying submapping to the input