web developer & system programmer

coder . cl

ramblings and thoughts on programming...


[ann] pyxser-1.4.6r released

published: 03-08-2010 / updated: 03-08-2010
posted in: c, programming, projects, python, pyxser
by Daniel Molina Wegener

pyxser is a Python extension which holds functions to serialize and deserialize Python objects into XML. It’s one of my FOSS projects. Some characteristics of of this serializer are the fact that it can serialize objects with circular references and cross referenced objects — try to serialize an object with circular references in .NET. Other facts is that it uses an O(n) algorithm, with n equal to the number of objects and not their references in the object tree. Today I’ve released pyxser-1.4.6r, let me show what I’ve done…


metrics

pyxser is complex. It requires some compiler construction techniques. The main one is memoization, which uses an internal and thread safe cache to allow the serializer to create unique XML instances of the pyxs:obj element. Also, it uses mutually recursive functions while is harvesting objects from the object tree using an preorder traversal tree algorithm. The result of implementing it, is the following table:

metric overall per module
number of modules 1  — 
lines of code 5343 5343
mccabe’s cyclomatic number 1025 1025
loc/com 10.024  — 
mvg/com 1.923  — 

But there is a fact, pyxser is fast as hell!. It can do 10010 serializations in 1.992 seconds and 11011 deserializations in 6.910 seconds. So, I was introducing some enhancements and securing the code doing code harndening test and shorting code flow paths. Now the code looks flat and more elegant.


profiling

After securing the code I got a slower pyxser implementation. So, I was impressed since I got to double the time on certain internal functions. Here is the metric on how code was executed:

But after some code cleanup, refactoring and code path shortening, I got much better results, and I’ve maintained the original perfomance with hardened, ordered and flat code. The reduction of nested statements was quite hard, but I’ve done it. It has just three levels as an average of nested statements:


enhancements

pyxser now can serialize, for example SqlAlchemy objects, sent them through the network and restore the instance so far away. For those kind of objects you must follow a simple rule, use the selector argument and the depth argument. The pyxser distribution comes with some test files, test-utf8-sqlalchemy.py is one of those files, where we define a class User:

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    fullname = Column(String)
    password = Column(String)

    def __init__(self):
        self.name = None
        self.fullname = None
        self.password = None

    def get_set(self):
        return (self.name, self.fullname, self.password)

    def __repr__(self):
        return "User('%s','%s', '%s')" % self.get_set()

The first rule is quite simple: the constructor must be a simple constructor, without arguments, but it can hold initialization code, just remain it without arguments. And the second rule is to use the selector argument:

def sqlalch_filter(v):
    r = ((not callable(v[1])) 
         and (not (v[0].startswith("_"))) 
         and ((v[0] != "metadata")))
    return r

def sqlalch_selector(o):
    r = dict(filter(sqlalch_filter, inspect.getmembers(o)))
    return r

serialized = pyxser.serialize(obj = ed_user, enc = 'utf-8', selector = sqlalch_selector, depth = 2)

This will create a very simple object:

<?xml version="1.0" encoding="utf-8"?>
<pyxs:obj xmlns:pyxs="http://projects.coder.cl/pyxser/model/" version="1.0" type="User" module="testpkg.sample" objid="id170543564">
  <pyxs:prop type="str" name="fullname">Ed Jones</pyxs:prop>
  <pyxs:prop type="str" name="password">password</pyxs:prop>
  <pyxs:prop type="str" name="name">ed</pyxs:prop>
</pyxs:obj>

But when it is restored on the end, it will remain intact.

Some people was saying that pyxser do not comes with documentation. Those people have not seen the documentation. pyxser as any Python extension, is self documented, this means that you must launch pydoc to read it. If you never have used pydoc, just try it: pydoc -p8080, this will create a small web server running on localhost to let you read the documentation. It has some other interesting parameters, others than depth and selector, such as typemap, which allows you to create custom serializations of certain objects — such as files.


where is located

The project is hosted at SourceForge: pyxser@sourceforge and the home page of the project is located pyxser@coder.cl.


No coments yet.

post a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>