This module provides the base layer for all generic flattening schemas. With this classes - if not yet exists as a flatty module - you can easily write a module to support flatty schemas with your favorite marshaller/unmarshaller. As an example look at the other modules eg. flatty.couchdb
This class is the base Class for all special flatty schema types. These are TypedList and TypedDict
sets the type for the inherited flatty schema class
Class for managing the converters
checks the type of value and type
attr_type: the type which the attr_value should have
attr_value: obj which we check against attr_type
deletes the converter object for a given conv_type
sets a converter object for a given conv_type
conv_type: the type for which the converter is responsible
converter: a subclass of the Converter class
calls the right converter and converts to a flat type
val_type: the type of the object
obj: the object which should be converted
calls the right converter and converts the flat val to a schema object
val_type: the type to which we want to convert
val: the flattened data which needs to be converted here
Base class for all Converters. New converters of custom types can be build by inherit from this class and implement the following two methods
should be implemented to check if the attr_type from the schema matches the real type of attr_value
need to be implemented to convert a python object to a primitive
need to be implemented to convert a primitive to a python object
Converter for datetime.date
Converter for datetime.datetime
This class builds the base class for all schema classes. All schema classes must inherit from this class
>>> import flatty
>>>
>>> class Bar(flatty.Schema):
... a_num = int
... a_str = str
... a_thing = None
one way to flatten the instance of this class
one way to unflatten and load the data back in the schema objects
Convert basic schema classes
Converter for datetime.time
This class is used for typed dict. During flattening and unflattening the types are checked and restored.
>>> import flatty
>>>
>>>
>>> class Bar(flatty.Schema):
... a_num = int
... a_str = str
... a_thing = None
...
>>> class Foo(flatty.Schema):
... my_typed_dict = flatty.TypedDict.set_type(Bar)
>>>
>>>
>>> my_bar = Bar(a_num=42, a_str='hello world', a_thing='whatever type here')
>>> foo = Foo(my_typed_dict={'my_key':my_bar})
>>>
>>> flatted = foo.flatit()
>>> print flatted
{'my_typed_dict': {'my_key': {'a_num': 42, 'a_str': 'hello world', 'a_thing': 'whatever type here'}}}
>>>
>>> restored_obj = Foo.unflatit(flatted)
>>>
>>> isinstance(restored_obj, Foo)
True
>>> isinstance(restored_obj.my_typed_dict['my_key'], Bar)
True
Convert TypedList classes
This class is used for typed lists. During flattening and unflattening the types are checked and restored.
>>> import flatty
>>>
>>>
>>> class Bar(flatty.Schema):
... a_num = int
... a_str = str
... a_thing = None
...
>>> class Foo(flatty.Schema):
... my_typed_list = flatty.TypedList.set_type(Bar)
>>>
>>>
>>> my_bar = Bar(a_num=42, a_str='hello world', a_thing='whatever type here')
>>> foo = Foo(my_typed_list=[my_bar,])
>>>
>>> flatted = foo.flatit()
>>> print flatted
{'my_typed_list': [{'a_num': 42, 'a_str': 'hello world', 'a_thing': 'whatever type here'}]}
>>>
>>> restored_obj = Foo.unflatit(flatted)
>>>
>>> isinstance(restored_obj, Foo)
True
>>> isinstance(restored_obj.my_typed_list[0], Bar)
True
Convert TypedList classes
check the type of attr_value against attr_type
one way to flatten the obj
one way to unflatten and load the data back in the cls