xe.py is a Python module to make it very easy to work with XML. I have
attempted to make xe classes very Pythonic and intuitive. xe is intended
for working with structured XML, such as syndication feed files, but I
have added some support for unstructured XML as well.
Example:
[color=blue][color=green][color=darkred]
>>> import xe
>>> bird = xe.TextElement( "bird")
>>> bird.attrs["type"] = "Norwegian Blue"
>>> bird.text = "just resting"
>>> print bird[/color][/color][/color]
<bird type="Norwegian Blue">just resting</bird>[color=blue][color=green][color=darkred]
>>> cheese = xe.TextElement( "cheese")
>>> cheese.text = "a bit runny"
>>> examples = xe.NestElement( "examples")
>>> examples.bird = bird
>>> examples.cheese = cheese
>>> print examples[/color][/color][/color]
<examples>
<bird type="Norwegian Blue">just resting</bird>
<cheese>a bit runny</cheese>
</examples>
PyFeed is a Python module package set with modules for working with
syndication feeds. These modules use the xe classes. PyFeed currently
includes modules for working with Atom feeds, RSS 2.0 feeds, OPML 1.0
files, and OPML files.
Example:
[color=blue][color=green][color=darkred]
>>> import feed.rss
>>> channel = feed.rss.Channe l()
>>> lst_errors = []
>>> channel.import_ xml("http://www.planetpytho n.org/rss20.xml", lst_errors)[/color][/color][/color]
<feed.rss.Chann el object at 0x77d0c18c>[color=blue][color=green][color=darkred]
>>> print len(lst_errors) or "No errors!"[/color][/color][/color]
No errors![color=blue][color=green][color=darkred]
>>> print channel.title[/color][/color][/color]
<title>Unoffici al Planet Python</title>[color=blue][color=green][color=darkred]
>>> print len(channel.ite ms)[/color][/color][/color]
45[color=blue][color=green][color=darkred]
>>> print channel.items[0].title.text[/color][/color][/color]
Mike Fletcher: Minor TwistedSNMP release is up[color=blue][color=green][color=darkred]
>>> item = channel.items[0]
>>> print item.pub_date.t ext[/color][/color][/color]
Thu, 23 Mar 2006 15:45:31 -0800[color=blue][color=green][color=darkred]
>>> # note that the date printed with "-0800", my time zone[/color][/color][/color]
....[color=blue][color=green][color=darkred]
>>> item.pub_date.u pdate()[/color][/color][/color]
<feed.rss.PubDa te object at 0x778fed4c>[color=blue][color=green][color=darkred]
>>> print item.pub_date[/color][/color][/color]
<pubDate>Thu, 23 Mar 2006 17:15:55 -0800</pubDate>[color=blue][color=green][color=darkred]
>>> item.pub_date = "not a valid timestamp"[/color][/color][/color]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/local/lib/python/xe.py", line 1327, in __setattr__
o.direct(value)
File "/usr/local/lib/python/xe.py", line 1148, in direct
raise ValueError, "value must be a valid timestamp string"
ValueError: value must be a valid timestamp string[color=blue][color=green][color=darkred]
>>> item.pub_date = "01 Jan 2006 08:01:23 GMT"
>>> print item.pub_date.t ext[/color][/color][/color]
Sun, 01 Jan 2006 00:01:23 -0800[color=blue][color=green][color=darkred]
>>> print item.pub_date.t f[/color][/color][/color]
1136102483.0
If there is any interest in these modules, I'll probably put them on
Sourceforge.net and on the Cheese Shop someday. For now, they live on my
personal web space:
I look forward to any feedback.
--
Steve R. Hastings "Vita est"
steve@hastings. org http://www.blarg.net/~steveha
attempted to make xe classes very Pythonic and intuitive. xe is intended
for working with structured XML, such as syndication feed files, but I
have added some support for unstructured XML as well.
Example:
[color=blue][color=green][color=darkred]
>>> import xe
>>> bird = xe.TextElement( "bird")
>>> bird.attrs["type"] = "Norwegian Blue"
>>> bird.text = "just resting"
>>> print bird[/color][/color][/color]
<bird type="Norwegian Blue">just resting</bird>[color=blue][color=green][color=darkred]
>>> cheese = xe.TextElement( "cheese")
>>> cheese.text = "a bit runny"
>>> examples = xe.NestElement( "examples")
>>> examples.bird = bird
>>> examples.cheese = cheese
>>> print examples[/color][/color][/color]
<examples>
<bird type="Norwegian Blue">just resting</bird>
<cheese>a bit runny</cheese>
</examples>
PyFeed is a Python module package set with modules for working with
syndication feeds. These modules use the xe classes. PyFeed currently
includes modules for working with Atom feeds, RSS 2.0 feeds, OPML 1.0
files, and OPML files.
Example:
[color=blue][color=green][color=darkred]
>>> import feed.rss
>>> channel = feed.rss.Channe l()
>>> lst_errors = []
>>> channel.import_ xml("http://www.planetpytho n.org/rss20.xml", lst_errors)[/color][/color][/color]
<feed.rss.Chann el object at 0x77d0c18c>[color=blue][color=green][color=darkred]
>>> print len(lst_errors) or "No errors!"[/color][/color][/color]
No errors![color=blue][color=green][color=darkred]
>>> print channel.title[/color][/color][/color]
<title>Unoffici al Planet Python</title>[color=blue][color=green][color=darkred]
>>> print len(channel.ite ms)[/color][/color][/color]
45[color=blue][color=green][color=darkred]
>>> print channel.items[0].title.text[/color][/color][/color]
Mike Fletcher: Minor TwistedSNMP release is up[color=blue][color=green][color=darkred]
>>> item = channel.items[0]
>>> print item.pub_date.t ext[/color][/color][/color]
Thu, 23 Mar 2006 15:45:31 -0800[color=blue][color=green][color=darkred]
>>> # note that the date printed with "-0800", my time zone[/color][/color][/color]
....[color=blue][color=green][color=darkred]
>>> item.pub_date.u pdate()[/color][/color][/color]
<feed.rss.PubDa te object at 0x778fed4c>[color=blue][color=green][color=darkred]
>>> print item.pub_date[/color][/color][/color]
<pubDate>Thu, 23 Mar 2006 17:15:55 -0800</pubDate>[color=blue][color=green][color=darkred]
>>> item.pub_date = "not a valid timestamp"[/color][/color][/color]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/local/lib/python/xe.py", line 1327, in __setattr__
o.direct(value)
File "/usr/local/lib/python/xe.py", line 1148, in direct
raise ValueError, "value must be a valid timestamp string"
ValueError: value must be a valid timestamp string[color=blue][color=green][color=darkred]
>>> item.pub_date = "01 Jan 2006 08:01:23 GMT"
>>> print item.pub_date.t ext[/color][/color][/color]
Sun, 01 Jan 2006 00:01:23 -0800[color=blue][color=green][color=darkred]
>>> print item.pub_date.t f[/color][/color][/color]
1136102483.0
If there is any interest in these modules, I'll probably put them on
Sourceforge.net and on the Cheese Shop someday. For now, they live on my
personal web space:
I look forward to any feedback.
--
Steve R. Hastings "Vita est"
steve@hastings. org http://www.blarg.net/~steveha