serpent, a serializer based around ast.literal_eval

Irmen de Jong irmen.NOSPAM at xs4all.nl
Mon Jan 21 14:25:54 EST 2013


Hi,

I've been toying a bit with ast.literal_eval. I've come up with "serpent", a serializer
based around that. Which means that it takes a Python object tree and turns it into a
serialized form that can be safely read back by ast.literal_eval().

Why I wrote serpent and didn't simply use repr()+ast.literal_eval:

* it serializes directly to bytes (utf-8 encoded), instead of a string, so it can
immediately be saved to a file or sent over a socket
* it encodes byte-types as base-64 instead of inefficient escaping notation that repr
would use (this does mean you have to base-64 decode these strings manually on the
receiving side to get your bytes back)
* it contains a few custom serializers for several additional Python types such as uuid,
datetime, array and decimal
* it tries to serialize unrecognised types as a dict (you can control this with
__getstate__ on your own types)
* it can create a pretty-printed (indented) output for readability purposes
* it works around a few quirks of ast.literal_eval() on the various Python implementations.

It works with Python 2.6+ (including 3.x), IronPython 2.7+, Jython 2.7+.

Serpent can be downloaded from Pypi: http://pypi.python.org/pypi/serpent
A simple example session can be seen here: https://gist.github.com/4588429


I'm considering writing Java and .NET counterparts for this as well so I'll be able to
exchange messages between the three.

What do you think? Would you consider this useful at all?


Cheers
Irmen de Jong



More information about the Python-list mailing list