flatty - the core

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

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 check_type(attr_type, attr_value)

checks the type of value and type

Args:

attr_type: the type which the attr_value should have

attr_value: obj which we check against attr_type

Returns:
None if everything is ok, otherwise raise TypeError
classmethod del_converter(conv_type)

deletes the converter object for a given conv_type

classmethod set_converter(conv_type, converter, exact=True)

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

exact: When True only matches converter if type of obj is
the type of the converter. If exact=False then converter matches also if obj is just a subclass of the converter type. E.g the Schema Class is added to the converter with exact=False because Schema Classes are always inherited at least once. (default=True)
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

classmethod check_type(attr_type, attr_value)

should be implemented to check if the attr_type from the schema matches the real type of attr_value

Args:
attr_type: type from schema attr_value: value/obj with unknown type
Returns:
Nothing if type of attr_value is ok, otherwise should raise a TypeError Exception
classmethod to_flat(obj_type, 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
classmethod to_obj(val_type, 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(**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.SchemaConverter

Convert basic schema classes

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.TypedDictConverter

Convert TypedList classes

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

Convert TypedList classes

flatty.flatty.check_type(attr_type, attr_value)

check the type of attr_value against attr_type

Args:
attr_type: a type attr_value: an object
Returns:
None in normal cases, if attr_type doesn’t match type of attr_value, raise TypeError
flatty.flatty.flatit(obj, obj_type=None)

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

Next topic

flatty.couch - the couchdb adapter

This Page