web developer & system programmer

coder . cl

ramblings and thoughts on programming...


pyxser, a work in progress

published on 10-01-2009 / updated: 10-01-2009
posted in: projects, python, pyxser
by Daniel Molina Wegener

Who knows me and knows my work, is concious about my current projects. One of them is pyxser, an XML serialization/deserialization Python extension written in C. I’ve finally finished the preliminar serialization algorithm, as O(n) algorithm. It’s a recursive one, that serializes the a series of nested Python objects into XML.

Now the hard work is comming. The serialization algorithm requires a tow pass over the XML tree. This means a O(2n) algorithm. I hope that I will find a better and faster solution, something near to O(n). My current strategy it’s to make one pass to collect object references and then create the object tree in the second pass. If you know a better option, just submit it as comment or participate in the extension development when it will be released as beta release.

When it’s finished, I will bring more options to the serializer extension, but for now, I want it working and working well, without memory leaks or similar bugs. In example, I will try to register custom tags under python doclets or under documentation comments, or similar features.

A small example of nested objects with circular references, we get this sample module in the package testpkg.sample inside the unit tests of pyxser, which it’s a part of a python module:


__all__ = [
    'SubNestedChild',
    'NestedChild',
    'ChildObject',
    'ParentObject']

class SubNestedChild:
    subnested1 = None
    def __init__(self, m1):
        self.subnested1 = m1

class NestedChild:
    nested1 = None
    nested2 = None
    nested3 = None
    nested4 = None
    def __init__(self, m1, m2, m3):
        self.nested1 = m1
        self.nested2 = m2
        self.nested3 = m3

class ChildObject:
    child1 = None
    child2 = None
    child3 = None
    child4 = None
    def __init__(self, m1, m2, m3):
        self.child1 = m1
        self.child2 = m2
        self.child3 = m3

class ParentObject:
    parent1 = None
    parent2 = None
    parent3 = None
    def __init__(self, m1, m2, m3):
        self.parent1 = m1
        self.parent2 = m2
    def child(self, m1, m2, m3):
        self.parent3 = ChildObject(m1, m2, m3)
    def nested(self, m1, m2, m3):
        self.parent3.child4 = NestedChild(m1, m2, m3)
    def subnested(self, m1):
        self.parent3.child4.nested4 = SubNestedChild(m1)

Then we get a single example of object instances in the next small python program:

import pyxser
import testpkg.sample

if __name__ == "__main__":
    test = testpkg.sample.ParentObject("hola", "chao", 2354345L)
    test.child("hi", 4.5, 2354)
    test.nested("holahola", 345, "hola")
    test.subnested(test)
    print pyxser.serialize_xml(test)

As result of the XML serialization we get the following code:

<?xml version="1.0"?>
<ParentObject module="testpkg.sample" id="136095148">
  <ChildObject module="testpkg.sample" id="136094284">
    <child1 type="str">hi</child1>
    <child2 type="float">4.5</child2>
    <child3 type="int">2354</child3>
    <NestedChild module="testpkg.sample" id="136095180">
      <SubNestedChild module="testpkg.sample" id="136095244">
        <ParentObject module="testpkg.sample" reference="#136095148" name="subnested1"/>
      </SubNestedChild>
      <nested1 type="str">holahola</nested1>
      <nested2 type="int">345</nested2>
      <nested3 type="str">hola</nested3>
    </NestedChild>
  </ChildObject>
  <parent2 type="str">chao</parent2>
  <parent1 type="str">hola</parent1>
</ParentObject>


4 comments to “pyxser, a work in progress”

  1. We want deserialize feature! :X

  2. [...] the last post was signed with pyxser as finished python-object to xml serializer, but my mistake was to forget [...]

  3. [...] we continue with our last example, we have sample called [...]

  4. [...] my last posts: "pyxser, a work in progress", "pyxser, serializing collections" and "pyxser, a work in progress". [...]

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>