<?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>Reliably Broken &#187; trac</title>
	<atom:link href="http://reliablybroken.com/b/tag/trac/feed/" rel="self" type="application/rss+xml" />
	<link>http://reliablybroken.com/b</link>
	<description>It&#039;s a blog: let&#039;s do funch!</description>
	<lastBuildDate>Sat, 04 Sep 2010 21:22:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Tuples to dicts, toot sweet!</title>
		<link>http://reliablybroken.com/b/2009/06/tuples-to-dicts-toot-sweet/</link>
		<comments>http://reliablybroken.com/b/2009/06/tuples-to-dicts-toot-sweet/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 07:12:42 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[trac]]></category>

		<guid isPermaLink="false">http://reliablybroken.com/b/?p=201</guid>
		<description><![CDATA[Looking through Trac&#8217;s search internals I came across a chunk where a list of tuples is converted to a list of dictionaries for the convenience of the template engine. Each tuple has five fields: href, title, date, author and excerpt.

for idx, result in enumerate(results):
    results[idx] = {'href': result[0], 'title': result[1],
   [...]]]></description>
			<content:encoded><![CDATA[<p>Looking through <a href="http://trac.edgewall.org">Trac</a>&#8217;s search internals I came across <a href="http://trac.edgewall.org/browser/tags/trac-0.11.4/trac/search/web_ui.py#L111">a chunk where a list of tuples is converted to a list of dictionaries</a> for the convenience of the template engine. Each tuple has five fields: <em>href</em>, <em>title</em>, <em>date</em>, <em>author</em> and <em>excerpt</em>.</p>

<pre><code>for idx, result in enumerate(results):
    results[idx] = {'href': result[0], 'title': result[1],
                    'date': format_datetime(result[2]),
                    'author': result[3], 'excerpt': result[4]}
</code></pre>

<p>This allows the template author to use nice names for the fields in a row, like <code>${result.href}</code> etc. Looking at this reminded me of another approach that uses <a href="http://docs.python.org/tutorial/datastructures.html#list-comprehensions">list comprehension</a>, <a href="http://docs.python.org/library/functions.html#zip"><code>zip</code></a> and <a href="http://docs.python.org/library/stdtypes.html#typesmapping"><code>dict</code></a>.</p>

<pre><code>keys = ('href', 'title', 'date', 'author', 'excerpt')
results = [dict(zip(keys, row)) for row in results]
for row in results:
    row['date'] = format_datetime(row['date'])
</code></pre>

<p>The second line in this snippet is where the list of dictionaries is created, but one still has to go back and format the datetime values (the third and fourth lines). There&#8217;s no advantage in speed (the majority of the execution time is spent in <code>format_datetime</code>) but I like it a little better.</p>

<p>Maybe if Trac used the second approach I would like Trac a little better too.</p>
]]></content:encoded>
			<wfw:commentRss>http://reliablybroken.com/b/2009/06/tuples-to-dicts-toot-sweet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
