Sign In/My Account | View Cart  
advertisement

Article:
 XML Data Bindings in Python
Subject: XML data binding only half done, Pt II
Date: 2003-06-12 16:33:42
From: Peter Herndon
Response to: XML data binding only half done

I've looked briefly at Skyron, and it doesn't really "fit my head". More specifically, I'd rather not have to learn some esoteric XML format for creating transformation recipes. In Python at least, it's just as easy to code it yourself.


YAML is not without its own set of problems, but they look eminently solvable. For the project on which I'm currently working, I need an all-Python solution. My target platform is Windows, and I haven't the luxury of Visual Studio or Cygwin. So, of my two candidates, Syck and PyYAML, I must choose PyYAML. And not unwillingly, since it seems ridiculously easy to use so far. I have run into a limitation, though, in its limited support for Python types. Specifically, PyYAML can handle most Python 2.1 or earlier built-in types, and handles classic classes quite well, but it cannot currently handle new-style classes by default. Thankfully, my current project does not require my serializable classes be new-style, so I am safe.


PyYAML does have a means of getting around this limitation, though I have not yet fully explored its usage. If your class defines a to_yaml() method, serializing calls this method instead of the underlying type-munging methods in yaml.Dumper(). to_yaml() should return a tuple consisting of the data you wish to serialize and a string defining the type of your class. Thus if your new-style class simply inherits from object, you can return (self.__dict__, " !!classname\n") and everything should be fine. N.B.: I have not thoroughly tested this solution, as I have not yet begun to deserialize these results, nor have I made sure that the output is valid YAML. Still, this solution provides a first step.


No Previous Message Previous Message Move up to Parent Message Up Next Message No Next Message


Sponsored By: