It’s just data

Lazy DOM

Creating XML to Python mappings seems to be quite popular these days.  Here's mine.  Expect this to evolve over the next few days.

Hm, the test fails:

[blalor@beaker ...ps/Phonebook/data]> python lazydom.py
Traceback (most recent call last):
  File "lazydom.py", line 102, in ?
  for name in feed[atom.entry][atom.author][atom.name]:
  File "lazydom.py", line 83, in _iter_
  return result._iter_()
AttributeError: 'list' object has no attribute '_iter_'

Posted by blalor at

What version of python are you using?  Iterator types are new in version 2.2.

On some versions of Redhat, this is installed as python2.

Posted by Sam Ruby at

I got the same error and I'm using Python 2.2.2 on Red Hat. I fixed it by changing line 83 to "return iter(result)"

Posted by Simon Willison at

Feedback on lazydom.py

Here's my feedback on the clever lazydom.py. You can use yield to implement __iter__: from __future__ import generators # must be first line of python code ... def __iter__(self): for element in self.list: yield self.new(element) You can use the...

Excerpt from gary burd at

Thanks, all, for the feedback.  Particularly Gary.

Here's a new version.  Tested on Python 2.2 and Python 2.3.

One note: I am definitely trying to maintain the XPath-like abstraction whereby attributes are simply specially named elements.  I did, however, add some support to the namespace class to make namespace qualified attributes easier to access.

Posted by Sam Ruby at

Look a look at the SF project pywebsvcs; Brian Overhof is doing a great job of adding schema<->python code generation.

Posted by Rich Salz at

Another option for this kind of thing is Aaron's xmltramp: http://www.aaronsw.com/2002/xmltramp/

Posted by Nelson at

This is fun.  Python's '/' operator can be used to implement path syntax.  If you add this line of code to the lazydom class:

  __div__ = __getitem__

then you can write this:

  # iteration and traversal
  for name in feed/atom.entry/atom.author/atom.name:
    print name
  print

  # attribute access and comparison
  print feed/atom.generator/'@name'
  print feed/atom.generator/'@name' == 'Blogger'

Posted by Gary Burd at

Egads, Gary, you're evil!  And here I thought overloading '+' to mean both numeric addition and string concatenation was bad! :-)

Posted by Ken MacLeod at

Gary: deliciously evil.  Playing with it a bit, my one problem is that the precedence order is wrong.

feed/atom.entry[0] will be evaluated as:

My (initial) conclusion: perhaps div should be considered instead of index, but having both is dangerous.

Posted by Sam Ruby at

More evil.  If you add "_floordiv_ = find", then you can write:

  for issued in feed.find//atom.issued:
  print issued

Posted by Gary Burd at

Java and Web Services are not a match made in heaven

The more I think about this, and read about it, e.g. here, or here, or think about it in this context, the more firmly I believe that Java, or any strongly typed programming language, is a good vehicle for building... [more]

Trackback from Random Stuff

at

What's with that trackback? I assumed it's autogenerated, but it's
missing a critical "not". Here's from the link target:

"""
The more I think about this, and read about it, e.g. here, or here, or think about it in this context, the more firmly I believe that Java, or any strongly typed programming language, is not a good vehicle for building document-oriented Web services.
"""

Maybe the 1st version of the linked entry was wrong?

Posted by Jean Jordaan at

New lazydom version

Here's the code.  Key new features vs the original:  element[:] returns all children of all elements in the list element[f] will apply function f to all elements in the list, and act either as a  filter or as a  map or even... [more]

Trackback from Sam Ruby

at

The content is utmost interesting! I have completely enjoyed reading your points and have come to the conclusion that you are right about many of them.  You are great, and your efforts are outstanding!

Posted by robbery bob 2 download at

Add your comment