<?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>rlazo&#039;s blog &#187; Python</title>
	<atom:link href="http://www.rlazo.org/category/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rlazo.org</link>
	<description></description>
	<lastBuildDate>Thu, 01 Dec 2011 18:17:44 +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>Nice Python Gems</title>
		<link>http://www.rlazo.org/2011/02/12/nice-python-gems/</link>
		<comments>http://www.rlazo.org/2011/02/12/nice-python-gems/#comments</comments>
		<pubDate>Sun, 13 Feb 2011 01:44:02 +0000</pubDate>
		<dc:creator>Rodrigo</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.rlazo.org/?p=272</guid>
		<description><![CDATA[EDIT (9/18/2011): There is another way of having a default value in python dictionaries, and it&#8217;s using the defaultdict class in the collections module (python 2.5).  Here are the docs. I was checking my old bookmarks and I stumbled upon these two blog posts: Gems of Python by Eric Florenzano and Python gems of my own [...]]]></description>
			<content:encoded><![CDATA[<p><strong>EDIT (9/18/2011):</strong> There is another way of having a default value in python dictionaries, and it&#8217;s using the <tt>defaultdict</tt> class in the <tt>collections</tt> module (python 2.5).  <a href="http://docs.python.org/library/collections.html?highlight=dict#collections.defaultdict">Here are the docs</a>.</p>
<p>I was checking my old bookmarks and I stumbled upon these two blog posts: <a href="http://www.eflorenzano.com/blog/post/gems-python/">Gems of Python</a> by Eric Florenzano and <a href="http://ericholscher.com/blog/2008/nov/3/python-gems-my-own/">Python gems of my own</a> by Eric Holscher.</p>
<p>Did you know about setdefault for dicts? I&#8217;ve found myself more than once using a dict as a multimap and I always felt that there must be a better way of doing it than this:</p>
<pre class="src src-python"><span style="color: #a0522d;">dct</span> = {}
<span style="color: #a0522d;">items</span> = [<span style="color: #8b2252;">'anne'</span>, <span style="color: #8b2252;">'david'</span>, <span style="color: #8b2252;">'kevin'</span>, <span style="color: #8b2252;">'eric'</span>, <span style="color: #8b2252;">'anthony'</span>, <span style="color: #8b2252;">'andrew'</span>]
<span style="color: #a020f0;">for</span> name <span style="color: #a020f0;">in</span> items:
    <span style="color: #a020f0;">if</span> name[0] <span style="color: #a020f0;">not</span> <span style="color: #a020f0;">in</span> dct:
        dct[name[0]] = []
    dct[name[0]].append(name)</pre>
<p>And I was right, from Eric Florenzano&#8217;s post:</p>
<pre class="src src-python"><span style="color: #a0522d;">dct</span> = {}
<span style="color: #a0522d;">items</span> = [<span style="color: #8b2252;">'anne'</span>, <span style="color: #8b2252;">'david'</span>, <span style="color: #8b2252;">'kevin'</span>, <span style="color: #8b2252;">'eric'</span>, <span style="color: #8b2252;">'anthony'</span>, <span style="color: #8b2252;">'andrew'</span>]
<span style="color: #a020f0;">for</span> name <span style="color: #a020f0;">in</span> items:
    dct.setdefault(name[0], []).append(name)</pre>
<p>I knew it…</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rlazo.org/2011/02/12/nice-python-gems/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Create a tarball from a specific git commit</title>
		<link>http://www.rlazo.org/2010/03/13/create-a-tarball-from-a-specific-git-commit/</link>
		<comments>http://www.rlazo.org/2010/03/13/create-a-tarball-from-a-specific-git-commit/#comments</comments>
		<pubDate>Sun, 14 Mar 2010 03:10:28 +0000</pubDate>
		<dc:creator>Rodrigo</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.rlazo.org/?p=180</guid>
		<description><![CDATA[I have this code I&#8217;m working on that is needs to be deployed remotely (Fabric makes this SO easy) and I&#8217;m using git as my version control system. Well, I was creating tarballs for this, but I was basically doing them as similar as I could to what git had in the tree (ignoring the [...]]]></description>
			<content:encoded><![CDATA[<p>I have this code I&#8217;m working on that is needs to be deployed remotely (<a href="http://docs.fabfile.org/0.9.0/index.html">Fabric makes this SO easy</a>) and I&#8217;m using git as my version control system. Well, I was creating tarballs for this, but I was basically doing them as similar as I could to what git had in the tree (ignoring the same files, etc.), so it was kind of repetitive and, boring. Well, git to the rescue!!!</p>
<p><code><br />
git archive --format=tar HEAD | gzip > myproject.tar.gz<br />
</code></p>
<p>And you have a nice clean zipped tarball of your code as is on HEAD, without tears <img src='http://www.rlazo.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Git is awesome!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rlazo.org/2010/03/13/create-a-tarball-from-a-specific-git-commit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Performance of in operator using list and set</title>
		<link>http://www.rlazo.org/2010/02/25/performance-of-in-operator-using-list-and-set/</link>
		<comments>http://www.rlazo.org/2010/02/25/performance-of-in-operator-using-list-and-set/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 04:16:49 +0000</pubDate>
		<dc:creator>Rodrigo</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.rlazo.org/?p=163</guid>
		<description><![CDATA[I had this use case where I have to check which elements of a list of words where available in another list of words. So I decided to use the operator in﻿. Just for further reference a tried the following: # common code for all test base_list = [...] query_list = [...] Pretty simple method: [...]]]></description>
			<content:encoded><![CDATA[<p>I had this use case where I have to check which elements of a list of words where available in another list of words. So I decided to use the operator <strong>in﻿.</strong> Just for further reference a tried the following:</p>
<div>
<pre name="code" class="python"># common code for all test
base_list = [...]
query_list = [...]</pre>
</div>
<ol>
<li><strong>Pretty simple method:</strong>
<div>
<pre name="code" class="python">for word in query_list:
  if word in base_list:
    # do something</pre>
</div>
<p>For a list of 4284 elements against a list of 107 it took 9 seconds. Using simple lists, this method is the most straight forward of all, and also the slowest one.</li>
<li><strong>Sorting things:</strong>
<div>
<pre name="code" class="python">base_list.sort()
for word in query_list:
  if word in base_list:
    # do something</pre>
</div>
<p>After sorting the list, guess what? Yeap, nothing changed, same 9 seconds</li>
<li><strong>What about sets?</strong>
<div>
<pre name="code" class="python">bs = set(base_list)
for word in query_list:
  if word in bs:
    # do something</pre>
</div>
<p>Using sets this is <strong>another history</strong>, 0.6 seconds for the same amount of data; but&#8230; if this could be achived turning one of lists into a set, what if&#8230;</li>
<li><strong>Using more sets</strong>
<div>
<pre name="code" class="python">bs = set(base_list)
qs = set(query_list)
solution = bs.intersection(qs)</pre>
</div>
<p>0.02 seconds.</li>
</ol>
<p>Well, as you can see, sets are great.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rlazo.org/2010/02/25/performance-of-in-operator-using-list-and-set/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

