[lxml-dev] atom model
Stefan Behnel
stefan_ml at behnel.de
Thu Jun 7 09:46:46 CEST 2007
Hi Ian,
Ian Bicking wrote:
> After writing a object model for Atom that serialized to and parsed from
> XML, I realized Atom and its XML representation really shouldn't be
> separated, so I created some custom elements to make it a bit easier to
> handle, while still using the XML as the sole source of information.
That's the way I would do it, too.
I brought up the idea of having an "lxml.elementlib" package with this kind of
Namespace implementations a couple of times, but I now think it would actually
make sense. However, "lxml.ns" might be a better name (XIST uses the same
package name, BTW).
That way, we'd have "lxml.ns.html" and "lxml.ns.atom" and hopefully others in
the future.
> I'm a little unsure what to do about the namespaces. Everything is in a
> namespace, but it's tedious to put in everywhere. I've put in some
> little helper methods internally, and mostly you don't need to use the
> namespace globally, but I'm unsure about it all. I've created an
> Element() function that automatically adds the namespace if no namespace
> is given; helps a little I guess.
As you say below, builder.py would definitely make this more usable. RSS is
even the example FL uses to present the factory:
http://online.effbot.org/2006_11_01_archive.htm#et-builder-rss
> The standard ways of creating elements is a bit tedious, really. I
> guess builder can help there a bit, though I don't see a way to give my
> own parser (which I need in this case). Any suggestions about any of it
> are welcome.
I added a "parser" keyword argument to the factory (trunk), which reuses the
"makeelement" method of the parser for Element creation.
> https://svn.openplans.org/svn/TaggerStore/trunk/taggerstore/atom.py
A few remarks:
- for the lookup, you can either use the Namespace registry mechanism of lxml,
which makes it a global setup (I'm considering to make this parser local in
lxml 2.0)
http://codespeak.net/lxml/dev/element_classes.html#namespace-class-lookup
http://codespeak.net/lxml/dev/element_classes.html#id1
or just a dictionary (.get) instead of the many ifs.
- please avoid "@property" as lxml wants to stay compatible with Python 2.3.
Otherwise: looks like you're on the right track.
Stefan
More information about the lxml-dev
mailing list