flatty

Classes

class flatty.flatty.BaseFlattyType

This class is the base Class for all special flatty schema types. These are TypedList and TypedDict

classmethod set_type(ftype)

sets the type for the inherited flatty schema class

Args:
ftype: the type/class of the instance objects in the schema
Returns:
a class object with the class variable ftype set, used to determine the instance type during unflattening
class flatty.flatty.ConvertManager

Class for managing the converters

classmethod del_converter(conv_type)

deletes the converter object for a given conv_type

classmethod set_converter(conv_type, converter)

sets a converter object for a given conv_type

Args:

conv_type: the type for which the converter is responsible

converter: a subclass of the Converter class

classmethod to_flat(val_type, obj)

calls the right converter and converts to a flat type

Args:

val_type: the type of the object

obj: the object which should be converted

Returns:
a converted primitive object
classmethod to_obj(val_type, val)

calls the right converter and converts the flat val to a schema object

Args:

val_type: the type to which we want to convert

val: the flattened data which needs to be converted here

Returns:
a converted high level schema object
class flatty.flatty.Converter

Base class for all Converters. New converters of custom types can be build by inherit from this class and implement the following two methods

to_flat(obj)

need to be implemented to convert a python object to a primitive

Args:
obj: the src obj which needs to be converted here
Returns:
a converted primitive object
to_obj(val)

need to be implemented to convert a primitive to a python object

Args:
val: the flattened data which needs to be converted here
Returns:
a converted high level schema object
class flatty.flatty.DateConverter

Converter for datetime.date

class flatty.flatty.DateTimeConverter

Converter for datetime.datetime

class flatty.flatty.Schema(*args, **kwargs)

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  
flatit()

one way to flatten the instance of this class

Returns:
a dict where the instance is flattened to primitive types
classmethod unflatit(flat_dict)

one way to unflatten and load the data back in the schema objects

Returns:
the object
class flatty.flatty.TimeConverter

Converter for datetime.time

class flatty.flatty.TypedDict

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
class flatty.flatty.TypedList

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
flatty.flatty.flatit(obj)

one way to flatten the obj

Args:
obj: a Schema instance which will be flatted
Returns:
a dict where the obj is flattened to primitive types
flatty.flatty.unflatit(cls, flat_dict)

one way to unflatten and load the data back in the cls

Args:
flat_dict: a flat dict which will be loaded into an instance of
the cls
cls: the class from which the instance is builded where the
data is merged
Returns:
an instance of type cls

Table Of Contents

Previous topic

Flatty - marshaller/unmarshaller for light-schema python objects

This Page