<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Rob O'Dwyer]]></title>
  <link href="http://blog.doteight.com/atom.xml" rel="self"/>
  <link href="http://blog.doteight.com/"/>
  <updated>2013-03-19T11:02:03-07:00</updated>
  <id>http://blog.doteight.com/</id>
  <author>
    <name><![CDATA[Rob O'Dwyer]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[A simple solution for mobile web touch events]]></title>
    <link href="http://blog.doteight.com/blog/2012/08/19/a-simple-solution-for-mobile-web-touch-events/"/>
    <updated>2012-08-19T22:40:00-07:00</updated>
    <id>http://blog.doteight.com/blog/2012/08/19/a-simple-solution-for-mobile-web-touch-events</id>
    <content type="html"><![CDATA[<p>I think anyone who&#8217;s worked on building a &#8220;mobile-friendly&#8221; website has run into
the tricky problem of dealing with touch events. Let&#8217;s face it, the default
behaviour for touching &#8220;clickable stuff&#8221; on a web page on all mobile platforms
is pretty nasty. You tap something, and 5 minutes later the page reacts.</p>

<p>When looking at
<a href="http://www.w3.org/TR/touch-events/#the-touchstart---------event">touch events</a>
and how much more responsive they can make your page, there&#8217;s a temptation to
just add them everywhere. After all, you get an almost native-like feel to
buttons and links when you make them respond to <code>ontouch</code>, instead of waiting
for the browser&#8217;s <code>click</code> event to kick in. Unfortunately, this also has the
undesirable effect of messing up scrolling, resizing, and everything else you
can do with a multitouch device around these areas of the page. Ideally, we&#8217;d
like to be able to get the benefit of responding quickly to these touch events
without breaking the natural user experience of using the browser.</p>

<p>I&#8217;ve worked out a simple solution to this in the form of a minimal jQuery plugin
that gives you a replacement for <code>$.click()</code>, and binds both click events for
desktop platforms and uses a slightly more intelligent handling of the
<code>touchstart</code>, <code>touchmove</code>, and <code>touchend</code> to speed up touch event handling
without breaking the natural usage of the page. <!-- more -->To be honest, I&#8217;m still not sure
what the browsers do that takes so much longer to fire the click events, but
this results in a significantly faster experience on both MobileSafari and
Chrome for Android.</p>

<div><script src='https://gist.github.com/3401532.js'></script>
<noscript><pre><code>/**
 * Simple jQuery plugin that replaces $.click with a mobile-responsive version
 * that doesn't break scrolling.
 */
(function($){
  $.fn.extend({ 
    clickTouch: function(handler) {
       return this.each(function() {
         var touchedWithoutScroll = false;
         var self = $(this);

         self.bind('touchstart', function(event) {
           // Ignore multi-touches
           if(event.originalEvent.touches.length &gt; 1) {
             return;
           }
           touchedWithoutScroll = true;
         })
         // If user starts scrolling/panning, let the touch through
         .bind('touchmove', function(event) {
           touchedWithoutScroll = false;
         })
         // If user releases without scrolling/panning/multitouching, it's a touch
         .bind('touchend', function(event) {
           if(touchedWithoutScroll) {
             handler.apply(this, arguments);
             return false;
           }
         })
         .click(handler);
       });
    }
  });
})(jQuery);
</code></pre></noscript></div>



]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Mental Models of Software]]></title>
    <link href="http://blog.doteight.com/blog/2012/06/29/mental-models-of-software/"/>
    <updated>2012-06-29T00:00:00-07:00</updated>
    <id>http://blog.doteight.com/blog/2012/06/29/mental-models-of-software</id>
    <content type="html"><![CDATA[<p>I recently had an interesting experience when trying to buy food at a cafeteria
that made me think about how most people reason about computers. I tend to pay
for most things either with debit or credit cards, because I like how they track
my purchases automatically, and because I&#8217;m honestly just too lazy to make sure
I always have cash with me.</p>

<p>Anyone who&#8217;s used an Interac machine knows how unreliable and finicky they can
be - although I&#8217;m sure there are difficult engineering and cost challenges
involved in designing them, I think they could do a lot better than the
currently available systems and their UI&#8217;s. Usually when something goes wrong,
you can try the transaction again, or at least just pay with a different method.
However, this time, the machine completely locked up, and then somehow froze the
cash register as well.</p>

<p>The interesting part of what happened next had nothing to do with the payment
machines themselves, but how the cashier responded to this. <!-- more -->She began to push
random buttons on the machine, and remove and re-insert the card repeatedly.
When this had no effect after several minutes, I offered to pay with cash that I
borrowed from a friend who was luckily ahead of me in line. When she discovered
that the cash register interface had failed as well, she responded the same way -
blindly pushing buttons and waiting for something to change. At this point, a
long line was forming and a co-worker wandered over to help. He suggested to
restart the machine, mentioning &#8220;it usually helps, but it takes forever to start
up again&#8221;. She gave this a try, and a couple of minutes later the machine was up
and working again.</p>

<p>So what&#8217;s the point of this totally mundane story? I realized something
interesting about the behavior of the cashier, and about people who don&#8217;t have
significant technical experience (e.g. programming, engineering) when they try
to troubleshoot software problems.</p>

<p>A software developer would see this problem and immediately go through a thought
process like this:</p>

<ul>
<li>The payment machine has run into some sort of software bug</li>
<li>It is a computer system that most likely has (a) permanent storage like
flash or a hard drive and (b) non-permanent state stored in memory that
affects the running of the software and (c) a network interface that
receives data from an external service</li>
<li>The buggy state is probably not permanent, and probably results from the
state of the software being messed up, perhaps from unexpected input from
the UI or the network</li>
<li>Therefore, restarting the device should provide it with a &#8220;clean slate&#8221;
that allows it to go back to a known working state and should be the easiest
way to fix the problem</li>
</ul>


<p>A non-technical person might think about it like this:</p>

<ul>
<li>The machine is broken, either because of something I did wrong, or because
of something wrong with the card</li>
<li>I know how to use it when it&#8217;s not broken, but not when it&#8217;s in this
incorrect state</li>
<li>The only effect I can have on this machine is through interacting with it
in a similar way that I did when it was working</li>
<li>If I restart the machine, it will still be broken</li>
</ul>


<p>The question is, why didn&#8217;t the cashier think of restarting the device as a
solution right away, instead of trying to keep interacting with a device
that was obviously not responding? I think this is due to a flawed mental model
of how the machine works.</p>

<p>A computer is somewhat unique among tools in that it has temporary memory. In
comparison, a power drill doesn&#8217;t have any memory, and expecting it to behave
differently when you unplug it and plug it back in again would be silly.
However, I think most people&#8217;s mental model of how software works IS based on
the same mental model that they have for tools and simple appliances. Any change
to a physical tool&#8217;s behavior (such as stripping a drill bit, or burning out the
motor) is a physical one, and thus you can expect it to remain through a power
outage. Many software bugs, on the other hand, are ephemeral and won&#8217;t persist
through a restart of the machine.</p>

<p>So what does this mean for people who design software? It means that you can&#8217;t
assume the user of an application you design shares your mental model of how the
application works, or even of the computer system that it runs atop. The way the
user uses the application, and how errors are handled or presented to the user,
should be based on metaphors for real-world things that they&#8217;re used to and
understand intuitively.</p>

<p>One example of this would be an mobile app that keeps a record of its state so
that it can seamlessly restore back to the same state when the app is closed and
reopened. This gives the illusion of an interface that is persistent -
the user left it a certain way, and when they came back, it was still like that,
just like a physical object. As an application developer, it&#8217;s obvious that the
app doesn&#8217;t REALLY work this way, but that doesn&#8217;t mean we should avoid carefully
constructing this illusion for the user. Otherwise they likely won&#8217;t be able to
reason about how to fix a problem, since they have a very limited concept of how
mobile apps are started/restarted and when they stay running.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[This link will crash your browser]]></title>
    <link href="http://blog.doteight.com/blog/2011/08/24/this-link-will-crash-your-browser/"/>
    <updated>2011-08-24T00:00:00-07:00</updated>
    <id>http://blog.doteight.com/blog/2011/08/24/this-link-will-crash-your-browser</id>
    <content type="html"><![CDATA[<p><a href="data:text/html;base64,PHNjcmlwdD5zZXRUaW1lb3V0KGZ1bmN0aW9uKCl7YT1hdG9iKGxvY2F0aW9uLmhyZWYuc3BsaXQoJywnKVsxXSk7Yj1kb2N1bWVudC5ib2R5LmlubmVySFRNTDt3aW5kb3cubG9jYXRpb249J2RhdGE6dGV4dC9odG1sO2Jhc2U2NCwnK2J0b2EoYStiKX0sMCk8L3NjcmlwdD48ZGl2IHN0eWxlPSJmbG9hdDpsZWZ0Ij4kPC9kaXY+" target="_BLANK" style="margin:20px 0;font-size:1.2em; text-decoration:underline">
Here it is. Go ahead and click on it, we both know you can&#8217;t resist.
</a></p>

<p>If you look closely at the location that link&#8217;s pointing to, you&#8217;ll notice it&#8217;s
not a website. It&#8217;s actually a <a href="http://en.wikipedia.org/wiki/Data_URI_scheme">Data URI</a>, a special kind of address
that allows you to embed data for a browser directly into the link itself.</p>

<p>Data URIs begin with <code>data:</code> and a mimetype to tell the browser how to interpret
the content. After that comes an optional directive <code>;base64</code>, which means that
the following content is encoded as a <a href="http://en.wikipedia.org/wiki/Base64#HTML">Base64</a> string. In this case, the
content is encoded to make it easier to process the URL from inside the page.
The content then follows after a comma.</p>

<!-- more -->


<p>Here is the contents of that link, when decoded, stripped out of the URI and
prettified:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span class="nt">&lt;script&gt;</span>
</span><span class='line'>    <span class="nx">setTimeout</span><span class="p">(</span><span class="kd">function</span><span class="p">(){</span>
</span><span class='line'>        <span class="nx">a</span><span class="o">=</span><span class="nx">atob</span><span class="p">(</span><span class="nx">location</span><span class="p">.</span><span class="nx">href</span><span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="p">)[</span><span class="mi">1</span><span class="p">]);</span>
</span><span class='line'>        <span class="nx">b</span><span class="o">=</span><span class="nb">document</span><span class="p">.</span><span class="nx">body</span><span class="p">.</span><span class="nx">innerHTML</span><span class="p">;</span>
</span><span class='line'>        <span class="nb">window</span><span class="p">.</span><span class="nx">location</span><span class="o">=</span><span class="s1">&#39;data:text/html;base64,&#39;</span> <span class="o">+</span> <span class="nx">btoa</span><span class="p">(</span><span class="nx">a</span> <span class="o">+</span> <span class="nx">b</span><span class="p">)</span>
</span><span class='line'>    <span class="p">},</span> <span class="mi">0</span><span class="p">)</span>
</span><span class='line'><span class="nt">&lt;/script&gt;</span>
</span><span class='line'><span class="nt">&lt;div</span> <span class="na">style=</span><span class="s">&quot;float:left&quot;</span><span class="nt">&gt;</span>$<span class="nt">&lt;/div&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<h2>How does it work?</h2>

<p>Because the contents of window.location are encoded in Base64, it&#8217;s a relatively
simple task to parse the entire page out of it by splitting on the single comma.
We can then grab just the page content with the <code>innerHTML</code> property of the
body tag. Adding these two together and reencoding into a new Data URI yields
the exact same page, <em>but with the content doubled</em>.</p>

<p>We then simply redirect the browser to our newly created &#8220;page&#8221;. The content in
this case is a simple div tag with a dollar sign, which causes the browser
window to fill up with these symbols before crashing or freezing.</p>

<p>The key trick here is that the browser will load the new page almost instantly,
and the content is being produced entirely locally, without having to wait for a
server and read data with HTTP. I&#8217;m actually not sure what crashes each browser,
but I&#8217;m guessing it&#8217;s that none of them place limits on the amount of content,
which in this case is growing exponentially.</p>

<h2>Does it work on all browsers?</h2>

<p>Actually, no. Ironically enough, it doesn&#8217;t affect Internet Explorer. Although
Data URIs are supported in IE8+, <a href="http://msdn.microsoft.com/en-us/library/cc848897.aspx">they can only be used for images and certain
CSS
declarations</a>. Also,
because of its one-process-per-tab model, Google Chrome isn&#8217;t affected too much
by this; it just gives you the unhappy file folder.</p>

<h2>Is this dangerous?</h2>

<p>I don&#8217;t think so. At least, I can&#8217;t think of anything useful to do with it other
than posting a link somewhere to annoy people. Also, most websites should flag a
Data URI as an invalid link, preventing you from posting it to a public area.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Auto-reloading Python applications]]></title>
    <link href="http://blog.doteight.com/blog/2011/03/10/autoreloading-python-apps/"/>
    <updated>2011-03-10T00:00:00-08:00</updated>
    <id>http://blog.doteight.com/blog/2011/03/10/autoreloading-python-apps</id>
    <content type="html"><![CDATA[<p>I wrote a really handy little script for a project I&#8217;m working on involving a
<a href="http://activities.sugarlabs.org//en-US/sugar/">Sugar Activity</a> that
automatically reloads the code whenever a file is changed. It borrows heavily
from the Django project&#8217;s manage.py runserver command, and takes the form of an
activity launcher script.  You could use this for auto-reloading of any
single-threaded Python application, not just GUI applications.</p>

<!-- more -->


<p>Although the code monitoring part is basically the same as the Django server&#8217;s,
I added an feature that&#8217;s always seemed missing to me - When an
exception is thrown starting up the program, it prints out a stack trace, waits
10 seconds, and then resumes. This is really handy if you leave it running, and
are in the habit of compusively saving every 15 seconds, like me. Sometimes when
you make a syntax error in a core part of the application, the server crashes
and you have to restart it, which seems a bit silly. I also had to re-work it a
bit to play along with the GUI framework (PyGTK).</p>

<p>I started off with the <a href="http://code.djangoproject.com/browser/django/trunk/django/utils/autoreload.py">autoreload module used in
Django</a>,
which has a handy <code>python_reloader</code> wrapper function. If the application is
single threaded and doesn&#8217;t use an event loop, you could probably just use this.
It works by starting a child process, which then starts two threads - one for
your application&#8217;s entry point, and the other that monitors the code for changes
by inspecting <code>sys.modules</code> every second. If a change is detected, the monitor
thread shuts down the process. Meanwhile, the first process has been waiting for
this, and starts a brand new child process. This is all done through one script,
by cleverly setting an environment variable <code>RUN_MAIN</code> for the child process
which makes it start the two threads instead of its own child.</p>

<p>Unfortunately, I needed this to work inside of the GTK event loop, and not in
two different threads. There was an easy workaround though - the
<code>gtk.timeout_add</code> and <code>gobject.timeout_add</code> functions allow callbacks to be
scheduled at a predefined interval. I wrote a handler for calling the
<code>code_changed</code> function, and one for quitting the GTK main loop. <code>RESTART_CODE</code>
is a return code that&#8217;s used to indicate the application ended because of a code
change, instead of just crashing.</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="kn">from</span> <span class="nn">autoreload</span> <span class="kn">import</span> <span class="n">code_changed</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">logging</span><span class="o">,</span> <span class="nn">gtk</span><span class="o">,</span> <span class="nn">subprocess</span>
</span><span class='line'>
</span><span class='line'><span class="n">RESTART_CODE</span> <span class="o">=</span> <span class="mi">8</span>
</span><span class='line'><span class="n">ERROR_SLEEP_TIME</span> <span class="o">=</span> <span class="mi">10</span>
</span><span class='line'>
</span><span class='line'><span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span><span class="n">level</span><span class="o">=</span><span class="n">logging</span><span class="o">.</span><span class="n">INFO</span><span class="p">)</span>
</span><span class='line'><span class="n">log</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s">&#39;activity_runner&#39;</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="k">def</span> <span class="nf">exit</span><span class="p">():</span>
</span><span class='line'>    <span class="sd">&quot;&quot;&quot; Quits the GTK event loop &quot;&quot;&quot;</span>
</span><span class='line'>    <span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s">&#39;Quitting now&#39;</span><span class="p">)</span>
</span><span class='line'>    <span class="n">gtk</span><span class="o">.</span><span class="n">main_quit</span><span class="p">()</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="k">def</span> <span class="nf">watch_files</span><span class="p">():</span>
</span><span class='line'>    <span class="sd">&quot;&quot;&quot; Watches imported files for changes &quot;&quot;&quot;</span>
</span><span class='line'>    <span class="k">if</span> <span class="n">code_changed</span><span class="p">():</span>
</span><span class='line'>        <span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s">&#39;Code was changed, exiting...&#39;</span><span class="p">)</span>
</span><span class='line'>        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="n">RESTART_CODE</span><span class="p">)</span>
</span><span class='line'>    <span class="k">else</span><span class="p">:</span>
</span><span class='line'>        <span class="k">return</span> <span class="bp">True</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now I needed the main part of the activity runner, which sets up the options and
environment variables. I used <code>optparse</code> to add two command-line options, a
watch flag (which does what you&#8217;d expect), and a timeout flag. The timeout flag
exits the application after a given number of seconds, which is sometimes handy
for testing.</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="kn">import</span> <span class="nn">optparse</span>
</span><span class='line'>
</span><span class='line'><span class="n">parser</span> <span class="o">=</span> <span class="n">optparse</span><span class="o">.</span><span class="n">OptionParser</span><span class="p">()</span>
</span><span class='line'>
</span><span class='line'><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&#39;-w&#39;</span><span class="p">,</span> <span class="s">&#39;--watch&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&#39;store_true&#39;</span><span class="p">,</span>
</span><span class='line'>        <span class="n">help</span><span class="o">=</span><span class="s">&quot;Restart activity when files are changed&quot;</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&#39;-t&#39;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s">&#39;int&#39;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&#39;timeout&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&#39;store&#39;</span><span class="p">,</span>
</span><span class='line'>        <span class="n">help</span><span class="o">=</span><span class="s">&quot;Stop activity after timeout&quot;</span><span class="p">,</span> <span class="n">metavar</span><span class="o">=</span><span class="s">&#39;seconds&#39;</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="n">options</span><span class="p">,</span> <span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
</span><span class='line'>
</span><span class='line'><span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">timeout</span><span class="p">:</span>
</span><span class='line'>    <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&#39;Timeout after </span><span class="si">%d</span><span class="s"> seconds&#39;</span> <span class="o">%</span> <span class="n">options</span><span class="o">.</span><span class="n">timeout</span><span class="p">)</span>
</span><span class='line'>    <span class="n">gtk</span><span class="o">.</span><span class="n">timeout_add</span><span class="p">(</span><span class="mi">1000</span> <span class="o">*</span> <span class="n">options</span><span class="o">.</span><span class="n">timeout</span><span class="p">,</span> <span class="nb">exit</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>This next part is trickier. The first block should only execute in the child
process, but comes first so that it can disable starting a child process of its
own. Similar to <code>python_reloader</code> and friends, the next block turns on the
environment variable flag and runs itself as a child process. When the child
exits, the return code is used to decide what to do next. A zero means that the
event loop was exited normally, probably through the application GUI, so the
main loop ends when this happens. I changed the script here to allow for
errors, by adding a brief delay when a non-zero, non-restart code is returned,
instead of just quitting (the original behaviour)</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;EXIT_ON_CHANGE&quot;</span><span class="p">)</span> <span class="o">==</span> <span class="s">&quot;true&quot;</span><span class="p">:</span>
</span><span class='line'>    <span class="n">gtk</span><span class="o">.</span><span class="n">timeout_add</span><span class="p">(</span><span class="mi">1000</span><span class="p">,</span> <span class="n">watch_files</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>    <span class="c"># Disable watching for sub-process</span>
</span><span class='line'>    <span class="n">options</span><span class="o">.</span><span class="n">watch</span> <span class="o">=</span> <span class="bp">False</span>
</span><span class='line'>
</span><span class='line'><span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">watch</span><span class="p">:</span>
</span><span class='line'>    <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&#39;Watching code files...&#39;</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
</span><span class='line'>        <span class="c"># Run again with env marker added</span>
</span><span class='line'>        <span class="n">env</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
</span><span class='line'>        <span class="n">env</span><span class="p">[</span><span class="s">&quot;EXIT_ON_CHANGE&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s">&#39;true&#39;</span>
</span><span class='line'>        <span class="n">retcode</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">call</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">,</span> <span class="n">shell</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">close_fds</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">env</span><span class="o">=</span><span class="n">env</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&#39;sugar-activity exited with code </span><span class="si">%d</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">retcode</span><span class="p">)</span>
</span><span class='line'>        <span class="k">if</span> <span class="n">retcode</span> <span class="o">!=</span> <span class="n">RESTART_CODE</span><span class="p">:</span>
</span><span class='line'>            <span class="k">if</span> <span class="ow">not</span> <span class="n">retcode</span><span class="p">:</span>
</span><span class='line'>                <span class="c"># If sugar-activity returns zero, just exit</span>
</span><span class='line'>                <span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s">&#39;Activity exited&#39;</span><span class="p">)</span>
</span><span class='line'>                <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
</span><span class='line'>            <span class="k">else</span><span class="p">:</span>
</span><span class='line'>                <span class="c"># If sugar-activity returns an error code, restart after a delay</span>
</span><span class='line'>                <span class="n">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&#39;Activity did not exit safely!&#39;</span><span class="p">)</span>
</span><span class='line'>                <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">ERROR_SLEEP_TIME</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Since the master process is stuck in that loop until the application exits, all
that&#8217;s left now is to start the app! I&#8217;m leaving out some Sugar-specific
environment variable stuff, but this is where you&#8217;d call your application entry
point.</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="kn">from</span> <span class="nn">sugar.activity</span> <span class="kn">import</span> <span class="n">main</span>
</span><span class='line'><span class="n">main</span><span class="o">.</span><span class="n">main</span><span class="p">()</span>
</span></code></pre></td></tr></table></div></figure>


<p>This might seem like a lot of effort just for an auto-reloading script, but in
my case it had a more important use. I&#8217;m now able to develop on my Mac while
sharing the code folders through NFS with a virtual machine I use to run the
application. The application just stays running on the VM all the time, and
whenever I save a code file, it restarts automatically. I set up the VM once,
display it on another monitor, and can leave it alone after that!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Decorating With Processing.js]]></title>
    <link href="http://blog.doteight.com/blog/2011/02/22/decorate-your-site-with-processing-js/"/>
    <updated>2011-02-22T00:00:00-08:00</updated>
    <id>http://blog.doteight.com/blog/2011/02/22/decorate-your-site-with-processing-js</id>
    <content type="html"><![CDATA[<p>I finally got around to porting <a href="http://blog.doteight.com/sketches/processingjstitle.html">the script that powers the title
animation</a> to Javascript. Why would I
want to do this? Two reasons:</p>

<ul>
<li>To adaptively adjust the framerate based on the user&#8217;s browser</li>
<li>To make it respond interactively to browser events other than on the canvas</li>
</ul>


<p>One problem I&#8217;ve run into with Processing.js is that
the code ends up encapsulated in its own little &#8220;Java syntax bubble&#8221;, and as far
as I know, the outside page can&#8217;t access it at all. I rewrote it based on the
example given on the Processing.js site (<a href="http://processingjs.org/learning">Writing Processing Code with
Javascript</a>), which made it simple to
add the next two changes.</p>

<!-- more -->


<p>The performance of HTML canvas on iOS (and probably other mobile OSs as well) is
pretty poor, so I thought it would be a good idea to drop the framerate a bit. A
simple browser check (using a jQuery plugin) is enough to do this.</p>

<p>Since the animation looks a lot cooler in fast motion, I decided to make it so
it speeds up to 3X the frame rate when a visitor mouses over the title. Since
I&#8217;m using jQuery for everything else on this site, it was easy enough to bind to
the event listener and toggle back and forth between the two. At first I thought
that the frameRate function would only work inside setup(), but it was not so.
It looks like it&#8217;s possible to change the speed at any time from any event in
the page.</p>

<p>This is most of what I added:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">mobile</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span>
</span><span class='line'><span class="k">try</span> <span class="p">{</span>
</span><span class='line'>    <span class="nx">mobile</span> <span class="o">=</span> <span class="nx">jQuery</span><span class="p">.</span><span class="nx">browser</span><span class="p">.</span><span class="nx">mobile</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span> <span class="k">catch</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="kd">var</span> <span class="nx">sketch</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">p</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="kd">var</span> <span class="nx">num_elements</span> <span class="o">=</span> <span class="mi">80</span><span class="p">;</span>
</span><span class='line'>    <span class="nx">p</span><span class="p">.</span><span class="nx">size</span><span class="p">(</span><span class="mi">720</span><span class="p">,</span><span class="mi">100</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">// Wimp out for mobile browsers</span>
</span><span class='line'>    <span class="nx">p</span><span class="p">.</span><span class="nx">frameRate</span><span class="p">(</span><span class="nx">mobile</span><span class="o">?</span> <span class="mi">5</span> <span class="o">:</span> <span class="mi">10</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">// Speed up 3X on mouseenter</span>
</span><span class='line'>    <span class="nx">$</span><span class="p">(</span><span class="nx">canvas</span><span class="p">).</span><span class="nx">parent</span><span class="p">().</span><span class="nx">mouseenter</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>        <span class="nx">p</span><span class="p">.</span><span class="nx">frameRate</span><span class="p">(</span><span class="mi">30</span><span class="p">);</span>
</span><span class='line'>    <span class="p">})</span>
</span><span class='line'>    <span class="p">.</span><span class="nx">mouseleave</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>        <span class="nx">p</span><span class="p">.</span><span class="nx">frameRate</span><span class="p">(</span><span class="mi">10</span><span class="p">);</span>
</span><span class='line'>    <span class="p">});</span>
</span><span class='line'>    <span class="p">...</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>The <a href="https://gist.github.com/864953">full javascript version</a> is on GitHub.
I think it&#8217;s a pretty simple example of how to convert a Processing sketch to
integrate it with your page.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Why OAuth is awesome]]></title>
    <link href="http://blog.doteight.com/blog/2011/02/08/oauth-is-awesome/"/>
    <updated>2011-02-08T00:00:00-08:00</updated>
    <id>http://blog.doteight.com/blog/2011/02/08/oauth-is-awesome</id>
    <content type="html"><![CDATA[<p>So I finally got the authentication working on my Twitter application, and I&#8217;ve
decided that OAuth is actually pretty awesome. When it works, that is.</p>

<p>I had some initial troubles doing it though. Although there&#8217;s quite a few
libraries out there for setting it up, there&#8217;s a certain unavoidable amount of
difficulty involved. There&#8217;s multiple redirects going on between your app and
the OAuth provider, and when you screw up, it&#8217;s easy to run into the infamous
&#8220;infinite redirect&#8221; bug.</p>

<p>It turns out my problem was the result of passing a relative URL (like
<code>/twitter/authorize</code>) to the API instead of an absolute one (such as
<code>http://mysupersecretapp.appspot.com/twitter/authorize</code>). The path
matched up to the Twitter API&#8217;s one, so it ended up authorizing the
user, then redirecting to itself indefinitely.  You&#8217;d think they&#8217;d check for
that, but they don&#8217;t.</p>

<p>Anyway, what I realized after setting it up is that for most applications, you
don&#8217;t even really need to store the data you get back in a database! You can
just do what <a href="http://yfrog.com">yFrog</a> (and others) do, and just perform a task
for the user after logging in. Then later on the login cookie expires, but who
cares? They can log in again later through Twitter&#8230;</p>

<p>Maybe I&#8217;ve just been missing out on something obvious here, but it seems like
this is a new and powerful way of building web applications. One where the app
is more like a plugin for one of the big sites, like Twitter or Facebook, and
doesn&#8217;t have to store data for or manage its users in any way. If the whole
development process could be made just a little easier, it could really lower
the barrier for building small useful web applications by getting rid of the
user and data management overhead.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Deploying Django on Google App Engine]]></title>
    <link href="http://blog.doteight.com/blog/2011/02/01/django-app-engine/"/>
    <updated>2011-02-01T00:00:00-08:00</updated>
    <id>http://blog.doteight.com/blog/2011/02/01/django-app-engine</id>
    <content type="html"><![CDATA[<p>Today I spent an embarrassingly long time trying to figure out the &#8220;nicest&#8221; way
to set up my Django project to work with Google App Engine. The main problem is
that the development server mangles the Python package import system in some
mysterious way to emulate the environment of App Engine itself. This makes using
<a href="http://pypi.python.org/pypi/virtualenv">virtualenv</a> pretty much impossible, at
least from what I&#8217;ve seen so far. The best I was able to pull off was running an
interactive shell that could access the GAE libraries. This is kind of useless
without being able to run the server though. What I wanted was to have a
workflow like the following, where I could extend the <code>deploy</code> command to
bundle the installed packages from the virtualenv automatically:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>rob@macpro: ~/ <span class="nv">$ </span>workon appengine
</span><span class='line'><span class="o">(</span>appengine<span class="o">)</span>rob@macpro: ~/ <span class="nv">$ </span>cdvirtualenv myapp/
</span><span class='line'><span class="o">(</span>appengine<span class="o">)</span>rob@macpro: myapp/ <span class="nv">$ </span>pip install django-awesome-app another-lib
</span><span class='line'>...
</span><span class='line'><span class="o">(</span>appengine<span class="o">)</span>rob@macpro: myapp/ <span class="nv">$ </span>./manage.py runserver
</span><span class='line'>...
</span><span class='line'><span class="o">(</span>appengine<span class="o">)</span>rob@macpro: myapp/ <span class="nv">$ </span>./manage.py deploy
</span><span class='line'>...
</span></code></pre></td></tr></table></div></figure>


<p>Luckily, it turns out the <a href="http://www.allbuttonspressed.com/projects/djangoappengine">django-nonrel and
djangoappengine</a>
projects have solved most of the problems with this, as long as you do things
their way. I first tried to follow my own &#8220;modified&#8221; version of their setup
tutorial where I bundled all of the libraries into zip files to be used with
zipimport. Don&#8217;t do this; It doesn&#8217;t work. The djangoappengine setup code
actually assumes it&#8217;s located in a top-level folder in your project, and
hardcodes the path to the app.yaml configuration file accordingly. More
importantly, a lot of Django apps won&#8217;t work from inside a zipfile, probably
because of the way templates and static files are loaded.</p>

<p>Anyways, it&#8217;s working quite well now. There&#8217;s still some weird things missing
in the django-nonrel project, like being able to reliably use the dumpdata or
remote commands, but nothing that can&#8217;t be worked around. More soon on actually
building a working site with Django on GAE. Previously I&#8217;ve only used it for
miniature experiments, like <a href="http://turk-keypad.appspot.com">turk-keypad</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Using rlwrap with the Node.js REPL]]></title>
    <link href="http://blog.doteight.com/blog/2011/01/16/rlwrap-and-node/"/>
    <updated>2011-01-16T00:00:00-08:00</updated>
    <id>http://blog.doteight.com/blog/2011/01/16/rlwrap-and-node</id>
    <content type="html"><![CDATA[<p>I&#8217;ve recently been working on a couple of small projects using
<a href="http://nodejs.org/">Node.js</a>, and I&#8217;m loving it&#8217;s simplicity and speed so far.
One thing I have been missing though, thanks to my experience with Python and
IPython, is the lack of a sane REPL for prototyping and testing out bits of
code. I know, you can just run <code>node</code> and type stuff in, but the way that
interpreter behaves is far from what I&#8217;d call &#8220;sane&#8221;. There&#8217;s no history, and if
you screw up the order of brackets, or get lost in a multiline expression, you
get to experience this wonderful prompt:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="o">&gt;</span> <span class="kd">var</span> <span class="nx">x</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'><span class="p">[</span><span class="nx">object</span> <span class="nx">Context</span><span class="p">]</span><span class="o">:</span><span class="mi">1</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="p">...</span>
</span></code></pre></td></tr></table></div></figure>


<p>Fortunately, the node.js docs suggest an alternative, using the handy
<a href="http://utopia.knoware.nl/~hlub/rlwrap/rlwrap.html">rlwrap</a> tool, which wraps
any command inside a readline shell and gives you about a million options. I
extended the provided example a bit, and ended up with this:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nb">alias </span><span class="nv">node</span><span class="o">=</span><span class="s1">&#39;env NODE_NO_READLINE=1 rlwrap -p Green -S &quot;node &gt;&gt;&gt; &quot; node&#39;</span>
</span></code></pre></td></tr></table></div></figure>


<p>This tells node not to use it&#8217;s built-in readline functionality, replaces the
prompt with a custom colored one, and automatically starts maintaining a history
file for this command at <code>~/.node_history</code>. Pretty fantastic. Although rlwrap
does have some other options for messing around with alternate prompts, it looks
like the simplest way of getting around node&#8217;s multiline REPL issues is to just
use the built-in <code>.break</code> command. This seems to fix it in most cases.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Twelve Days of Christmas]]></title>
    <link href="http://blog.doteight.com/blog/2010/12/30/days-of-christmas/"/>
    <updated>2010-12-30T00:00:00-08:00</updated>
    <id>http://blog.doteight.com/blog/2010/12/30/days-of-christmas</id>
    <content type="html"><![CDATA[<p>I wrote a program to print out the verses of the Twelve Days of Christmas. This
started from an argument with my sister about whether the gifts are cumulative
or not (i.e. do you end up with twelve partridges, or just one?). Sadly, this
program doesn&#8217;t really answer the question, any more than singing it would.</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">gifts</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;Partridge in a pear tree&#39;</span><span class="p">,</span> <span class="s">&#39;Turtle doves&#39;</span><span class="p">,</span> <span class="s">&#39;French hens&#39;</span><span class="p">,</span>
</span><span class='line'><span class="s">&#39;Calling birds&#39;</span><span class="p">,</span> <span class="s">&#39;Golden rings&#39;</span><span class="p">,</span> <span class="s">&#39;Geese-a-Laying&#39;</span><span class="p">,</span>
</span><span class='line'><span class="s">&#39;Swans-a-Swimming&#39;</span><span class="p">,</span> <span class="s">&#39;Maids-a-Milking&#39;</span><span class="p">,</span> <span class="s">&#39;Ladies Dancing&#39;</span><span class="p">,</span>
</span><span class='line'><span class="s">&#39;Lords-a-Leaping&#39;</span><span class="p">,</span> <span class="s">&#39;Pipers Piping&#39;</span><span class="p">,</span> <span class="s">&#39;Drummers Drumming&#39;</span><span class="p">]</span>
</span><span class='line'>
</span><span class='line'><span class="k">def</span> <span class="nf">days_of_christmas</span><span class="p">(</span><span class="n">how_many</span><span class="p">):</span>
</span><span class='line'>    <span class="k">for</span> <span class="n">day</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">how_many</span><span class="p">):</span>
</span><span class='line'>        <span class="k">print</span> <span class="s">&#39;On the </span><span class="si">%s</span><span class="s"> day of Christmas, my true love gave to me:&#39;</span> <span class="o">%</span> <span class="n">ordinal</span><span class="p">(</span><span class="n">day</span><span class="p">)</span>
</span><span class='line'>        <span class="k">for</span> <span class="n">number</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">day</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">):</span>
</span><span class='line'>            <span class="k">print</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s"> </span><span class="si">%s%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">named</span><span class="p">(</span><span class="n">number</span><span class="p">),</span> <span class="n">gifts</span><span class="p">[</span><span class="n">number</span><span class="p">],</span> <span class="s">&#39;,&#39;</span> <span class="k">if</span> <span class="n">number</span> <span class="k">else</span> <span class="s">&#39;.&#39;</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="k">def</span> <span class="nf">ordinal</span><span class="p">(</span><span class="n">num</span><span class="p">):</span>
</span><span class='line'>    <span class="k">return</span> <span class="p">(</span><span class="s">&#39;first&#39;</span><span class="p">,</span><span class="s">&#39;second&#39;</span><span class="p">,</span><span class="s">&#39;third&#39;</span><span class="p">,</span><span class="s">&#39;fourth&#39;</span><span class="p">,</span><span class="s">&#39;fifth&#39;</span><span class="p">,</span><span class="s">&#39;sixth&#39;</span><span class="p">,</span>
</span><span class='line'>            <span class="s">&#39;seventh&#39;</span><span class="p">,</span><span class="s">&#39;eighth&#39;</span><span class="p">,</span><span class="s">&#39;ninth&#39;</span><span class="p">,</span><span class="s">&#39;tenth&#39;</span><span class="p">,</span><span class="s">&#39;eleventh&#39;</span><span class="p">,</span><span class="s">&#39;twelfth&#39;</span><span class="p">)[</span><span class="n">num</span><span class="p">]</span>
</span><span class='line'>
</span><span class='line'><span class="k">def</span> <span class="nf">named</span><span class="p">(</span><span class="n">num</span><span class="p">):</span>
</span><span class='line'>    <span class="k">return</span> <span class="p">(</span><span class="s">&#39;a&#39;</span><span class="p">,</span><span class="s">&#39;two&#39;</span><span class="p">,</span><span class="s">&#39;three&#39;</span><span class="p">,</span><span class="s">&#39;four&#39;</span><span class="p">,</span><span class="s">&#39;five&#39;</span><span class="p">,</span><span class="s">&#39;six&#39;</span><span class="p">,</span>
</span><span class='line'>            <span class="s">&#39;seven&#39;</span><span class="p">,</span><span class="s">&#39;eight&#39;</span><span class="p">,</span><span class="s">&#39;nine&#39;</span><span class="p">,</span><span class="s">&#39;ten&#39;</span><span class="p">,</span><span class="s">&#39;eleven&#39;</span><span class="p">,</span><span class="s">&#39;twelve&#39;</span><span class="p">)[</span><span class="n">num</span><span class="p">]</span>
</span><span class='line'>
</span><span class='line'><span class="n">days_of_christmas</span><span class="p">(</span><span class="mi">12</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>It turns out you get 364 gifts if they are cumulative, which, oddly enough, is
exactly enough for you to get a present every day for a year <em>except</em>
Christmas Day. Coincidence?</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[My First Post]]></title>
    <link href="http://blog.doteight.com/blog/2010/12/25/first-post/"/>
    <updated>2010-12-25T00:00:00-08:00</updated>
    <id>http://blog.doteight.com/blog/2010/12/25/first-post</id>
    <content type="html"><![CDATA[<p>It&#8217;s Christmas Day, and I&#8217;m building a blog. Because I can.</p>

<p>Luckily, it hasn&#8217;t taken me very long. I decided to check out
<a href="http://github.com/mojombo/jekyll">Jekyll</a> by starting with a fork of its
<a href="http://github.com/mojombo/mojombo.github.com">creator&#8217;s site</a>.</p>

<p>Now that I have it working, I just need to come up with a fantastic new design
of my own, point some domain names at this and I&#8217;m set. Might even port my
portfolio-website-in-progess over to Jekyll and Liquid as well.</p>

<p>Porting of old posts from my other attempts at blogging might happen at some
point in the distant future, when I&#8217;m very bored. Definitely not tonight.
Here&#8217;s a random piece of Python code to show how awesome Pygments support is:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="k">def</span> <span class="nf">circle</span><span class="p">(</span><span class="n">size</span><span class="p">,</span> <span class="n">fore</span><span class="p">,</span> <span class="n">back</span><span class="p">):</span>
</span><span class='line'>    <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">size</span><span class="p">):</span>
</span><span class='line'>        <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">size</span><span class="p">):</span>
</span><span class='line'>            <span class="n">xi</span><span class="p">,</span> <span class="n">yi</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="n">size</span><span class="o">/</span><span class="mi">2</span> <span class="o">+</span> <span class="mf">0.5</span><span class="p">),</span> <span class="nb">round</span><span class="p">(</span><span class="n">y</span> <span class="o">-</span> <span class="n">size</span><span class="o">/</span><span class="mi">2</span> <span class="o">+</span> <span class="mf">0.5</span><span class="p">)</span>
</span><span class='line'>            <span class="n">radius</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">((</span><span class="n">xi</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">yi</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span><span class="n">size</span><span class="o">/</span><span class="mi">2</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
</span><span class='line'>            <span class="k">print</span> <span class="n">fore</span> <span class="k">if</span> <span class="n">radius</span> <span class="o">&lt;</span> <span class="p">(</span><span class="n">size</span><span class="o">/</span><span class="mi">2</span><span class="p">)</span> <span class="k">else</span> <span class="n">back</span><span class="p">,</span>
</span><span class='line'>        <span class="k">print</span>
</span><span class='line'>
</span><span class='line'><span class="n">circle</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="s">&#39;x&#39;</span><span class="p">,</span> <span class="s">&#39; &#39;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>



]]></content>
  </entry>
  
</feed>
