<?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>Push cx &#187; Python</title>
	<atom:link href="http://push.cx/tag/python/feed" rel="self" type="application/rss+xml" />
	<link>http://push.cx</link>
	<description>A traveling geek&#039;s blog on development, games, and the web</description>
	<lastBuildDate>Thu, 19 Apr 2012 20:39:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Caching Dictionaries in Python vs. Ruby</title>
		<link>http://push.cx/2008/caching-dictionaries-in-python-vs-ruby</link>
		<comments>http://push.cx/2008/caching-dictionaries-in-python-vs-ruby#comments</comments>
		<pubDate>Sat, 02 Feb 2008 17:01:52 +0000</pubDate>
		<dc:creator>Peter Harkins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[dictionaries]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://push.cx/2008/caching-dictionaries-in-python-vs-ruby</guid>
		<description><![CDATA[A while ago I made a slightly-underinformed post (see the corrections in the comments) trying to draw a difference between Python and Ruby. I&#8217;ve finally got a decent example and can explain what I&#8217;m getting at. I&#8217;m processing all the items in a big list, and part of that is performing some expensive calculation on [...]]]></description>
			<content:encoded><![CDATA[<p>
A while ago I made <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3B1c2guY3gvMjAwNy9hbi1hY2FkZW1pYy1pbmNvbnZlbmllbmNlLW9mLXB5dGhvbg==">a slightly-underinformed post</a> (see the corrections in the comments) trying to draw a difference between Python and Ruby. I&#8217;ve finally got a decent example and can explain what I&#8217;m getting at.
</p>

<p>
I&#8217;m processing all the items in a big list, and part of that is performing some expensive calculation on an item attribute. There are only a few dozen values for the attribute, so I cache them: 
</p>

<pre>&nbsp;
results = <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">for</span> item <span style="color: #b1b100;">in</span> really_big_list:
    <span style="color: #b1b100;">if</span> item.<span style="color: #202020;">attribute</span> <span style="color: #b1b100;">in</span> results:
        result = results<span style="color: #66cc66;">&#91;</span>item.<span style="color: #202020;">attribute</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #b1b100;">else</span>:
        result = expensive_calculation<span style="color: #66cc66;">&#40;</span>item.<span style="color: #202020;">attribute</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #808080; font-style: italic;"># ... do something useful with item and result</span></pre>

<p>
It&#8217;s simple and readable, but it&#8217;s also lengthy. If I&#8217;m doing several expensive calculations on different attributes (and I am), my actual work gets lost in the noise. So I defined a dictionary that can do the heavy operation and cache the result:
</p>

<pre>&nbsp;
<span style="color: #b1b100;">class</span> LambdaDict<span style="color: #66cc66;">&#40;</span>dict<span style="color: #66cc66;">&#41;</span>:
    <span style="color: #b1b100;">def</span> <span style="color: #b1b100;">__init__</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">self</span>, l<span style="color: #66cc66;">&#41;</span>:                                                                                                                       
        super<span style="color: #66cc66;">&#40;</span>LambdaDict, <span style="color: #b1b100;">self</span><span style="color: #66cc66;">&#41;</span>.__init__<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">self</span>.<span style="color: #202020;">l</span> = l
&nbsp;
    <span style="color: #b1b100;">def</span> <span style="color: #b1b100;">__getitem__</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">self</span>, key<span style="color: #66cc66;">&#41;</span>:                                                                                                                  
        <span style="color: #b1b100;">if</span> key <span style="color: #b1b100;">in</span> <span style="color: #b1b100;">self</span>:
            <span style="color: #b1b100;">return</span> <span style="color: #b1b100;">self</span>.<span style="color: #202020;">get</span><span style="color: #66cc66;">&#40;</span>key<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">else</span>:
            <span style="color: #b1b100;">self</span>.__setitem__<span style="color: #66cc66;">&#40;</span>key, <span style="color: #b1b100;">self</span>.<span style="color: #202020;">l</span><span style="color: #66cc66;">&#40;</span>key<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #b1b100;">self</span>.<span style="color: #202020;">get</span><span style="color: #66cc66;">&#40;</span>key<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># and now my code becomes</span>
&nbsp;
results = LambdaDict<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span>  key:expensive_calculation<span style="color: #66cc66;">&#40;</span>key<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #b1b100;">for</span> item <span style="color: #b1b100;">in</span> really_big_list:
    result = results<span style="color: #66cc66;">&#91;</span>item.<span style="color: #202020;">attribute</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #808080; font-style: italic;"># ... do something useful with item and result</span>
&nbsp;</pre>

<p>
That&#8217;s really nice, clean code that I&#8217;m satisfied with. The difference in Python and Ruby here is that Ruby hashes (the equivalent of dicts) include this behavior by default, just pass a block (lambda) when constructing the hash and it&#8217;ll be called for every missing key.
</p>

<p>
I&#8217;ll have the same behavior in Python or Ruby, it&#8217;s just that the default Python object doesn&#8217;t give me a handy method for building a dictionary that might perform some arbitrary expensive method on a simple lookup. Ruby is in favor of implicit magic, so it holds out the hook. This is the difference I was trying to get at: Python&#8217;s builtin objects have fewer methods and convenient hooks for me to do weird and useful things than Ruby&#8217;s, and Ruby will even let me tinker with them. Ruby has open classes, so I can extend both the builtin and defined classes with my own methods:
</p>

<pre>&nbsp;
module Enumerable <span style="color: #808080; font-style: italic;"># included by Arrays and similar objects</span>
  def sum
    inject<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> |x, y| x + <span style="color: #000066;">y</span> <span style="color: #66cc66;">&#125;</span>
  <span style="color: #b1b100;">end</span>
end</pre>

<p>
This adds a sum() method every Array in my program, whether I construct it (like I created my own <kbd>results</kbd> dict) or get it from some library code. In Python I&#8217;d have to define my own Array type and I&#8217;d be out of luck if I&#8217;m getting back arrays from a library. <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9Nb25rZXlfcGF0Y2g=">Monkey patching</a> is Python&#8217;s (deliberately clunky) name for adding a method to a single object at runtime. I&#8217;d have to monkey patch every object as it&#8217;s returned from the library rather than just being able to declare that every object of that class should have my method.
</p>

<p>
If this is the first time you&#8217;ve seen open classes: yes, when I first saw it I felt exactly the same way you do. Dangerous unreliable hackery, a recipe for disaster. But I&#8217;ve seen a lot of useful things happen in Rails because of it, and my Ruby projects have benefited from being able to add a method to an existing library&#8217;s objects, to overwrite (or just chain my code before or after) another method.
</p>

<p>
It&#8217;s something of a last resort that lets me build the cleanest object system I can as I interface with builtin objects and library code. I don&#8217;t have any utility methods floating around or have to tweak each object as I get it back from an API call. As I cook, Ruby&#8217;s metaprogramming is the garnish that finishes the dish. I called Python academically inconvenient because it feels like there&#8217;s an academic designer at my shoulder saying, &#8220;No, you don&#8217;t want to do that, it&#8217;d be messy and might be be abused. Pedagogically unsound.&#8221; I love that Python pushes me to build an explicit and obvious code, but I find myself wanting to tuck in one little bit of magic to make the code perfect.
</p> <img src="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=299" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://push.cx/2008/caching-dictionaries-in-python-vs-ruby/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>An Academic Inconvenience of Python</title>
		<link>http://push.cx/2007/an-academic-inconvenience-of-python</link>
		<comments>http://push.cx/2007/an-academic-inconvenience-of-python#comments</comments>
		<pubDate>Thu, 08 Nov 2007 14:51:34 +0000</pubDate>
		<dc:creator>Peter Harkins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://push.cx/2007/an-academic-inconvenience-of-python</guid>
		<description><![CDATA[Sometimes Python&#8217;s roots in academia bug me. Lots of functions have a computer science feel instead of a software development feel. Here&#8217;s an example I just ran into: I wanted to fit as many sentences as possible from a long text into 255 characters. So I wrote: &#160; s = s&#91;:255&#93;&#91;:max&#40;s.rindex&#40;'.'&#41;, s.rindex&#40;'!'&#41;, s.rindex&#40;'?'&#41;&#41; + 1&#93; [...]]]></description>
			<content:encoded><![CDATA[<p>
Sometimes Python&#8217;s roots in academia bug me. Lots of functions have a computer science feel instead of a software development feel. Here&#8217;s an example I just ran into: I wanted to fit as many sentences as possible from a long text into 255 characters. So I wrote:
</p>

<pre>&nbsp;
s = s<span style="color: #66cc66;">&#91;</span>:<span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>:<span style="color: #b1b100;">max</span><span style="color: #66cc66;">&#40;</span>s.<span style="color: #202020;">rindex</span><span style="color: #66cc66;">&#40;</span>'.'<span style="color: #66cc66;">&#41;</span>, s.<span style="color: #202020;">rindex</span><span style="color: #66cc66;">&#40;</span>'!'<span style="color: #66cc66;">&#41;</span>, s.<span style="color: #202020;">rindex</span><span style="color: #66cc66;">&#40;</span>'?'<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span></pre>

<p>
This snippet chops it down to the 255 max, finds the ., !, or ? marking the end of the last sentence, and chops there. Great, right? Except it doesn&#8217;t work.
</p>

<p>
Instead of returning <kbd>None</kbd> when it can&#8217;t match the substring, <kbd>rindex</kbd> throws <kbd>ValueError</kbd>. So unless the first 255 characters of the string contain a ., !, <em>and</em> ? it&#8217;ll throw an exception. OK, let&#8217;s try:
</p>

<pre>&nbsp;
rightmost = -<span style="color: #cc66cc;">1</span>
<span style="color: #b1b100;">try</span>:
    rightmost = s.<span style="color: #202020;">rindex</span><span style="color: #66cc66;">&#40;</span>'.'<span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">except</span> <span style="color: #b1b100;">ValueError</span>:
    <span style="color: #b1b100;">pass</span>
<span style="color: #b1b100;">try</span>:
    rightmost = <span style="color: #b1b100;">max</span><span style="color: #66cc66;">&#40;</span>rightmost, s.<span style="color: #202020;">rindex</span><span style="color: #66cc66;">&#40;</span>'!'<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">except</span> <span style="color: #b1b100;">ValueError</span>:
    <span style="color: #b1b100;">pass</span>
<span style="color: #b1b100;">try</span>:
    rightmost = <span style="color: #b1b100;">max</span><span style="color: #66cc66;">&#40;</span>rightmost, s.<span style="color: #202020;">rindex</span><span style="color: #66cc66;">&#40;</span>'?'<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">except</span> <span style="color: #b1b100;">ValueError</span>:
    <span style="color: #b1b100;">pass</span>
s = s<span style="color: #66cc66;">&#91;</span>:<span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>:rightmost + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span></pre>

<p>
Eww. OK, let&#8217;s encapsulate that redundancy:
</p>

<pre>&nbsp;
<span style="color: #b1b100;">def</span> no_exception_rindex<span style="color: #66cc66;">&#40;</span>s, substring<span style="color: #66cc66;">&#41;</span>:
    <span style="color: #b1b100;">try</span>:
        <span style="color: #b1b100;">return</span> s.<span style="color: #202020;">rindex</span><span style="color: #66cc66;">&#40;</span>substring<span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">except</span> <span style="color: #b1b100;">ValueError</span>:
        <span style="color: #b1b100;">return</span> <span style="color: #b1b100;">None</span>
&nbsp;
s = s<span style="color: #66cc66;">&#91;</span>:<span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>:<span style="color: #b1b100;">max</span><span style="color: #66cc66;">&#40;</span>no_exception_rindex<span style="color: #66cc66;">&#40;</span>s, '.'<span style="color: #66cc66;">&#41;</span>, no_exception_rindex<span style="color: #66cc66;">&#40;</span>s, '!'<span style="color: #66cc66;">&#41;</span>, no_exception_rindex<span style="color: #66cc66;">&#40;</span>s, '?'<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span></pre>

<p>
That&#8217;s&#8230; well, it&#8217;s at least a little better. Lucky that <kbd>max</kbd> doesn&#8217;t mind seeing <kbd>None</kbd>, I could imagine it throwing its own <kbd>ValueError</kbd>. But I wouldn&#8217;t call this code <em>good</em>, we&#8217;ve been forced to switch out of object-oriented code because we can&#8217;t add our <kbd>no_exception_rindex</kbd> to the string objects.
</p>

<p>
Here&#8217;s another approach:
</p>

<pre>&nbsp;
<span style="color: #b1b100;">def</span> rightmost_punctuation<span style="color: #66cc66;">&#40;</span>s<span style="color: #66cc66;">&#41;</span>:
    index = <span style="color: #b1b100;">len</span><span style="color: #66cc66;">&#40;</span>s<span style="color: #66cc66;">&#41;</span> - <span style="color: #cc66cc;">1</span>
    <span style="color: #b1b100;">while</span> index &gt; <span style="color: #cc66cc;">0</span> <span style="color: #b1b100;">and</span> s<span style="color: #66cc66;">&#91;</span>index<span style="color: #66cc66;">&#93;</span> <span style="color: #b1b100;">not</span> <span style="color: #b1b100;">in</span> <span style="color: #66cc66;">&#91;</span>'.', '!', '?'<span style="color: #66cc66;">&#93;</span>:
        index -= <span style="color: #cc66cc;">1</span>
    <span style="color: #b1b100;">return</span> index
&nbsp;
s = s<span style="color: #66cc66;">&#91;</span>:<span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>:rightmost_punctuation<span style="color: #66cc66;">&#40;</span>s<span style="color: #66cc66;">&#41;</span> + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span></pre>

<p>
I&#8217;d actually call this one worse, as it&#8217;s not immediately obvious what it&#8217;s doing. And anytime I create a variable and then tinker with it inside a loop I feel like I want to rewrite that code to use <kbd>map</kbd> and/or <kbd>reduce</kbd>.
</p>

<p>
Tomorrow I&#8217;ll redo this example in Ruby to talk about open classes, but for today does anyone have a better approach in Python?
</p> <img src="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=286" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://push.cx/2007/an-academic-inconvenience-of-python/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>One Laptop Per Chicago</title>
		<link>http://push.cx/2007/one-laptop-per-chicago</link>
		<comments>http://push.cx/2007/one-laptop-per-chicago#comments</comments>
		<pubDate>Fri, 09 Mar 2007 18:37:56 +0000</pubDate>
		<dc:creator>Peter Harkins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Chicago]]></category>
		<category><![CDATA[grassy knoll]]></category>
		<category><![CDATA[Lucene]]></category>
		<category><![CDATA[OLPC]]></category>
		<category><![CDATA[pylucene]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Unicode]]></category>

		<guid isPermaLink="false">http://push.cx/2007/one-laptop-per-chicago</guid>
		<description><![CDATA[ChiPy held our largest meeting yet at Google (again) last night, so here&#8217;s a linkriffic post about it. Feihong Hsu presented lessons learned on doing Unicode in Python. I love hearing real-world experience, and Feihong knows his stuff. After his presentation there was a discussion where other folks chipped in their notes and answered questions. [...]]]></description>
			<content:encoded><![CDATA[<p>
<a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2NoaXB5Lm9yZw==">ChiPy</a> held our largest meeting yet at Google (<a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3B1c2guY3gvMjAwNi9jaGlweS1hdC1nb29nbGU=">again</a>) last night, so here&#8217;s a linkriffic post about it.
</p>

<p>
Feihong Hsu presented lessons learned on doing <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3B1c2guY3gvMjAwNi9yaXBwaW5nLXVuaWNvZGU=">Unicode</a> in Python. I love hearing real-world experience, and Feihong knows his stuff. After his presentation there was a discussion where other folks chipped in their notes and answered questions. The gist of it is that Python is good for Unicode, and in <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5weXRob24ub3JnL2Rldi9wZXBzL3BlcC0zMDAwLw==">Python 3000</a> it&#8217;ll be great.
</p>

<p>
<a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2lhbmJpY2tpbmcub3Jn">Ian Bicking</a> is working part time on the <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2xhcHRvcC5vcmc=">One Laptop Per Child</a> project and brought <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2xhcHRvcC5vcmcvbGFwdG9wL2hhcmR3YXJlL3NwZWNzLnNodG1s">a prototype</a>. I couldn&#8217;t get over how nice it is, it has a lot of thoughtful design touches like the antennae doubling as latches to close it. I&#8217;ve heard a lot of folks say they want one, but you really don&#8217;t &#8212; the keyboard is physically too small for adults to type on. Ian mentioned it&#8217;s currently fairly difficult to get started developing, but there&#8217;s a lot of attention on the problem and he&#8217;d like to set up a sprint to develop for the laptop after dev envs are easy to set up.
</p>

<p>
Ted Pollari took a few minutes to talk about how he and others worked to bring <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5weXRob24ub3JnL3B5Y29uLw==">PyCon</a> to Chicago in 2008. They&#8217;re finalizing the hotel contract this week, and planning for a thousand developers. Ted thanked everyone who helped, and encourages anyone who&#8217;s curious or might be able to volunteer to join the <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL21haWwucHl0aG9uLm9yZy9tYWlsbWFuL2xpc3RpbmZvL3B5Y29uLW9yZ2FuaXplcnM=">pycon-organizers mailing list</a>.
</p>

<p>
Pete Fein presented <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2NvZGUuZ29vZ2xlLmNvbS9wL2dyYXNzeWtub2xsLw==">Grassy Knoll</a> a <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9SRVNU">REST</a>-style interface to <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3B5bHVjZW5lLm9zYWZvdW5kYXRpb24ub3JnLw==">PyLucene</a> using <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9KU09O">JSON</a> as the data format on the wire. REST is a natural fit for the document management and searching, so it looks like a good project. He&#8217;s looking for folks to lend a hand.
</p> <img src="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=230" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://push.cx/2007/one-laptop-per-chicago/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ChiPy at Google</title>
		<link>http://push.cx/2006/chipy-at-google</link>
		<comments>http://push.cx/2006/chipy-at-google#comments</comments>
		<pubDate>Fri, 15 Sep 2006 22:04:04 +0000</pubDate>
		<dc:creator>Peter Harkins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[BigTable]]></category>
		<category><![CDATA[Chicago]]></category>
		<category><![CDATA[ChiPy]]></category>
		<category><![CDATA[Google Code]]></category>
		<category><![CDATA[project hosting]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Selenium]]></category>
		<category><![CDATA[Subversion]]></category>

		<guid isPermaLink="false">http://push.cx/2006/chipy-at-google</guid>
		<description><![CDATA[I had a great time last night at the ChiPy meeting last night that was held at Google&#8217;s Chicago office. I suspect a lot of people turned up just to see the venue: usually ChiPy gets 15-20 people but we got 51 last night. So to talk about the office: it&#8217;s a nice, if very [...]]]></description>
			<content:encoded><![CDATA[<p>
I had a great time last night at the <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2NoaXB5Lm9yZw==">ChiPy</a> meeting last night that was held at Google&#8217;s Chicago office. I suspect a lot of people turned up just to see the venue: usually ChiPy gets 15-20 people but we got 51 last night.
</p>

<p>
So to talk about the office: it&#8217;s a nice, if very deliberate space. It&#8217;s nice to see that someone thought about all the little decorative touches (like lava lamps) and useful touches (like an acoustic shell above where the projector displayed), but it didn&#8217;t feel very lived-in yet. Maybe it was just that having to sign an NDA to enter the office for a public meeting was an unpleasant surprise.
</p>

<p>
First up, <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2JyaWFucmF5LmNoaXB5Lm9yZy8=">Brian Ray</a> gave a presentation on <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3B1c2guY3gvMjAwNi9lcXVhbGl0eS1mb3ItcHl0aG9u">operator overloading in Python</a>. He covered what operators are available, how Python picks which operand&#8217;s code is called, how assignment operators like += and -= are handled, and tips on good style.
</p>

<p>
Next, <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3JlZC1iZWFuLmNvbS9maXR6Lw==">Brian Fitzpatrick</a> and <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5yZWQtYmVhbi5jb20vc3Vzc21hbi8=">Ben Collins-Sussman</a> talked about how they&#8217;ve been working on <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2NvZGUuZ29vZ2xlLmNvbQ==">Google Code</a>, specifically the free <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2NvZGUuZ29vZ2xlLmNvbS9ob3N0aW5n">Project Hosting</a> for open source software. The highlight (and clearly Fitz&#8217;s favorite part of the project) was how they used <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2xhYnMuZ29vZ2xlLmNvbS9wYXBlcnMvYmlndGFibGUuaHRtbA==">Google&#8217;s BigTable</a> as their svn repository. 
</p>

<p>
Last, <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2pyYW5kb2xwaC5jb20vYmxvZy8=">Jason Huggins</a> demo&#8217;d <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL29wZW5xYS5vcmcvc2VsZW5pdW0=">Selenium</a>, the browser-based website testing framework he developed. He had a nice demo showing how Selenium can be integrated into a continuous integration system so that browser tests are run on multiple OSs on every checkin, with screen-capture movies of the test process automatically checked into a repository. 
</p>

<p>
All-in-all, this was a very fun meeting with lots of conversation that didn&#8217;t break up until security wanted to close up at 23:00 (5h after the meeting started). I had fun meeting and chatting with at least a half-dozen new folks. Mostly I yammered on and on about <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2NhbWJyaWFuaG91c2UuY29tL2hvdy1pdC13b3Jrcw==">how Cambrian House works</a>, but I swear it&#8217;s because folks kept asking and not because I&#8217;m turning myself into a mobile billboard.
</p>

<p>
Also, <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3RlY2hzb2NpYWwuY29t">Jonathan Wolter</a> talked me into coming out to San Francisco <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy50ZWNoc29jaWFsLmNvbS9hcmNoaXZlcy8xMDMveWFob28taGFjay1kYXktZ29pbmctdG8tc2YtZm9yLXdlZWtlbmQtZmVzdGl2YWwtb2YtaGFja2luZy1jYW1waW5nLW11c2ljLWFuZC13ZWIyMA==">with him and a few others</a> to attend Yahoo&#8217;s <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2hhY2tkYXkub3Jn">Hack Day</a> in two weeks. Looks to be a good time. 
</p> <img src="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=178" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://push.cx/2006/chipy-at-google/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Ripping Unicode</title>
		<link>http://push.cx/2006/ripping-unicode</link>
		<comments>http://push.cx/2006/ripping-unicode#comments</comments>
		<pubDate>Fri, 16 Jun 2006 03:57:05 +0000</pubDate>
		<dc:creator>Peter Harkins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://push.cx/2006/ripping-unicode</guid>
		<description><![CDATA[I love shoving around large amounts of data. Unicode is an industry standard for encoding data in most every written script there&#8217;s ever been. It has over 97,000 characters. A while ago I read about a guy who made his own Unicode poster and I realized I had an opportunity for a fun project. I [...]]]></description>
			<content:encoded><![CDATA[<p>
<a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3B1c2guY3gvd3AtY29udGVudC91cGxvYWRzLzIwMDYvMDYvMUQ1MEYucG5n"><img class="decoration" src="http://push.cx/wp-content/uploads/2006/06/1D50F.thumbnail.png" width="67" height="96" alt="some unicode glyphs" /></a>
I love shoving around large amounts of data. Unicode is an industry standard for encoding data in most every written script there&#8217;s ever been. It has over 97,000 characters. A while ago I read about a guy who <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2lhbi1hbGJlcnQuY29tL21pc2MvdW5pY2hhcnQucGhw">made his own Unicode poster</a> and I realized I had an opportunity for a fun project. I think Unicode is an invaluable and beautiful project, and this is my tribute to it.
</p>

<p>
Unicode puts out PDF code charts of each and every glyph so people can have some idea of what they should look like (though the actual display to the user is left up to fonts). If you&#8217;d like to see one, the chart for <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy51bmljb2RlLm9yZy9jaGFydHMvUERGL1UxMDAwMC5wZGY=">Linear B</a> is a nice little example. Because I didn&#8217;t want to mess around with putting dozens of PDFs together, I just grabbed the <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3VuaWNvZGUub3JnL1B1YmxpYy80LjEuMC9jaGFydHMvQ29kZUNoYXJ0cy5wZGY=">single 30MB 777-page PDF</a> for the latest version, 4.1.0. (New versions are released as they add more scripts &#8212; cuneiform is scheduled for 5.0, yay!)
</p>

<p>
Looking at the PDF (whether you grabbed the small example or decided to be macho and pulled up the whole thing), it&#8217;s obvious that the &#8220;one big table per page&#8221; approach makes it pretty easy to extract the glyphs. I wrote a program to do this, because it would be <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5sb2djYWJpbi5vcmc=">stupid and ultimately self-defeating</a> to open the PDF with <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2dpbXAub3Jn">GIMP</a> to manually select and save each glyph.
</p>

<p>
First, I used <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5jcy53aXNjLmVkdS9+Z2hvc3Qv">ghostscript</a> to convert the PDF to a series of 600 DPI pngs, one image per page. This took about 39 minutes and 471MB of disk space. (I&#8217;ll explain why I converted at such a high resolution in about two paragraphs.)
</p>

<p>
Next, I wrote a <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2RpdmVpbnRvcHl0aG9uLm9yZw==">Python</a> script using the <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5weXRob253YXJlLmNvbS9wcm9kdWN0cy9waWwv">Python Imaging Library</a> to analyze every page, locate the glyph table if the page has one, and walk through the table exporting each glyph to its own individual PNG. The 769MB of individual glyph images took about 5.6 hours to rip. And that&#8217;s OK! I kept the code simple to read because <i>performance doesn&#8217;t matter</i>. That&#8217;s an odd thing to say for a program that takes hours to finish, but you only ever run this program <i>once</i>. Note, that&#8217;s never an excuse for shoddy quality, because you never run a program just once. Yes, those two statements are at odds. If you don&#8217;t see that they&#8217;re both true, you have not yet learned the Tao of Code.
</p>

<p>
<a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3B1c2guY3gvd3AtY29udGVudC91cGxvYWRzLzIwMDYvMDYvQTI5Qy5wbmc="><img class="decoration" src="http://push.cx/wp-content/uploads/2006/06/A29C.thumbnail.png" alt="Unicode glyph A29C" width="71" height="96" /></a>
Well, there&#8217;s one important step elided from the previous paragraph: figuring out which glyph is which so I know what the filename should be. Every glyph is labeled with a four or five-digit hexadecimal number, but of course the label is part of the image rather than text. Converting images to text is known as OCR (optical character recognition) and it&#8217;s difficult to avoid errors. In my favor I had images that were big (600 DPI, see, I told you I&#8217;d get back to it), error-free, and black-and-white; but a strike against me was that it can&#8217;t spell-check a word like &#8220;A29C&#8221;. After trying a few different OCR programs, I got <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5nbnUub3JnL3NvZnR3YXJlL29jcmFkL29jcmFkLmh0bWw=">ocrad</a> working well (with a little tinkering, it often mistook the number 0 for the letter O).
</p>

<p>
Well, mission accomplished. I&#8217;ve ripped 97,715 glyphs from the Unicode code charts to individual PNGs. So what should I do with them all?
</p>

<p>
<a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3B1c2guY3gvd3AtY29udGVudC91cGxvYWRzLzIwMDYvMDYvaG9zdHdheV9sb2dvLnBuZw=="><img class="decoration" src="http://push.cx/wp-content/uploads/2006/06/hostway_logo.thumbnail.png" alt="Hostway logo" width="128" height="11" /></a>
I work at <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2hvc3R3YXkuY29t">Hostway</a>, a web hosting company with about a dozen offices worldwide. I picked up a copy of our logo in vector format from one of our graphic designers and resized it until it had about the same number of black pixels as I have glyphs.
</p>

<img class="content" src="http://push.cx/wp-content/uploads/2006/06/zoom1.png" width="392" height="401" alt="some unicode glyphs" />

<p>
Then, of course, I wrote another script to render a final image. It created a huge new image in which every pixel from the source was a downsampled (because I&#8217;m printing at 300 DPI) glyph, and it tries to make sure it puts the darkest glyphs in place of the darkest pixel so I have a (very) rough antialiasing effect to soften the edges of letters. (If it&#8217;s not already painfully obvious, yes, I did <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9BU0NJSV9hcnQ=">ascii art</a> as a teenager.) This script immediately <i>crashed and burned</i>.
</p>

<img class="content" src="http://push.cx/wp-content/uploads/2006/06/zoom2.png"  width="392" height="399" alt="some unicode glyphs" />

<p>
The problem is scale. The source image is 1618 x 151 pixels, and I&#8217;m outputting at 300 DPI: 1618 x 151 x 300 x 300 = 20.9GB. As cool as it would be, I just don&#8217;t have that much RAM. I had to break up the image into pieces three feet wide, which is good because that&#8217;s also how wide the biggest printer in the office is. And I got to write a great error message:  &#8220;Can&#8217;t create images this big. Reduce max_piece_width or buy more RAM.&#8221;
</p>

<img class="content" src="http://push.cx/wp-content/uploads/2006/06/zoom3.png"  width="392" height="400" alt="some unicode glyphs" />

<p>
Made of 16 images totaling 310MB (it took 45m), the Hostway logo worked out to be 6 feet tall and 46 feet long. My boss told me it was &#8220;too big&#8221; to hang up. I told him that was &#8220;exactly the point of the whole thing&#8221;. I&#8217;ll change his mind yet &#8212; everyone in the office I show it to loves it, so it&#8217;s just a matter of time.
</p>

<img class="content" src="http://push.cx/wp-content/uploads/2006/06/zoom4.png"  width="170" height="343" alt="some unicode glyphs" />

<p>
I presented the code at the June 2006 meeting of <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2NoaXB5Lm9yZw==">ChiPy</a> and it was well-received. One attendee told me afterwards it was the funniest, nerdiest presentation he&#8217;s ever seen, which I consider high praise. As promised, I&#8217;m sharing the source code.
</p>

<p>
<a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3B1c2guY3gvd3AtY29udGVudC91cGxvYWRzLzIwMDYvMDYvdW5pY29kZS1hcnQuMjAwNi0wNi0xNS50YXIuZ3o=">unicode-art.2006-06-15.tar.gz</a>
</p>

<p>
You&#8217;ll have to be a little familiar with graphics and Python to make use
of this. It is not a copy of all the glyph graphics, and I will not
provide you with them or make posters for you, so please don&#8217;t ask. I&#8217;d love to hear what about what you do with it, please do something cool and post a comment or trackback.
</p> <img src="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=10" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://push.cx/2006/ripping-unicode/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Ripping Unicode at ChiPy</title>
		<link>http://push.cx/2006/ripping-unicode-at-chipy</link>
		<comments>http://push.cx/2006/ripping-unicode-at-chipy#comments</comments>
		<pubDate>Fri, 09 Jun 2006 16:00:53 +0000</pubDate>
		<dc:creator>Peter Harkins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://push.cx/2006/ripping-unicode-at-chipy</guid>
		<description><![CDATA[Last night at the June 2006 ChiPy meeting I gave a presentation on how I wrote a few small Python scripts to take apart the Unicode PDF of all its glyphs and recombine them into giant ascii-art-like posters. I know a few folks are dropping by looking for the source I promised to post. It&#8217;ll [...]]]></description>
			<content:encoded><![CDATA[<p>
Last night at the June 2006 <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2NoaXB5Lm9yZw==">ChiPy</a> meeting I gave a presentation on how I wrote a few small Python scripts to take apart the <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3VuaWNvZGUub3Jn">Unicode</a> PDF of all its glyphs and recombine them into giant ascii-art-like posters.
</p>

<p>
I know a few folks are dropping by looking for the source I promised to post. It&#8217;ll be up later tonight or tomorrow morning &#8212; I had to recreate one of my scripts from scratch and I&#8217;d like to make sure I did it right so I don&#8217;t post broken code. And as all of you at the presentation understand, running any of these scripts takes a few hours. So it&#8217;ll be up Real Soon Now.
</p>

<p>
Thanks again to Fawad Halim for loaning me the use of his laptop for my presentation.
</p>

<p class="update">
Of course it took a little longer than I expected, but <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3B1c2guY3gvMjAwNi9yaXBwaW5nLXVuaWNvZGU=">it&#8217;s up now</a>.
</p> <img src="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=127" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://push.cx/2006/ripping-unicode-at-chipy/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Strings are a Domain-Specific Language</title>
		<link>http://push.cx/2006/strings-are-a-domain-specific-language</link>
		<comments>http://push.cx/2006/strings-are-a-domain-specific-language#comments</comments>
		<pubDate>Sat, 27 May 2006 23:11:26 +0000</pubDate>
		<dc:creator>Peter Harkins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://push.cx/2006/strings-are-a-domain-specific-language</guid>
		<description><![CDATA[Question: Isn&#8217;t a domain-specific language just the same thing as a library? Source: Pretty much everyone the first time they hear of DSLs. Answer: No, a DSL is much more than a library, and I have an example that won&#8217;t make you say, &#8220;Well, sure, if you&#8217;re doing something that esoteric&#8230;&#8221; My example of a [...]]]></description>
			<content:encoded><![CDATA[<blockquote>
Question: Isn&#8217;t a domain-specific language just the same thing as a library?
<p style="font-size: 75%; text-align: right">Source: Pretty much everyone the first time they hear of DSLs.</p>
</blockquote>

<p>
Answer: No, a DSL is much more than a library, and I have an example that won&#8217;t make you say, &#8220;Well, sure, if you&#8217;re doing something <i>that</i> esoteric&#8230;&#8221;
</p>

<p>
My example of a domain-specific language is strings. No, seriously. Let&#8217;s figure out the length of a string in <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5pbnQ4MGgub3JnL3N0cmxlbi8=">x86 assembly</a>:
</p>

<pre>&nbsp;
strlen:
	<span style="color: #00007f;">push</span>	<span style="color: #46aa03; font-weight:bold;">edi</span>
	<span style="color: #00007f;">sub</span>	<span style="color: #46aa03; font-weight:bold;">ecx</span>, <span style="color: #46aa03; font-weight:bold;">ecx</span>
	<span style="color: #00007f;">mov</span>	<span style="color: #46aa03; font-weight:bold;">edi</span>, <span style="color: #66cc66;">&#91;</span><span style="color: #46aa03; font-weight:bold;">esp</span>+<span style="color: #ff0000;">8</span><span style="color: #66cc66;">&#93;</span>
	<span style="color: #00007f;">not</span>	<span style="color: #46aa03; font-weight:bold;">ecx</span>
	<span style="color: #00007f;">sub</span>	<span style="color: #46aa03; font-weight:bold;">al</span>, <span style="color: #46aa03; font-weight:bold;">al</span>
	<span style="color: #00007f;">cld</span>
<span style="color: #00007f;">repne</span>	<span style="color: #00007f;">scasb</span>
	<span style="color: #00007f;">not</span>	<span style="color: #46aa03; font-weight:bold;">ecx</span>
	<span style="color: #00007f;">pop</span>	<span style="color: #46aa03; font-weight:bold;">edi</span>
	<span style="color: #00007f;">lea</span>	<span style="color: #46aa03; font-weight:bold;">eax</span>, <span style="color: #66cc66;">&#91;</span>ecx-<span style="color: #ff0000;">1</span><span style="color: #66cc66;">&#93;</span>
	<span style="color: #00007f;">ret</span></pre>

<p>
Computer memory is one big linear stream of bytes we can scan through, looking for the null that terminates our string. Boy, is that some fast code &#8212; we might even call it efficient, if we ignore the fact that we&#8217;ll reach the eventual heat death of the universe before we finish our <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5jZXJhZG8uY29tL3dlYjIwcXVpei5odG0=">web 2.0</a> app.
</p>

<p>
So we move up in abstraction to C, which has arrays. And you can pretend a string is an array and walk it looking for that null terminator:
</p>

<pre>&nbsp;
<span style="color: #993333;">static</span> <span style="color: #993333;">int</span> my_strlen<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">char</span> *c<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #993333;">int</span> l = <span style="color: #cc66cc;">0</span>;
    <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>*c++<span style="color: #66cc66;">&#41;</span> l++;
    <span style="color: #b1b100;">return</span> l;
<span style="color: #66cc66;">&#125;</span></pre>

<p>
This code is basically the same as in assembly, but it must be nicer to read because it uses all that stylish punctuation. Well, it&#8217;s not really nicer, maybe a string isn&#8217;t really just an array. So let&#8217;s look at Python:
</p>

<pre>&nbsp;
<span style="color: #b1b100;">len</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"Hello, world!"</span><span style="color: #66cc66;">&#41;</span></pre>

<p>
Now that&#8217;s downright human-readable. And I&#8217;ll admit I&#8217;m fudging here by just calling the built-in <kbd>len()</kbd> instead of writing one, but it just works and there&#8217;s none of this messing around with null bytes.
</p>

<p>
Well, maybe there&#8217;s messing around with null bytes. I don&#8217;t have to know how Python implements <kbd>len()</kbd> and, more importantly, I don&#8217;t have to pretend a string is only an array or a small bit of sequentially-addressed memory.
</p>

<p>
To continue the example let&#8217;s look at regular expressions, a powerful way to search strings. We&#8217;ll write a pirate detector in Python:
</p>

<pre>&nbsp;
<span style="color: #b1b100;">import</span> re
matches = re.<span style="color: #202020;">search</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"ar+g+h"</span>, <span style="color: #ff0000;">"Oim a poirate, arrrgh!"</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">if</span> matches:
    <span style="color: #b1b100;">print</span> <span style="color: #ff0000;">"There must be a pirate, I heard someone say '%s'."</span> % matches.<span style="color: #202020;">group</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">else</span>:
    <span style="color: #b1b100;">print</span> <span style="color: #ff0000;">"No pirates detected."</span></pre>

<p>
This is important code, as pirates hide <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL29zdGVlbGUuY29tL2FyY2hpdmVzLzIwMDUvMTIvYWFyZ2g=">all over the web</a>. But it&#8217;s pretty clunky, we have to import a library and call functions and evaluating responses and save objects&#8230; It&#8217;d sure be handy if regular expressions were part of the language like in Ruby:
</p>

<pre>&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #ff0000;">"Oim a poirate, arrrgh!"</span> =~ /ar+g+h/ <span style="color: #b1b100;">then</span>
  puts <span style="color: #ff0000;">"There must be a pirate, I heard someone say '#{$&amp;}'."</span>
<span style="color: #b1b100;">else</span>
  puts <span style="color: #ff0000;">"No pirates detected."</span>
<span style="color: #b1b100;">end</span></pre>

<p>
This code is even nicer, our regexp is a first-class type and tightly integrated into the language. The increase in stylish puncutation might make for a higher learning curve, but we can express ourselves much more naturally.
</p>

<p>
A DSL is all about moving up in abstraction until your code directly reflects the high-level concepts you&#8217;re working in. You don&#8217;t have to peer through a haze of bits and pointers, your actions become synonymous with your intentions.
</p> <img src="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=110" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://push.cx/2006/strings-are-a-domain-specific-language/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lambda at Work</title>
		<link>http://push.cx/2006/lambda-at-work</link>
		<comments>http://push.cx/2006/lambda-at-work#comments</comments>
		<pubDate>Tue, 28 Mar 2006 04:50:17 +0000</pubDate>
		<dc:creator>Peter Harkins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://push.cx/2006/lambda-at-work</guid>
		<description><![CDATA[Finally, several years after learning lambda expressions, I got a chance to use one at work a few days ago. As long as I&#8217;m putting a notch in my nerd belt, I&#8217;d like to write about what lambda is and how it can be useful. A lambda expression defines an anonymous function. Here&#8217;s a regular [...]]]></description>
			<content:encoded><![CDATA[<p>
<img class="decoration"
src="http://push.cx/wp-content/uploads/2006/03/03BB.png" alt="Lambda" width="106" height="238" />
Finally, several years after learning lambda expressions, I got a chance to use one at work a few days ago. As long as I&#8217;m putting a notch in my nerd belt, I&#8217;d like to write about what lambda is and how it can be useful.
</p>

<p>
A lambda expression defines an anonymous function. Here&#8217;s a regular function definition:
</p>

<pre>&nbsp;
<span style="color: #b1b100;">def</span> inc<span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span>:
    <span style="color: #b1b100;">return</span> x + <span style="color: #cc66cc;">1</span></pre>

<p>
This definition binds the name <kbd>inc</kbd> in the local namespace to a function object. To get the exact same functionality using lambda, assign the lambda expression to a variable:
</p>

<pre>&nbsp;
inc = <span style="color: #b1b100;">lambda</span> x: x + <span style="color: #cc66cc;">1</span></pre>

<p>
A lambda has two parts: the argument list (only one arg in this case) before the<kbd>:</kbd> and an expression after. It can&#8217;t contain statements (like assignments or <kbd>print</kbd>) because it&#8217;s an expression itself. This is a pretty useless example, so let me show how I used it today.
</p>

<p>
When you call <kbd>list.sort()</kbd>, Python sorts it by calling the built-in <kbd>cmp()</kbd> function on pairs of elements from the list, which is defined as:
</p>

<pre>&nbsp;
<span style="color: #b1b100;">def</span> <span style="color: #b1b100;">cmp</span><span style="color: #66cc66;">&#40;</span>x, y<span style="color: #66cc66;">&#41;</span>:
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>x &lt; y<span style="color: #66cc66;">&#41;</span>:
        <span style="color: #b1b100;">return</span> -<span style="color: #cc66cc;">1</span>
    <span style="color: #b1b100;">elif</span> <span style="color: #66cc66;">&#40;</span>x &gt; y<span style="color: #66cc66;">&#41;</span>:
        <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">1</span>
    <span style="color: #b1b100;">else</span>
        <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span></pre>

<p>
My problem was that I was sorting a list of lists, a couple rows of statistics for a spreadsheet. Python was sorting it by the columns in order from left to right, and I needed to sort by one of the middle columns. Luckily, you can pass a new <kbd>cmp</kbd> function object to <kbd>sort()</kbd>.
</p>

<pre>&nbsp;
<span style="color: #b1b100;">list</span>.<span style="color: #202020;">sort</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> x,y: <span style="color: #b1b100;">cmp</span><span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span>, y<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre>

<p>
The lambda defines a function that takes arguments <kbd>x, y</kbd> and calls the built-in <kbd>cmp</kbd> function with item two of the inner list. The sort happens an inside column of the list rather than the first.
</p>

<p>
I could have defined a function, but that would be overkill for code I&#8217;d only call from one place. This simpler and more readable. It&#8217;s not a big win, but every little bit helps.
</p>

<p>
As a side note, Python 2.4 added a <kbd>key</kbd> argument to sort() that simplifies my code. If it&#8217;s set, Python calls it with the list element and passes what it returns into <kbd>cmp</kbd>. I figured the <kbd>cmp</kbd> example would show off lambda better, but in case you&#8217;re curious:
</p>

<pre>&nbsp;
<span style="color: #b1b100;">list</span>.<span style="color: #202020;">sort</span><span style="color: #66cc66;">&#40;</span>key=<span style="color: #b1b100;">lambda</span> x:x<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span></pre>

<p>
It&#8217;s been tough to write this and not start pulling in concepts from functional programming. Maybe I&#8217;ll get to use them next week. In the meantime, I&#8217;d like to hear more real-world uses of lambda if you&#8217;ve got one to share.
</p> <img src="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=81" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://push.cx/2006/lambda-at-work/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Python Flyweights</title>
		<link>http://push.cx/2006/python-flyweights</link>
		<comments>http://push.cx/2006/python-flyweights#comments</comments>
		<pubDate>Tue, 21 Mar 2006 02:13:33 +0000</pubDate>
		<dc:creator>Peter Harkins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://push.cx/2006/python-flyweights</guid>
		<description><![CDATA[<p>
When I wrote <a href="http://push.cx/2006/equality-for-python">Equality for Python</a>, my example didn't mention how the Card objects could actually be a terrific waste of memory. A commenter named versimilidude (great handle!) beat me to this post, briefly describing the Flyweight Pattern. Luckily he didn't provide example code, so I still get to publish this post.
</p>]]></description>
			<content:encoded><![CDATA[<p>
When I wrote <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3B1c2guY3gvMjAwNi9lcXVhbGl0eS1mb3ItcHl0aG9u">Equality for Python</a>, my example didn&#8217;t mention how the Card objects could actually be a terrific waste of memory. A commenter named versimilidude (great handle!) beat me to this post, briefly describing the Flyweight Pattern. Luckily he didn&#8217;t provide example code, so I still get to publish this post.
</p>

<p>
Let&#8217;s look at the final version of the Card class and its usage again:
</p>

<pre>&nbsp;
values = <span style="color: #66cc66;">&#40;</span>'<span style="color: #cc66cc;">2</span>', '<span style="color: #cc66cc;">3</span>', '<span style="color: #cc66cc;">4</span>', '<span style="color: #cc66cc;">5</span>', '<span style="color: #cc66cc;">6</span>', '<span style="color: #cc66cc;">7</span>', '<span style="color: #cc66cc;">8</span>', '<span style="color: #cc66cc;">9</span>', '<span style="color: #cc66cc;">10</span>', 'J', 'Q', 'K', 'A'<span style="color: #66cc66;">&#41;</span>
suits = <span style="color: #66cc66;">&#40;</span>'h', 'c', 'd', 's'<span style="color: #66cc66;">&#41;</span>
 
<span style="color: #b1b100;">class</span> Card:
    <span style="color: #b1b100;">def</span> <span style="color: #b1b100;">__init__</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">self</span>, value, suit<span style="color: #66cc66;">&#41;</span>:
        <span style="color: #b1b100;">self</span>.<span style="color: #202020;">value</span>, <span style="color: #b1b100;">self</span>.<span style="color: #202020;">suit</span> = value, suit
&nbsp;
    <span style="color: #b1b100;">def</span> <span style="color: #b1b100;">__repr__</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">self</span><span style="color: #66cc66;">&#41;</span>:
        <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">"&lt;Card: %s%s&gt;"</span> % <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">self</span>.<span style="color: #202020;">value</span>, <span style="color: #b1b100;">self</span>.<span style="color: #202020;">card</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #b1b100;">def</span> __eq__<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">self</span>, card<span style="color: #66cc66;">&#41;</span>:
        <span style="color: #b1b100;">return</span> <span style="color: #b1b100;">self</span>.<span style="color: #202020;">value</span> == card.<span style="color: #202020;">value</span> <span style="color: #b1b100;">and</span> <span style="color: #b1b100;">self</span>.<span style="color: #202020;">suit</span> == card.<span style="color: #202020;">suit</span>
&nbsp;
    <span style="color: #b1b100;">def</span> __ne__<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">self</span>, card<span style="color: #66cc66;">&#41;</span>:
       <span style="color: #b1b100;">return</span> <span style="color: #b1b100;">not</span> <span style="color: #b1b100;">self</span>.__eq__<span style="color: #66cc66;">&#40;</span>card<span style="color: #66cc66;">&#41;</span>
&nbsp;
&gt;&gt;&gt; c1 = Card<span style="color: #66cc66;">&#40;</span>'J', 'h'<span style="color: #66cc66;">&#41;</span>
&gt;&gt;&gt; c2 = Card<span style="color: #66cc66;">&#40;</span>'J', 'h'<span style="color: #66cc66;">&#41;</span>
&gt;&gt;&gt; c1 == c2
True
&gt;&gt;&gt; <span style="color: #b1b100;">id</span><span style="color: #66cc66;">&#40;</span>c1<span style="color: #66cc66;">&#41;</span>, <span style="color: #b1b100;">id</span><span style="color: #66cc66;">&#40;</span>c2<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>-<span style="color: #cc66cc;">1210559028</span>, -<span style="color: #cc66cc;">1210558804</span><span style="color: #66cc66;">&#41;</span></pre>

<p>
As designed in the previous blog post, two different objects (as seen by their different ids) equate because of the custom <kbd>__eq__()</kbd> handler.
</p>

<p>
But this is a bit silly &#8212; Cards are so small and simple, having identical objects is wasteful. If we&#8217;re writing the code for a casino, we don&#8217;t really want several thousand Jacks of Hearts, we want many references to one object. The idea is that instantiating a Card object checks a hidden pool of objects for a duplicate and returns it, creating a new object only if needed. (Code based on that of <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5zdXR0b25jb3VydGVuYXkub3JnLnVrL2R1bmNhbi9hY2N1L3B5dGhvbnBhdHRlcm5zLmh0bWwjZmx5d2VpZ2h0">Duncan Booth</a>.)
</p>

<pre>&nbsp;
<span style="color: #b1b100;">import</span> weakref
&nbsp;
<span style="color: #b1b100;">class</span> Card<span style="color: #66cc66;">&#40;</span>object<span style="color: #66cc66;">&#41;</span>:
    _CardPool = weakref.<span style="color: #202020;">WeakValueDictionary</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #b1b100;">def</span> __new__<span style="color: #66cc66;">&#40;</span>cls, value, suit<span style="color: #66cc66;">&#41;</span>:
        obj = Card._CardPool.<span style="color: #202020;">get</span><span style="color: #66cc66;">&#40;</span>value + suit, <span style="color: #b1b100;">None</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #b1b100;">not</span> obj:
            obj = object.__new__<span style="color: #66cc66;">&#40;</span>cls<span style="color: #66cc66;">&#41;</span>
            Card._CardPool<span style="color: #66cc66;">&#91;</span>value + suit<span style="color: #66cc66;">&#93;</span> = obj
&nbsp;
        <span style="color: #b1b100;">return</span> obj
&nbsp;
    <span style="color: #b1b100;">def</span> <span style="color: #b1b100;">__init__</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">self</span>, value, suit<span style="color: #66cc66;">&#41;</span>:
        <span style="color: #b1b100;">self</span>.<span style="color: #202020;">value</span>, <span style="color: #b1b100;">self</span>.<span style="color: #202020;">suit</span> = value, suit</pre>

<p>
Notice that this uses new-style classes. (Side note: I cringe at anything named &#8220;new&#8221;, &#8220;next&#8221;, &#8220;updated&#8221;, etc. because they tell me nothing. If the only thing you can say about a reimplementation is that it&#8217;s not the old one, put down your copy of Refactoring and back away from the computer. Take up writing toothpaste advertisements.)  If you&#8217;re wondering what&#8217;s up with <kbd>__new__</kbd>, <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5weXRob24ub3JnL2Rvd25sb2FkL3JlbGVhc2VzLzIuMi4zL2Rlc2NyaW50cm8v">Guido explains</a> the difference best:
</p>

<blockquote>
<kbd>__new__</kbd> is the first step in instance construction, invoked before <kbd>__init__</kbd>. The <kbd>__new__</kbd> method is called with the class as its first argument; its responsibility is to return a new instance of that class. Compare this to <kbd>__init__</kbd>: <kbd>__init__</kbd> is called with an instance as its first argument, and it doesn&#8217;t return anything; its responsibility is to initialize the instance.
</blockquote>

<p>
The other possibly-unfamiliar element is the WeakValueDictionary. In a nutshell, it&#8217;s a dictionary that deletes its entries if no other variables point to them. When instantiating a Card object, it checks in the Card class&#8217;s <kbd>_CardPool</kbd> to return an existing object, or creates a new Card object if needed.
</p>

<p>
If you&#8217;re confused, take a few minutes to play with the code. Insert print statements into <kbd>__new__</kbd> to show you the contents of <kbd>_CardPool.items()</kbd> and whether a new object is created. There&#8217;s a lot of important object orientation concepts at work in a short block of code: inheritance, the difference between classes and objects, references, and overriding.
</p>

<p>
I&#8217;ve removed the <kbd>__eq__</kbd> and <kbd>__ne__</kbd> calls because two Cards with the same value and suit are the same objects, saving our casino many gigs of precious RAM:
</p>

<pre>&nbsp;
&gt;&gt;&gt; c1 = Card<span style="color: #66cc66;">&#40;</span>'<span style="color: #cc66cc;">9</span>', 'h'<span style="color: #66cc66;">&#41;</span>
&gt;&gt;&gt; c2 = Card<span style="color: #66cc66;">&#40;</span>'<span style="color: #cc66cc;">9</span>', 'h'<span style="color: #66cc66;">&#41;</span>
&gt;&gt;&gt; c1 == c2
True
&gt;&gt;&gt; <span style="color: #b1b100;">id</span><span style="color: #66cc66;">&#40;</span>c1<span style="color: #66cc66;">&#41;</span>, <span style="color: #b1b100;">id</span><span style="color: #66cc66;">&#40;</span>c2<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>-<span style="color: #cc66cc;">1210940772</span>, -<span style="color: #cc66cc;">1210940772</span><span style="color: #66cc66;">&#41;</span></pre>

<p>
The c2 wiki has more info on the <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2MyLmNvbS9jZ2kvd2lraT9GbHl3ZWlnaHRQYXR0ZXJu">Flyweight Pattern</a>.
</p> <img src="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=66" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://push.cx/2006/python-flyweights/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Equality for Python</title>
		<link>http://push.cx/2006/equality-for-python</link>
		<comments>http://push.cx/2006/equality-for-python#comments</comments>
		<pubDate>Fri, 03 Mar 2006 06:30:20 +0000</pubDate>
		<dc:creator>Peter Harkins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://push.cx/2006/equality-for-python</guid>
		<description><![CDATA[<p>
<img class="decorator" width="200" height="133" src="http://push.cx/wp-content/uploads/2006/03/cards.jpg" alt="" />
A few days ago in #chipy, the chat room for the <a href="http://chipy.org/#info">Chicago Python Users Group</a>, we had a chat about how Python determines equality. It's a pretty neat and extensible technique, so I'm going to walk through how I recently used it for playing cards.
</p>]]></description>
			<content:encoded><![CDATA[<p>
<a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5mbGlja3IuY29tL3Bob3Rvcy9hYXJvbmphY29icy84MzExNTc0My8="><img class="decoration" width="200" height="133" src="http://push.cx/wp-content/uploads/2006/03/cards.jpg" alt="" /></a>
A few days ago in #chipy, the chat room for the <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2NoaXB5Lm9yZy8jaW5mbw==">Chicago Python Users Group</a>, we had a chat about how Python determines equality. It&#8217;s a pretty neat and extensible technique, so I&#8217;m going to walk through how I recently used it for playing cards.
</p>

<p>
Here&#8217;s the basic Card class. Note that I&#8217;m going to totally skip things like error-checking and documentation to keep the example obvious.
</p>

<pre>&nbsp;
values = <span style="color: #66cc66;">&#40;</span>'<span style="color: #cc66cc;">2</span>', '<span style="color: #cc66cc;">3</span>', '<span style="color: #cc66cc;">4</span>', '<span style="color: #cc66cc;">5</span>', '<span style="color: #cc66cc;">6</span>', '<span style="color: #cc66cc;">7</span>', '<span style="color: #cc66cc;">8</span>', '<span style="color: #cc66cc;">9</span>', '<span style="color: #cc66cc;">10</span>', 'J', 'Q', 'K', 'A'<span style="color: #66cc66;">&#41;</span>
suits = <span style="color: #66cc66;">&#40;</span>'h', 'c', 'd', 's'<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #b1b100;">class</span> Card:
    <span style="color: #b1b100;">def</span> <span style="color: #b1b100;">__init__</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">self</span>, value, suit<span style="color: #66cc66;">&#41;</span>:
        <span style="color: #b1b100;">self</span>.<span style="color: #202020;">value</span>, <span style="color: #b1b100;">self</span>.<span style="color: #202020;">suit</span> = value, suit
&nbsp;
    <span style="color: #b1b100;">def</span> <span style="color: #b1b100;">__repr__</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">self</span><span style="color: #66cc66;">&#41;</span>:
        <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">"&lt;Card: %s%s&gt;"</span> % <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">self</span>.<span style="color: #202020;">value</span>, <span style="color: #b1b100;">self</span>.<span style="color: #202020;">card</span><span style="color: #66cc66;">&#41;</span></pre>

<p>
Man, does code get short when you don&#8217;t bother checking for errors. The usage is pretty clear, but there&#8217;s one odd issue:
</p>

<pre>&nbsp;
&gt;&gt;&gt; Card<span style="color: #66cc66;">&#40;</span>'<span style="color: #cc66cc;">3</span>', 's'<span style="color: #66cc66;">&#41;</span>
&lt;Card: 3s&gt;
&gt;&gt;&gt; Card<span style="color: #66cc66;">&#40;</span>'<span style="color: #cc66cc;">3</span>', 's'<span style="color: #66cc66;">&#41;</span> == Card<span style="color: #66cc66;">&#40;</span>'<span style="color: #cc66cc;">3</span>', 's'<span style="color: #66cc66;">&#41;</span>
False</pre>

<p>
Huh? That&#8217;s odd, an instance of Card doesn&#8217;t equal another Card just like itself? Well, let&#8217;s look at the <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2RvY3MucHl0aG9uLm9yZy9yZWYvY29tcGFyaXNvbnMuaHRtbCNsMmgtNDI5">Python docs</a>. It talks a bit about comparing the builtin types (numbers, strings, lists&#8230;) and then says: &#8220;Most other types compare unequal unless they are the same object&#8221;. 
</p>

<p>
Python does this by comparing the ids of the objects. You can call id() on your objects and see that even identically-constructed objects have different ids because they&#8217;re in different locations in memory. This is decent default because you wouldn&#8217;t want Python walking deeply through all of your objects, a potentially expensive operation. Python does do a little bit more for equality, as implied by that &#8220;most&#8221; in the documentation.
</p>

<p>
Python does one more thing for us. It looks for a function named __eq__ to call on the left-hand object and uses it to determine equality. So let&#8217;s add it to Card:
</p>

<pre>&nbsp;
    <span style="color: #b1b100;">def</span> __eq__<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">self</span>, card<span style="color: #66cc66;">&#41;</span>:
        <span style="color: #b1b100;">return</span> <span style="color: #b1b100;">self</span>.<span style="color: #202020;">value</span> == card.<span style="color: #202020;">value</span> <span style="color: #b1b100;">and</span> <span style="color: #b1b100;">self</span>.<span style="color: #202020;">suit</span> == card.<span style="color: #202020;">suit</span></pre>

<p>
Easy enough. And usage:
</p>

<pre>&nbsp;
&gt;&gt;&gt; Card<span style="color: #66cc66;">&#40;</span>'<span style="color: #cc66cc;">3</span>', 's'<span style="color: #66cc66;">&#41;</span> == Card<span style="color: #66cc66;">&#40;</span>'<span style="color: #cc66cc;">3</span>', 's'<span style="color: #66cc66;">&#41;</span>
True
&gt;&gt;&gt; Card<span style="color: #66cc66;">&#40;</span>'<span style="color: #cc66cc;">3</span>', 's'<span style="color: #66cc66;">&#41;</span> == Card<span style="color: #66cc66;">&#40;</span>'K', 'h'<span style="color: #66cc66;">&#41;</span>
False
&gt;&gt;&gt; Card<span style="color: #66cc66;">&#40;</span>'<span style="color: #cc66cc;">3</span>', 's'<span style="color: #66cc66;">&#41;</span> != Card<span style="color: #66cc66;">&#40;</span>'<span style="color: #cc66cc;">3</span>', 's'<span style="color: #66cc66;">&#41;</span>
True</pre>

<p>
Now that last one is a bit surprising. Back at <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2RvY3MucHl0aG9uLm9yZy9yZWYvY3VzdG9taXphdGlvbi5odG1sI2wyaC0xODg=">the docs</a>, we learn &#8220;There are no implied relationships among the comparison operators. The truth of x==y does not imply that x!=y is false. Accordingly, when defining __eq__(), one should also define __ne__() so that the operators will behave as expected.&#8221; That&#8217;s:
</p>

<pre>&nbsp;
    <span style="color: #b1b100;">def</span> __ne__<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">self</span>, card<span style="color: #66cc66;">&#41;</span>:
        <span style="color: #b1b100;">return</span> <span style="color: #b1b100;">not</span> <span style="color: #b1b100;">self</span>.__eq__<span style="color: #66cc66;">&#40;</span>card<span style="color: #66cc66;">&#41;</span></pre>

<p>
After that, the Cards equate properly and everything&#8217;s happy. One of the best things about Python is that it regularly gives you a sensible default and then lets you customize your code to work seamlessly with the language. This is what us Python fans mean when we go on and on about code being &#8220;Pythonic&#8221;.
</p> <img src="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=57" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://push.cx/2006/equality-for-python/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

