<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Reliably Broken &#187; Ben Dodd</title>
	<atom:link href="http://reliablybroken.com/b/tag/ben-dodd/feed/" rel="self" type="application/rss+xml" />
	<link>http://reliablybroken.com/b</link>
	<description>It&#039;s a blog: let&#039;s do funch!</description>
	<lastBuildDate>Sat, 04 Sep 2010 21:22:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>More Python features that I really like</title>
		<link>http://reliablybroken.com/b/2010/05/more-python-features-that-i-really-like/</link>
		<comments>http://reliablybroken.com/b/2010/05/more-python-features-that-i-really-like/#comments</comments>
		<pubDate>Fri, 28 May 2010 15:00:00 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Ben Dodd]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://reliablybroken.com/b/?p=446</guid>
		<description><![CDATA[Another thing that makes using Python pleasing is decorators. A decorator is a wrapper for a function (or method) that takes a function (or method) as an argument and returns a new function (or&#8230;) which is then bound to the name for the original function.

The newly-decorated function can then do things like checking the called [...]]]></description>
			<content:encoded><![CDATA[<p>Another thing that makes using <a href="http://www.python.org">Python</a> pleasing is decorators. <a href="http://docs.python.org/reference/compound_stmts.html#function">A decorator is a wrapper for a function</a> (or method) that takes a function (or method) as an argument and returns a new function (or&#8230;) which is then bound to the name for the original function.</p>

<p>The newly-decorated function can then do things like checking the called arguments before invoking the original un-decorated function.</p>

<p><a href="http://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.decorators.user_passes_test">Django provides decorators for authentication</a> so that you can wrap a view function with a check for client credentials before deciding whether to return the original response or a deny access.</p>

<p>In this manner Django&#8217;s authentication decorators encourage orthogonal code: the logic for displaying a view is separated from the logic for deciding whether you should be permitted to see the view&#8217;s output. By keeping them separate, it becomes simpler to re-use the authentication logic and apply it to other views.</p>

<p>Suppose you have a view that accepts <a href="http://docs.djangoproject.com/en/dev/ref/request-response/">a Django request object</a> and checks whether the user is signed in:</p>

<pre><code>def administration_page(request):
    if request.user.is_authenticated():
        return HttpResponse("Welcome, dear user.")
    else:
        return HttpResponseRedirect("/signin/")
</code></pre>

<p>With a decorator you can simplify and clarify things:</p>

<pre><code>@login_required
def administration_page(request):
    return HttpResponse("Welcome, dear user.")
</code></pre>

<p>For older versions of Python (pre 2.4) <a href="http://docs.python.org/whatsnew/2.4.html#pep-318-decorators-for-functions-and-methods">which don&#8217;t understand the <code>@</code> operator</a> one must explicitly decorate the view function like so:</p>

<pre><code>def administration_page(request):
    return HttpResponse("Welcome, dear administrator.")

administration_page = login_required(administration_page)
</code></pre>

<p>Note in the example that the original <code>administration_page</code> function is passed to the decorator. The <code>@</code> syntax in the first example makes that implicit but the two are equivalent.</p>

<p>The implementation of a decorator is interesting. It takes the function itself as an argument and returns a new function which does the actual checking. Here is how the decorator used above might do its stuff:</p>

<pre><code>def login_required(view_function):
    def decorated_function(request):
        if request.user.is_authenticated():
            return view_function(request)
        else:
            return HttpResponseRedirect("/signin/")

    return decorated_function
</code></pre>

<p><em>The actual <a href="http://code.djangoproject.com/browser/django/tags/releases/1.2.1/django/contrib/auth/decorators.py">implementation of Django&#8217;s <code>login_required</code> decorator</a> is considerably less idiotic. Python&#8217;s <a href="http://docs.python.org/library/functools.html">functools module</a> has helpers for writing well-behaved decorators.</em></p>

<p>Because functions in Python are themselves objects the decorator can accept a function reference, construct a new function that checks for authentication and then return a reference to that new function.</p>

<p>Simples!</p>

<p>(Simples gets less simples when you want to write a decorator that accepts configuration arguments because you then need either another layer of nested function definitions or a class whose instances can be called directly, but I&#8217;m going to ignore you for a bit and <em>wow is that Concorde&#8230;?</em>)</p>
]]></content:encoded>
			<wfw:commentRss>http://reliablybroken.com/b/2010/05/more-python-features-that-i-really-like/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python features for a PHP refugee</title>
		<link>http://reliablybroken.com/b/2009/04/python-features-for-a-php-refugee/</link>
		<comments>http://reliablybroken.com/b/2009/04/python-features-for-a-php-refugee/#comments</comments>
		<pubDate>Fri, 10 Apr 2009 22:11:21 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Ben Dodd]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://reliablybroken.com/b/?p=130</guid>
		<description><![CDATA[These are things that particularly impressed me
when I decided I had had enough of PHP and I really ought to look at
the crazy white-space language called Python that was used by
Plone, Trac and Django.

The Zen of Python states most of this in 19 lines, for all you
tl;dr types.

Name-spaces and a minimal set of built-ins

I like [...]]]></description>
			<content:encoded><![CDATA[<p>These are things that particularly impressed me
when I decided I had had enough of <a href="http://www.php.net/">PHP</a> and I really ought to look at
<a href="http://www.python.org/">the crazy white-space language called Python</a> that was used by
<a href="http://plone.org/">Plone</a>, <a href="http://trac.edgewall.org/">Trac</a> and <a href="http://www.djangoproject.com/">Django</a>.</p>

<p>The <a href="http://www.python.org/dev/peps/pep-0020/">Zen of Python</a> states most of this in 19 lines, for all you
<em>tl;dr</em> types.</p>

<h2>Name-spaces and a minimal set of built-ins</h2>

<p>I like that the set of keywords is small, and that the built-in methods are
not much larger. This leaves you with an unpolluted name-space (and if you
enjoy confusing people you can always override the built-ins).</p>

<h2>Explicit versus implicit</h2>

<p>Related to name-spaces is the notion that Python is explicit: there is very
little magic in a Python script. Perhaps the closest thing to magic are the
various special methods that define a behaviour, for example the <code>__getattr__</code>
/ <code>__setattr__</code> / <code>__delattr__</code> methods on a class to control attribute access.
But even then Python makes it obvious those methods have special meaning by
using a double-underscore for the method names.</p>

<p>See <a href="http://docs.python.org/reference/datamodel.html#special-method-names">the page on the data model</a> in the Python documentation for
a description of these methods and their purpose.</p>

<h2>Generators and list comprehensions</h2>

<p>I never realized how much I missed these until I went back to PHP for a small
web project. So much of my code seems to be looping through lists and
accumulating a result or applying a function to each member of the list. I
don&#8217;t think the syntax is particularly obvious, but then I can&#8217;t think of a
better way to do it. At first glance generators looked to be the same as
list comprehensions, but eventually I began to understand the difference
between needing a finite list of objects (<a href="http://docs.python.org/tutorial/datastructures.html#list-comprehensions">list comprehension</a>)
and consuming a sequence of objects (<a href="http://docs.python.org/tutorial/classes.html#generators">generators</a>).</p>

<h2>Named arguments for methods and functions</h2>

<p>Gosh, not having named arguments is painful. As a consumer, named arguments
allow one to forget a function&#8217;s precise argument signature, and as a
designer it allows one to provide sensible defaults and flexibility.</p>

<h2>Dates and times as a native type</h2>

<p>Well, not <em>native</em>, but readily available.</p>

<p><a href="http://docs.python.org/library/datetime.html">Python&#8217;s <code>datetime</code> module</a> provides representations of calendar
dates and times and a bunch of obvious behaviour for comparing two moments.
PHP 5 introduced a proper DateTime class, but I had jumped ship a while
before then &#8211; my affection for Python&#8217;s date handling is borne of a time
when one had to rely on PEAR for useful date functions. Converting everything
to seconds since the Unix epoch was never fun.</p>

<p>The greatest annoyance in Python&#8217;s date implementation is its shrugging
support for timezones &#8211; you nearly always need to resort to a third-party
module (<a href="http://pytz.sourceforge.net/">such as pytz</a> or <a href="http://labix.org/python-dateutil">python-dateutil</a>) to handle
timezones without jeopardizing one&#8217;s sanity.</p>

<h2>Batteries included</h2>

<p>It is odd that one <em>does</em> need an additional module to handle timezones
seeing as the Python standard library includes so many useful modules for
common tasks.</p>

<p>Need to work with <a href="http://docs.python.org/library/csv.html">CSV files</a>? Or <a href="http://docs.python.org/library/getopt.html">command-line arguments</a>?
Or <a href="http://docs.python.org/library/plistlib.html">Mac OS X-style .plist</a> files? Or configuration files in
<a href="http://docs.python.org/library/configparser.html">INI format</a>? Or <a href="http://docs.python.org/library/tarfile.html">tar archives</a> (with gzip or bzip2 compression)?</p>

<p>Oh golly so much tedious work has been done for you in the Python standard
library. I suppose this reflects PHP&#8217;s emphasis as a scripting language for
the Web versus Python&#8217;s use as a general purpose language, but I am very
grateful for that distinction.</p>
]]></content:encoded>
			<wfw:commentRss>http://reliablybroken.com/b/2009/04/python-features-for-a-php-refugee/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ben turns 30</title>
		<link>http://reliablybroken.com/b/2008/06/ben-turns-30/</link>
		<comments>http://reliablybroken.com/b/2008/06/ben-turns-30/#comments</comments>
		<pubDate>Fri, 06 Jun 2008 20:51:30 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Ben Dodd]]></category>

		<guid isPermaLink="false">http://reliablybroken.com/b/?p=3</guid>
		<description><![CDATA[Happy birthday Ben!
]]></description>
			<content:encoded><![CDATA[<p>Happy birthday Ben!</p>
]]></content:encoded>
			<wfw:commentRss>http://reliablybroken.com/b/2008/06/ben-turns-30/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
