theme selector

light blue screenshot grey screenshot navy screenshot dark green screenshot red and black screenshot
 

by Tony Chang
tony@ponderer.org

All opinions on this site are my own and do not represent those of my employer.

Creative Commons Attribution License

P4X

Sep 19, 2005, 03:05am EDT

 

 

One of the new features in Firefox 1.5 is support for ECMAScript for XML (E4X). It’s pretty crazy and it lets you do all sorts of wacky stuff with very “scripty” syntax.

So, I thought that it might be nice to be able to use a similar syntax in python. With all sorts of abusive operator overloading, I came up with P4X. It works in a very similar way.

>>> import urllib2, p4x
>>> RSS_URL = 'http://ponderer.org/index.xml'
>>> rss_raw = urllib2.urlopen(RSS_URL).read()
>>> rss = p4x.P4X(rss_raw)
>>> print rss.channel.title
<title>Are You Pondering What I'm Pondering?</title>
>>> rss.channel.title = 'new title' >>> print rss.channel.title <title>new title</title>
>>> print len(rss.channel.item) 6 >>> print rss.channel.item.title <title>ads in feeds</title> <title>scribbly</title> <title>canvas demo</title> <title>open source catastrophe</title> <title>usability, power, and senior project</title> <title>flock</title>
>>> rss.channel.item[6] = '' >>> rss.channel.item[6].title = 'E4X is hot!' >>> rss.channel.item[6].description = 'OMG!!1!one' >>> rss.channel.item[6].link = 'http://ponderer.org/p4x' >>> rss.channel.item[6].guid = 'non-unique guid' >>> rss.channel.item[6].guid._isPermalink = 'false' >>> print rss.channel.item[6] <item><title>E4X is hot!</title><description>OMG!!1!one </description><link>http://ponderer.org/p4x</link><guid isPermalink=”false”>non-unique guid</guid></item> >>> print rss.channel.item.title <title>ads in feeds</title> <title>scribbly</title> <title>canvas demo</title> <title>open source catastrophe</title> <title>usability, power, and senior project</title> <title>flock</title> <title>E4X is hot!</title>

Some things are pretty broken and probably unfixable (del doesn’t have quite the same accuracy, attributes start with _ instead of @, no support for *, etc), but this is still more convenient than say, using the DOM.

allowed HTML: a, blockquote, ul, ol, li, dl, dt, dd, b, i, strong, em, code, abbr, acronym, sub, sup, span, pre

allowed HTML: a, blockquote, ul, ol, li, dl, dt, dd, b, i, strong, em, code, abbr, acronym, sub, sup, span, pre