<?xml version="1.0" encoding="UTF-8"?>
<feed
  xmlns="http://www.w3.org/2005/Atom"
  xmlns:thr="http://purl.org/syndication/thread/1.0"
  xml:lang="en"
   >
  <title type="text">Code Kills</title>
  <subtitle type="text"></subtitle>

  <updated>2012-01-22T21:05:58Z</updated>
  <generator uri="http://blogofile.com/">Blogofile</generator>

  <link rel="alternate" type="text/html" href="http://blog.codekills.net" />
  <id>http://blog.codekills.net/feed/atom/</id>
  <link rel="self" type="application/atom+xml" href="http://blog.codekills.net/feed/atom/" />
  <entry>
    <author>
      <name></name>
      <uri>http://blog.codekills.net</uri>
    </author>
    <title type="html">The evils of `except:`</title>
    <link rel="alternate" type="text/html" href="http://blog.codekills.net/2011/09/29/the-evils-of--except--" />
    <id>http://blog.codekills.net/2011/09/29/the-evils-of--except--</id>
    <updated>2011-09-29T19:34:24Z</updated>
    <published>2011-09-29T19:34:24Z</published>
    <category scheme="http://blog.codekills.net" term="Python" />
    <summary type="html">The evils of `except:`</summary>
    <content type="html" xml:base="http://blog.codekills.net/2011/09/29/the-evils-of--except--">&lt;div class=&#34;document&#34;&gt;
&lt;p&gt;I had some discussion recently about the evils of using a naked &lt;tt class=&#34;docutils literal&#34;&gt;except:&lt;/tt&gt;.
Here is a more complete description of the dangers, and of the correct
solutions.&lt;/p&gt;
&lt;p&gt;In short, &lt;tt class=&#34;docutils literal&#34;&gt;except:&lt;/tt&gt; is bad because hides the source source of the exception
and frustrates debugging.  For example, consider this code:&lt;/p&gt;
&lt;pre class=&#34;literal-block&#34;&gt;
try:
    parsed = parse(file_name)
except:
    raise ParseError(&amp;quot;can&#39;t parse file&amp;quot;)
&lt;/pre&gt;
&lt;p&gt;It will likely produce an error something like this:&lt;/p&gt;
&lt;pre class=&#34;literal-block&#34;&gt;
$ ./my_program.py
Traceback (most recent call last):
    ...
ParseError: can&#39;t parse file
&lt;/pre&gt;
&lt;p&gt;This kind of error makes me want to high-five someone. In the face. With a
chair.&lt;/p&gt;
&lt;p&gt;Notice that it does not contain &lt;em&gt;any&lt;/em&gt; information about:&lt;/p&gt;
&lt;ul class=&#34;simple&#34;&gt;
&lt;li&gt;The file which caused the error&lt;/li&gt;
&lt;li&gt;The line which caused the error&lt;/li&gt;
&lt;li&gt;The nature of the error (is it an expected error? A bug? Who knows!)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And tracking down the source of this error would likely involve some binary
searching on the input file or dropping into a debugger.&lt;/p&gt;
&lt;p&gt;These are some other, equally unhelpful, bits of code that I have seen:&lt;/p&gt;
&lt;pre class=&#34;literal-block&#34;&gt;
# There isn&#39;t much worse than completely hiding the error
except:
    pass

# Almost as bad is not giving any hit at what it was
except:
    print &amp;quot;there was an error!&amp;quot;

# And even showing the original error can be unhelpful if the error is
# something like an IndexError which could come from anywhere
except Exception, e:
    raise MyException(&amp;quot;there was an error: %r&amp;quot; %(e, ))
&lt;/pre&gt;
&lt;p&gt;Now, there &lt;em&gt;is&lt;/em&gt; a situations where using a naked &lt;tt class=&#34;docutils literal&#34;&gt;except:&lt;/tt&gt; can be used
safely. Exactly one.&lt;/p&gt;
&lt;div class=&#34;section&#34; id=&#34;the-except-block-is-terminated-with-a-raise&#34;&gt;
&lt;h1&gt;1. The &lt;tt class=&#34;docutils literal&#34;&gt;except:&lt;/tt&gt; block is terminated with a &lt;tt class=&#34;docutils literal&#34;&gt;raise&lt;/tt&gt;&lt;/h1&gt;
&lt;p&gt;For example, when some cleanup needs to be done before leaving the function:&lt;/p&gt;
&lt;pre class=&#34;literal-block&#34;&gt;
cxn = open_connection()
try:
    use_connection(cxn)
except:
    close_connection(cxn)
    raise
&lt;/pre&gt;
&lt;p&gt;(note that, usually, the &lt;tt class=&#34;docutils literal&#34;&gt;finally:&lt;/tt&gt; block should be used for this kind of
cleanup, but there are some situations where the code above makes more sense)&lt;/p&gt;
&lt;p&gt;Every other situation should use &lt;tt class=&#34;docutils literal&#34;&gt;except Exception, e:&lt;/tt&gt;:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&#34;section&#34; id=&#34;a-new-exception-is-raised-but-the-original-stack-trace-is-used&#34;&gt;
&lt;h1&gt;2. A new exception is raised but the original stack trace is used&lt;/h1&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;pre class=&#34;literal-block&#34;&gt;
try:
    parsed = parse(file_name)
except Exception, e:
    raise ParseError(&amp;quot;error parsing %r: %r&amp;quot; %(file_name, e)), None, sys.exc_info()[2]
&lt;/pre&gt;
&lt;p&gt;A few things to note: first, the &lt;a class=&#34;reference external&#34; href=&#34;http://docs.python.org/reference/simple_stmts.html#grammar-token-raise_stmt&#34;&gt;three expression&lt;/a&gt;
version of &lt;tt class=&#34;docutils literal&#34;&gt;raise&lt;/tt&gt; is used, the third of which being the current stack trace.
This means that the stack trace will point to the &lt;em&gt;original source&lt;/em&gt; of the
error:&lt;/p&gt;
&lt;pre class=&#34;literal-block&#34;&gt;
File &amp;quot;my_program.py&amp;quot;, line 9, in &amp;lt;module&amp;gt;
  parse(file_name)
File &amp;quot;parser.py&amp;quot;, line 2, in parse
  for lineno, line in enumerate(open(file_name), &amp;quot;rb&amp;quot;):
ParseError: error parsing &#39;input.bin&#39;: TypeError(&amp;quot;&#39;str&#39; object cannot be interpreted as an index&amp;quot;,)
&lt;/pre&gt;
&lt;p&gt;Instead of the (less helpful) line which re-raised the error:&lt;/p&gt;
&lt;pre class=&#34;literal-block&#34;&gt;
File &amp;quot;my_program.py&amp;quot;, line 11, in &amp;lt;module&amp;gt;
  raise ParseError(&amp;quot;error parsing %r: %r&amp;quot; %(file_name, e))
ParseError: error parsing &#39;input.bin&#39;: TypeError(&amp;quot;&#39;str&#39; object cannot be interpreted as an index&amp;quot;,)
&lt;/pre&gt;
&lt;p&gt;Second, the error includes the file name and original exception, which will
make debugging significantly easier. When I&#39;m writing particularly fragile code
I&#39;ll often wrap the entire block in a try/except which will include as much
state as is sensible in the error. For example, the main loop of the &lt;tt class=&#34;docutils literal&#34;&gt;parse&lt;/tt&gt;
function might be:&lt;/p&gt;
&lt;pre class=&#34;literal-block&#34;&gt;
def parse(file_name):
    lineno = -1
    current_foo = None
    try:
        f = open(file_name)
        for lineno, line in enumerate(f):
            current_foo = line.split()[0]
            ...
    except Exception, e:
        raise ParseError(&amp;quot;error while parsing %r (line %r; current_foo: %r): %r&amp;quot;
                         %(file_name, lineno, current_foo, e)), None, sys.exc_info()[2]
&lt;/pre&gt;
&lt;/div&gt;
&lt;div class=&#34;section&#34; id=&#34;the-exception-and-stack-trace-are-logged&#34;&gt;
&lt;h1&gt;3. The exception and stack trace are logged&lt;/h1&gt;
&lt;p&gt;For example, the main runloop of an application might be:&lt;/p&gt;
&lt;pre class=&#34;literal-block&#34;&gt;
while 1:
    try:
        do_stuff()
    except Exception, e:
        log.exception(&amp;quot;error in mainloop&amp;quot;)
        time.sleep(1)
&lt;/pre&gt;
&lt;p&gt;A few things to note: first, a naked &lt;tt class=&#34;docutils literal&#34;&gt;except:&lt;/tt&gt; should &lt;em&gt;not&lt;/em&gt; be used here, as
it will also catch &lt;tt class=&#34;docutils literal&#34;&gt;KeyboardInterrupt&lt;/tt&gt; and &lt;tt class=&#34;docutils literal&#34;&gt;SystemExit&lt;/tt&gt; exceptions, which
is almost certainly a bad thing.&lt;/p&gt;
&lt;p&gt;Second, &lt;tt class=&#34;docutils literal&#34;&gt;log.exception&lt;/tt&gt; is used, which includes a complete stack trace in the
log (care should also be taken to make sure that these logs will be checked -
for example by sending an email on exception logs).&lt;/p&gt;
&lt;p&gt;Third, the &lt;tt class=&#34;docutils literal&#34;&gt;time.sleep(1)&lt;/tt&gt; ensures that the system won&#39;t get clobbered if the
&lt;tt class=&#34;docutils literal&#34;&gt;do_stuff()&lt;/tt&gt; function immediately raises an exception.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
</content>
  </entry>
  <entry>
    <author>
      <name></name>
      <uri>http://blog.codekills.net</uri>
    </author>
    <title type="html">Checking types in Python</title>
    <link rel="alternate" type="text/html" href="http://blog.codekills.net/2011/09/26/checking-types-in-python" />
    <id>http://blog.codekills.net/2011/09/26/checking-types-in-python</id>
    <updated>2011-09-26T17:53:08Z</updated>
    <published>2011-09-26T17:53:08Z</published>
    <category scheme="http://blog.codekills.net" term="Python" />
    <summary type="html">Checking types in Python</summary>
    <content type="html" xml:base="http://blog.codekills.net/2011/09/26/checking-types-in-python">&lt;div class=&#34;document&#34;&gt;
&lt;p&gt;A friend asked me recently when it&#39;s acceptable to check types in Python. Here
is my reply:&lt;/p&gt;
&lt;p&gt;It is almost never a good idea to check that function arguments are exactly the
type you expect. For example, these two functions are very, very bad:&lt;/p&gt;
&lt;pre class=&#34;literal-block&#34;&gt;
def add(a, b):
    if not isinstance(a, int):
        raise ValueError(&amp;quot;a is not an int&amp;quot;)
    if not isinstance(b, int):
        raise ValueError(&amp;quot;b is not an int&amp;quot;)
    return a + b

def sum(xs):
    if not isinstance(xs, list):
        raise ValueError(&amp;quot;xs is not a list&amp;quot;)
    base = 0
    for x in xs:
        base += x
    return base
&lt;/pre&gt;
&lt;p&gt;There&#39;s no reason to impose those restrictions, and it makes life difficult if,
for example, you want to add floats or sum an iterator:&lt;/p&gt;
&lt;pre class=&#34;literal-block&#34;&gt;
&amp;gt;&amp;gt;&amp;gt; add(1.2, 3)
...
ValueError(&amp;quot;a is not an int&amp;quot;)
&amp;gt;&amp;gt;&amp;gt; sum(person.age for person in people)
...
ValueError(&amp;quot;xs is not a list&amp;quot;)
&lt;/pre&gt;
&lt;p&gt;Type checking to correctly handle different kinds of input is occasionally
acceptable, but should be used carefully (ex, to do optimizations, or
situations where method overloading would be used in other languages). For
example, these functions could be ok:&lt;/p&gt;
&lt;pre class=&#34;literal-block&#34;&gt;
def contains_all(haystack, needles):
    if not isinstance(haystack, (set, dict)):
        haystack = set(haystack)
    return all(needle in haystack for needle in needles)

def ping_ip(addr):
    if isinstance(addr, numbers.Number):
        addr = numeric_ip_to_string(addr)
    # ping &#39;addr&#39; which should be a string in &amp;quot;1.2.3.4&amp;quot; form
    ...
&lt;/pre&gt;
&lt;p&gt;But it&#39;s almost always better to check for &lt;em&gt;capabilities&lt;/em&gt; instead of checking for
types. For example, if you want to make sure that &lt;tt class=&#34;docutils literal&#34;&gt;add&lt;/tt&gt; throws an error on
invalid input, this would be a better way:&lt;/p&gt;
&lt;pre class=&#34;literal-block&#34;&gt;
def add(a, b):
    if not (hasattr(a, &amp;quot;__add__&amp;quot;) or hasattr(b, &amp;quot;__radd__&amp;quot;)):
        raise ValueError(&amp;quot;can&#39;t add a to b&amp;quot;))
    return a + b
&lt;/pre&gt;
&lt;p&gt;This would be equivalent to excepting an interface instead of an implementation
in a statically typed language:&lt;/p&gt;
&lt;pre class=&#34;literal-block&#34;&gt;
// This is equivilent to ``isinstance(xs, list)`` -- usually bad
public static int sum(ArrayList xs) {
    ...
}

// This is equivilent to ``hasattr(xs, &amp;quot;__iter__&amp;quot;)`` -- almost always better
public static int sum(Collection xs) {
    ...
}
&lt;/pre&gt;
&lt;p&gt;Or better yet, Just Do It and wrap any exceptions which pop up:&lt;/p&gt;
&lt;pre class=&#34;literal-block&#34;&gt;
def add(a, b):
    try:
        return a + b
    except Exception, e:
        raise ValueError(&amp;quot;cannot add %r and %r: %r&amp;quot; %(a, b, e)), None, sys.exc_info()[2]
&lt;/pre&gt;
&lt;p&gt;In general, though, &lt;em&gt;code should assume that function arguments will behave
correctly&lt;/em&gt;, then let the caller use your documentation and Python&#39;s helpful
stack traces and debugging facilities to figure out what they did wrong.&lt;/p&gt;
&lt;/div&gt;
</content>
  </entry>
  <entry>
    <author>
      <name></name>
      <uri>http://blog.codekills.net</uri>
    </author>
    <title type="html">Python 2.X&#39;s str.format is unsafe</title>
    <link rel="alternate" type="text/html" href="http://blog.codekills.net/2011/09/22/python-2.x's-str.format-is-unsafe" />
    <id>http://blog.codekills.net/2011/09/22/python-2.x's-str.format-is-unsafe</id>
    <updated>2011-09-22T23:33:00Z</updated>
    <published>2011-09-22T23:33:00Z</published>
    <category scheme="http://blog.codekills.net" term="Python" />
    <category scheme="http://blog.codekills.net" term="Unicode" />
    <summary type="html">Python 2.X&#39;s str.format is unsafe</summary>
    <content type="html" xml:base="http://blog.codekills.net/2011/09/22/python-2.x's-str.format-is-unsafe">&lt;div class=&#34;document&#34;&gt;
&lt;p&gt;I posted &lt;a class=&#34;reference external&#34; href=&#34;http://twitter.com/wolever/status/116966636606603264&#34;&gt;a tweet&lt;/a&gt;
today when I learned that Python&#39;s %-string-formatting isn&#39;t actually a special
case - the &lt;tt class=&#34;docutils literal&#34;&gt;str&lt;/tt&gt; class just implements the &lt;tt class=&#34;docutils literal&#34;&gt;__mod__&lt;/tt&gt; method.&lt;/p&gt;
&lt;p&gt;One side effect of this is that a few people commented that %-formatting is to
be replaced with &lt;tt class=&#34;docutils literal&#34;&gt;.format&lt;/tt&gt; formatting... So I&#39;d like to take this opportunity
to explain why &lt;tt class=&#34;docutils literal&#34;&gt;.format&lt;/tt&gt; string formatting &lt;strong&gt;is unsafe&lt;/strong&gt; in Python 2.X.&lt;/p&gt;
&lt;p&gt;With %-formatting, if the format string is a &lt;tt class=&#34;docutils literal&#34;&gt;str&lt;/tt&gt; while one of the
replacements is a &lt;tt class=&#34;docutils literal&#34;&gt;unicode&lt;/tt&gt; the result will be &lt;tt class=&#34;docutils literal&#34;&gt;unicode&lt;/tt&gt;:&lt;/p&gt;
&lt;pre class=&#34;literal-block&#34;&gt;
&amp;gt;&amp;gt;&amp;gt; &amp;quot;Hello %s&amp;quot; %(u&amp;quot;world&amp;quot;, )
u&#39;Hello world&#39;
&lt;/pre&gt;
&lt;p&gt;However, &lt;tt class=&#34;docutils literal&#34;&gt;.format&lt;/tt&gt; will always return the same type of string (&lt;tt class=&#34;docutils literal&#34;&gt;str&lt;/tt&gt; or
&lt;tt class=&#34;docutils literal&#34;&gt;unicode&lt;/tt&gt;) as the format string:&lt;/p&gt;
&lt;pre class=&#34;literal-block&#34;&gt;
&amp;gt;&amp;gt;&amp;gt; &amp;quot;Hello {}&amp;quot;.format(u&amp;quot;world&amp;quot;)
&#39;Hello world&#39;
&lt;/pre&gt;
&lt;p&gt;This is a problem in Python 2.X because unqualified string literals are
instances of &lt;tt class=&#34;docutils literal&#34;&gt;str&lt;/tt&gt;, and the implicit encoding of &lt;tt class=&#34;docutils literal&#34;&gt;unicode&lt;/tt&gt; arguments will
almost certainly explode at the least opportune moments:&lt;/p&gt;
&lt;pre class=&#34;literal-block&#34;&gt;
&amp;gt;&amp;gt;&amp;gt; &amp;quot;Hello {}&amp;quot;.format(u&amp;quot;\u263a&amp;quot;)
Traceback (most recent call last):
  File &amp;quot;&amp;lt;stdin&amp;gt;&amp;quot;, line 1, in &amp;lt;module&amp;gt;
UnicodeEncodeError: &#39;ascii&#39; codec can&#39;t encode character u&#39;\u263a&#39; in position 0: ordinal not in range(128)
&lt;/pre&gt;
&lt;p&gt;Of course, one possible solution to this is remembering to prefix all string
literals with &lt;tt class=&#34;docutils literal&#34;&gt;u&lt;/tt&gt;:&lt;/p&gt;
&lt;pre class=&#34;literal-block&#34;&gt;
&amp;gt;&amp;gt;&amp;gt; u&amp;quot;Hello {}&amp;quot;.format(u&amp;quot;\u263a&amp;quot;)
u&#39;Hello \u263a&#39;
&lt;/pre&gt;
&lt;p&gt;But I prefer to simply use %-style formatting, because then I don&#39;t need to
remember anything:&lt;/p&gt;
&lt;pre class=&#34;literal-block&#34;&gt;
&amp;gt;&amp;gt;&amp;gt; &amp;quot;Hello %s&amp;quot; %(u&amp;quot;\u263a&amp;quot;, )
u&#39;Hello \u263a&#39;
&amp;gt;&amp;gt;&amp;gt; print _.encode(&#39;utf-8&#39;)
Hello ☺
&lt;/pre&gt;
&lt;p&gt;Of course, as you&#39;ve probably noticed, this means that the format string is
being implicitly decoded to unicode... But since my string literals generally
don&#39;t contain non-ASCII characters it&#39;s not much of an issue.&lt;/p&gt;
&lt;p&gt;Note that this is &lt;strong&gt;not&lt;/strong&gt; a problem in Py 3k because string literals are
&lt;tt class=&#34;docutils literal&#34;&gt;unicode&lt;/tt&gt;.&lt;/p&gt;
&lt;/div&gt;
</content>
  </entry>
  <entry>
    <author>
      <name></name>
      <uri>http://blog.codekills.net</uri>
    </author>
    <title type="html">Lies, More Lies and Python Packaging Documentation on `package_data`</title>
    <link rel="alternate" type="text/html" href="http://blog.codekills.net/2011/07/15/lies,-more-lies-and-python-packaging-documentation-on--package_data-" />
    <id>http://blog.codekills.net/2011/07/15/lies,-more-lies-and-python-packaging-documentation-on--package_data-</id>
    <updated>2011-07-16T00:35:18Z</updated>
    <published>2011-07-16T00:35:18Z</published>
    <category scheme="http://blog.codekills.net" term="Python" />
    <summary type="html">Lies, More Lies and Python Packaging Documentation on `package_data`</summary>
    <content type="html" xml:base="http://blog.codekills.net/2011/07/15/lies,-more-lies-and-python-packaging-documentation-on--package_data-">&lt;div class=&#34;document&#34;&gt;
&lt;p&gt;My slice of Python packaging hell today was thanks to the lie that is
&lt;tt class=&#34;docutils literal&#34;&gt;package_data&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;You see, I&#39;ve been trying to create an package that includes non-Python files
in the distribution... So I did what any good developer would do and hit the
documentation:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Package data can be added to packages using the package_data keyword
argument to the setup() function.&lt;/p&gt;
&lt;p class=&#34;attribution&#34;&gt;&amp;mdash;&lt;a class=&#34;reference external&#34; href=&#34;http://docs.python.org/distutils/setupscript.html#installing-package-data&#34;&gt;Distutils documentation&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;and&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;If you want finer-grained control over what files are included (for
example, if you have documentation files in your package directories and
want to exclude them from installation), then you can also use the
&lt;tt class=&#34;docutils literal&#34;&gt;package_data&lt;/tt&gt; keyword.&lt;/p&gt;
&lt;p class=&#34;attribution&#34;&gt;&amp;mdash;&lt;a class=&#34;reference external&#34; href=&#34;http://packages.python.org/distribute/setuptools.html#including-data-files&#34;&gt;Distribute documentation&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Over the last hour, though, I&#39;ve learned that these statements are somewhere
between “dangerously misleading” and “damn lies”.&lt;/p&gt;
&lt;p&gt;This is because the primary type of Python package is a source package, and the
canonical method for creating a source package is by using &lt;tt class=&#34;docutils literal&#34;&gt;setup.py sdist&lt;/tt&gt;.
However, the data specified in &lt;tt class=&#34;docutils literal&#34;&gt;package_data&lt;/tt&gt; are &lt;strong&gt;not&lt;/strong&gt; included in source
distributions — they are &lt;strong&gt;only&lt;/strong&gt; included in binary (&lt;tt class=&#34;docutils literal&#34;&gt;setup.py bdist&lt;/tt&gt;)
distributions and installs (&lt;tt class=&#34;docutils literal&#34;&gt;setup.py install&lt;/tt&gt;).&lt;/p&gt;
&lt;p&gt;The only way to get package data included in source packages is the
&lt;tt class=&#34;docutils literal&#34;&gt;MANIFEST.in&lt;/tt&gt; file... Which will &lt;strong&gt;also&lt;/strong&gt; include data in binary
distributions and installs.&lt;/p&gt;
&lt;p&gt;Which renders the &lt;tt class=&#34;docutils literal&#34;&gt;package_data&lt;/tt&gt; option useful only if &lt;tt class=&#34;docutils literal&#34;&gt;sdist&lt;/tt&gt; is not used…
And dangerously misleading if &lt;tt class=&#34;docutils literal&#34;&gt;sdist&lt;/tt&gt; is used.&lt;/p&gt;
&lt;p&gt;tl;dr: &lt;tt class=&#34;docutils literal&#34;&gt;package_data&lt;/tt&gt; is a lie. Ignore it. Only use &lt;tt class=&#34;docutils literal&#34;&gt;MANIFEST.in&lt;/tt&gt;.&lt;/p&gt;
&lt;/div&gt;
</content>
  </entry>
  <entry>
    <author>
      <name></name>
      <uri>http://blog.codekills.net</uri>
    </author>
    <title type="html">Tips for Managing a Django Project</title>
    <link rel="alternate" type="text/html" href="http://blog.codekills.net/2011/06/22/tips-for-managing-a-django-project" />
    <id>http://blog.codekills.net/2011/06/22/tips-for-managing-a-django-project</id>
    <updated>2011-06-22T20:44:05Z</updated>
    <published>2011-06-22T20:44:05Z</published>
    <category scheme="http://blog.codekills.net" term="Python" />
    <summary type="html">Tips for Managing a Django Project</summary>
    <content type="html" xml:base="http://blog.codekills.net/2011/06/22/tips-for-managing-a-django-project">&lt;div class=&#34;document&#34;&gt;
&lt;p&gt;During the time I&#39;ve spent with Django, I&#39;ve picked up a couple tricks for
making life a little bit less terrible.&lt;/p&gt;
&lt;p&gt;First, split the project project into three (or more) parts, each with its own
&lt;tt class=&#34;docutils literal&#34;&gt;settings.py&lt;/tt&gt;: the main project, the development environment and the
production environment. For example, my current project, code named &lt;tt class=&#34;docutils literal&#34;&gt;eos&lt;/tt&gt;,
has a directory structure something like this:&lt;/p&gt;
&lt;pre class=&#34;literal-block&#34;&gt;
eos/
    .hg/
    .hgignore
    manage.py
    run
    eos/
        __init__.py
        settings.py
        templates/
        urls.py
        ...
    hacking/
        __init__.py
        settings.py
        db.sqlite3
        ...
    production/
        __init__.py
        settings.py
        run.wsgi
    ...
&lt;/pre&gt;
&lt;p&gt;The &lt;tt class=&#34;docutils literal&#34;&gt;eos/&lt;/tt&gt; directory is more or less a standard Django project (ie, created
by &lt;tt class=&#34;docutils literal&#34;&gt;&lt;span class=&#34;pre&#34;&gt;django-admin.py&lt;/span&gt; startproject&lt;/tt&gt;), except that &lt;tt class=&#34;docutils literal&#34;&gt;eos/settings.py&lt;/tt&gt; does not
have any environment-specific information in it (for example, it doesn&#39;t have
any database settings or URLs).&lt;/p&gt;
&lt;p&gt;The &lt;tt class=&#34;docutils literal&#34;&gt;hacking/&lt;/tt&gt; and &lt;tt class=&#34;docutils literal&#34;&gt;production/&lt;/tt&gt; directories also contain &lt;tt class=&#34;docutils literal&#34;&gt;settings.py&lt;/tt&gt;
files, except they define &lt;strong&gt;only&lt;/strong&gt; environment specific settings. For example,
&lt;tt class=&#34;docutils literal&#34;&gt;hacking/settings.py&lt;/tt&gt; looks a bit like this:&lt;/p&gt;
&lt;pre class=&#34;literal-block&#34;&gt;
from eos.settings import *
path = lambda *parts: os.path.join(os.path.dirname(__file__), *parts)

DATABASE_ENGINE = &amp;quot;sqlite3&amp;quot;
DATABASE_NAME = path(&amp;quot;db.sqlite3&amp;quot;)

DEBUG = True
&lt;/pre&gt;
&lt;p&gt;While &lt;tt class=&#34;docutils literal&#34;&gt;production/settings.py&lt;/tt&gt; contains:&lt;/p&gt;
&lt;pre class=&#34;literal-block&#34;&gt;
from eos.settings import *

DATABASE_ENGINE = &amp;quot;psycopg2&amp;quot;
DATABASE_NAME = &amp;quot;eos&amp;quot;
DATABASE_USER = &amp;quot;eos&amp;quot;
DATABASE_PASSWORD = &amp;quot;secret&amp;quot;

DEBUG = False
&lt;/pre&gt;
&lt;p&gt;Then, instead of configuring Django (ie, calling &lt;tt class=&#34;docutils literal&#34;&gt;setup_environment&lt;/tt&gt;) on
&lt;tt class=&#34;docutils literal&#34;&gt;eos.settings&lt;/tt&gt;, it is called on either &lt;tt class=&#34;docutils literal&#34;&gt;hacking.settings&lt;/tt&gt; or
&lt;tt class=&#34;docutils literal&#34;&gt;production.settings&lt;/tt&gt;. For example, &lt;tt class=&#34;docutils literal&#34;&gt;manage.py&lt;/tt&gt; contains:&lt;/p&gt;
&lt;pre class=&#34;literal-block&#34;&gt;
...
import hacking.settings
execute_manager(hacking.settings)
&lt;/pre&gt;
&lt;p&gt;And &lt;tt class=&#34;docutils literal&#34;&gt;production/run.wsgi&lt;/tt&gt; contains:&lt;/p&gt;
&lt;pre class=&#34;literal-block&#34;&gt;
...
os.environ[&amp;quot;DJANGO_SETTINGS_MODULE&amp;quot;] = &amp;quot;production.settings&amp;quot;
...
&lt;/pre&gt;
&lt;p&gt;Second, every &lt;tt class=&#34;docutils literal&#34;&gt;settings.py&lt;/tt&gt; file should contain the &lt;tt class=&#34;docutils literal&#34;&gt;path&lt;/tt&gt; lambda:&lt;/p&gt;
&lt;pre class=&#34;literal-block&#34;&gt;
path = lambda *parts: os.path.join(os.path.dirname(__file__), *parts)
&lt;/pre&gt;
&lt;p&gt;It will make specifying paths relative to the &lt;tt class=&#34;docutils literal&#34;&gt;settings.py&lt;/tt&gt; file very easy,
and completely do away with relative-path-related issues. For example:&lt;/p&gt;
&lt;pre class=&#34;literal-block&#34;&gt;
MEDIA_ROOT = path(&amp;quot;media/&amp;quot;)
DATA_ROOT = path(&amp;quot;data/&amp;quot;)
DATABASE_NAME = path(&amp;quot;db.sqlite3&amp;quot;)
&lt;/pre&gt;
&lt;p&gt;Third, there should be scripts for running, saving and re-building the
environment. I use two scripts for this: &lt;tt class=&#34;docutils literal&#34;&gt;run&lt;/tt&gt; and &lt;tt class=&#34;docutils literal&#34;&gt;dump_dev_data&lt;/tt&gt;. By
default the &lt;tt class=&#34;docutils literal&#34;&gt;run&lt;/tt&gt; script calls &lt;tt class=&#34;docutils literal&#34;&gt;./manage.py runserver 8631&lt;/tt&gt;
(specifying a port is useful so that web browsers can distinguish between
different applications - keeping passwords, history, etc. separate). Run can
also be passed a &lt;tt class=&#34;docutils literal&#34;&gt;reset&lt;/tt&gt; argument, which will delete the development database
and rebuild it from the &lt;tt class=&#34;docutils literal&#34;&gt;dev&lt;/tt&gt; fixtures. These fixtures are created by the
&lt;tt class=&#34;docutils literal&#34;&gt;dump_dev_data&lt;/tt&gt; script, which calls &lt;tt class=&#34;docutils literal&#34;&gt;./manage.py dumpdata&lt;/tt&gt; for each
application, saving the data to fixtures named &lt;tt class=&#34;docutils literal&#34;&gt;dev&lt;/tt&gt; (these fixtures are
committed along side the code, so all developers can work off the same data).&lt;/p&gt;
&lt;p&gt;So, for example, when I&#39;m developing a new model, my workflow will look
something like this:&lt;/p&gt;
&lt;pre class=&#34;literal-block&#34;&gt;
... add new model to models.py ...
$ ./run reset # Reset the database adding the new model
... use the website to create data for the new model ...
$ ./dump_dev_data # Dump the newly created data
$ hg commit -m &amp;quot;Adding new model + test data&amp;quot;
&lt;/pre&gt;
&lt;/div&gt;
</content>
  </entry>
  <entry>
    <author>
      <name>David Wolever</name>
      <uri>http://blog.codekills.net</uri>
    </author>
    <title type="html">Python security tip: urllib/urllib2 will read `file://` URLs</title>
    <link rel="alternate" type="text/html" href="http://blog.codekills.net/2011/06/05/python-security-tip--urllib-urllib2-will-read--file-----urls" />
    <id>http://blog.codekills.net/2011/06/05/python-security-tip--urllib-urllib2-will-read--file-----urls</id>
    <updated>2011-06-06T03:03:00Z</updated>
    <published>2011-06-06T03:03:00Z</published>
    <category scheme="http://blog.codekills.net" term="Python" />
    <summary type="html">Python security tip: urllib/urllib2 will read `file://` URLs</summary>
    <content type="html" xml:base="http://blog.codekills.net/2011/06/05/python-security-tip--urllib-urllib2-will-read--file-----urls">

&lt;p&gt;I discovered, entirely by accident, that &lt;code&gt;urllib2.urlopen&lt;/code&gt;, &lt;code&gt;urllib.urlretrieve&lt;/code&gt;, and probably others, will happily read &lt;code&gt;file://&lt;/code&gt; urls and filesystem paths. For example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; import urllib, urllib2
&amp;gt;&amp;gt;&amp;gt; urllib.urlretrieve(&#34;database_connection_settings.txt&#34;, &#34;/tmp/temp_file&#34;)
(&#39;/tmp/temp_file&#39;, &amp;lt;mimetools.Message instance at 0x…&amp;gt;)
&amp;gt;&amp;gt;&amp;gt; urllib2.urlopen(&#34;file:///dev/urandom&#34;).read(10)
&#39;\xf1r?\x0fC\x86p\x05\xa4\xdd&#39;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This means that applications which blindly &lt;code&gt;urlopen&lt;/code&gt; untrusted URLs (for example, from RSS feeds) are potentially vulnerable to information disclosure and denial of service attacks.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <author>
      <name>David Wolever</name>
      <uri>http://blog.codekills.net</uri>
    </author>
    <title type="html">Bug caused by Python&#39;s significant whitespace</title>
    <link rel="alternate" type="text/html" href="http://blog.codekills.net/2011/05/25/bug-caused-by-python's-significant-whitespace" />
    <id>http://blog.codekills.net/2011/05/25/bug-caused-by-python's-significant-whitespace</id>
    <updated>2011-05-25T19:12:00Z</updated>
    <published>2011-05-25T19:12:00Z</published>
    <category scheme="http://blog.codekills.net" term="Python" />
    <summary type="html">Bug caused by Python&#39;s significant whitespace</summary>
    <content type="html" xml:base="http://blog.codekills.net/2011/05/25/bug-caused-by-python's-significant-whitespace">

&lt;p&gt;I encountered my first bug (in recent memory) that was caused by Python&#39;s significant whitespace today.&lt;/p&gt;
&lt;p&gt;As I was editing a class, I accidentally left-shifted an entire method, effectively removing the rest of the methods from the class:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;class Foo:
    def foo(self):
        …

def accidentally_shifted_left(self):
    …

    def bar(self):
        …
&lt;/code&gt;&lt;/pre&gt;
</content>
  </entry>
  <entry>
    <author>
      <name>David Wolever</name>
      <uri>http://blog.codekills.net</uri>
    </author>
    <title type="html">PyCon 2011 Recommended Watching</title>
    <link rel="alternate" type="text/html" href="http://blog.codekills.net/2011/03/18/pycon-2011-recommended-watching" />
    <id>http://blog.codekills.net/2011/03/18/pycon-2011-recommended-watching</id>
    <updated>2011-03-19T01:05:00Z</updated>
    <published>2011-03-19T01:05:00Z</published>
    <category scheme="http://blog.codekills.net" term="Python" />
    <summary type="html">PyCon 2011 Recommended Watching</summary>
    <content type="html" xml:base="http://blog.codekills.net/2011/03/18/pycon-2011-recommended-watching">

&lt;p&gt;Like always, the best parts of PyCon 2011 happened outside of scheduled talks (one particular highlight was a long
conversation with Tavis Rudd, author of &lt;a href=&#34;http://www.cheetahtemplate.org/&#34;&gt;the Cheeta template engine&lt;/a&gt;, about &lt;a href=&#34;https://bitbucket.org/tavisrudd/throw-out-your-templates/src&#34;&gt;why HTML
templates are bad&lt;/a&gt;)… But since those parts weren&#39;t
recorded, I can&#39;t very well recommend watching them. The talks &lt;em&gt;were&lt;/em&gt; recorded, though, and here are the ones I&#39;d
recommend watching.&lt;/p&gt;
&lt;p&gt;(&lt;strong&gt;NB&lt;/strong&gt;: this is not a complete list of good talks, just the ones I found/will find interesting. Check &lt;a href=&#34;http://us.pycon.org/2011/schedule/&#34;&gt;the PyCon 2011
schedule&lt;/a&gt; for a complete list of talks, then search Google for &lt;code&gt;{talk title}
site:blip.tv&lt;/code&gt; to find the videos.)&lt;/p&gt;
&lt;h2&gt;Talks I Attended&lt;/h2&gt;
&lt;p&gt;Ordered roughly by how much I enjoyed them.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;http://us.pycon.org/2011/schedule/presentations/96/&#34;&gt;Using Python 3 to Build a Cloud Computing Service for my Superboard II&lt;/a&gt; by David Beazley (&lt;a href=&#34;http://pycon.blip.tv/file/4878868/&#34;&gt;video link&lt;/a&gt;) — very amusing&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://us.pycon.org/2011/schedule/presentations/137/&#34;&gt;How to write obfuscated python&lt;/a&gt; by Rev. Johnny Healey (&lt;a href=&#34;http://blip.tv/file/4881220&#34;&gt;video link&lt;/a&gt;) — also very amusing&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://us.pycon.org/2011/schedule/presentations/10/&#34;&gt;Everything You Wanted To Know About Pickling, But Were Afraid To Ask!&lt;/a&gt; by Richard T. Saunders (&lt;a href=&#34;http://pycon.blip.tv/file/4882867/&#34;&gt;video link&lt;/a&gt;) — enjoyable, informative&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://us.pycon.org/2011/schedule/presentations/25/&#34;&gt;&#34;Dude, Where&#39;s My RAM?&#34; - A deep dive into how Python uses memory&lt;/a&gt; by Dave Malcolm (&lt;a href=&#34;http://blip.tv/file/4878749&#34;&gt;video link&lt;/a&gt;) — it would be hard to get much lower-level than this talk&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://us.pycon.org/2011/schedule/presentations/198/&#34;&gt;Reverse-engineering Ian Bicking&#39;s brain: inside pip and virtualenv&lt;/a&gt; by Carl Meyer (&lt;a href=&#34;http://pycon.blip.tv/file/4881525/&#34;&gt;video link&lt;/a&gt;) — Carl builds a virtualenv from scratch to show how virtualenv works&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://us.pycon.org/2011/schedule/presentations/263/&#34;&gt;API Design: Lessons Learned&lt;/a&gt; by Raymond Hettinger (&lt;a href=&#34;http://blip.tv/file/4883290&#34;&gt;video link&lt;/a&gt;) — Raymond Hettinger wrote many of Python&#39;s core data structures, and he presents some good advice on API design with examples to back it up&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://us.pycon.org/2011/schedule/presentations/12/&#34;&gt;Genetic Programming in Python&lt;/a&gt; by Eric Floehr (&lt;a href=&#34;http://blip.tv/file/4879765&#34;&gt;video link&lt;/a&gt;) — enjoyable, good examples, interesting stuff&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://us.pycon.org/2011/schedule/presentations/90/&#34;&gt;The Python That Wasn&#39;t&lt;/a&gt; by Larry Hastings (&lt;a href=&#34;http://pycon.blip.tv/file/4881006/&#34;&gt;video link&lt;/a&gt;) — rejected PEPs and features which didn&#39;t make it into Python (also, I enjoy Larry&#39;s talks)&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://us.pycon.org/2011/schedule/presentations/34/&#34;&gt;Testing with mock&lt;/a&gt; by Michael Foord (&lt;a href=&#34;http://blip.tv/file/4881513&#34;&gt;video link&lt;/a&gt;) — the title says it all (Michael Foord is the author of Mock)&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://us.pycon.org/2011/schedule/presentations/98/&#34;&gt;Advanced Network Architectures With ZeroMQ&lt;/a&gt; by Zed Shaw (&lt;a href=&#34;http://blip.tv/file/4878885&#34;&gt;video link&lt;/a&gt;) — ZeroMQ looks really neat, talk is a nice overview&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://us.pycon.org/2011/schedule/presentations/261/&#34;&gt;Fun with Python&#39;s Newer Tools&lt;/a&gt; by Raymond Hettinger (&lt;a href=&#34;http://blip.tv/file/4883247&#34;&gt;video link&lt;/a&gt;) — another talk by Hettinger, shows some interesting tidbits&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://us.pycon.org/2011/schedule/presentations/123/&#34;&gt;Best Practices for Impossible Deadlines&lt;/a&gt; by Christopher Groskopf (&lt;a href=&#34;http://blip.tv/file/4881519&#34;&gt;video link&lt;/a&gt;) — makes me glad I don&#39;t work for a newspaper&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Talks I want to watch&lt;/h2&gt;
&lt;p&gt;In alphabetical order.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;http://us.pycon.org/2011/schedule/presentations/194/&#34;&gt;Get new contributors (and diversity) through outreach&lt;/a&gt; by Asheesh Laroia (&lt;a href=&#34;http://pycon.blip.tv/file/4880711/&#34;&gt;video link&lt;/a&gt;) — Asheesh is an awesome guy, I&#39;m looking forward to hearing this talk&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://us.pycon.org/2011/schedule/presentations/222/&#34;&gt;Handling ridiculous amounts of data with probabilistic data structures&lt;/a&gt; by C. Titus Brown (&lt;a href=&#34;http://pycon.blip.tv/file/4881076/&#34;&gt;video link&lt;/a&gt;) — Titus is a good presenter, and I heard a lot of good things about this talk&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://us.pycon.org/2011/schedule/presentations/169/&#34;&gt;How to kill a patent with Python&lt;/a&gt; by Van Lindberg (&lt;a href=&#34;http://pycon.blip.tv/file/4879824/&#34;&gt;video link&lt;/a&gt;) — Van is also a good presenter, heard good things about this talk&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://us.pycon.org/2011/schedule/presentations/267/&#34;&gt;Linguistics of Twitter&lt;/a&gt; by Michael D. Healy (&lt;a href=&#34;http://blip.tv/file/4883374&#34;&gt;video link&lt;/a&gt;) — looks interesting&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://us.pycon.org/2011/schedule/presentations/74/&#34;&gt;Python and Robots: Teaching Programming in High School&lt;/a&gt; by Vern Ceder (&lt;a href=&#34;http://blip.tv/file/4880794&#34;&gt;video link&lt;/a&gt;) — heard lots of good things about this talk&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://us.pycon.org/2011/schedule/presentations/58/&#34;&gt;Python-Aware Python&lt;/a&gt; by Ned Batchelder (&lt;a href=&#34;http://blip.tv/file/4878776&#34;&gt;video link&lt;/a&gt;) — looks interesting&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://us.pycon.org/2011/schedule/presentations/266/&#34;&gt;Status of Unicode in Python 3&lt;/a&gt; by Victor Stinner (&lt;a href=&#34;http://blip.tv/file/4883349&#34;&gt;video link&lt;/a&gt;) — looks interesting (but, then, I&#39;m abnormally interested in Unicode)&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://us.pycon.org/2011/schedule/presentations/101/&#34;&gt;Swarming the Web: Evolving the Perfect Config File&lt;/a&gt; by Kurt Grandis (&lt;a href=&#34;http://blip.tv/file/4881010&#34;&gt;video link&lt;/a&gt;) — looks interesting&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://us.pycon.org/2011/schedule/presentations/15/&#34;&gt;Through the Side Channel: Timing and Implementation Attacks in Python&lt;/a&gt; by Geremy Condra (&lt;a href=&#34;http://blip.tv/file/4879979&#34;&gt;video link&lt;/a&gt;) — looks interesting&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://us.pycon.org/2011/schedule/presentations/224/&#34;&gt;Useful Namespaces: Context Managers and Decorators&lt;/a&gt; by Jack Diederich (&lt;a href=&#34;http://blip.tv/file/4881235&#34;&gt;video link&lt;/a&gt;) — looks interesting&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://us.pycon.org/2011/schedule/presentations/82/&#34;&gt;Using Blender&#39;s new BPY Python API&lt;/a&gt; by Christopher Allan Webber (&lt;a href=&#34;http://blip.tv/file/4880934&#34;&gt;video link&lt;/a&gt;) — heard good things about it, I&#39;d like to learn Blender&#39;s API&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://us.pycon.org/2011/schedule/presentations/27/&#34;&gt;Using Python to debug C and C++ code (using gdb)&lt;/a&gt; by Dave Malcolm (&lt;a href=&#34;http://blip.tv/file/4877544&#34;&gt;video link&lt;/a&gt;) — looks interesting&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://us.pycon.org/2011/schedule/presentations/160/&#34;&gt;Why is Python slow and how PyPy can help?&lt;/a&gt; by Maciej Fijałkowski and Alex Gaynor (&lt;a href=&#34;http://blip.tv/file/4879780&#34;&gt;video link&lt;/a&gt;) — PyPy &lt;a href=&#34;http://speed.pypy.org/&#34;&gt;is freaking awesome&lt;/a&gt;, I want to learn more about it&lt;/li&gt;
&lt;/ul&gt;
</content>
  </entry>
  <entry>
    <author>
      <name>David Wolever</name>
      <uri>http://blog.codekills.net</uri>
    </author>
    <title type="html">Python quickie: using `itertools.product` to generate binary strings</title>
    <link rel="alternate" type="text/html" href="http://blog.codekills.net/2011/03/18/python-quickie--using--itertools.product--to-generate-binary-strings" />
    <id>http://blog.codekills.net/2011/03/18/python-quickie--using--itertools.product--to-generate-binary-strings</id>
    <updated>2011-03-18T20:08:00Z</updated>
    <published>2011-03-18T20:08:00Z</published>
    <category scheme="http://blog.codekills.net" term="Python" />
    <summary type="html">Python quickie: using `itertools.product` to generate binary strings</summary>
    <content type="html" xml:base="http://blog.codekills.net/2011/03/18/python-quickie--using--itertools.product--to-generate-binary-strings">

&lt;p&gt;Problem: you need to enumerate the binary strings between &lt;code&gt;000&lt;/code&gt; and &lt;code&gt;111&lt;/code&gt; (ie, &lt;code&gt;000&lt;/code&gt;, &lt;code&gt;001&lt;/code&gt;, &lt;code&gt;010&lt;/code&gt;, …).&lt;/p&gt;
&lt;p&gt;Solution: &lt;code&gt;itertools.product&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; from itertools import product
&amp;gt;&amp;gt;&amp;gt; for bits in product([0, 1], repeat=3):
...     print &#34;&#34;.join(str(bit) for bit in bits)
...
000
001
010
011
…
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For more like this, check out Raymond Hettinger&#39;s (really good) talk &lt;a href=&#34;http://pycon.blip.tv/file/4883247/&#34;&gt;Fun with Python&#39;s Newer Tools&lt;/a&gt;, from PyCon 2011.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <author>
      <name>David Wolever</name>
      <uri>http://blog.codekills.net</uri>
    </author>
    <title type="html">Why I like `nose`</title>
    <link rel="alternate" type="text/html" href="http://blog.codekills.net/2010/12/23/why-i-like--nose-" />
    <id>http://blog.codekills.net/2010/12/23/why-i-like--nose-</id>
    <updated>2010-12-23T21:15:00Z</updated>
    <published>2010-12-23T21:15:00Z</published>
    <category scheme="http://blog.codekills.net" term="Python" />
    <summary type="html">Why I like `nose`</summary>
    <content type="html" xml:base="http://blog.codekills.net/2010/12/23/why-i-like--nose-">

&lt;p&gt;Four reasons &lt;a href=&#34;http://somethingaboutorange.com/mrl/projects/nose/1.0.0/&#34;&gt;nose&lt;/a&gt; rocks:&lt;/p&gt;
&lt;h2&gt;Test discovery&lt;/h2&gt;
&lt;p&gt;After I&#39;ve written my &lt;code&gt;test_*.py&lt;/code&gt; files, I just run &lt;code&gt;nosetests&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ ls
foo.py bar.py test_foo.py test_bar.py
$ nosetests
........
--------
Ran 9 tests in 0.03s
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Nose is more Pythonic&lt;/h2&gt;
&lt;p&gt;The xUnit-esque frameworks fit well in languages like Java where methods can be called without the &lt;code&gt;this&lt;/code&gt; prefix, but they don&#39;t work so well in Python which forces the explicit &lt;code&gt;self&lt;/code&gt;. xUnit-esque frameworks also assume that tests will always exist inside a class and &lt;code&gt;camelCase&lt;/code&gt; is pretty. Compare:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;from unittest import TestCase
class UnittestTestCase(TestCase):
    def testStuff(self):
        self.assertEqual(foo, bar)

from nose.tools import assert_equal
def test_stuff():
    assert_equal(foo, bar)
class NoseTestCase(object):
    def test_stuff(self):
        assert_equal(foo, bar)
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Test generators&lt;/h2&gt;
&lt;p&gt;The name basically says it all:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ cat test_add.py
add_tests = [ (1, 2, 3), (1, 3, 4), (0, 0, 0) ]
def test_add():
    def test_add_helper(a, b, expected):
        assert_equal(a + b, expected)
    for test in add_tests:
        yield (test_add_helper, test)
$ nosetests
...
------
Ran 3 tests in 0.03s
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Logging/stdout capturing&lt;/h2&gt;
&lt;p&gt;Nose automatically captures all text sent to stdout during tests, discarding it if the test passes or printing it if the test fails. For example:
    $ cat test_stuff.py
    from nose.tools import assert_equals&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;def passing_test():
    print &#34;everything is happy!&#34;
    assert_equals(1+2, 3)

def failing_test():
    print &#34;oh no...&#34;
    assert_equals(1+2, 0)
$ nosetests
.F
======================================================================
FAIL: test_stuff.failing_test
----------------------------------------------------------------------
Traceback (most recent call last):
  File &#34;/Library/Python/2.6/site-packages/nose/case.py&#34;, line 186, in runTest
    self.test(*self.arg)
  File &#34;/private/tmp/test_stuff.py&#34;, line 9, in failing_test
    assert_equals(1+2, 0)
AssertionError: 3 != 0
-------------------- &amp;gt;&amp;gt; begin captured stdout &amp;lt;&amp;lt; ---------------------
oh no...

--------------------- &amp;gt;&amp;gt; end captured stdout &amp;lt;&amp;lt; ----------------------

----------------------------------------------------------------------
Ran 2 tests in 0.001s

FAILED (failures=1)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; &lt;a href=&#34;http://twitter.com/#!/voidspace&#34;&gt;Michael Foord&lt;/a&gt; has been doing some crazy stuff with &lt;a href=&#34;http://pypi.python.org/pypi/unittest2&#34;&gt;unittest2&lt;/a&gt;, so it&#39;s possible that it may be as awesome as nose.&lt;/p&gt;
</content>
  </entry>
</feed>

