Hive can convert just about anything into basic python data structures that can later be converted into the format required (json, xml, ...).
>>> from pyhive.serializers import GenericObjectSerializer
>>> class A(object):
def __init__(self):
self.a = 22
self.b = 44
>>> ser = GenericObjectSerializer()
>>> ser.serialize(A())
{'a': 22, 'b': 44}A list serializer works by applying a serializer instance to convert every single element of the list provided. The item_serializer parameter is the instance to be used to serialize single elements of the list (it is required).
>>> from pyhive.serializer import ListSerializer, GenericObjectSerializer
>>> class A(object):
def __init__(self, n):
self.n = n
>>> list_ser = ListSerializer(GenericObjectSerializer())
>>> list_ser.serialize([A(i) for i in range(3)])
[{'n': 0}, {'n': 1}, {'n': 2}]>>> from pyhive.extra.django import DjangoModelSerializer
>>> from myproject import SomeModel
>>> ser = DjangoModelSerializer()
>>> ser.serialize(SomeModel.objects.get(pk=1))The most useful feature of pyhive are modifiers. Modifiers are functions that alter the current representation of the object being converted. The modifiers take 4 parameters:
- obj: The object being converted
- current: the current representation of the object (usually a dict)
- *args, **kwargs: future usage A modifier should return an altered version of current.
>>> from pyhive.serializers import GenericObjectSerializer
>>> class A(object):
def __init__(self):
self.a = 22
self.b = 44
>>> ser = GenericObjectSerializer()
>>> ser.serialize(A())
{'a': 22, 'b': 44}
>>> def mod(obj, current, *args, **kwargs):
current['foo']='bar'
return current
>>> ser.serialize(A(), modifiers=[mod])
{'a': 22, 'b': 44, 'foo': 'bar'}