<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><title>David Winterbottom (@codeinthehole)</title><link>http://codeinthehole.com/writing/</link><description>Latest writing</description><language>en-us</language><lastBuildDate>Tue, 21 May 2013 06:15:56 -0000</lastBuildDate><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/codeintheholecom" /><feedburner:info uri="codeintheholecom" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:browserFriendly></feedburner:browserFriendly><item><title>PyPI README badges</title><link>http://codeinthehole.com/writing/pypi-readme-badges/</link><description>&lt;p&gt;Thanks to &lt;a class="reference external" href="https://twitter.com/kuramanga"&gt;&amp;#64;kuramanga&lt;/a&gt;, it's now possible to add shiny PyPi badges to your Python project
READMEs that indicate the latest released version on PyPI and the total number
of downloads.&lt;/p&gt;
&lt;a class="reference external image-reference" href="https://github.com/tangentlabs/django-oscar"&gt;&lt;img alt="django-oscar PyPi badges" class="screenshot" src="/static/images/screenshots/oscar-pypi-badges.png" /&gt;&lt;/a&gt;
&lt;p&gt;This screenshot is taken from &lt;a class="reference external" href="https://github.com/tangentlabs/django-oscar"&gt;django-oscar&lt;/a&gt;'s README.&lt;/p&gt;
&lt;p&gt;Embed these badges in your own repo as Restructured text:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="p"&gt;..&lt;/span&gt; &lt;span class="ow"&gt;image&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt; https://pypip.in/v/$REPO/badge.png
    &lt;span class="nc"&gt;:target:&lt;/span&gt; &lt;span class="nf"&gt;https://crate.io/packages/$REPO/&lt;/span&gt;
    &lt;span class="nc"&gt;:alt:&lt;/span&gt; &lt;span class="nf"&gt;Latest PyPI version&lt;/span&gt;

&lt;span class="p"&gt;..&lt;/span&gt; &lt;span class="ow"&gt;image&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt; https://pypip.in/d/$REPO/badge.png
    :target: https://crate.io/packages/$REPO/
    &lt;span class="nc"&gt;:alt:&lt;/span&gt; &lt;span class="nf"&gt;Number of PyPI downloads&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;or Markdown:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;[![PyPi version](https://pypip.in/v/$REPO/badge.png)](https://crate.io/packages/$REPO/)
[![PyPi downloads](https://pypip.in/d/$REPO/badge.png)](https://crate.io/packages/$REPO/)
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;a class="reference external" href="https://github.com/kura/pypipins"&gt;code is available ...&lt;/a&gt;&lt;/p&gt;</description><guid>http://codeinthehole.com/writing/pypi-readme-badges/</guid></item><item><title>A useful Git post-checkout hook for Python repos</title><link>http://codeinthehole.com/writing/a-useful-git-post-checkout-hook-for-python-repos/</link><description>&lt;p&gt;Every now and again, an innocent python developer checks out a new Git branch
then proceeds to bang their head against a bug caused by an orphaned &lt;tt class="docutils literal"&gt;.pyc&lt;/tt&gt;
file from the previous branch.  Since &lt;tt class="docutils literal"&gt;*.pyc&lt;/tt&gt; files are typically in the repo's
&lt;tt class="docutils literal"&gt;.gitignore&lt;/tt&gt; file, they are not removed when switching branches and can cause
issues if the corresponding &lt;tt class="docutils literal"&gt;.py&lt;/tt&gt; is removed.&lt;/p&gt;
&lt;p&gt;This can be neatly addressed through a 'post checkout' hook which deletes all
such files.  Here is such a script, which also removes empty folders and prints
a summary:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c"&gt;#!/usr/bin/env bash&lt;/span&gt;

&lt;span class="c"&gt;# Delete .pyc files and empty directories from root of project&lt;/span&gt;
&lt;span class="nb"&gt;cd&lt;/span&gt; ./&lt;span class="k"&gt;$(&lt;/span&gt;git rev-parse --show-cdup&lt;span class="k"&gt;)&lt;/span&gt;

&lt;span class="c"&gt;# Clean-up&lt;/span&gt;
find . -name &lt;span class="s2"&gt;&amp;quot;.DS_Store&amp;quot;&lt;/span&gt; -delete

&lt;span class="nv"&gt;NUM_PYC_FILES&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$(&lt;/span&gt; find . -name &lt;span class="s2"&gt;&amp;quot;*.pyc&amp;quot;&lt;/span&gt; | wc -l | tr ...&lt;/pre&gt;&lt;/div&gt;</description><guid>http://codeinthehole.com/writing/a-useful-git-post-checkout-hook-for-python-repos/</guid></item><item><title>Disable database access when writing unit tests in Django</title><link>http://codeinthehole.com/writing/disable-database-access-when-writing-unit-tests-in-django/</link><description>&lt;p&gt;Consider this curio:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;mock&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.utils.functional&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;curry&lt;/span&gt;

&lt;span class="n"&gt;no_database&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;curry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;mock&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;patch&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;django.db.backends.util.CursorWrapper&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;side_effect&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="ne"&gt;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Using the database is not permitted&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This snippet creates a decorator that can wrap a test case or method and
raises an exception if the database is accessed.  This can be useful if you're a
puritan about &lt;em&gt;true&lt;/em&gt; unit tests.&lt;/p&gt;
&lt;p&gt;Use by wrapping a &lt;tt class="docutils literal"&gt;TestCase&lt;/tt&gt; subclass:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.test&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;TestCase&lt;/span&gt;

&lt;span class="nd"&gt;@no_database&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UnitTestCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;TestCase&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;or method:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.test&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;TestCase&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UnitTestCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;TestCase&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

    &lt;span class="nd"&gt;@no_database&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_something&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="o"&gt;...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This snippet is a reformulation of one from Carl Meyer's excellent &lt;a class="reference external" href="http://pyvideo.org/video/699/testing-and-django"&gt;'Testing and
Django'&lt;/a&gt; (about 24 minutes in).&lt;/p&gt;
&lt;p&gt;Challenge: create a similar decorator that prevents all ...&lt;/p&gt;</description><guid>http://codeinthehole.com/writing/disable-database-access-when-writing-unit-tests-in-django/</guid></item><item><title>How to install PIL on 64-bit Ubuntu 12.04</title><link>http://codeinthehole.com/writing/how-to-install-pil-on-64-bit-ubuntu-1204/</link><description>&lt;div class="section" id="problem"&gt;
&lt;h1&gt;Problem&lt;/h1&gt;
&lt;p&gt;You want to install PIL on 64-bit Ubuntu 12.04 (Precise Pangolin).&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="solution"&gt;
&lt;h1&gt;Solution&lt;/h1&gt;
&lt;p&gt;With pip already installed, install the required development packages:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;sudo apt-get install python-dev libjpeg-dev libfreetype6-dev zlib1g-dev
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;and symlink the three image libraries into &lt;tt class="docutils literal"&gt;/usr/lib&lt;/tt&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;sudo ln -s /usr/lib/&lt;span class="sb"&gt;`&lt;/span&gt;uname -i&lt;span class="sb"&gt;`&lt;/span&gt;-linux-gnu/libfreetype.so /usr/lib/
&lt;span class="nv"&gt;$ &lt;/span&gt;sudo ln -s /usr/lib/&lt;span class="sb"&gt;`&lt;/span&gt;uname -i&lt;span class="sb"&gt;`&lt;/span&gt;-linux-gnu/libjpeg.so /usr/lib/
&lt;span class="nv"&gt;$ &lt;/span&gt;sudo ln -s /usr/lib/&lt;span class="sb"&gt;`&lt;/span&gt;uname -i&lt;span class="sb"&gt;`&lt;/span&gt;-linux-gnu/libz.so /usr/lib/
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;PIL should now install with support for JPEGs, PNGs and FreeType, as indicated
by the compilation output:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;--------------------------------------------------------------------
PIL 1.1.7 SETUP SUMMARY
--------------------------------------------------------------------
version       1.1.7
platform      linux2 2.7.3 &lt;span class="o"&gt;(&lt;/span&gt;default, Apr 20 2012, 22:39:59&lt;span class="o"&gt;)&lt;/span&gt;
              &lt;span class="o"&gt;[&lt;/span&gt;GCC 4.6.3&lt;span class="o"&gt;]&lt;/span&gt;
--------------------------------------------------------------------
*** TKINTER support ...&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</description><guid>http://codeinthehole.com/writing/how-to-install-pil-on-64-bit-ubuntu-1204/</guid></item><item><title>Converting Github issues into pull requests</title><link>http://codeinthehole.com/writing/converting-github-issues-into-pull-requests/</link><description>&lt;p&gt;Using the &lt;a class="reference external" href="http://defunkt.io/hub/"&gt;Hub&lt;/a&gt; library, it's possible to convert Github issues into pull
requests.  This gives rise to a useful Github workflow which this article
describes.&lt;/p&gt;
&lt;p&gt;This is nothing new; it's been &lt;a class="reference external" href="http://www.topbug.net/blog/2012/03/25/attach-a-pull-request-to-an-existing-github-issue/"&gt;written about before&lt;/a&gt;.  However, this is something
I do all the time whilst developing &lt;a class="reference external" href="https://github.com/tangentlabs/django-oscar"&gt;Oscar&lt;/a&gt; and I'm fed up with explaining it.
This article is a reference I can point people at.&lt;/p&gt;
&lt;div class="section" id="workflow"&gt;
&lt;h1&gt;Workflow&lt;/h1&gt;
&lt;div class="section" id="discuss"&gt;
&lt;h2&gt;Discuss&lt;/h2&gt;
&lt;p&gt;Discuss an idea for a new feature on the project mailing list.  Agree
on what needs to be done.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="specify"&gt;
&lt;h2&gt;Specify&lt;/h2&gt;
&lt;p&gt;Create a Github issue for the feature.&lt;/p&gt;
&lt;p&gt;It's often useful to write the ticket as a brief functional spec, documenting
the requirements as user stories.  Github's &lt;a class="reference external" href="https://github.com/blog/1375-task-lists-in-gfm-issues-pulls-comments"&gt;support for checkboxes in ...&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;</description><guid>http://codeinthehole.com/writing/converting-github-issues-into-pull-requests/</guid></item><item><title>Altering Postgres table columns with South</title><link>http://codeinthehole.com/writing/altering-postgres-table-columns-with-south/</link><description>&lt;div class="section" id="problem"&gt;
&lt;h1&gt;Problem&lt;/h1&gt;
&lt;p&gt;You're using Postgres with Django.&lt;/p&gt;
&lt;p&gt;You change a field type of one of your models, generate an &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;--auto&lt;/span&gt;&lt;/tt&gt; South migration and
attempt to run it.  However, South chokes on the new migration complaining that
the data in the column cannot be cast to the new type.&lt;/p&gt;
&lt;p&gt;For instance, I recently changed a &lt;tt class="docutils literal"&gt;CharField&lt;/tt&gt; to a &lt;tt class="docutils literal"&gt;TimeField&lt;/tt&gt; but the
corresponding migration lead to:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="go"&gt;Running migrations for stores:&lt;/span&gt;
&lt;span class="go"&gt;- Migrating forwards to 0009_auto__chg_field_openingperiod_start__chg_field_openingperiod_end.&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt; stores:0008_auto__del_unique_store_slug
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt; stores:0009_auto__chg_field_openingperiod_start__chg_field_openingperiod_end
&lt;span class="go"&gt;FATAL ERROR - The following SQL query failed: ALTER TABLE &amp;quot;stores_openingperiod&amp;quot; ALTER COLUMN &amp;quot;start&amp;quot; TYPE time, ALTER COLUMN &amp;quot;start&amp;quot; DROP NOT NULL, ALTER COLUMN &amp;quot;start&amp;quot; DROP DEFAULT;&lt;/span&gt;
&lt;span class="go"&gt;The error was: column &amp;quot;start&amp;quot; cannot be cast to type time without time zone&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Postgres did not know ...&lt;/p&gt;&lt;/div&gt;</description><guid>http://codeinthehole.com/writing/altering-postgres-table-columns-with-south/</guid></item><item><title>Configuring logging for Postgres.app</title><link>http://codeinthehole.com/writing/configuring-logging-for-postgresapp/</link><description>&lt;div class="section" id="problem"&gt;
&lt;h1&gt;Problem&lt;/h1&gt;
&lt;p&gt;You're using &lt;a class="reference external" href="http://postgresapp.com/"&gt;Postgres.app&lt;/a&gt; on a Mac for local development but are getting SQL
errors from your application.  You're seeing an error message:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;ERROR:  current transaction is aborted, commands ignored &lt;span class="k"&gt;until &lt;/span&gt;end of
transaction block
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This isn't very useful: you want to know which query is generating the error.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="solution"&gt;
&lt;h1&gt;Solution&lt;/h1&gt;
&lt;p&gt;Turn on Postgres' logging and watch the log files when the error is generated.&lt;/p&gt;
&lt;p&gt;This is done by editing &lt;tt class="docutils literal"&gt;~/Library/Application Support/Postgres/var/postgresql.conf&lt;/tt&gt; and setting:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;logging_collector = on
log_directory = &amp;#39;pg_log&amp;#39;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;then restarting Postgres to pick up the new settings.  You can then watch the
log files to find out which queries are failing:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;tail -f ~/Library/Application&lt;span class="se"&gt;\ &lt;/span&gt;Support/Postgres/var/pg_log/*
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="discussion"&gt;
&lt;h1&gt;Discussion&lt;/h1&gt;
&lt;p&gt;By default ...&lt;/p&gt;&lt;/div&gt;</description><guid>http://codeinthehole.com/writing/configuring-logging-for-postgresapp/</guid></item><item><title>Mathematics and engineering</title><link>http://codeinthehole.com/writing/mathematics-and-engineering/</link><description>&lt;img alt="/static/images/bookcovers/9780262510875.jpg" class="align-right" src="/static/images/bookcovers/9780262510875.jpg" /&gt;
&lt;p&gt;&lt;a class="reference external" href="http://en.wikipedia.org/wiki/Structure_and_Interpretation_of_Computer_Programs"&gt;Structure and Interpretation of Computer Programs&lt;/a&gt; is full of interesting
footnotes.  Here's a good one about the Fermat test for prime numbers:&lt;/p&gt;
&lt;blockquote&gt;
Numbers that fool the Fermat test are called &lt;em&gt;Carmichael numbers&lt;/em&gt;, and
little is known about them other than that they are extremely rare.  There
255 Carmichael below 100,000,000.  The smallest few are 561, 1105, 1729,
2465, 2821 and 6601.  In testing primality of very large numbers chosen at
random, the chance of stumbling upon a value that fools the Fermat test is
less than the chance that cosmic radiation will cause the computer to make
an error in carrying out a correct algorithm.  Considering an algorithm to
be inadequate for the first reason but not ...&lt;/blockquote&gt;</description><guid>http://codeinthehole.com/writing/mathematics-and-engineering/</guid></item><item><title>Effective pull requests and other good practices for teams using github</title><link>http://codeinthehole.com/writing/pull-requests-and-other-good-practices-for-teams-using-github/</link><description>&lt;p&gt;I work at &lt;a class="reference external" href="http://www.tangentlabs.co.uk"&gt;an agency&lt;/a&gt; where we pay $200 a month to Github for their &lt;a class="reference external" href="https://github.com/plans"&gt;platinum
plan&lt;/a&gt;.  This article is a summary of an internal talk I gave on making the most
of our subscription.&lt;/p&gt;
&lt;p&gt;There's nothing original here: it's just a collection of tips that I've
harvested over the last few years.  I'm publishing this article mainly so I have
something to refer future employees to.&lt;/p&gt;
&lt;div class="section" id="use-pull-requests"&gt;
&lt;h1&gt;Use pull requests&lt;/h1&gt;
&lt;p&gt;Pull requests are an excellent tool for fostering code review.  If you're using
Github for team projects, you should be using these extensively.&lt;/p&gt;
&lt;p&gt;Many people don't realise that you can make pull requests between two branches
of the same repository (the so-called &lt;a class="reference external" href="https://help.github.com/articles/using-pull-requests"&gt;&amp;quot;shared repository ...&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;</description><guid>http://codeinthehole.com/writing/pull-requests-and-other-good-practices-for-teams-using-github/</guid></item><item><title>How to chroot a user in Ubuntu 12.04</title><link>http://codeinthehole.com/writing/how-to-chroot-a-user-in-ubuntu-1204/</link><description>&lt;p&gt;External parties often need to upload data to your application.  Sadly, most
ask for a FTP server.  Push back against this and suggest they use sFTP.&lt;/p&gt;
&lt;p&gt;This article explains how to set-up a chroot-ed user in Ubuntu 12.04 so that an
external party can upload data to your application securely.&lt;/p&gt;
&lt;p&gt;This is mainly for my own reference.&lt;/p&gt;
&lt;div class="section" id="user-set-up"&gt;
&lt;h1&gt;User set-up&lt;/h1&gt;
&lt;p&gt;Create user with a dummy shell:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;adduser --shell&lt;span class="o"&gt;=&lt;/span&gt;/bin/false barry
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;and alter the ownership and permissions of their home folder:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;chown root:barry /home/barry
&lt;span class="nv"&gt;$ &lt;/span&gt;chmod 755 /home/barry
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now create a folder to upload to:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;mkdir /home/barry/uploads
&lt;span class="nv"&gt;$ &lt;/span&gt;chown barry:barry /home/barry/uploads
&lt;span class="nv"&gt;$ &lt;/span&gt;chmod 755 /home/barry/uploads
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="ssh-config"&gt;
&lt;h1&gt;SSH config&lt;/h1&gt;
&lt;p&gt;Edit &lt;tt class="docutils literal"&gt;/etc/ssh/sshd_config&lt;/tt&gt; and comment ...&lt;/p&gt;&lt;/div&gt;</description><guid>http://codeinthehole.com/writing/how-to-chroot-a-user-in-ubuntu-1204/</guid></item><item><title>Prefer WebTest to Django's test client for functional tests</title><link>http://codeinthehole.com/writing/prefer-webtest-to-djangos-test-client-for-functional-tests/</link><description>&lt;p&gt;Since watching Carl Meyer's superb '&lt;a class="reference external" href="http://pyvideo.org/video/699/testing-and-django"&gt;Testing and Django&lt;/a&gt;' talk, I've been using
Ian Bicking's &lt;a class="reference external" href="http://webtest.pythonpaste.org/en/latest/index.html"&gt;WebTest&lt;/a&gt; library for functional tests, via &lt;a class="reference external" href="http://pypi.python.org/pypi/django-webtest"&gt;django-webtest&lt;/a&gt;.  I've
been really impressed and I'd like to stress one of Carl's points - that using
WebTest for functional tests is superior to using the Django client.&lt;/p&gt;
&lt;div class="section" id="why"&gt;
&lt;h1&gt;Why?&lt;/h1&gt;
&lt;p&gt;Several reasons - here's a few:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;WebTest allows you to model a user's experience much more closely as it is
smart about mark-up.  Instead of hand-crafting GET and POST requests, you can
use the WebTest API to follow links and submit forms - this is what users
actually do.  As a result, your tests accurately capture user stories.&lt;/li&gt;
&lt;li&gt;A corollary to the last point is that ...&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;</description><guid>http://codeinthehole.com/writing/prefer-webtest-to-djangos-test-client-for-functional-tests/</guid></item><item><title>Cacheback - asynchronous cache refreshing for Django</title><link>http://codeinthehole.com/writing/cacheback-asynchronous-cache-refreshing-for-django/</link><description>&lt;p&gt;Inspired by &lt;a class="reference external" href="http://jacobian.org/"&gt;Jacob Kaplan-Moss&lt;/a&gt;'s excellent talk &amp;quot;&lt;a class="reference external" href="http://www.oscon.com/oscon2012/public/schedule/detail/24030"&gt;Django doesn't scale&lt;/a&gt;&amp;quot; at
this year's OSCon, I've put together a Django package for re-populating caches
asynchronously.&lt;/p&gt;
&lt;p&gt;It provides a simple API for wrapping expensive read operations that caches
results and uses &lt;a class="reference external" href="http://celeryproject.org/"&gt;Celery&lt;/a&gt; to repopulate items when they become stale.  It can be
used as a decorator for simple cases but provides an extensible class for more
fine-grained control.  It also provides helper classes for working with querysets.&lt;/p&gt;
&lt;p&gt;The package is MIT-licensed, published to &lt;a class="reference external" href="http://pypi.python.org/pypi/django-cacheback"&gt;PyPI&lt;/a&gt; and the source is available on
&lt;a class="reference external" href="https://github.com/codeinthehole/django-cacheback"&gt;Github&lt;/a&gt;.  It's best explained with an ...&lt;/p&gt;
&lt;div class="section" id="example"&gt;
&lt;h1&gt;Example&lt;/h1&gt;
&lt;p&gt;Consider a view that renders a user's tweets:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.shortcuts&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;render&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;myproject.twitter&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;fetch_tweets&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show_tweets ...&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</description><guid>http://codeinthehole.com/writing/cacheback-asynchronous-cache-refreshing-for-django/</guid></item><item><title>Use models for uploads</title><link>http://codeinthehole.com/writing/use-models-for-uploads/</link><description>&lt;p&gt;All Django developers will deal with file uploads at some point.  I contend
that it's a good practice to use models to capture the upload metadata and to
track processing status.  This article explains how and why.&lt;/p&gt;
&lt;div class="section" id="an-e-commerce-example"&gt;
&lt;h1&gt;An e-commerce example&lt;/h1&gt;
&lt;p&gt;Suppose your e-commerce application allows admins to upload CSV files to update
product stock levels (a common requirement).  A typical file may
comprise a SKU and a stock level:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
9781231231999,0
9781231231999,4
9781231231999,2
...
&lt;/pre&gt;
&lt;p&gt;&lt;a class="reference external" href="https://docs.djangoproject.com/en/dev/topics/http/file-uploads/?from=olddocs"&gt;Django's docs&lt;/a&gt; detail a common pattern for dealing with file uploads such as
this.  The steps are generally:&lt;/p&gt;
&lt;ol class="arabic simple"&gt;
&lt;li&gt;Validate the form submission;&lt;/li&gt;
&lt;li&gt;Write upload data to permanent storage;&lt;/li&gt;
&lt;li&gt;Process the file;&lt;/li&gt;
&lt;li&gt;Delete the file (optional)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;handle_upload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;method ...&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</description><guid>http://codeinthehole.com/writing/use-models-for-uploads/</guid></item><item><title>Vim macros for adding i18n support to Django templates</title><link>http://codeinthehole.com/writing/vim-macros-for-adding-i18n-support-to-django-templates/</link><description>&lt;div class="section" id="problem"&gt;
&lt;h1&gt;Problem&lt;/h1&gt;
&lt;p&gt;You want to add i18n support to an existing project.  One part of this is
modifying all templates to use the &lt;tt class="docutils literal"&gt;{% trans &lt;span class="pre"&gt;&amp;quot;...&amp;quot;&lt;/span&gt; %}&lt;/tt&gt; block around all
hard-coded strings.&lt;/p&gt;
&lt;p&gt;When you have a lot of templates, this gets pretty tedious.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="solution"&gt;
&lt;h1&gt;Solution&lt;/h1&gt;
&lt;p&gt;Use Vim macros!&lt;/p&gt;
&lt;div class="section" id="macro-1-convert-tag-text"&gt;
&lt;h2&gt;Macro 1 - Convert tag text&lt;/h2&gt;
&lt;p&gt;To convert&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Welcome to my site&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;to&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;{% trans &amp;quot;Welcome to my site&amp;quot; %}&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;use the macro&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;vitc{% trans &amp;quot;&amp;quot; %}&amp;lt;ESC&amp;gt;4hp
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;which breaks down as:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;vit&lt;/tt&gt; - select content inside the tag;&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;c{% trans &amp;quot;&amp;quot; %}&lt;/tt&gt; - change tag content to be &lt;tt class="docutils literal"&gt;{% trans &amp;quot;&amp;quot; %}&lt;/tt&gt; while saving the
original tag content to the anonymous register;&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;&amp;lt;ESC&amp;gt;4hp&lt;/tt&gt; - move the cursor to the first speech mark and paste the original
tag contents.  Note that &lt;tt class="docutils literal"&gt;&amp;lt;ESC&amp;gt;&lt;/tt&gt; is one ...&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;/div&gt;</description><guid>http://codeinthehole.com/writing/vim-macros-for-adding-i18n-support-to-django-templates/</guid></item><item><title>A data migration for every Django project</title><link>http://codeinthehole.com/writing/a-data-migration-for-every-django-project/</link><description>&lt;p&gt;How to use a South data migration to avoid accidentally sending emails from example.com.&lt;/p&gt;
&lt;div class="section" id="problem"&gt;
&lt;h1&gt;Problem&lt;/h1&gt;
&lt;p&gt;Consider the following snippet from Django's docs &lt;a class="footnote-reference" href="#id2" id="id1"&gt;[1]&lt;/a&gt; for sending a confirmation email:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.contrib.sites.models&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Site&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.core.mail&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;send_mail&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;register_for_newsletter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;current_site&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Site&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_current&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;send_mail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s"&gt;&amp;#39;Thanks for subscribing to &lt;/span&gt;&lt;span class="si"&gt;%s&lt;/span&gt;&lt;span class="s"&gt; alerts&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;current_site&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;&amp;#39;Thanks for your subscription. We appreciate it.&lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="s"&gt;-The &lt;/span&gt;&lt;span class="si"&gt;%s&lt;/span&gt;&lt;span class="s"&gt; team.&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;current_site&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;&amp;#39;editor@&lt;/span&gt;&lt;span class="si"&gt;%s&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;current_site&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;domain&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here the domain for the email sender is taken from the 'current site' instance,
which is controlled by &lt;a class="reference external" href="https://docs.djangoproject.com/en/dev/ref/contrib/sites/?from=olddocs"&gt;Django's 'Sites' framework&lt;/a&gt; and accessible by a custom
method on the manager of the  &lt;tt class="docutils literal"&gt;Site&lt;/tt&gt; model.&lt;/p&gt;
&lt;p&gt;By default, a &lt;tt class="docutils literal"&gt;Site&lt;/tt&gt; instance is ...&lt;/p&gt;&lt;/div&gt;</description><guid>http://codeinthehole.com/writing/a-data-migration-for-every-django-project/</guid></item></channel></rss>
