edeposit.amqp.serializers module

This module is just wrapper over python’s JSON module. It allows you to serialize namedtuple and other default python data in very comfortable way - if you initialize it properly, it just works and you don’t have to take care about anything.


This module exists only because standard JSON module can’t serialize namedtuple. If you don’t need to serialize namedtuple, you don’t need this module.

Serialization details

namedtuple is serialized to dict with special property __nt_name, where the name of the namedtuple class is stored.

Live example:

>>> from collections import namedtuple
>>> from edeposit.amqp.serializers import serialize
>>> Person = namedtuple("Person", ["name", "surname"])
>>> p = Person("Lishaak", "Bystroushaak")
>>> p
Person(name='Lishaak', surname='Bystroushaak')
>>> serialize(p)
'{"surname": "Bystroushaak", "name": "Lishaak", "__nt_name": "Person"}'


If you try to serialize module from different hierarchy path, than where it will be deserialized, you may run into problems with isinstance(), which compares by full path, not just by class identity.

Lets take object p from previous example:

>>> p
Person(name='Lishaak', surname='Bystroushaak')
>>> type(p)
<class '__main__.Person'>

As you can see, type of the object is <class '__main__.Person'>. In case where the namedtuple class would be defined in module Y, you would get something like <class 'Y.Person'> and you would run into errors, when you would try to compare these two for identity.

This is not the issue in normal namedtuple usage, because you usually wouldn´t have two definitions of same class. In case of desesrialization, you can run into this problems.

Trivial solution is to compare without full paths, just the names of the classes by using iiofany().



Serialize class hierarchy into JSON.

Parameters:data (any) – any python type serializable to JSON, with added support of namedtuples
Returns:JSON string
Return type:unicode
edeposit.amqp.serializers.serializers.deserialize(json_str, glob)[source]

Deserialize classes from JSON back to python data.

  • json_str (str) – JSON encoded string.
  • glob (dict) – Output from globals() call - your context of variables.
Call example::
deserialize(data, globals())


Call the globals() every time, you call this function, because your variable context could change. Also don’t pass a blank dict - it may work sometimes, but fail unpredictably later.

Returns:any python type (make sure you have namedtuples imported)
Return type:any
edeposit.amqp.serializers.serializers.iiOfAny(instance, classes)[source]

Returns true, if instance is instance of any (iiOfAny) of the classes.

This function doesn’t use isinstance() check, it just compares the class names.

This can be generaly dangerous, but it is really useful when you are comparing class serialized in one module and deserialized in another.

This causes, that module paths in class internals are different and isinstance() and type() comparsions thus fails.

Use this function instead, if you wan’t to check what type is your deserialized message.

  • instance (object) – class instance you want to know the type
  • classes (list) – classes, or just one class you want to compare - func automatically converts nonlist/nontuple parameters to list

True if instance can be instance of any of the classes.

Return type:


Project details

Whole project is hosted at github:

and can be installed using PIP:

pip install edeposit.amqp.serializers

Indices and tables