Voyeur is an simple library to serialize an object into another object.
The goal is to create a representation that is easily json.dumps() friendly.
I've made it while working with datastore such as Riak, MongoDB or CouchBase where the response is a dict and I wanted to transform it into another dict for API output.
It's as easy as this:
from voyeur import view
definition = {
'id': int
}
data = {
'id': '1'
}
result = view(data, definition)
assert result = {'id' : 1}
The definition is a dictionary with key/callable pairs. Voyeur will use the key to get the value from the data then apply the callable.
That works too:
from voyeur import view
definition = {
'id': int
'prop': int
}
class Data(object):
id = 1
@property
def prop(self):
return "12"
result = view(Data(), definition)
assert result = {'id' : 1, 'prop' : 12}
A callable can take any kwargs and use them
from voyeur import view
def mycallable(value, test=None):
return "foo:%s:%s" % (value, test)
definition = {
'id': int
'prop': mycallable
}
class Data(object):
id = 1
@property
def prop(self):
return "12"
result = view(Data(), definition, test='bar')
assert result = {'id' : 1, 'prop' : 'foo:12:bar'}
Voyeur can take a class as a callable if it inherits from :py:class:`voyeur.types.Type`. This allows building more complex serializer.
A good example is the :py:class:`voyeur.types.Type` that reads the value from a different field.
from voyeur import view
definition = {
'field': DeferredType('anotherfield', int),
}
data = {'anotherfield': '2'}
result = view(data, definition)
assert result == {'field':2}
.. toctree:: :maxdepth: 2 api