<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>TikiRobot! &#187; python</title>
	<atom:link href="http://www.tikirobot.net/wp/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tikirobot.net/wp</link>
	<description>Mai Tais and Blinky Lights, Ahoy!</description>
	<lastBuildDate>Wed, 01 Feb 2012 22:14:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Replacing URLs with links&#8230; halp?</title>
		<link>http://www.tikirobot.net/wp/2010/01/26/replacing-urls-with-links-halp/</link>
		<comments>http://www.tikirobot.net/wp/2010/01/26/replacing-urls-with-links-halp/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 04:16:03 +0000</pubDate>
		<dc:creator>rajbot</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[support]]></category>

		<guid isPermaLink="false">http://www.tikirobot.net/wp/?p=2973</guid>
		<description><![CDATA[I found this crazy regex for matching URLs in python. Even though some very smart people went through the trouble of concocting that regex, I can&#8217;t seem to use for all test cases&#8230; hmm. I&#8217;m trying to turn bare urls into links for our Twitter sidebar widget, and it almost works, except for the last [...]]]></description>
			<content:encoded><![CDATA[<p>I found <a href="http://flanders.co.nz/2009/11/08/a-good-url-regular-expression-repost/">this crazy regex</a> for matching URLs in python. Even though some very smart people went through the trouble of concocting that regex, I can&#8217;t seem to use for all test cases&#8230; hmm.</p>
<p>I&#8217;m trying to turn bare urls into links for our Twitter sidebar widget, and it almost works, except for the last crazy case below (query string + anchor):</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/python</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> convertLinks<span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;">#crazy regex from Shag, based on http://flanders.co.nz/2009/11/08/a-good-url-regular-expression-repost/</span>
&nbsp;
    prog = <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'(?#FQURL)(?:(?#Protocol)(?:(?:ht|f)tp(?:s?)<span style="color: #000099; font-weight: bold;">\:</span><span style="color: #000099; font-weight: bold;">\/</span><span style="color: #000099; font-weight: bold;">\/</span>)(?#Username:Password)(?:<span style="color: #000099; font-weight: bold;">\w</span>+:<span style="color: #000099; font-weight: bold;">\w</span>+@)?(?#Subdomains)(?:(?:[-<span style="color: #000099; font-weight: bold;">\w</span>]+<span style="color: #000099; font-weight: bold;">\.</span>)*(?#TopLevel Domains)(?:[a-z]+<span style="color: #000099; font-weight: bold;">\.</span>?))(?#Port)(?::[<span style="color: #000099; font-weight: bold;">\d</span>]{1,5})?<span style="color: #000099; font-weight: bold;">\.</span>?(?#Directories)(?:(?:(?:<span style="color: #000099; font-weight: bold;">\/</span>(?:[-<span style="color: #000099; font-weight: bold;">\w</span>~!$+|.,=]|%[a-f<span style="color: #000099; font-weight: bold;">\d</span>]{2})+)+|<span style="color: #000099; font-weight: bold;">\/</span>|)+|<span style="color: #000099; font-weight: bold;">\?</span>|#)?(?#Query)(?:(?:<span style="color: #000099; font-weight: bold;">\?</span>(?:[-<span style="color: #000099; font-weight: bold;">\w</span>~!$+|.,*:]|%[a-f<span style="color: #000099; font-weight: bold;">\d</span>{2}])+=?(?:[-<span style="color: #000099; font-weight: bold;">\w</span>~!$+|.,*:=]|%[a-f<span style="color: #000099; font-weight: bold;">\d</span>]{2})*)(?:&amp;amp;(?:[-<span style="color: #000099; font-weight: bold;">\w</span>~!$+|.,*:]|%[a-f<span style="color: #000099; font-weight: bold;">\d</span>{2}])+=?(?:[-<span style="color: #000099; font-weight: bold;">\w</span>~!$+|.,*:=]|%[a-f<span style="color: #000099; font-weight: bold;">\d</span>]{2})*)*)*(?#Anchor)(?:#(?:[-<span style="color: #000099; font-weight: bold;">\w</span>~!$+|.,*:=]|%[a-f<span style="color: #000099; font-weight: bold;">\d</span>]{2})*)?|(?#BareURL)(?#Username:Password)(?:<span style="color: #000099; font-weight: bold;">\w</span>+:<span style="color: #000099; font-weight: bold;">\w</span>+@)?(?#Subdomains)(?:(?:[-<span style="color: #000099; font-weight: bold;">\w</span>]+<span style="color: #000099; font-weight: bold;">\.</span>)+(?#TopLevel Domains)(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|travel|[a-z]{2}))(?#Port)(?::[<span style="color: #000099; font-weight: bold;">\d</span>]{1,5})?<span style="color: #000099; font-weight: bold;">\.</span>?(?#Directories)(?:(?:(?:<span style="color: #000099; font-weight: bold;">\/</span>(?:[-<span style="color: #000099; font-weight: bold;">\w</span>~!$+|.,=]|%[a-f<span style="color: #000099; font-weight: bold;">\d</span>]{2})+)+|<span style="color: #000099; font-weight: bold;">\/</span>|)+|<span style="color: #000099; font-weight: bold;">\?</span>|#)?(?#Query)(?:(?:<span style="color: #000099; font-weight: bold;">\?</span>(?:[-<span style="color: #000099; font-weight: bold;">\w</span>~!$+|.,*:]|%[a-f<span style="color: #000099; font-weight: bold;">\d</span>{2}])+=?(?:[-<span style="color: #000099; font-weight: bold;">\w</span>~!$+|.,*:=]|%[a-f<span style="color: #000099; font-weight: bold;">\d</span>]{2})*)(?:&amp;amp;(?:[-<span style="color: #000099; font-weight: bold;">\w</span>~!$+|.,*:]|%[a-f<span style="color: #000099; font-weight: bold;">\d</span>{2}])+=?(?:[-<span style="color: #000099; font-weight: bold;">\w</span>~!$+|.,*:=]|%[a-f<span style="color: #000099; font-weight: bold;">\d</span>]{2})*)*)*(?#Anchor)(?:#(?:[-<span style="color: #000099; font-weight: bold;">\w</span>~!$+|.,*:=]|%[a-f<span style="color: #000099; font-weight: bold;">\d</span>]{2})*)?)'</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> prog.<span style="color: black;">sub</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'&lt;a href=&quot;<span style="color: #000099; font-weight: bold;">\g</span>&amp;lt;0&amp;gt;&quot;&gt;<span style="color: #000099; font-weight: bold;">\g</span>&amp;lt;0&amp;gt;&lt;/a&gt;'</span>, <span style="color: #008000;">str</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#single url</span>
<span style="color: #ff7700;font-weight:bold;">print</span> convertLinks<span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://tikirobot.net<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#string</span>
<span style="color: #ff7700;font-weight:bold;">print</span> convertLinks<span style="color: black;">&#40;</span><span style="color: #483d8b;">'See http://tikirobot.net for more info.<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#string w/ anchor</span>
<span style="color: #ff7700;font-weight:bold;">print</span> convertLinks<span style="color: black;">&#40;</span><span style="color: #483d8b;">'See http://tikirobot.net/#foo for more info.<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#two urls in a string</span>
<span style="color: #ff7700;font-weight:bold;">print</span> convertLinks<span style="color: black;">&#40;</span><span style="color: #483d8b;">'See http://tikirobot.net or http://wikipedia.org for more info.<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#some test urls from flanders.co.nz</span>
<span style="color: #ff7700;font-weight:bold;">print</span> convertLinks<span style="color: black;">&#40;</span><span style="color: #483d8b;">'This is a google search: http://www.google.com/search?q=good+url+regex&amp;amp;rls=com.microsoft:*&amp;amp;ie=UTF-8&amp;amp;oe=UTF-8&amp;amp;startIndex=&amp;amp;startPage=1<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> convertLinks<span style="color: black;">&#40;</span><span style="color: #483d8b;">'ftp://joe:password@ftp.filetransferprotocal.com is a ftp url<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> convertLinks<span style="color: black;">&#40;</span><span style="color: #483d8b;">'There is a bare url google.ru somewhere in this sentence<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#test cases from shag</span>
<span style="color: #ff7700;font-weight:bold;">print</span> convertLinks<span style="color: black;">&#40;</span><span style="color: #483d8b;">'query string: https://some-url.com/?query=&amp;amp;name=joe&amp;amp;filter=*.*<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> convertLinks<span style="color: black;">&#40;</span><span style="color: #483d8b;">'both a query string and an anchor with no host name separator slash: https://some-url.com?query=&amp;amp;name=joe?filter=*.*#some_anchor<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> convertLinks<span style="color: black;">&#40;</span><span style="color: #483d8b;">'both a query string and an anchor: https://some-url.com/?query=&amp;amp;name=joe&amp;amp;filter=*.*#some_anchor<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> convertLinks<span style="color: black;">&#40;</span><span style="color: #483d8b;">'DNS name with a concluding period: http://some-url.com./<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> convertLinks<span style="color: black;">&#40;</span><span style="color: #483d8b;">'DNS name with a concluding period and query string: http://some-url.com./?foo=bar<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> convertLinks<span style="color: black;">&#40;</span><span style="color: #483d8b;">'single-component DNS name plus root: http://to./<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> convertLinks<span style="color: black;">&#40;</span><span style="color: #483d8b;">'single-component DNS name: http://to/<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> convertLinks<span style="color: black;">&#40;</span><span style="color: #483d8b;">'words with slashes: unread/unregistered<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span></pre></div></div>

<p><del datetime="2010-01-27T04:27:08+00:00">Also note my double-grouping on the regex.. There must be a better way!</del><br />
Update, figured out that the group zero backreference is \g&lt;0&gt; (\0 doesn&#8217;t work, so I was double-grouping so that I could use \1).</p>
<p><del datetime="2010-01-29T02:20:35+00:00">Little help, regex ninjas?</del> Update 2: Shag to the rescue!</p>
<p><strong>Updates 3, 4, and 5</strong>: Shag has provided us with a even moar better regex in the comments.. Yay Shag!!!!!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tikirobot.net/wp/2010/01/26/replacing-urls-with-links-halp/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Signing Amazon Web Services API Requests in Python</title>
		<link>http://www.tikirobot.net/wp/2010/01/24/signing-amazon-web-services-api-requests-in-python/</link>
		<comments>http://www.tikirobot.net/wp/2010/01/24/signing-amazon-web-services-api-requests-in-python/#comments</comments>
		<pubDate>Sun, 24 Jan 2010 18:33:09 +0000</pubDate>
		<dc:creator>rajbot</dc:creator>
				<category><![CDATA[amazon]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[archive]]></category>
		<category><![CDATA[books]]></category>
		<category><![CDATA[code code]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.tikirobot.net/wp/?p=2915</guid>
		<description><![CDATA[I wanted to ping the &#8220;Amazon Product Advertising API&#8221; which now requires an HMAC signature, and the pyAWS library doesn&#8217;t sign requests and is no longer maintained. Here is some Python code to create a signed request: # pyAWS no longer works with the AWS signed request requirement # Sign an AWS REST request using [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted to ping the &#8220;Amazon Product Advertising API&#8221; which now requires an HMAC signature, and the pyAWS library doesn&#8217;t sign requests and is no longer maintained. Here is some Python code to create a signed request:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># pyAWS no longer works with the AWS signed request requirement</span>
<span style="color: #808080; font-style: italic;"># Sign an AWS REST request using the method described here</span>
<span style="color: #808080; font-style: italic;"># http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/index.html?RequestAuthenticationArticle.html</span>
<span style="color: #808080; font-style: italic;">#_______________________________________________________________________________</span>
<span style="color: #ff7700;font-weight:bold;">def</span> getSignedUrl<span style="color: black;">&#40;</span>accessKey, secretKey, params<span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #808080; font-style: italic;">#Step 0: add accessKey, Service, Timestamp, and Version to params</span>
    params<span style="color: black;">&#91;</span><span style="color: #483d8b;">'AWSAccessKeyId'</span><span style="color: black;">&#93;</span> = accessKey
    params<span style="color: black;">&#91;</span><span style="color: #483d8b;">'Service'</span><span style="color: black;">&#93;</span>        = <span style="color: #483d8b;">'AWSECommerceService'</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#Amazon adds hundredths of a second to the timestamp (always .000), so we do too.</span>
    <span style="color: #808080; font-style: italic;">#(see http://associates-amazon.s3.amazonaws.com/signed-requests/helper/index.html)</span>
    params<span style="color: black;">&#91;</span><span style="color: #483d8b;">'Timestamp'</span><span style="color: black;">&#93;</span>      = <span style="color: #dc143c;">time</span>.<span style="color: black;">strftime</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%Y-%m-%dT%H:%M:%S.000Z&quot;</span>, <span style="color: #dc143c;">time</span>.<span style="color: black;">gmtime</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    params<span style="color: black;">&#91;</span><span style="color: #483d8b;">'Version'</span><span style="color: black;">&#93;</span>        = <span style="color: #483d8b;">'2009-03-31'</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#Step 1a: sort params</span>
    paramsList = params.<span style="color: black;">items</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    paramsList.<span style="color: black;">sort</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#Step 1b-d: create canonicalizedQueryString</span>
    <span style="color: #808080; font-style: italic;"># This code comes from http://blog.umlungu.co.uk/blog/2009/jul/12/pyaws-adding-request-authentication/</span>
    <span style="color: #808080; font-style: italic;"># and the resulting discussion</span>
    canonicalizedQueryString = <span style="color: #483d8b;">'&amp;'</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'%s=%s'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>k,<span style="color: #dc143c;">urllib</span>.<span style="color: black;">quote</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>v<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> <span style="color: black;">&#40;</span>k,v<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">in</span> paramsList <span style="color: #ff7700;font-weight:bold;">if</span> v<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#Step 2: create string to sign</span>
    host          = <span style="color: #483d8b;">'ecs.amazonaws.com'</span>
    requestUri    = <span style="color: #483d8b;">'/onca/xml'</span>
    stringToSign  = <span style="color: #483d8b;">'GET<span style="color: #000099; font-weight: bold;">\n</span>'</span>
    stringToSign += host +<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span>
    stringToSign += requestUri+<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span>
    stringToSign += canonicalizedQueryString.<span style="color: black;">encode</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'utf-8'</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#Step 3: create HMAC</span>
    digest = <span style="color: #dc143c;">hmac</span>.<span style="color: #dc143c;">new</span><span style="color: black;">&#40;</span>secretKey, stringToSign, hashlib.<span style="color: black;">sha256</span><span style="color: black;">&#41;</span>.<span style="color: black;">digest</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#Step 4: base64 the hmac</span>
    sig = <span style="color: #dc143c;">base64</span>.<span style="color: black;">b64encode</span><span style="color: black;">&#40;</span>digest<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#Step 5: append signature to query</span>
    url  = <span style="color: #483d8b;">'http://'</span> + host + requestUri + <span style="color: #483d8b;">'?'</span>
    url += canonicalizedQueryString + <span style="color: #483d8b;">&quot;&amp;Signature=&quot;</span> + <span style="color: #dc143c;">urllib</span>.<span style="color: black;">quote</span><span style="color: black;">&#40;</span>sig<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> url</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.tikirobot.net/wp/2010/01/24/signing-amazon-web-services-api-requests-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How To Pretty-Print a Python ElementTree Structure</title>
		<link>http://www.tikirobot.net/wp/2009/07/29/how-to-pretty-print-a-python-elementtree-structure/</link>
		<comments>http://www.tikirobot.net/wp/2009/07/29/how-to-pretty-print-a-python-elementtree-structure/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 19:59:06 +0000</pubDate>
		<dc:creator>rajbot</dc:creator>
				<category><![CDATA[code code]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.tikirobot.net/wp/?p=2611</guid>
		<description><![CDATA[ElementTree doesn&#8217;t support pretty-printing XML. lxml does, but isn&#8217;t installed on our system. minidom&#8216;s toprettyxml() is seriously fucked up. What to do? Turned out PyXML was installed, so I took some advice from here and came up with this function, which takes an ET node and returns a pretty-printed string: import xml.etree.ElementTree as ET &#160; [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://docs.python.org/library/xml.etree.elementtree.html">ElementTree</a> doesn&#8217;t support pretty-printing XML. <a href="http://codespeak.net/lxml/">lxml</a> does, but isn&#8217;t installed on our system. <a href="http://docs.python.org/library/xml.dom.minidom.html">minidom</a>&#8216;s toprettyxml() is seriously fucked up. What to do? Turned out PyXML  was installed, so I took some advice <a href="http://ronrothman.com/public/leftbraned/xml-dom-minidom-toprettyxml-and-silly-whitespace/">from here</a> and came up with this function, which takes an ET node and returns a pretty-printed string:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">xml</span>.<span style="color: black;">etree</span>.<span style="color: black;">ElementTree</span> <span style="color: #ff7700;font-weight:bold;">as</span> ET
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">xml</span>.<span style="color: black;">dom</span>.<span style="color: black;">ext</span>.<span style="color: black;">reader</span> <span style="color: #ff7700;font-weight:bold;">import</span> Sax2
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">xml</span>.<span style="color: black;">dom</span>.<span style="color: black;">ext</span> <span style="color: #ff7700;font-weight:bold;">import</span> PrettyPrint
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">StringIO</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">StringIO</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> prettyPrintET<span style="color: black;">&#40;</span>etNode<span style="color: black;">&#41;</span>:
    reader = Sax2.<span style="color: black;">Reader</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    docNode = reader.<span style="color: black;">fromString</span><span style="color: black;">&#40;</span>ET.<span style="color: black;">tostring</span><span style="color: black;">&#40;</span>etNode<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    tmpStream = <span style="color: #dc143c;">StringIO</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    PrettyPrint<span style="color: black;">&#40;</span>docNode, stream=tmpStream<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> tmpStream.<span style="color: black;">getvalue</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.tikirobot.net/wp/2009/07/29/how-to-pretty-print-a-python-elementtree-structure/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Announcing YouTubeFilter</title>
		<link>http://www.tikirobot.net/wp/2008/03/05/announcing-youtubefilter/</link>
		<comments>http://www.tikirobot.net/wp/2008/03/05/announcing-youtubefilter/#comments</comments>
		<pubDate>Wed, 05 Mar 2008 07:58:58 +0000</pubDate>
		<dc:creator>rajbot</dc:creator>
				<category><![CDATA[video]]></category>
		<category><![CDATA[BeautifulSoup]]></category>
		<category><![CDATA[code code]]></category>
		<category><![CDATA[opensource]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[youtube]]></category>
		<category><![CDATA[YouTubeFilter]]></category>

		<guid isPermaLink="false">http://www.tikirobot.net/wp/2008/03/05/announcing-youtubefilter/</guid>
		<description><![CDATA[YouTubeFilter is a simple tool that scrapes the MetaFilter RSS feed and embeds the YouTube videos inline. I wrote it to make it easier to find cool videos to watch on my Wii. Unfortunately, the Wii runs out memory when loading YouTubeFilter! And of course, Firefox bugs on the mac prevent some of the embedded [...]]]></description>
			<content:encoded><![CDATA[<p><strong><a href="http://tikirobot.net/YouTubeFilter/">YouTubeFilter</a></strong> is a simple tool that scrapes the <a href="http://metafilter.com">MetaFilter</a> RSS feed and embeds the YouTube videos inline. I wrote it to make it easier to find cool videos to watch on my Wii.</p>
<p>Unfortunately, the Wii runs out memory when loading YouTubeFilter! And of course, Firefox bugs on the mac prevent some of the embedded videos from showing up unless you resize the window just right. Stupid firefox.</p>
<p>The code is <a href="http://tikitasks.svn.sourceforge.net/viewvc/tikitasks/YouTubeFilter/">checked into SourceForge</a>. I use <a href="http://www.crummy.com/software/BeautifulSoup/">Beautiful Soup for parsing the RSS</a>. Someone please help me make it work on the wii!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tikirobot.net/wp/2008/03/05/announcing-youtubefilter/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Announcing ChatBubble!</title>
		<link>http://www.tikirobot.net/wp/2008/01/29/announcing-chatbubble/</link>
		<comments>http://www.tikirobot.net/wp/2008/01/29/announcing-chatbubble/#comments</comments>
		<pubDate>Tue, 29 Jan 2008 08:57:04 +0000</pubDate>
		<dc:creator>rajbot</dc:creator>
				<category><![CDATA[ChatBubble]]></category>
		<category><![CDATA[code code]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[iChat]]></category>
		<category><![CDATA[opensource]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.tikirobot.net/wp/2008/01/29/announcing-chatbubble/</guid>
		<description><![CDATA[I&#8217;ve finally made it easy to post good-looking iChat transcripts to the blog! We use CSS to style DIVs to look like iChat speech balloons. Cool! Where I can get the CSS? All the code is checked into SourceForge. You can browse it here. But how does it work? A brief description is here. Scott [...]]]></description>
			<content:encoded><![CDATA[<div class="CBmsg CBmsgR CBblueR">
  <div class="CBtxt">
    <div class="CBcontent">
      <div class="CBt"></div>
      I&#8217;ve finally made it easy to post good-looking iChat transcripts to the blog! We use CSS to style DIVs to look like iChat speech balloons.
    </div>
    <div class="CBb"><div></div></div>
  </div>
  <div class="CBiconR"><img src="http://www.tikirobot.net/wp/wp-content/themes/gentle_calm/ChatBubble/icons/rajbot.jpg"></div>
</div>
<br clear=all>

<div class="CBmsg CBmsgL CBpinkL">
  <div class="CBiconL"><img src="http://www.tikirobot.net/wp/wp-content/themes/gentle_calm/ChatBubble/icons/peliom.jpg"></div>
  <div class="CBtxt">
    <div class="CBcontent">
      <div class="CBt"></div>
      Cool! Where I can get the CSS?
    </div>
    <div class="CBb"><div></div></div>
  </div>
</div>
<br clear=all>

<div class="CBmsg CBmsgR CBblueR">
  <div class="CBtxt">
    <div class="CBcontent">
      <div class="CBt"></div>
      All the code is checked into SourceForge. You can browse it <a href="http://tikitasks.svn.sourceforge.net/viewvc/tikitasks/ChatBubble/">here</a>.
    </div>
    <div class="CBb"><div></div></div>
  </div>
  <div class="CBiconR"><img src="http://www.tikirobot.net/wp/wp-content/themes/gentle_calm/ChatBubble/icons/rajbot.jpg"></div>
</div>
<br clear=all>

<div class="CBmsg CBmsgL CBpinkL">
  <div class="CBiconL"><img src="http://www.tikirobot.net/wp/wp-content/themes/gentle_calm/ChatBubble/icons/peliom.jpg"></div>
  <div class="CBtxt">
    <div class="CBcontent">
      <div class="CBt"></div>
      But how does it work?
    </div>
    <div class="CBb"><div></div></div>
  </div>
</div>
<br clear=all>

<div class="CBmsg CBmsgR CBblueR">
  <div class="CBtxt">
    <div class="CBcontent">
      <div class="CBt"></div>
      A brief description is <a href="http://www.tikirobot.net/wp/2007/12/17/how-to-style-a-div-to-look-like-an-ichat-speech-balloon-using-css/">here</a>. Scott Schiller came up with the <a href="http://www.schillmania.com/projects/dialog2/">Even More Rounded Corners</a> technique that we use. There is a CSS file to include and a python script that turns transcripts into html that you can paste into a blog post. We need more documentation, CSS cleanup, cross-bowswer support, and more speech balloon colors, if you feel like contributing patches.
    </div>
    <div class="CBb"><div></div></div>
  </div>
  <div class="CBiconR"><img src="http://www.tikirobot.net/wp/wp-content/themes/gentle_calm/ChatBubble/icons/rajbot.jpg"></div>
</div>
<br clear=all>

<div class="CBmsg CBmsgL CBpinkL">
  <div class="CBiconL"><img src="http://www.tikirobot.net/wp/wp-content/themes/gentle_calm/ChatBubble/icons/peliom.jpg"></div>
  <div class="CBtxt">
    <div class="CBcontent">
      <div class="CBt"></div>
      Doesn’t WordPress completely bork the formatting in Safari by adding unmatched &lt;/p&gt; tags?
    </div>
    <div class="CBb"><div></div></div>
  </div>
</div>
<br clear=all>

<div class="CBmsg CBmsgR CBblueR">
  <div class="CBtxt">
    <div class="CBcontent">
      <div class="CBt"></div>
      Yup! WordPress is crap! You can use the <a href="http://wordpress.org/extend/plugins/wp-unformatted/">wp-unformatted</a> plugin to disable autop() on posts that contain ChatBubbles.
    </div>
    <div class="CBb"><div></div></div>
  </div>
  <div class="CBiconR"><img src="http://www.tikirobot.net/wp/wp-content/themes/gentle_calm/ChatBubble/icons/rajbot.jpg"></div>
</div>
<br clear=all>


]]></content:encoded>
			<wfw:commentRss>http://www.tikirobot.net/wp/2008/01/29/announcing-chatbubble/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Example Scripts: REST web services and system calls</title>
		<link>http://www.tikirobot.net/wp/2007/03/18/example-scripts-rest-web-services-and-system-calls/</link>
		<comments>http://www.tikirobot.net/wp/2007/03/18/example-scripts-rest-web-services-and-system-calls/#comments</comments>
		<pubDate>Sun, 18 Mar 2007 09:35:02 +0000</pubDate>
		<dc:creator>rajbot</dc:creator>
				<category><![CDATA[code code]]></category>
		<category><![CDATA[example scripts]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.tikirobot.net/wp/2007/03/18/example-scripts-rest-web-services-and-system-calls/</guid>
		<description><![CDATA[I&#8217;ve been translating all the new perl and php scripts I write into Python and Ruby in order to learn more about those two languages. I checked some more example scripts into SourceForge, which might be useful for others who know one of these languages and want to learn a new one. These scripts are [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been translating all the new perl and php scripts I write into Python and Ruby in order to learn more about those two languages. I checked some more example scripts into SourceForge, which might be useful for others who know one of these languages and want to learn a new one.</p>
<p>These scripts are available in perl, php, python, and ruby:<br />
<UL>
<li> <a href="http://tikitasks.svn.sourceforge.net/viewvc/tikitasks/ExampleScripts/RESTWebService/">How to query a REST web api</a> using HTTP GET and parsing the XML response.
<li> <a href="http://tikitasks.svn.sourceforge.net/viewvc/tikitasks/ExampleScripts/SystemCall/">How to make a system call</a> and read the return value.<br />
</UL><br />
The REST Web Service PHP and Perl scripts don&#8217;t work in Mac OS X, because OS X doesn&#8217;t ship with Perl&#8217;s XML::Simple or PHP&#8217;s simplexml. More surprisingly, OS X doesn&#8217;t ship with Perl&#8217;s <a href="http://search.cpan.org/~gaas/libwww-perl-5.805/lib/LWP.pm">LWP module</a>.</p>
<p>I&#8217;m starting to like Ruby more every day. It would be nice if Ruby and Python had a XML::Simple equivalent in their standard distributions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tikirobot.net/wp/2007/03/18/example-scripts-rest-web-services-and-system-calls/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Example scripts: directory listing in perl, php, python, and ruby</title>
		<link>http://www.tikirobot.net/wp/2007/03/15/example-scripts-directory-listing-in-perl-php-python-and-ruby/</link>
		<comments>http://www.tikirobot.net/wp/2007/03/15/example-scripts-directory-listing-in-perl-php-python-and-ruby/#comments</comments>
		<pubDate>Thu, 15 Mar 2007 08:36:17 +0000</pubDate>
		<dc:creator>rajbot</dc:creator>
				<category><![CDATA[code code]]></category>
		<category><![CDATA[example scripts]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.tikirobot.net/wp/2007/03/15/example-scripts-directory-listing-in-perl-php-python-and-ruby/</guid>
		<description><![CDATA[I remember when I fell in love with Perl. It was the summer of 1995, and peliom and I had just met, and were working at the Lab. Postscript hacking using MacPerl on OS 8. It was beautiful. That was more than ten years ago, and even though I&#8217;ve remained a Perl hacker the whole [...]]]></description>
			<content:encoded><![CDATA[<p>I remember when I fell in love with Perl. It was the summer of 1995, and peliom and I had just met, and were working at the Lab. Postscript hacking using MacPerl on OS 8. It was beautiful. </p>
<p>That was more than ten years ago, and even though I&#8217;ve remained a Perl hacker the whole time, I see massive amounts of development happening on Python and Ruby, and the Perl community seems to be slowing down (what&#8217;s up with Perl 6 anyway?), so, despite the lack of block-level scope, I think it might finally be time to move on.</p>
<p>I don&#8217;t know enough about either Python or Ruby to figure out which to learn, so I&#8217;ll learn them both, and deal with choosing one later. Along the way I&#8217;ll post some example scripts. Anyone else making the jump from Perl or PHP to something modern might find these useful. Here is the first example: printing out a directory listing using readdir and glob in your favorite scripting language:</p>
<p><span id="more-967"></span></p>
<p>[perl]<br />
<OL>
<li>!/usr/bin/perl<br />
</OL>use strict;<br />
use warnings;</p>
<p>my $dir = &#8216;/&#8217;;</p>
<p><OL>
<li> Use readdir
<li>_______________________________________________________________________________<br />
</OL>print &#8220;Directory listing in perl of $dir using readdir\n&#8221;;<br />
opendir(HANDLE, $dir) or die &#8220;Cannot opendir $dir: $!&#8221;;<br />
foreach my $file (readdir(HANDLE)) {<br />
    print &#8220;\t$file\n&#8221;;<br />
}<br />
closedir(HANDLE);</p>
<p><OL>
<li> Use globbing
<li>_______________________________________________________________________________<br />
</OL>print &#8220;Directory listing in perl of $dir using globbing\n&#8221;;<br />
foreach my $file (glob(&#8220;/*&#8221;)) {<br />
    print &#8220;\t$file\n&#8221;;<br />
}<br />
[/perl]</p>
<p>[python]<br />
<OL>
<li>!/usr/bin/python<br />
</OL><br />
import os<br />
import dircache<br />
import glob</p>
<p>dir = &#8220;/&#8221;</p>
<p><OL>
<li> Use os.listdir
<li>_______________________________________________________________________________<br />
</OL>print &#8220;Directory listing in python of &#8221; + dir + &#8221; using os.listdir&#8221;<br />
files = os.listdir(dir)<br />
for file in files:<br />
    print &#8220;\t&#8221; + file</p>
<p><OL>
<li> Use dircache
<li>_______________________________________________________________________________<br />
</OL>print &#8220;Directory listing in python of &#8221;  + dir + &#8221; using dircache&#8221;<br />
files = dircache.listdir(dir)<br />
for file in files:<br />
    print &#8220;\t&#8221; + file</p>
<p><OL>
<li> Use glob
<li>_______________________________________________________________________________<br />
</OL>print &#8220;Directory listing in python of &#8221;  + dir + &#8221; using glob&#8221;<br />
files = glob.glob(&#8220;/*&#8221;)<br />
for file in files:<br />
    print &#8220;\t&#8221; + file<br />
[/python]</p>
<p>[ruby]<br />
<OL>
<li>!/usr/bin/ruby<br />
</OL><br />
dir = &#8220;/&#8221;</p>
<p><OL>
<li> Use foreach
<li>_______________________________________________________________________________<br />
</OL>print &#8220;Directory listing in ruby of #{dir} using Dir.foreach\n&#8221;<br />
Dir.foreach(dir) {|file| print &#8220;\t#{file}\n&#8221; }</p>
<p><OL>
<li> Use glob
<li>_______________________________________________________________________________<br />
</OL>print &#8220;Directory listing in ruby of #{dir} using Dir.glob\n&#8221;<br />
files = Dir.glob(&#8220;/*&#8221;)<br />
files.each do |file| print &#8220;\t#{file}\n&#8221; end<br />
[/ruby]</p>
<p>These scripts, as well as a PHP version, are checked in <a href="http://tikitasks.svn.sourceforge.net/viewvc/tikitasks/ExampleScripts/DirectoryListing/">at SourceForge</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tikirobot.net/wp/2007/03/15/example-scripts-directory-listing-in-perl-php-python-and-ruby/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

