serpent, a serializer based around ast.literal_eval

Discussion in 'Python' started by Irmen de Jong, Jan 21, 2013.

  1. 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
    Irmen de Jong, Jan 21, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gabriel Genellina
    Replies:
    0
    Views:
    809
    Gabriel Genellina
    Sep 17, 2009
  2. Irmen de Jong
    Replies:
    0
    Views:
    98
    Irmen de Jong
    Apr 3, 2013
  3. Frank Millman

    Question about ast.literal_eval

    Frank Millman, May 20, 2013, in forum: Python
    Replies:
    2
    Views:
    116
    Frank Millman
    May 21, 2013
  4. Carlos Nepomuceno

    RE: Question about ast.literal_eval

    Carlos Nepomuceno, May 20, 2013, in forum: Python
    Replies:
    0
    Views:
    69
    Carlos Nepomuceno
    May 20, 2013
  5. Chris Angelico

    Re: Question about ast.literal_eval

    Chris Angelico, May 20, 2013, in forum: Python
    Replies:
    0
    Views:
    83
    Chris Angelico
    May 20, 2013
Loading...

Share This Page