[XML-SIG] Schema -> Python Objects framework?

David Mertz, Ph.D. mertz@gnosis.cx
Fri, 24 May 2002 14:56:17 -0400


Ian Sparks <ians@etrials.com> wrote:
|Thanks Praveen, looks like xml_Objectify does some of what I want.
|I have on occasion found myself writing python classes
|with the ability to read and write themselves to XML :

I'm coming into the middle of this conversation, so I might be missing
some context.  But I think that xml_pickle might come even closer to
accomplishing what you want.

All my utilities/libraries are now in a common package, and you can find
it at:

    http://gnosis.cx/download/Gnosis_Utils-current.tar.gz

You've always been able to add the ability to serialize an object to
(xml_pickle dialect) XML, by mixing in inheritence from XML_Pickler.
But I've recently, quietly, added a (mostly untested, semi-documented)
metaclass approach to the idea.  I find this possibility really quite
fascinating, but probably too magical for most users:

    Python 2.2 (#0, Dec 24 2001, 18:42:48) [EMX GCC 2.8.1] on os2emx
    Type "help", "copyright", "credits" or "license" for more information
    >>> import gnosis.magic
    >>> __metaclass__ = gnosis.magic.MetaPickler
    >>> class Boring:
    ...     def __init__(self):
    ...         self.this = 'that'
    ...         self.spam = 'eggs'
    ...     def print_spam(self):
    ...         print self.spam
    ...
    >>> boring = Boring()
    >>> boring.print_spam()
    eggs
    >>> print boring.dumps()
    <?xml version="1.0"?>
    <!DOCTYPE PyObject SYSTEM "PyObjects.dtd">
    <PyObject module="__main__" class="Boring" id="1276364">
    <attr name="this" type="string" value="that" />
    <attr name="spam" type="string" value="eggs" />
    </PyObject>

Basically, the contrast between xml_pickle and xml_objectify is the
following:  if you have arbitrary Python objects, and you want to make
XML out of them, use xml_pickle; if you have arbitrary XML dialects, and
you want to make Python objects out of them, use xml_objectify.

Unfortunately, I think the two aspects cannot be 100% unified.

Yours, David...

--
---[ to our friends at TLAs (spread the word) ]--------------------------
Echelon North Korea Nazi cracking spy smuggle Columbia fissionable Stego
White Water strategic Clinton Delta Force militia TEMPEST Libya Mossad
---[ Postmodern Enterprises <mertz@gnosis.cx> ]--------------------------