<?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"
	>

<channel>
	<title>Electric Words</title>
	<atom:link href="http://www.electricwords.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.electricwords.org</link>
	<description>Dan Ellis's technical blog</description>
	<pubDate>Wed, 05 Nov 2008 00:45:21 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<item>
		<title>Fuzzy date matching in PostgreSQL</title>
		<link>http://www.electricwords.org/2008/11/fuzzy-date-matching-in-postgresql/</link>
		<comments>http://www.electricwords.org/2008/11/fuzzy-date-matching-in-postgresql/#comments</comments>
		<pubDate>Wed, 05 Nov 2008 00:45:21 +0000</pubDate>
		<dc:creator>dan</dc:creator>
		
		<category><![CDATA[databases]]></category>

		<category><![CDATA[postgresql]]></category>

		<category><![CDATA[webdev]]></category>

		<guid isPermaLink="false">http://www.electricwords.org/?p=34</guid>
		<description><![CDATA[As part of one of my side-projects, I wanted a way for users of a web site to search for events by date, but with some flexibility. I also wanted users who are creating events to be able to express uncertainty about the dates on which they happened.

For example, if I record an event that [...]]]></description>
			<content:encoded><![CDATA[<p>As part of one of my side-projects, I wanted a way for users of a web site to search for events by date, but with some flexibility. I also wanted users who are creating events to be able to express uncertainty about the dates on which they happened.</p>

<p>For example, if I record an event that happened sometime in September 1998, I can say it happened on September 15th, 1998 +/- 15 days. That should then be included in the results of a query for events that happened on October 1st, 1998 +/ 1 month, or June 1st, 1998 +/- 6 months, or even September 10th, 1998 +/- 1 day. An exact date would be represented as +/- 0 days.</p>

<p>One way to visualise these is as line segments on a literal date line, stretching from the past into the future, with a mark per day. PostgreSQL&#8217;s spatial types can be used to represent this in an indexable way. First of all, we&#8217;ll create a composite type to represent fuzzy dates:</p>


<div class="wp_syntax"><div class="code"><pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> TYPE fuzzydate <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span>midpoint date<span style="color: #66cc66;">,</span> fuzziness interval<span style="color: #66cc66;">&#41;</span>;</pre></div></div>


<p>The midpoint and fuzziness fields should be self-explanatory. Next, we create a function that will take a fuzzydate and convert it into a box. (PostgreSQL doesn&#8217;t have an &#8216;overlaps&#8217; operator for line segments, so I chose to use zero-height boxes.) The function calculates the start and end dates of the date range, and uses <tt>extract</tt> to convert them into epoch times (the number of seconds since the beginning of 1970). These are then divided by 86400 to get the number of days since 1970. These values form the x coordinates of the returned box.</p>


<div class="wp_syntax"><div class="code"><pre class="plsql plsql" style="font-family:monospace;"><span style="color: #00F;">CREATE</span> <span style="color: #00F;">FUNCTION</span> GeometricDate<span style="color: #00F;">&#40;</span>fd fuzzydate<span style="color: #00F;">&#41;</span> RETURNS box <span style="color: #00F;">AS</span> $$
  <span style="color: #00F;">DECLARE</span>
    start_day <span style="color: #00F;">INTEGER</span>;
    end_day <span style="color: #00F;">INTEGER</span>;
  <span style="color: #00F;">BEGIN</span>
    start_day <span style="color: #00F;">:=</span> <span style="color: #000;">EXTRACT</span><span style="color: #00F;">&#40;</span>epoch <span style="color: #00F;">FROM</span> fd<span style="color: #00F;">.</span>midpoint <span style="color: #00F;">-</span> fd<span style="color: #00F;">.</span>fuzziness<span style="color: #00F;">&#41;</span><span style="color: #00F;">::</span><span style="color: #00F;">INTEGER</span> <span style="color: #00F;">/</span> <span style="color: #800;">86400</span>;
    end_day <span style="color: #00F;">:=</span> <span style="color: #000;">EXTRACT</span><span style="color: #00F;">&#40;</span>epoch <span style="color: #00F;">FROM</span> fd<span style="color: #00F;">.</span>midpoint <span style="color: #00F;">+</span> fd<span style="color: #00F;">.</span>fuzziness<span style="color: #00F;">&#41;</span><span style="color: #00F;">::</span><span style="color: #00F;">INTEGER</span> <span style="color: #00F;">/</span> <span style="color: #800;">86400</span>;
    <span style="color: #00F;">RETURN</span> box<span style="color: #00F;">&#40;</span>point<span style="color: #00F;">&#40;</span>start_day<span style="color: #00F;">,</span> 0<span style="color: #00F;">&#41;</span><span style="color: #00F;">,</span> point<span style="color: #00F;">&#40;</span>end_day<span style="color: #00F;">,</span> 0<span style="color: #00F;">&#41;</span><span style="color: #00F;">&#41;</span>;
  <span style="color: #00F;">END</span>;
$$ LANGUAGE <span style="color: #F00;">'plpgsql'</span> IMMUTABLE;</pre></div></div>


<p>Now we can now perform a query like this:</p>


<div class="wp_syntax"><div class="code"><pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> GeometricDate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'2008-11-04'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'2 days'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>


<p>to get a result like (14189,0),(14185,0), so let&#8217;s create a table that we can perform our date range queries on:</p>


<div class="wp_syntax"><div class="code"><pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> events <span style="color: #66cc66;">&#40;</span>
  id serial <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">,</span>
  name varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
  fdate fuzzydate
<span style="color: #66cc66;">&#41;</span>;</pre></div></div>


<p>Note that the fdate column is only storing the original midpoint and fuzziness. So where is the geometric representation? Well, PostgreSQL allows us to index not just a column, but the results of applying a function to a column:</p>


<div class="wp_syntax"><div class="code"><pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">INDEX</span> events_fdate_index <span style="color: #993333; font-weight: bold;">ON</span> events <span style="color: #993333; font-weight: bold;">USING</span> gist <span style="color: #66cc66;">&#40;</span>GeometricDate<span style="color: #66cc66;">&#40;</span>fdate<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>


<p>Now we can use the geometric date in a query like this one, which says &#8220;give me all the events that were within 10 days before or after November 3rd, 2008&#8243;:</p>


<div class="wp_syntax"><div class="code"><pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> events <span style="color: #993333; font-weight: bold;">WHERE</span> GeometricDate<span style="color: #66cc66;">&#40;</span>fdate<span style="color: #66cc66;">&#41;</span> &amp;amp;&amp;amp; GeometricDate<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'2008-11-03'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'10 days'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>


<p>The really important thing to note here is that GeometricDate is <em>not</em> being called for every row in the table. The result of GeometricDate is taken directly from the index we created, and it&#8217;s the <tt>IMMUTABLE</tt> flag on the function that tells PostgreSQL that it&#8217;s okay to do this.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.electricwords.org/2008/11/fuzzy-date-matching-in-postgresql/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Parameterised classes in Python</title>
		<link>http://www.electricwords.org/2008/08/parameterised-classes-in-python/</link>
		<comments>http://www.electricwords.org/2008/08/parameterised-classes-in-python/#comments</comments>
		<pubDate>Thu, 07 Aug 2008 15:41:06 +0000</pubDate>
		<dc:creator>dan</dc:creator>
		
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.electricwords.org/?p=16</guid>
		<description><![CDATA[Recently, when working with Django, I wanted to create a class that would automatically render instances of a model as an editable HTML table. I used a Django-esque approach to defining the table columns, such that it was similar to Model and Form classes:


class ProductTable&#40;Table&#41;:
    _model = Product
    name [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, when working with <a title="Django project" href="http://www.djangoproject.com">Django</a>, I wanted to create a class that would automatically render instances of a model as an editable HTML table. I used a Django-esque approach to defining the table columns, such that it was similar to Model and Form classes:</p>


<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> ProductTable<span style="color: black;">&#40;</span>Table<span style="color: black;">&#41;</span>:
    _model = Product
    name = TextWidget<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    description = TextWidget<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    size = DropDownWidget<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>


<p>The underscore in <tt>_model</tt> is there to prevent a name collision with the column names, but it&#8217;s a bit ugly. If I was strictly following the Django approach, I probably would have added an inner class:</p>


<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> ProductTable<span style="color: black;">&#40;</span>Table<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">class</span> Meta:
        _model = Product
&nbsp;
    name = TextWidget<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    description = TextWidget<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    size = DropDownWidget<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>


<p>But that&#8217;s a lot of extra noise. What I really wanted was something more like template classes in C++.</p>


<div class="wp_syntax"><div class="code"><pre class="cpp cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> Model<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">class</span> Table <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> TableBase <span style="color: #008000;">&#123;</span>
  <span style="color: #666666;">// ...</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">class</span> ProductTable <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Table<span style="color: #000080;">&lt;</span>Product<span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span>
  <span style="color: #666666;">// ...</span>
<span style="color: #008000;">&#125;</span></pre></div></div>


<p>If Python supported class decorators, I could have just put <tt>@model(Product)</tt> before the class definition. The approach I chose is to inherit from a class dynamically generated by a function that takes the parameter I need, so my <tt>ProductTable</tt> class now becomes:</p>


<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> ProductTable<span style="color: black;">&#40;</span>Table<span style="color: black;">&#40;</span>Product<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
    name = TextWidget<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    description = TextWidget<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    size = DropDownWidget<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>


<p>Much nicer! Now it pretty much reads as &#8216;a ProductTable is a kind of Table (a Product one)&#8217;, which makes sense. To support this, I have a <tt>Table</tt> function that looks like this:</p>


<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> Table<span style="color: black;">&#40;</span>model<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">class</span> TableDynamicBase<span style="color: black;">&#40;</span>TableBase<span style="color: black;">&#41;</span>:
        _model = model
    <span style="color: #ff7700;font-weight:bold;">return</span> TableDynamicBase</pre></div></div>


<p><tt>TableBase</tt> contains whatever was in the original <tt>Table</tt> base class (which in my case was some unrelated metaclass magic).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.electricwords.org/2008/08/parameterised-classes-in-python/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Introduction to Django</title>
		<link>http://www.electricwords.org/2008/08/introduction-to-django/</link>
		<comments>http://www.electricwords.org/2008/08/introduction-to-django/#comments</comments>
		<pubDate>Wed, 06 Aug 2008 13:48:47 +0000</pubDate>
		<dc:creator>dan</dc:creator>
		
		<category><![CDATA[python]]></category>

		<category><![CDATA[articles]]></category>

		<category><![CDATA[django]]></category>

		<guid isPermaLink="false">http://www.electricwords.org/?p=3</guid>
		<description><![CDATA[I haven&#8217;t had any kind of personal web site for ages, but I recently wrote an article, Introduction to Django, for Digital Web magazine, so I thought I&#8217;d put a quick something up here as a point of contact.
]]></description>
			<content:encoded><![CDATA[<p>I haven&#8217;t had any kind of personal web site for ages, but I recently wrote an article, <a title="Helping Perfectionists With Deadlines" href="http://www.digital-web.com/articles/intro_to_django_helping_perfectionists_with_deadlines/">Introduction to Django</a>, for <a title="Digital Web" href="http://www.digital-web.com/">Digital Web magazine</a>, so I thought I&#8217;d put a quick something up here as a point of contact.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.electricwords.org/2008/08/introduction-to-django/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
