<?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.countergram.com/~d/styles/itemcontent.css"?><rss version="2.0"><channel><title>Countergram</title><link>http://countergram.com/</link><description>The Countergram feed</description><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.countergram.com/countergram" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="countergram" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item><title>Group a list into chunks in Python</title><link>http://countergram.com/python-group-iterator-list-function</link><description>&lt;div class="document"&gt;
&lt;p&gt;More than once I've had to take a list of items and group them. For instance, maybe I have a flat list of key-value pairs in alternation, like this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
[key1, value1, key2, value2, key3, value3]
&lt;/pre&gt;
&lt;p&gt;It could have been that they were easier to enter that way (fewer parentheses or brackets), or maybe that's just how they come from an outside source.&lt;/p&gt;
&lt;p&gt;Nevertheless, to actually use them, it would be better to have them in tuples:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
[(key1, value1), (key2, value2), (key3, value3)]
&lt;/pre&gt;
&lt;p&gt;That way, I could make them into a dictionary, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;urlencode&lt;/span&gt;&lt;/tt&gt; them, etc.&lt;/p&gt;
&lt;p&gt;I looked through Python's entire &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;itertools&lt;/span&gt;&lt;/tt&gt; module for a pre-existing solution, but didn't find one. &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;itertools&lt;/span&gt;&lt;/tt&gt; does have the reverse operation, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;chain&lt;/span&gt;&lt;/tt&gt;, but not this one.&lt;/p&gt;
&lt;p&gt;So, to save you the trouble, here's the code:&lt;/p&gt;
&lt;div class="code-header"&gt;group_iter.py (license: public domain)&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="python"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;group_iter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;iterator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;strict&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sd"&gt;&amp;quot;&amp;quot;&amp;quot; Transforms a sequence of values into a sequence of n-tuples.&lt;/span&gt;
&lt;span class="sd"&gt;    e.g. [1, 2, 3, 4, ...] =&amp;gt; [(1, 2), (3, 4), ...] (when n == 2)&lt;/span&gt;
&lt;span class="sd"&gt;    If strict, then it will raise ValueError if there is a group of fewer&lt;/span&gt;
&lt;span class="sd"&gt;    than n items at the end of the sequence. &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
    &lt;span class="n"&gt;accumulator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;iterator&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;accumulator&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="c"&gt;# tested as fast as separate counter&lt;/span&gt;
            &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="nb"&gt;tuple&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;accumulator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="c"&gt;# tested faster than accumulator[:] = []&lt;/span&gt;
            &lt;span class="c"&gt;# and tested as fast as re-using one list object&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;strict&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mf"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="ne"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Leftover values&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
</description><guid>http://countergram.com/python-group-iterator-list-function</guid><pubDate>Thu, 06 May 2010 00:00:00 EST</pubDate></item><item><title>YouTube video embedding with XSLT</title><link>http://countergram.com/youtube-embed-xslt</link><description>&lt;div class="document"&gt;
&lt;p&gt;A while ago, I published an article on how to use Python's docutils to expand some markup in reStructuredText into the HTML for a YouTube embedded video. Just for fun,  here's an XSLT stylesheet that does something similar. It hasn't had a chance to be used in production, but if you have an XSLT-based web application, it might come in useful.&lt;/p&gt;
&lt;div class="code-header"&gt;youtube.inc.xslt&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="xslt"&gt;&lt;span class="cp"&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;&amp;lt;xsl:stylesheet&lt;/span&gt; &lt;span class="na"&gt;version=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;1.0&amp;quot;&lt;/span&gt;
                &lt;span class="na"&gt;xmlns:xsl=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&lt;/span&gt;
                &lt;span class="na"&gt;xmlns=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;http://www.w3.org/1999/xhtml&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

    &lt;span class="c"&gt;&amp;lt;!-- Replaces elements of thei form &amp;lt;youtube id=&amp;quot;asdfasdf&amp;quot; /&amp;gt; with the&lt;/span&gt;
&lt;span class="c"&gt;    correct XHTML object element to embed a YouTube video --&amp;gt;&lt;/span&gt;

    &lt;span class="c"&gt;&amp;lt;!-- If desired, one can also place arbitrary elements inside the youtube&lt;/span&gt;
&lt;span class="c"&gt;    element that will be converted into parameters of the object element.&lt;/span&gt;
&lt;span class="c"&gt;    E.g. the element &amp;lt;fullscreen&amp;gt;true&amp;lt;/fullscreen&amp;gt; will be converted to&lt;/span&gt;
&lt;span class="c"&gt;    &amp;lt;param name=&amp;quot;fullscreen&amp;quot; value=&amp;quot;true&amp;quot; /&amp;gt; --&amp;gt;&lt;/span&gt;

    &lt;span class="k"&gt;&amp;lt;xsl:strip-space&lt;/span&gt; &lt;span class="na"&gt;elements=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;youtube&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

    &lt;span class="k"&gt;&amp;lt;xsl:template&lt;/span&gt; &lt;span class="na"&gt;match=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;youtube&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="k"&gt;&amp;lt;xsl:element&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;object&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="k"&gt;&amp;lt;xsl:attribute&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;type&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;application/x-shockwave-flash&lt;span class="k"&gt;&amp;lt;/xsl:attribute&amp;gt;&lt;/span&gt;
            &lt;span class="k"&gt;&amp;lt;xsl:attribute&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;width&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;425&lt;span class="k"&gt;&amp;lt;/xsl:attribute&amp;gt;&lt;/span&gt;
            &lt;span class="k"&gt;&amp;lt;xsl:attribute&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;height&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;344&lt;span class="k"&gt;&amp;lt;/xsl:attribute&amp;gt;&lt;/span&gt;
            &lt;span class="k"&gt;&amp;lt;xsl:attribute&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;class&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;youtube-embed&lt;span class="k"&gt;&amp;lt;/xsl:attribute&amp;gt;&lt;/span&gt;
            &lt;span class="k"&gt;&amp;lt;xsl:attribute&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;data&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                http://www.youtube.com/v/&lt;span class="k"&gt;&amp;lt;xsl:value-of&lt;/span&gt; &lt;span class="na"&gt;select=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;@id&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="k"&gt;&amp;lt;/xsl:attribute&amp;gt;&lt;/span&gt;
            &lt;span class="k"&gt;&amp;lt;xsl:element&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;param&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="k"&gt;&amp;lt;xsl:attribute&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;name&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;movie&lt;span class="k"&gt;&amp;lt;/xsl:attribute&amp;gt;&lt;/span&gt;
                &lt;span class="k"&gt;&amp;lt;xsl:attribute&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;value&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                    http://www.youtube.com/v/&lt;span class="k"&gt;&amp;lt;xsl:value-of&lt;/span&gt; &lt;span class="na"&gt;select=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;@id&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
                &lt;span class="k"&gt;&amp;lt;/xsl:attribute&amp;gt;&lt;/span&gt;
            &lt;span class="k"&gt;&amp;lt;/xsl:element&amp;gt;&lt;/span&gt;
            &lt;span class="k"&gt;&amp;lt;xsl:element&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;param&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="k"&gt;&amp;lt;xsl:attribute&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;name&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;wmode&lt;span class="k"&gt;&amp;lt;/xsl:attribute&amp;gt;&lt;/span&gt;
                &lt;span class="k"&gt;&amp;lt;xsl:attribute&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;value&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;transparent&lt;span class="k"&gt;&amp;lt;/xsl:attribute&amp;gt;&lt;/span&gt;
            &lt;span class="k"&gt;&amp;lt;/xsl:element&amp;gt;&lt;/span&gt;
            &lt;span class="k"&gt;&amp;lt;xsl:apply-templates&lt;/span&gt; &lt;span class="na"&gt;mode=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;youtube-params&amp;quot;&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="k"&gt;&amp;lt;/xsl:element&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;&amp;lt;/xsl:template&amp;gt;&lt;/span&gt;

    &lt;span class="k"&gt;&amp;lt;xsl:template&lt;/span&gt; &lt;span class="na"&gt;match=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;youtube/*&amp;quot;&lt;/span&gt; &lt;span class="na"&gt;mode=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;youtube-params&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="k"&gt;&amp;lt;xsl:element&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;param&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="k"&gt;&amp;lt;xsl:attribute&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;name&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;span class="k"&gt;&amp;lt;xsl:value-of&lt;/span&gt; &lt;span class="na"&gt;select=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;local-name()&amp;quot;&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="k"&gt;&amp;lt;/xsl:attribute&amp;gt;&lt;/span&gt;
            &lt;span class="k"&gt;&amp;lt;xsl:attribute&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;value&amp;quot;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;span class="k"&gt;&amp;lt;xsl:value-of&lt;/span&gt; &lt;span class="na"&gt;select=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;text()&amp;quot;&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="k"&gt;&amp;lt;/xsl:attribute&amp;gt;&lt;/span&gt;
        &lt;span class="k"&gt;&amp;lt;/xsl:element&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;&amp;lt;/xsl:template&amp;gt;&lt;/span&gt;

&lt;span class="k"&gt;&amp;lt;/xsl:stylesheet&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;As the comment notes, you need to run this on an XML document that contains elements like &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;&amp;lt;youtube&lt;/span&gt; &lt;span class="pre"&gt;id=&amp;quot;asdfasdf&amp;quot;&lt;/span&gt; &lt;span class="pre"&gt;/&amp;gt;&lt;/span&gt;&lt;/tt&gt;. It's not currently configurable as to the width, height, and other parameters (except by changing the XSLT) but could be made so.&lt;/p&gt;
&lt;/div&gt;
</description><guid>http://countergram.com/youtube-embed-xslt</guid><pubDate>Mon, 03 May 2010 00:00:00 EST</pubDate></item><item><title>Pylons deployment with Lighttpd and Flup</title><link>http://countergram.com/pylons-lighttpd-flup</link><description>&lt;div class="document"&gt;
&lt;p&gt;What's fast, easy to set up, and good for running web applications?&lt;/p&gt;
&lt;p&gt;Wait, did that sound too much like a joke? Anyway, I'm talking about the combination of &lt;a class="reference external" href="http://www.lighttpd.net"&gt;Lighttpd&lt;/a&gt; (a web server and alternative to Apache) and &lt;a class="reference external" href="http://www.pylonshq.com"&gt;Pylons&lt;/a&gt; (a Python web framework).&lt;/p&gt;
&lt;p&gt;Lighttpd (as well as the similar &lt;a class="reference external" href="http://nginx.net/"&gt;nginx&lt;/a&gt;) has been getting an increasing amount of attention in the past few years, especially for use with web applications in combination with dynamic languages such as Python and Ruby. It has a relatively small memory footprint and, due to its asynchronous network layer, can easily handle both dynamic web applications and lots of static files at the same time.&lt;/p&gt;
&lt;p&gt;The &amp;quot;catch&amp;quot; is that it never runs apps in embedded interpreters, as Apache modules such as &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;mod_php&lt;/span&gt;&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;mod_perl&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;mod_wsgi&lt;/span&gt;&lt;/tt&gt; do. Instead, it farms requests out to apps running in separate processes, using protocols such as FastCGI or SCGI, or acting as an HTTP proxy.&lt;/p&gt;
&lt;p&gt;This article will show you a basic setup for Pylons and Lighttpd, using the FastCGI protocol. I'm going to assume you already have a basic Pylons app set up and know how to run it; if not, hop over to the &lt;a class="reference external" href="http://pylonshq.com/docs/en/0.9.7/gettingstarted/"&gt;Pylons Getting Started documentation&lt;/a&gt;. Lighttpd installation instructions can be found on the &lt;a class="reference external" href="http://redmine.lighttpd.net/wiki/lighttpd"&gt;Lighttpd documentation page&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Let's go.&lt;/p&gt;
&lt;div class="section" id="pylons-configuration"&gt;
&lt;h2&gt;Pylons configuration&lt;/h2&gt;
&lt;p&gt;I've started off with the FastCGI protocol here for reasons discussed below. SCGI, for those who prefer, is a drop-in replacement (you can actually just change all instances of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;fcgi&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;fastcgi&lt;/span&gt;&lt;/tt&gt; to &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;scgi&lt;/span&gt;&lt;/tt&gt; in the Pylons and Lighttpd configuration examples.) HTTP proxying is only slightly different.&lt;/p&gt;
&lt;p&gt;You will need the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;flup&lt;/span&gt;&lt;/tt&gt; package installed. Use &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;easy_install&lt;/span&gt; &lt;span class="pre"&gt;flup&lt;/span&gt;&lt;/tt&gt; or &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;pip&lt;/span&gt; &lt;span class="pre"&gt;install&lt;/span&gt; &lt;span class="pre"&gt;flup&lt;/span&gt;&lt;/tt&gt;, or visit &lt;a class="reference external" href="http://trac.saddi.com/flup"&gt;the Flup home page&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Only a small change needs to be made to the INI file for your Pylons application (e.g.: &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;development.ini&lt;/span&gt;&lt;/tt&gt;) to use Flup instead of the default Paste HTTP server.&lt;/p&gt;
&lt;div class="code-header"&gt;development.ini (partial)&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="ini"&gt;&lt;span class="k"&gt;[server:main]&lt;/span&gt;
&lt;span class="na"&gt;use&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;Flup#fcgi&lt;/span&gt;
&lt;span class="na"&gt;host&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;localhost&lt;/span&gt;
&lt;span class="na"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;5000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You can then run the app with &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;paster&lt;/span&gt; &lt;span class="pre"&gt;serve&lt;/span&gt; &lt;span class="pre"&gt;--reload&lt;/span&gt; &lt;span class="pre"&gt;development.ini&lt;/span&gt;&lt;/tt&gt;, as usual.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="lighttpd-configuration"&gt;
&lt;h2&gt;Lighttpd configuration&lt;/h2&gt;
&lt;p&gt;Below is a minimal &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;lighttpd.conf&lt;/span&gt;&lt;/tt&gt; containing configuration that you can adapt to your needs. It's missing a lot of what you would actually want, such as logging and MIME types, so cut and paste liberally.&lt;/p&gt;
&lt;div class="code-header"&gt;lighttpd.conf&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="text"&gt;server.modules = (
    &amp;quot;mod_fastcgi&amp;quot;
)

server.port = 80
server.document-root = &amp;quot;/dev/null&amp;quot;

$HTTP[&amp;quot;host&amp;quot;] =~ &amp;quot;^example.com|localhost$&amp;quot; {
    server.document-root = &amp;quot;/path/to/files/&amp;quot;
    $HTTP[&amp;quot;url&amp;quot;] !~ &amp;quot;^/static/&amp;quot; {
        fastcgi.server = (&amp;quot;/&amp;quot; =&amp;gt; ((
            &amp;quot;host&amp;quot; =&amp;gt; &amp;quot;127.0.0.1&amp;quot;,
            &amp;quot;port&amp;quot; =&amp;gt; 5000,
            &amp;quot;check-local&amp;quot; =&amp;gt; &amp;quot;disable&amp;quot;,
            &amp;quot;disable-time&amp;quot; =&amp;gt; 1,
            &amp;quot;fix-root-scriptname&amp;quot; =&amp;gt; &amp;quot;enable&amp;quot;
        )))
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This configuration uses Lighttpd's regular-expression selectors to create a host that can be accessed either as &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;example.com&lt;/span&gt;&lt;/tt&gt; or as &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;localhost&lt;/span&gt;&lt;/tt&gt; on port 80. This is suitable if you are sharing one configuration file between your local development machine and the example.com server, but unnecessary if you are using separate configuration files, or remapping example.com to your local machine for development purposes.&lt;/p&gt;
&lt;p&gt;The positioning of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;server.document-root&lt;/span&gt;&lt;/tt&gt; and the second regular-expression selector means that URLs that start with &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;/static/&lt;/span&gt;&lt;/tt&gt; will be served by Lighttpd from &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;/path/to/files/static/&lt;/span&gt;&lt;/tt&gt;, while everything else will be served by the application.&lt;/p&gt;
&lt;p&gt;Disabling &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;check-local&lt;/span&gt;&lt;/tt&gt; means that for those URLs handled by the application, Lighttpd will not attempt to look at the filesystem at all. &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;disable-time&lt;/span&gt;&lt;/tt&gt;, on the other hand, is the amount of time Lighttpd will wait between tries if it fails to connect to the application server. Setting this to a small value makes restarts or redeployments of your app go faster, if you have not implemented a seamless restarting process for your app.&lt;/p&gt;
&lt;p&gt;The &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;fix-root-scriptname&lt;/span&gt;&lt;/tt&gt; argument is necessary for &lt;em&gt;any&lt;/em&gt; WSGI app, Pylons or otherwise, that is mounted on &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;/&lt;/span&gt;&lt;/tt&gt;. Otherwise &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;SCRIPT_NAME&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;PATH_INFO&lt;/span&gt;&lt;/tt&gt; will &lt;em&gt;not&lt;/em&gt; be set properly.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="why-fastcgi"&gt;
&lt;h2&gt;Why FastCGI?&lt;/h2&gt;
&lt;p&gt;Some perceive FastCGI as outdated, but it works well, and given that Flup and Lighttpd handle the protocol for you there's no real need to worry about it. Still, there are options.&lt;/p&gt;
&lt;p&gt;If you would like to use SCGI instead of FastCGI, simply load &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;mod_scgi&lt;/span&gt;&lt;/tt&gt; in &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;server.modules&lt;/span&gt;&lt;/tt&gt; and change &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;fastcgi.server&lt;/span&gt;&lt;/tt&gt; to &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;scgi.server&lt;/span&gt;&lt;/tt&gt; in the config above. Lighttpd 1.4.15 and previous have a segfaulting bug in &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;mod_scgi&lt;/span&gt;&lt;/tt&gt;, and it should not be used. Since SCGI and FastCGI are otherwise drop-in replacements and their performance seems equivalent, I simply used FastCGI.&lt;/p&gt;
&lt;p&gt;You could also use Paste's HTTP server (the same one used for development) behind &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;mod_proxy&lt;/span&gt;&lt;/tt&gt;. This looks like the above, but load &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;mod_proxy&lt;/span&gt;&lt;/tt&gt; in &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;server.modules&lt;/span&gt;&lt;/tt&gt;, and put following in place of the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;fastcgi.server&lt;/span&gt;&lt;/tt&gt; block:&lt;/p&gt;
&lt;pre&gt;&lt;code class="highlight" lang="text"&gt;proxy.server = (&amp;quot;&amp;quot; =&amp;gt; ((
    &amp;quot;host&amp;quot; =&amp;gt; &amp;quot;127.0.0.1&amp;quot;,
    &amp;quot;port&amp;quot; =&amp;gt; 5000
)))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Unfortunately, in my tests Paste's HTTP server (based on the standard library's &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;BaseHTTPServer&lt;/span&gt;&lt;/tt&gt;) did not hold up under load, whereas Flup did spectacularly. You could also try another Python HTTP server that speaks WSGI.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="load-balancing"&gt;
&lt;h2&gt;Load balancing&lt;/h2&gt;
&lt;p&gt;All three options (FastCGI, SCGI, and proxying) in Lighttpd come with load-balancing capability. See the &amp;quot;extra&amp;quot; set of parentheses around the host and port definition in the configuration examples above? Simply add multiple such blocks, like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class="highlight" lang="text"&gt;proxy.server = (&amp;quot;&amp;quot; =&amp;gt; (
    (&amp;quot;host&amp;quot; =&amp;gt; &amp;quot;10.0.0.10&amp;quot;,
    &amp;quot;port&amp;quot; =&amp;gt; 5000),
    (&amp;quot;host&amp;quot; =&amp;gt; &amp;quot;10.0.0.11&amp;quot;,
    &amp;quot;port&amp;quot; =&amp;gt; 5000)
))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If using &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;mod_proxy&lt;/span&gt;&lt;/tt&gt;, you can then alter the load-balancing algorithm using the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;proxy.balance&lt;/span&gt;&lt;/tt&gt; parameter (see the &lt;a class="reference external" href="http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:ModProxy"&gt;mod_proxy docs&lt;/a&gt;).&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="what-about-mod-wsgi"&gt;
&lt;h2&gt;What about mod_wsgi?&lt;/h2&gt;
&lt;p&gt;Apache (not Lighttpd) has &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;mod_wsgi&lt;/span&gt;&lt;/tt&gt;, which runs one or more embedded Python processes in each Apache process, passing requests directly to WSGI applications. This is a solid way to deploy Pylons apps.&lt;/p&gt;
&lt;p&gt;See the &lt;a class="reference external" href="http://code.google.com/p/modwsgi/wiki/IntegrationWithPylons"&gt;Integration With Pylons&lt;/a&gt; document on the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;mod_wsgi&lt;/span&gt;&lt;/tt&gt; site for information on how to use &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;paste.deploy.loadapp&lt;/span&gt;&lt;/tt&gt; to make Pylons compatible with &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;mod_wsgi&lt;/span&gt;&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;An advantage of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;mod_wsgi&lt;/span&gt;&lt;/tt&gt; is that you don't have to keep track of as many processes. However, if you &lt;a class="reference external" href="pylons-supervisor"&gt;use Supervisor to manage your Pylons apps and Lighttpd&lt;/a&gt;, it's no trouble at all.&lt;/p&gt;
&lt;p&gt;A disadvantage of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;mod_wsgi&lt;/span&gt;&lt;/tt&gt; is that you don't easily have the ability to split your HTTP server and app servers across multiple machines, which is easy with Lighttpd and a separate Python process. You can do it, of course, by running Apache on each machine and using &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;mod_proxy&lt;/span&gt;&lt;/tt&gt; or a load balancer.&lt;/p&gt;
&lt;p&gt;Also, if you are building your own Apache and Python from source, you need to be very careful about library versions. If Apache and Python are built against different versions of the Expat library, for instance, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;mod_wsgi&lt;/span&gt;&lt;/tt&gt; will segfault. This is not a concern when Python is running as a separate process, rather than embedded.&lt;/p&gt;
&lt;p&gt;(See Graham Dumpleton's comment, below, for some updates -- &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;mod_wsgi&lt;/span&gt;&lt;/tt&gt; can run daemon processes, although they're managed by &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;mod_wsgi&lt;/span&gt;&lt;/tt&gt;, and apparently the Expat library hasn't been a problem for a while.)&lt;/p&gt;
&lt;p&gt;This isn't too farfetched -- if you want to use a more recent version of Python than is available in your package manager (some stable Linux distributions seem to habitually lag behind Python point releases), or if you want to use the exact same version on different platforms, you'll probably build your own.&lt;/p&gt;
&lt;p&gt;One final note on &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;mod_wsgi&lt;/span&gt;&lt;/tt&gt;: writing to &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;STDOUT&lt;/span&gt;&lt;/tt&gt; (e.g. using the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;print&lt;/span&gt;&lt;/tt&gt; statement) will cause serious errors, so make sure to excise all &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;print&lt;/span&gt;&lt;/tt&gt; statements and use logging instead.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="addendum"&gt;
&lt;h2&gt;Addendum&lt;/h2&gt;
&lt;p&gt;Lighttpd is my personal favorite way to deploy Python web applications. (Some people prefer &lt;a class="reference external" href="http://nginx.net/"&gt;nginx&lt;/a&gt;, also a solid option.)&lt;/p&gt;
&lt;p&gt;Not only does separating the HTTP process from the Python process allow for easy and flexible deployment, but Lighttpd has an amazingly useful configuration system. The basic syntax is easy to work with, and for advanced uses you can generate configuration files dynamically (see: &lt;a class="reference external" href="http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:Configuration"&gt;include_shell&lt;/a&gt;) or run Lua scripts in the server process to do advanced URL rewriting/redirecting, maintenance notices, application firewalls, etc. (see: &lt;a class="reference external" href="http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:ModMagnet"&gt;mod_magnet&lt;/a&gt;).&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
</description><guid>http://countergram.com/pylons-lighttpd-flup</guid><pubDate>Thu, 22 Apr 2010 00:00:00 EST</pubDate></item><item><title>Show your IP address using a Lua/Lighttpd script</title><link>http://countergram.com/ip-reporting-script-lua-lighttpd</link><description>&lt;div class="document"&gt;
&lt;p&gt;Sometimes it's useful to be able to determine your external WAN IP address from a script. There are web sites that will tell you your IP, but they're formatted for human readership and might go away or change their HTML format at any time. You could access the HTML administration interface of your router, but that's subject to changes in router brand or login information.&lt;/p&gt;
&lt;p&gt;Another way is to set up a script on your own web server that reports the IP of the client accessing it.&lt;/p&gt;
&lt;p&gt;There are tons of ways to do this... a CGI script, a web application in your language of choice, a PHP file even if it's not your main development environment. But here's one you might not have thought of. If you're using Lighttpd, you can use its Lua scripting support to do this extremely simply, without much setup and without involving any of your (possibly more complex) web development environments.&lt;/p&gt;
&lt;p&gt;I don't even have CGI or PHP enabled on my web server, and my primary web-application development style based on long-running processes and frameworks is a bit &amp;quot;heavy&amp;quot; for a single-purpose script like this. And I didn't want to add it as a view to an existing app, as there would be no meaningful relationship between the two.&lt;/p&gt;
&lt;p&gt;So, Lua it was. Here's how the script looks:&lt;/p&gt;
&lt;div class="code-header"&gt;remote_ip.lua&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="lua"&gt;&lt;span class="n"&gt;lighty&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;lighty&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="s"&gt;request.remote-ip&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And here's the Lighttpd configuration required (adapt to your domain/URL structure):&lt;/p&gt;
&lt;div class="code-header"&gt;lighttpd.conf (excerpt)&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="text"&gt;server.modules = (
    &amp;quot;mod_lua&amp;quot;
)

# ...

$HTTP[&amp;quot;host&amp;quot;] =~ &amp;quot;^ip.example.com$&amp;quot; {
    magnet.attract-physical-path-to = ( &amp;quot;/path/to/remote_ip.lua&amp;quot; )
}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Simple, eh?&lt;/p&gt;
&lt;/div&gt;
</description><guid>http://countergram.com/ip-reporting-script-lua-lighttpd</guid><pubDate>Mon, 19 Apr 2010 00:00:00 EST</pubDate></item><item><title>Pylons multiple-domain (virtual hosting) configuration</title><link>http://countergram.com/pylons-virtual-host-multiple-domain</link><description>&lt;div class="document"&gt;
&lt;p&gt;The conventional approach to Python web-application frameworks such as Pylons is to run multiple Python processes when multiple apps need to be deployed on the same physical server. This would also often apply to serving up more than one domain.&lt;/p&gt;
&lt;p&gt;This works fine, especially if you use a process manager such as &lt;a class="reference external" href="/pylons-supervisor/"&gt;Supervisor to manage the web-app processes&lt;/a&gt;, but in some cases, you may want to pack more than one domain into the same process space, effectively moving your virtual hosts into the Python app configuration. This article will show you how.&lt;/p&gt;
&lt;div class="section" id="reasons-to-run-pylons-apps-in-the-same-process"&gt;
&lt;h2&gt;Reasons to run Pylons apps in the same process&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;Save lots of memory: In memory-constrained environments such as virtual private servers (VPS), using one process for multiple apps may be a requirement.&lt;/li&gt;
&lt;li&gt;Share: In some cases, you may want multiple apps to be able to share database connections or other resources.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="reasons-not-to"&gt;
&lt;h2&gt;Reasons not to&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;Global state may conflict.&lt;/li&gt;
&lt;li&gt;Specifically, SQLAlchemy models (which have some global state) may conflict.&lt;/li&gt;
&lt;li&gt;A syntax or import error in one app, or another error that triggers on initialization rather than on a page view, will take down the other apps.&lt;/li&gt;
&lt;li&gt;The apps will use the same server thread pool, so fewer threads will be available per site on average.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="using-paste-urlmap-for-multiple-domains"&gt;
&lt;h2&gt;Using Paste#urlmap for multiple domains&lt;/h2&gt;
&lt;p&gt;You've probably seen &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;Paste#urlmap&lt;/span&gt;&lt;/tt&gt; used to route different paths on the same domain to different applications. It's the main &lt;a class="reference external" href="http://pythonpaste.org/deploy/#composite-applications"&gt;example of composite applications in the Paste Deploy documentation&lt;/a&gt;, where it looks something like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class="highlight" lang="ini"&gt;&lt;span class="k"&gt;[composite:main]&lt;/span&gt;
&lt;span class="na"&gt;use&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;egg:Paste#urlmap&lt;/span&gt;
&lt;span class="na"&gt;/&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;mainapp&lt;/span&gt;
&lt;span class="na"&gt;/files&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;staticapp&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;But an apparently as-yet undocumented syntax also allows you to run apps on different domains:&lt;/p&gt;
&lt;pre&gt;&lt;code class="highlight" lang="ini"&gt;&lt;span class="k"&gt;[composite:main]&lt;/span&gt;
&lt;span class="na"&gt;use&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;egg:Paste#urlmap&lt;/span&gt;
&lt;span class="na"&gt;domain example.com /&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;mainapp&lt;/span&gt;
&lt;span class="na"&gt;domain files.example.com /&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;staticapp&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The syntax is &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;domain&lt;/span&gt; &lt;span class="pre"&gt;&amp;lt;hostname&amp;gt;&lt;/span&gt; &lt;span class="pre"&gt;&amp;lt;path&amp;gt;&lt;/span&gt; &lt;span class="pre"&gt;=&lt;/span&gt; &lt;span class="pre"&gt;&amp;lt;appname&amp;gt;&lt;/span&gt;&lt;/tt&gt;; the inclusion of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;&amp;lt;path&amp;gt;&lt;/span&gt;&lt;/tt&gt; means that you can also set up apps on several paths under the same domain.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="setting-cache-dir"&gt;
&lt;h2&gt;Setting &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cache_dir&lt;/span&gt;&lt;/tt&gt;&lt;/h2&gt;
&lt;p&gt;Whenever you have multiple Pylons applications running from config files that are in the same directory, whether the same file or multiple files, you must be careful to set the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cache_dir&lt;/span&gt;&lt;/tt&gt; for each application separately.&lt;/p&gt;
&lt;p&gt;The default created by &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;paster&lt;/span&gt;&lt;/tt&gt; looks like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class="highlight" lang="ini"&gt;&lt;span class="na"&gt;cache_dir&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;%(here)s/data&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;%(here)s&lt;/span&gt;&lt;/tt&gt; will be filled in with the directory where the configuration file is located. If two applications are configured in the same file, or in files in the same directory, and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cache_dir&lt;/span&gt;&lt;/tt&gt; is left as-is, their cache directories will overlap. They will overwrite or, worse, share each other's cached data. For example, Mako stores compiled templates in the cache directory, so one application will end up using the other's templates.&lt;/p&gt;
&lt;p&gt;To avoid this, always set &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cache_dir&lt;/span&gt;&lt;/tt&gt; to different values for applications whose configuration is in the same directory, using either relative or absolute paths:&lt;/p&gt;
&lt;pre&gt;&lt;code class="highlight" lang="ini"&gt;&lt;span class="c"&gt;#relative&lt;/span&gt;
&lt;span class="na"&gt;cache_dir&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;%(here)s/appname-data&lt;/span&gt;

&lt;span class="c"&gt;#absolute&lt;/span&gt;
&lt;span class="na"&gt;cache_dir&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;/tmp/pylons-cache/appname&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class="section" id="serving-an-app-on-more-than-one-path"&gt;
&lt;h2&gt;Serving an app on more than one path&lt;/h2&gt;
&lt;p&gt;When you set up the same application on multiple paths or domains using &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;urlmap&lt;/span&gt;&lt;/tt&gt;, it will be initialized multiple times. For example:&lt;/p&gt;
&lt;pre&gt;&lt;code class="highlight" lang="ini"&gt;&lt;span class="k"&gt;[composite:main]&lt;/span&gt;
&lt;span class="na"&gt;use&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;egg:Paste#urlmap&lt;/span&gt;
&lt;span class="na"&gt;domain foo.example.com /&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;app1&lt;/span&gt;
&lt;span class="na"&gt;domain example.com /foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;app1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This will cause &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;app1&lt;/span&gt;&lt;/tt&gt; to be loaded and initialized twice, which will cause problems if, for example, you create any models inside the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;init_model()&lt;/span&gt;&lt;/tt&gt; function. (You'll get an exception because they're being created twice in the same Python process).&lt;/p&gt;
&lt;p&gt;It's probably best, in this case, to place the app behind a web server such as Apache or Lighttpd and configure the web server to direct multiple hosts or URLs to the same application.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="global-data"&gt;
&lt;h2&gt;Global data&lt;/h2&gt;
&lt;p&gt;As I mentioned earlier, running multiple Pylons apps in the same process will cause potential conflicts in module-global data.&lt;/p&gt;
&lt;p&gt;The &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;init_model()&lt;/span&gt;&lt;/tt&gt; conflict mentioned in the previous section is one; SQLAlchemy models are stored globally, and trying to declare the same models more than once can cause errors. In order to facilitate this, you will need a separate piece of code for model creation that checks whether the shared models are already in existence before creating them.&lt;/p&gt;
&lt;p&gt;Some builtin Python modules also use global state, including &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;locale&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;timezone&lt;/span&gt;&lt;/tt&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="alternative-to-this-approach"&gt;
&lt;h2&gt;Alternative to this approach&lt;/h2&gt;
&lt;p&gt;If your use-case involves a number of very similar sites or apps, with small configuration differences, you could use the approach described above. Or, you could use just one app and handle multiple domains inside the app itself.&lt;/p&gt;
&lt;p&gt;For example, if you have a number of small content sites with identical structure, you could use just one app, with different template directories for each site, and a database column that indicates which site each content item belongs to.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
</description><guid>http://countergram.com/pylons-virtual-host-multiple-domain</guid><pubDate>Sat, 17 Oct 2009 00:00:00 EST</pubDate></item><item><title>Pylons apps on Google App Engine</title><link>http://countergram.com/pylons-google-app-engine</link><description>&lt;div class="document"&gt;
&lt;p&gt;This will show you how to get a Pylons web app running on Google App Engine (GAE) in about 15 minutes, assuming you already have your GAE account and a familiarity with Pylons and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;virtualenv&lt;/span&gt;&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;(Otherwise, start out with the &lt;a class="reference external" href="http://code.google.com/appengine/docs/whatisgoogleappengine.html"&gt;Google App Engine&lt;/a&gt; docs and the &lt;a class="reference external" href="http://pylonshq.com"&gt;Pylons&lt;/a&gt; docs).&lt;/p&gt;
&lt;p&gt;The approach described here is focused on using as &amp;quot;normal&amp;quot; a process as possible for creating a Pylons app, with minimal modifications for GAE. It uses a stock &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;virtualenv&lt;/span&gt;&lt;/tt&gt;, and a stock &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;paster&lt;/span&gt; &lt;span class="pre"&gt;create&lt;/span&gt;&lt;/tt&gt; template, instead of an entirely different setup just for GAE. Several manual edits are required (but fewer now!)&lt;/p&gt;
&lt;p&gt;This means that you could use an existing app or an existing virtualenv, with a few modifications. This is up to you, as I don't know what your exact setup looks like, but most of the steps below should apply.&lt;/p&gt;
&lt;div class="section" id="getting-started"&gt;
&lt;h2&gt;Getting started&lt;/h2&gt;
&lt;p&gt;To begin, run one of the following scripts in an appropriate directory, replacing $MYPROJ and $MYAPP with whatever values you like (they can be the same, though if they are the directory structure can end up slightly confusing. &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;paster&lt;/span&gt; &lt;span class="pre"&gt;create&lt;/span&gt;&lt;/tt&gt; will make two directories named $MYAPP as it is.)&lt;/p&gt;
&lt;p&gt;I've provided instructions for both &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;pip&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;easy_install&lt;/span&gt;&lt;/tt&gt;, so you can continue to use your preferred package manager.&lt;/p&gt;
&lt;p&gt;You could also browse &lt;a class="reference external" href="http://files.countergram.com/pylons-gae/app.yaml"&gt;app.yaml&lt;/a&gt; and &lt;a class="reference external" href="http://files.countergram.com/pylons-gae/app.py"&gt;app.py&lt;/a&gt; to get an idea of what's going on.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="getting-started-pip-version"&gt;
&lt;h2&gt;Getting started: &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;pip&lt;/span&gt;&lt;/tt&gt; version&lt;/h2&gt;
&lt;div class="code-header"&gt;console&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="text"&gt;virtualenv --python=python2.5 --no-site-packages --unzip-setuptools $MYPROJ
cd $MYPROJ
pip install -U -E . pylons
touch ./lib/python2.5/site-packages/paste/__init__.py
rm -f ./lib/python2.5/site-packages/simplejson*/_speedups*
./bin/paster create -t pylons $MYAPP template_engine=mako sqlalchemy=False
wget http://files.countergram.com/pylons-gae/app.yaml
wget http://files.countergram.com/pylons-gae/app.py
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class="section" id="getting-started-easy-install-version"&gt;
&lt;h2&gt;Getting started: &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;easy_install&lt;/span&gt;&lt;/tt&gt; version&lt;/h2&gt;
&lt;div class="code-header"&gt;console&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="text"&gt;virtualenv --python=python2.5 --no-site-packages --unzip-setuptools $MYPROJ
cd $MYPROJ
./bin/easy_install -UZa pylons
rm -f ./lib/python2.5/site-packages/simplejson*/_speedups*
./bin/paster create -t pylons $MYAPP template_engine=mako sqlalchemy=False
wget http://files.countergram.com/pylons-gae/app.yaml
wget http://files.countergram.com/pylons-gae/app.py
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If you use &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;easy_install&lt;/span&gt;&lt;/tt&gt;, you must then look in the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;lib/python2.*/site-packages&lt;/span&gt;&lt;/tt&gt; directory and see if multiple versions of any packages (especially WebOb) were installed due to differing dependencies. Delete all but the most recent; otherwise they will conflict. The &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-U&lt;/span&gt;&lt;/tt&gt; argument to &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;easy_install&lt;/span&gt;&lt;/tt&gt; should prevent this but does not necessarily do so.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="python-versions-other-than-2-5"&gt;
&lt;h2&gt;Python versions other than 2.5&lt;/h2&gt;
&lt;p&gt;GAE runs Python 2.5. As long as your code runs on 2.5, you can use whatever (2.x) version of Python you want locally. You'll need to change all instances of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;python2.5&lt;/span&gt;&lt;/tt&gt; in the script above, and also edit the value of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;libdir&lt;/span&gt;&lt;/tt&gt; in &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;app.py&lt;/span&gt;&lt;/tt&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="other-required-edits"&gt;
&lt;h2&gt;Other required edits&lt;/h2&gt;
&lt;p&gt;You'll need to manually edit a few files to get things working.&lt;/p&gt;
&lt;ol class="arabic simple"&gt;
&lt;li&gt;Edit &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;app.yaml&lt;/span&gt;&lt;/tt&gt; and replace &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;$REGNAME&lt;/span&gt;&lt;/tt&gt; with the name of your application as registered with Google. For example, if your hosted URL is &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;myapp.appspot.com&lt;/span&gt;&lt;/tt&gt;, the registered name is &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;myapp&lt;/span&gt;&lt;/tt&gt;.&lt;/li&gt;
&lt;li&gt;Edit &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;app.py&lt;/span&gt;&lt;/tt&gt; and replace &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;$APPNAME&lt;/span&gt;&lt;/tt&gt; with the name of your application that you passed to &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;paster&lt;/span&gt; &lt;span class="pre"&gt;create&lt;/span&gt;&lt;/tt&gt; (i.e. the directory name).&lt;/li&gt;
&lt;li&gt;In your Pylons app's &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;environment.py&lt;/span&gt;&lt;/tt&gt;, comment out or delete the line beginning &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;module_directory=&lt;/span&gt;&lt;/tt&gt; in the instantiation of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;TemplateLookup&lt;/span&gt;&lt;/tt&gt;. GAE does not have a writable filesystem, so Mako's file-based template caching will not work on it. If you are using a different template system, you'll have to figure out what is required to stop it from trying to write any files.&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;div class="section" id="running-locally"&gt;
&lt;h2&gt;Running locally&lt;/h2&gt;
&lt;p&gt;You can run your app locally in a GAE environment using the &lt;a class="reference external" href="http://code.google.com/appengine/downloads.html"&gt;Google App Engine SDK/Launcher&lt;/a&gt;. It's fairly simple, and documentation is available and beyond the scope of this article, but here are a few tips:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;In the GUI version of the SDK/Launcher, be sure to use &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;File-&amp;gt;Add&lt;/span&gt; &lt;span class="pre"&gt;Existing&lt;/span&gt; &lt;span class="pre"&gt;Application&lt;/span&gt;&lt;/tt&gt; rather than &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;File-&amp;gt;New&lt;/span&gt; &lt;span class="pre"&gt;Application&lt;/span&gt;&lt;/tt&gt;. The path to use is the &lt;em&gt;directory&lt;/em&gt; that contains &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;app.yaml&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;app.py&lt;/span&gt;&lt;/tt&gt; (i.e. $MYPROJ).&lt;/li&gt;
&lt;li&gt;Using the command-line version, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;dev_appserver.py&lt;/span&gt;&lt;/tt&gt;, &lt;em&gt;don't&lt;/em&gt; run it from within the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;virtualenv&lt;/span&gt;&lt;/tt&gt;; it won't be able to find the Google libraries.&lt;/li&gt;
&lt;li&gt;The SDK/Launcher has a primitive (compared to &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;paster&lt;/span&gt; &lt;span class="pre"&gt;serve&lt;/span&gt;&lt;/tt&gt;) reloading capability. In order to force an &amp;quot;automatic&amp;quot; reload while your app is running, you'll need to edit (or &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;touch&lt;/span&gt;&lt;/tt&gt;) &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;app.py&lt;/span&gt;&lt;/tt&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="deploying"&gt;
&lt;h2&gt;Deploying&lt;/h2&gt;
&lt;p&gt;There are two ways to deploy your application: the Google App Engine SDK/Launcher application, or the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;appcfg.py&lt;/span&gt;&lt;/tt&gt; command-line script.&lt;/p&gt;
&lt;p&gt;The SDK/Launcher application gives you a nice push-button GUI, while &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;appcfg.py&lt;/span&gt;&lt;/tt&gt; is more flexible with various options such as verbose mode (&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-v&lt;/span&gt;&lt;/tt&gt;), which prints a list of which files are being uploaded, and which are being skipped.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="file-count"&gt;
&lt;h2&gt;File count&lt;/h2&gt;
&lt;p&gt;When I deployed a barebones Pylons app in this way, with the included &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;skip_list&lt;/span&gt;&lt;/tt&gt;, the file count came out to 821 using &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;pip&lt;/span&gt;&lt;/tt&gt; and 814 using &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;easy_install&lt;/span&gt;&lt;/tt&gt;, both, well under the 3,000-file limit. Your &amp;quot;base&amp;quot; file count will probably vary slightly as new library versions come out.&lt;/p&gt;
&lt;p&gt;If you are using &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;easy_install&lt;/span&gt;&lt;/tt&gt; (but apparently &lt;em&gt;not&lt;/em&gt; if you are using &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;pip&lt;/span&gt;&lt;/tt&gt;), you can reduce the file count by excluding EGG-INFO directories from being uploaded. This will &lt;em&gt;probably&lt;/em&gt; not break anything (but see below), unless part of your code depends on entry points of modules other than your app. You'll need to add a line to the middle of the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;skip_list&lt;/span&gt;&lt;/tt&gt; regex in your &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;app.yaml&lt;/span&gt;&lt;/tt&gt;, as follows:&lt;/p&gt;
&lt;pre&gt;&lt;code class="highlight" lang="text"&gt;(.*/site-packages/.*\.egg/EGG-INFO/.*)|
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;For me, this reduced the &amp;quot;base&amp;quot; file count to 691, a savings of about 120 files. Unfortunately, excluding &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;.egg-info&lt;/span&gt;&lt;/tt&gt; directories when using &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;pip&lt;/span&gt;&lt;/tt&gt; breaks the setup.&lt;/p&gt;
&lt;p&gt;If you do this, you must also add &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;template_engine=None&lt;/span&gt;&lt;/tt&gt; as a keyword argument to the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;config.init_app&lt;/span&gt;&lt;/tt&gt; function call in your Pylons app's &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;environment.py&lt;/span&gt;&lt;/tt&gt;. Otherwise, some legacy code in Pylons (supporting the outdated Buffet template system) will break because it can't find some egg info.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="notes-on-workarounds-for-the-curious"&gt;
&lt;h2&gt;Notes on workarounds, for the curious&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;GAE does not appear to use &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;.pth&lt;/span&gt;&lt;/tt&gt; files, which are files that are placed on the Python path and contain additions to the path. This is why &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;sys.path&lt;/span&gt;&lt;/tt&gt; must be set up in &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;app.py&lt;/span&gt;&lt;/tt&gt;. Also, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;easy_install&lt;/span&gt;&lt;/tt&gt; uses its &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;easy_install.pth&lt;/span&gt;&lt;/tt&gt; file to make sure only the latest installed version of each package is on the path. This is why you have to manually delete duplicate packages using this GAE method.&lt;/li&gt;
&lt;li&gt;The provided &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;app.py&lt;/span&gt;&lt;/tt&gt; has a rather odd line: &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;os.mkdir&lt;/span&gt; &lt;span class="pre"&gt;=&lt;/span&gt; &lt;span class="pre"&gt;lambda&lt;/span&gt; &lt;span class="pre"&gt;*x:None&lt;/span&gt;&lt;/tt&gt;. The latest version of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;setuptools&lt;/span&gt;&lt;/tt&gt; (as of 0.6c11) attempts to import &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;mkdir&lt;/span&gt;&lt;/tt&gt; from &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;os&lt;/span&gt;&lt;/tt&gt;, and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;mkdir&lt;/span&gt;&lt;/tt&gt; does not exist on GAE. This monkey-patch allows &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;setuptools&lt;/span&gt;&lt;/tt&gt; to be imported.&lt;/li&gt;
&lt;li&gt;The provided &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;app.yaml&lt;/span&gt;&lt;/tt&gt; has a &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;skip_list&lt;/span&gt;&lt;/tt&gt; that prevents files that are part of the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;virtualenv&lt;/span&gt;&lt;/tt&gt; but useless on GAE, such as binary shared objects, from being uploaded.&lt;/li&gt;
&lt;li&gt;The &amp;quot;speedups&amp;quot; part of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;simplejson&lt;/span&gt;&lt;/tt&gt; (&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;_speedups.py&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;_speedups.so&lt;/span&gt;&lt;/tt&gt;) uses a C extension, which is not supported by GAE. But the package is set up so that it will still work if these files are missing.&lt;/li&gt;
&lt;li&gt;Paste uses a wonky system to allow separate packages (Paste, Paste Deploy, Paste Script) to inhabit the same &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;paste&lt;/span&gt;&lt;/tt&gt; namespace. When installed using &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;pip&lt;/span&gt;&lt;/tt&gt;, this system is dependent on several &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;.pth&lt;/span&gt;&lt;/tt&gt; files, which don't work on GAE. But sticking an &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;__init__.py&lt;/span&gt;&lt;/tt&gt; in makes it work normally.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="conclusion"&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;At this point, you should be up and running and see the Pylons welcome page when you visit your local server run by the SDK. There are still more steps to a real GAE app, of course, such as familiarizing yourself with Google's datastore models.&lt;/p&gt;
&lt;p&gt;I can't provide &amp;quot;support&amp;quot; on any of this, &lt;em&gt;per se&lt;/em&gt;, but if the steps here don't work for you, please e-mail me and I'll try to correct or amend the article.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
</description><guid>http://countergram.com/pylons-google-app-engine</guid><pubDate>Sun, 11 Oct 2009 00:00:00 EST</pubDate></item><item><title>Managing multiple Pylons apps with Supervisor</title><link>http://countergram.com/pylons-supervisor</link><description>&lt;div class="document"&gt;
&lt;p&gt;Wouldn't it be nice if your long-running processes, such as web applications and even HTTP servers, could be managed from one place?&lt;/p&gt;
&lt;p&gt;There are a number of tools, including &lt;a class="reference external" href="http://supervisord.org/"&gt;Supervisor&lt;/a&gt;, daemontools, and runit, that will launch processes, allow them to be configured in various ways, and restart them if they stop unexpectedly. This means you only have to make sure one process is started upon system startup, and it will take care of the rest. This article concentrates on how to manage Pylons web applications using Supervisor, which is fairly easy to configure.&lt;/p&gt;
&lt;p&gt;This is something of a whistle-stop tour, aimed at getting you started with a working configuration. Supervisor is quite flexible, and it's a good idea to read the &lt;a class="reference external" href="http://supervisord.org/manual/current/index.html"&gt;manual&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Assuming you have &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;easy_install&lt;/span&gt;&lt;/tt&gt; (and if you're using Pylons you almost certainly do) but not &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;supervisor&lt;/span&gt;&lt;/tt&gt; yet, here's how to get started. Note that Supervisor does not work on Windows, so this is a Linux/BSD/OS X/etc. tutorial.&lt;/p&gt;
&lt;div class="code-header"&gt;Console&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="text"&gt;easy_install supervisor
mkdir /apps
cd /apps
paster create -t pylons app1
paster create -t pylons app2
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I don't necessarily expect you to create &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;/apps&lt;/span&gt;&lt;/tt&gt; in the root of your filesystem. Create it where you like, but substitute your directory when I talk about &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;/apps&lt;/span&gt;&lt;/tt&gt;, which I'm using for simplicity in this article, later on.&lt;/p&gt;
&lt;p&gt;After executing these commands, you'll have two Pylons applications. They won't do much, but they'll serve as a demonstration. Use real apps instead if you like.&lt;/p&gt;
&lt;div class="section" id="configuring-supervisor"&gt;
&lt;h2&gt;Configuring Supervisor&lt;/h2&gt;
&lt;p&gt;The Supervisor configuration file should be located at &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;/etc/supervisord.conf&lt;/span&gt;&lt;/tt&gt;. If you would like to place it elsewhere for deployment purposes, I strongly recommend creating a symlink at &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;/etc/supervisord.conf&lt;/span&gt;&lt;/tt&gt; as that seems to be where Supervisor will look for configuration when restarted, even if you gave it a different initial configuration file.&lt;/p&gt;
&lt;p&gt;I'll show you the necessary configuration for Supervisor in several parts. Here's the first configuration block:&lt;/p&gt;
&lt;div class="code-header"&gt;/etc/supervisord.conf (partial)&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="ini"&gt;&lt;span class="k"&gt;[supervisord]&lt;/span&gt;
&lt;span class="na"&gt;logfile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;/tmp/supervisord.log&lt;/span&gt;
&lt;span class="na"&gt;logfile_maxbytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;50MB&lt;/span&gt;
&lt;span class="na"&gt;logfile_backups&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;10&lt;/span&gt;
&lt;span class="na"&gt;loglevel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;info&lt;/span&gt;
&lt;span class="na"&gt;pidfile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;/tmp/supervisord.pid&lt;/span&gt;
&lt;span class="na"&gt;nodaemon&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;false&lt;/span&gt;
&lt;span class="na"&gt;minfds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;1024&lt;/span&gt;
&lt;span class="na"&gt;minprocs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;200&lt;/span&gt;
&lt;span class="na"&gt;umask&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;022&lt;/span&gt;
&lt;span class="na"&gt;identifier&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;supervisor&lt;/span&gt;
&lt;span class="na"&gt;directory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;/tmp&lt;/span&gt;
&lt;span class="na"&gt;nocleanup&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;true&lt;/span&gt;
&lt;span class="na"&gt;childlogdir&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;/tmp&lt;/span&gt;
&lt;span class="na"&gt;strip_ansi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Supervisor uses an INI-style configuration file, which is divided into sections. Each section has a name enclosed in square brackets. This section sets up some basic parameters that you can adjust if you want.&lt;/p&gt;
&lt;p&gt;Now, let's talk about permissions. If run as root with this configuration, Supervisor will retain root permissions. You can then configure a user for each program that Supervisor runs, and it will drop root permissions for those specific programs. This is useful if some of the programs you manage need to run as root. For example, if you manage lighttpd as well as your Pylons apps, you may need lighttpd to run as root while your apps run as a non-root user.&lt;/p&gt;
&lt;p&gt;If you want Supervisord to drop root permissions across the board, add a &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;user&lt;/span&gt; &lt;span class="pre"&gt;=&lt;/span&gt; &lt;span class="pre"&gt;&amp;lt;username&amp;gt;&lt;/span&gt;&lt;/tt&gt; line to the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;[supervisord]&lt;/span&gt;&lt;/tt&gt; section of the configuration file.&lt;/p&gt;
&lt;p&gt;If you allow Supervisord to remain root, make sure the configuration file is writable only by root!&lt;/p&gt;
&lt;p&gt;Next, let's set up &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;supervisorctl&lt;/span&gt;&lt;/tt&gt;, which is a command-line utility that lets you see the status and output of processes and start, stop, and restart them.&lt;/p&gt;
&lt;div class="code-header"&gt;/etc/supervisord.conf (cont.)&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="ini"&gt;&lt;span class="k"&gt;[unix_http_server]&lt;/span&gt;
&lt;span class="na"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;/tmp/supervisord.sock&lt;/span&gt;

&lt;span class="k"&gt;[supervisorctl]&lt;/span&gt;
&lt;span class="na"&gt;serverurl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;unix:///tmp/supervisord.sock&lt;/span&gt;

&lt;span class="k"&gt;[rpcinterface:supervisor]&lt;/span&gt;
&lt;span class="na"&gt;supervisor.rpcinterface_factory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;supervisor.rpcinterface:make_main_rpcinterface&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;With this configuration, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;supervisorctl&lt;/span&gt;&lt;/tt&gt; will only be available from the command line on the local machine, because it's using a file-based socket. Supervisor also supports HTTP and has a web interface, but I'm leaving it disabled for security reasons.&lt;/p&gt;
&lt;p&gt;After saving &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;supervisord.conf&lt;/span&gt;&lt;/tt&gt;, you should be able to run &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;supervisord&lt;/span&gt;&lt;/tt&gt;, followed by &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;supervisorctl&lt;/span&gt;&lt;/tt&gt;, and end up in an interactive shell. There are no processes running, so it's not very interesting, but you can type &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;help&lt;/span&gt;&lt;/tt&gt; for a list of options, and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;exit&lt;/span&gt;&lt;/tt&gt; to leave.&lt;/p&gt;
&lt;p&gt;Finally, let's add some processes. Open that config file back up.&lt;/p&gt;
&lt;div class="code-header"&gt;/etc/supervisord.conf (cont.)&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="ini"&gt;&lt;span class="k"&gt;[program:app1]&lt;/span&gt;
&lt;span class="na"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;lt;yourusername&amp;gt;&lt;/span&gt;
&lt;span class="na"&gt;command&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;paster serve --reload /apps/app1/development.ini&lt;/span&gt;
&lt;span class="na"&gt;environment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;PYTHONPATH=/apps/app1/,PYTHON_EGG_DIR=/tmp/python-eggs/&lt;/span&gt;

&lt;span class="k"&gt;[program:app2]&lt;/span&gt;
&lt;span class="na"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;lt;yourusername&amp;gt;&lt;/span&gt;
&lt;span class="na"&gt;command&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;paster serve --reload /apps/app2/development.ini&lt;/span&gt;
&lt;span class="na"&gt;environment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;PYTHONPATH=/apps/app2/,PYTHON_EGG_DIR=/tmp/python-eggs/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Although in this example the names of the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;program&lt;/span&gt;&lt;/tt&gt; blocks are the same as the names of the applications, they don't have to be. For example, if your application was named &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;reallylongappname&lt;/span&gt;&lt;/tt&gt;, your Supervisor configuration could read &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;[program:rla]&lt;/span&gt;&lt;/tt&gt;, or whatever you like. The name in the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;program&lt;/span&gt;&lt;/tt&gt; heading is what will appear in &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;supervisorctl&lt;/span&gt;&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;The environment can be set individually for each process. Here, the Python path is set because we're in development and have not installed the applications as packages.&lt;/p&gt;
&lt;p&gt;After saving &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;supervisord.conf&lt;/span&gt;&lt;/tt&gt;, either type &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;supervisorctl&lt;/span&gt; &lt;span class="pre"&gt;update&lt;/span&gt;&lt;/tt&gt;, or type &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;supervisorctl&lt;/span&gt;&lt;/tt&gt; to enter the interactive shell and type &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;update&lt;/span&gt;&lt;/tt&gt;. Either style works, so it's a matter of whether you want to leave a shell open for convenience.&lt;/p&gt;
&lt;p&gt;Now, type &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;supervisorctl&lt;/span&gt; &lt;span class="pre"&gt;status&lt;/span&gt;&lt;/tt&gt;, or in the interactive shell type &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;status&lt;/span&gt;&lt;/tt&gt;. You should see two items, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;app1&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;app2&lt;/span&gt;&lt;/tt&gt;, both marked &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;RUNNING&lt;/span&gt;&lt;/tt&gt;. If they instead show an error or exit condition, something went wrong.&lt;/p&gt;
&lt;p&gt;Say &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;app1&lt;/span&gt;&lt;/tt&gt; failed to start. The best thing to do is &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;supervisorctl&lt;/span&gt; &lt;span class="pre"&gt;tail&lt;/span&gt; &lt;span class="pre"&gt;app1&lt;/span&gt; &lt;span class="pre"&gt;stderr&lt;/span&gt;&lt;/tt&gt;, which will show you the last log messages that &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;app1&lt;/span&gt;&lt;/tt&gt; printed. You can also add a &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-f&lt;/span&gt;&lt;/tt&gt; switch after &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;tail&lt;/span&gt;&lt;/tt&gt;, just as with the real &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;tail&lt;/span&gt;&lt;/tt&gt; utility, to see live updates of an application's output.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="where-to-go-from-here"&gt;
&lt;h2&gt;Where to go from here&lt;/h2&gt;
&lt;p&gt;It's up to you which programs you would like to manage using Supervisor, and which are better off being managed by your operating system's init system of choice, such as &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;init.d&lt;/span&gt;&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;rc.d&lt;/span&gt;&lt;/tt&gt;, or &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;launchd&lt;/span&gt;&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;It can be useful, however, to run your HTTP server, such as &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;lighttpd&lt;/span&gt;&lt;/tt&gt;, under Supervisor. This makes it easy to restart the server without &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;su&lt;/span&gt;&lt;/tt&gt; or &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;sudo&lt;/span&gt;&lt;/tt&gt;, since the Supervisor process is running as root (if configured as above &amp;amp;mdash; and remember to make the configuration file writable only by root to prevent abuse).&lt;/p&gt;
&lt;div class="code-header"&gt;/etc/supervisord.conf (cont.)&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="ini"&gt;&lt;span class="k"&gt;[program:http]&lt;/span&gt;
&lt;span class="na"&gt;command&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;/usr/sbin/lighttpd -D -f /etc/lighttpd.conf&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;(Remember that &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-D&lt;/span&gt;&lt;/tt&gt; argument, which prevents &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;lighttpd&lt;/span&gt;&lt;/tt&gt; from daemonizing itself.)&lt;/p&gt;
&lt;p&gt;If necessary, you can even run multiple HTTP servers on different ports, which gives you more flexibility than a system-wide startup script.&lt;/p&gt;
&lt;p&gt;Supervisor is well documented, so the logical next step is reading the &lt;a class="reference external" href="http://supervisord.org/manual/current/index.html"&gt;manual&lt;/a&gt; for more ideas of what it can do for you.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
</description><guid>http://countergram.com/pylons-supervisor</guid><pubDate>Fri, 28 Aug 2009 00:00:00 EST</pubDate></item><item><title>Pylons from scratch 1: a barebones app</title><link>http://countergram.com/pylons-from-scratch-1</link><description>&lt;div class="document"&gt;
&lt;p&gt;Typically when you start a new application using the Python web framework Pylons, you might use &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;paster&lt;/span&gt; &lt;span class="pre"&gt;create&lt;/span&gt; &lt;span class="pre"&gt;-t&lt;/span&gt; &lt;span class="pre"&gt;pylons&lt;/span&gt; &lt;span class="pre"&gt;&amp;lt;appname&amp;gt;&lt;/span&gt;&lt;/tt&gt; to do a little code generation for you and create a conventional directory structure.&lt;/p&gt;
&lt;p&gt;But in this series, I'll show you how to build a working Pylons app by hand, from scratch.&lt;/p&gt;
&lt;p&gt;Why do this? Two reasons:&lt;/p&gt;
&lt;ol class="arabic simple"&gt;
&lt;li&gt;Going behind the scenes, and building from scratch what code-generation tools would do for you, &lt;strong&gt;helps you learn how Pylons works&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;You will also find out how to &lt;strong&gt;wrap an existing, non-Pylons WSGI application&lt;/strong&gt; so that it can be deployed using &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;paster&lt;/span&gt;&lt;/tt&gt; and the same INI configuration files as Pylons applications, potentially easing legacy integration with Pylons apps.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If you do not already have Pylons installed, please &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;easy_install&lt;/span&gt; &lt;span class="pre"&gt;Pylons==0.9.7&lt;/span&gt;&lt;/tt&gt;. Or, use the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;virtualenv&lt;/span&gt;&lt;/tt&gt; instructions in the &lt;a class="reference external" href="http://pylonshq.com/docs/en/0.9.7/gettingstarted/#installing"&gt;Pylons installation documentation&lt;/a&gt;.&lt;/p&gt;
&lt;div class="section" id="starting-out"&gt;
&lt;h2&gt;Starting out&lt;/h2&gt;
&lt;p&gt;First of all, we'll be using setuptools and creating a package structure. Pylons, and particularly the Paste Deploy component, is really set up to work with apps that are packaged as eggs, which requires setuptools and a &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;setup.py&lt;/span&gt;&lt;/tt&gt;. So start by creating a few directories and (empty) files, in a structure looking like this:&lt;/p&gt;
&lt;div class="code-header"&gt;Directory structure&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="text"&gt;barebones
|-- barebones
|   |-- __init__.py
|   `-- app.py
|-- development.ini
`-- setup.py
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Next, let's make an application. This is going to look almost exactly like a &amp;quot;hello, world&amp;quot; WSGI application, but with an extra function. For reference, a basic WSGI application would look like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class="highlight" lang="python"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;app&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;start_response&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;start_response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;200 OK&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;Content-Type&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;text/plain&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)])&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;Hello, world&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If you're unfamiliar with the functions or concepts used here, read the &lt;a class="reference external" href="http://wsgi.org/wsgi/"&gt;WSGI wiki&lt;/a&gt; to understand the spec, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;environ&lt;/span&gt;&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;start_response&lt;/span&gt;&lt;/tt&gt;, and returning iterators.&lt;/p&gt;
&lt;p&gt;Now, the approach we need to use in order to work with Paste's configuration and deployment system is a &amp;quot;factory,&amp;quot; a function that creates a WSGI application. So our &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;app.py&lt;/span&gt;&lt;/tt&gt; will actually start out looking like this:&lt;/p&gt;
&lt;div class="code-header"&gt;app.py&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="python"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;make_app&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;global_conf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;app_conf&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;app&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;start_response&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;start_response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;200 OK&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;Content-Type&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;text/plain&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)])&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;Hello, &amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;app_conf&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;foo&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;world&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This is merely a function that takes configuration parameters and returns the WSGI app, which is the same as the one above it except for the use of a setting from &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;app_conf&lt;/span&gt;&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;In a standard Pylons app structure created by &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;paster&lt;/span&gt; &lt;span class="pre"&gt;create&lt;/span&gt;&lt;/tt&gt;, this &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;make_app&lt;/span&gt;&lt;/tt&gt; function would  be in &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;config/middleware.py&lt;/span&gt;&lt;/tt&gt; instead. That's an appropriate name, since a lot of middleware is applied in it, but it's also not immediately obvious that it contains the main application factory.&lt;/p&gt;
&lt;p&gt;Speaking of configuration, this is the barebones configuration file we'll be using:&lt;/p&gt;
&lt;div class="code-header"&gt;development.ini&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="ini"&gt;&lt;span class="k"&gt;[DEFAULT]&lt;/span&gt;
&lt;span class="na"&gt;debug&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;true&lt;/span&gt;

&lt;span class="k"&gt;[server:main]&lt;/span&gt;
&lt;span class="na"&gt;use&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;egg:Paste#http&lt;/span&gt;
&lt;span class="na"&gt;host&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;127.0.0.1&lt;/span&gt;
&lt;span class="na"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;8000&lt;/span&gt;

&lt;span class="k"&gt;[app:main]&lt;/span&gt;
&lt;span class="na"&gt;use&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;egg:barebones&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;It's rather spartan, isn't it? This configuration file is missing a lot of the sections a real Pylons app has, mostly to do with logging, which we can add later. It also does not configure components that we're not actually using yet, like SQLAlchemy and Beaker. This minimal configuration is plenty to satisfy the requirements of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;paster&lt;/span&gt; &lt;span class="pre"&gt;serve&lt;/span&gt;&lt;/tt&gt; and get our application running and serving on &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;localhost:8000&lt;/span&gt;&lt;/tt&gt;. Not yet, however. We still need to use setuptools to create an egg for our application with the proper entry point.&lt;/p&gt;
&lt;p&gt;By the way, the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;global_conf&lt;/span&gt;&lt;/tt&gt; argument to &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;make_app&lt;/span&gt;&lt;/tt&gt; above will be drawn from the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;DEFAULT&lt;/span&gt;&lt;/tt&gt; section, while the keyword arguments present in &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;app_conf&lt;/span&gt;&lt;/tt&gt; will be drawn from the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;app:main&lt;/span&gt;&lt;/tt&gt; section.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="explanation-of-setup-and-entry-points"&gt;
&lt;h2&gt;Explanation of setup and entry points&lt;/h2&gt;
&lt;p&gt;The &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;setup.py&lt;/span&gt;&lt;/tt&gt; file is what makes your package distributable as an egg, which is what allows &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;paster&lt;/span&gt; &lt;span class="pre"&gt;serve&lt;/span&gt;&lt;/tt&gt; to serve your app.&lt;/p&gt;
&lt;p&gt;Below is the barebones &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;setup.py&lt;/span&gt;&lt;/tt&gt; file. Stock Pylons apps also include a script that automatically installs &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;setuptools&lt;/span&gt;&lt;/tt&gt; if it's not found, which I have left out, along with some i18n-related configuration.&lt;/p&gt;
&lt;p&gt;The important part of this file to note for future use and changes is the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;entry_points&lt;/span&gt;&lt;/tt&gt; argument, which (explain)&lt;/p&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;zip_safe=False&lt;/span&gt;&lt;/tt&gt; is also important, as it causes your package not to be made into a compressed archive when installed. Instead, it will be installed as directories and files, which means your templates and static files will be accessible.&lt;/p&gt;
&lt;div class="code-header"&gt;setup.py&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="python"&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;setuptools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;find_packages&lt;/span&gt;

&lt;span class="n"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;barebones&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;version&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;0.1&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;author_email&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;install_requires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s"&gt;&amp;quot;Pylons&amp;gt;=0.9.7&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;setup_requires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;PasteScript&amp;gt;=1.6.3&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;packages&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;find_packages&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="n"&gt;include_package_data&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;zip_safe&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;paster_plugins&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;PasteScript&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;Pylons&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;entry_points&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;span class="s"&gt;    [paste.app_factory]&lt;/span&gt;
&lt;span class="s"&gt;    main = barebones.app:make_app&lt;/span&gt;
&lt;span class="s"&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class="section" id="starting-the-server"&gt;
&lt;h2&gt;Starting the server&lt;/h2&gt;
&lt;p&gt;With all three of these files saved, run the following command in the project directory:&lt;/p&gt;
&lt;div class="code-header"&gt;Console&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="text"&gt;python setup.py egg_info
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This will create a directory named &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;barebones.egg-info&lt;/span&gt;&lt;/tt&gt;, which is &lt;strong&gt;required for paster serve to work&lt;/strong&gt;. Remember, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;development.ini&lt;/span&gt;&lt;/tt&gt; specifies the main application as using &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;egg:barebones&lt;/span&gt;&lt;/tt&gt;. It won't work if your app is not packaged as an egg. The &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;paster&lt;/span&gt; &lt;span class="pre"&gt;create&lt;/span&gt;&lt;/tt&gt; utility normally does this for you when you create a new Pylons app, though &lt;strong&gt;you sometimes need to re-run this command if you significantly change the structure of your app&lt;/strong&gt;, including adding entry points or renaming the package.&lt;/p&gt;
&lt;p&gt;Now, at last, you can start your application:&lt;/p&gt;
&lt;div class="code-header"&gt;Console&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="text"&gt;paster serve --reload development.ini
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If all goes well, you now have a HTTP server running on &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;localhost:8000&lt;/span&gt;&lt;/tt&gt; that you can visit in your web browser to see the message &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;Hello,&lt;/span&gt; &lt;span class="pre"&gt;world&lt;/span&gt;&lt;/tt&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="configuration-and-next-steps"&gt;
&lt;h2&gt;Configuration and next steps&lt;/h2&gt;
&lt;p&gt;Now, to make sure the configuration file works properly, add the following line to the end, in the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;[app:main]&lt;/span&gt;&lt;/tt&gt; section:&lt;/p&gt;
&lt;div class="code-header"&gt;development.ini (cont.)&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="ini"&gt;&lt;span class="na"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;Pylons&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If you have left &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;paster&lt;/span&gt;&lt;/tt&gt; running while you did this, you should see in its output a message to the effect that it reloaded the app when you saved the new &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;development.ini&lt;/span&gt;&lt;/tt&gt; file. This reloading feature is extremely useful for rapid development. &lt;strong&gt;Changes to Python source files in your app and the main INI configuration file will result in a reload, while changes to secondary INI files, templates, and static files will not. Syntax and import errors will cause the server to terminate.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Visit &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;localhost:8000&lt;/span&gt;&lt;/tt&gt; again, and you should see the message &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;Hello,&lt;/span&gt; &lt;span class="pre"&gt;Pylons&lt;/span&gt;&lt;/tt&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="are-we-there-yet"&gt;
&lt;h2&gt;Are we there yet?&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Is this a Pylons app? Not yet.&lt;/strong&gt; Pylons is a collection of libraries and tools, one of which is Paste, which we are using. But this app includes very little of what makes Pylons what it is, including the special way it handles controllers and pseudo-global, context-specific variables. Still, it's a start, and &lt;strong&gt;this provides you with a way to create any WSGI application you want in a way that can be configured using INI files and deployed with Paste.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;What's more important is that this is a way for you to &lt;strong&gt;easily package existing WSGI applications&lt;/strong&gt; so that they can be served up alongside your Pylons applications, using most of the same configuration and deployment. You'll just need to create a &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;setup.py&lt;/span&gt;&lt;/tt&gt; that resembles the one above, create an app factory that returns your WSGI application, and set up your INI file and egg_info.&lt;/p&gt;
&lt;p&gt;Using this method, &lt;strong&gt;I successfully converted three existing web sites, which used a mostly homebrew WSGI-based system, to using Paste Deploy&lt;/strong&gt; and running alongside my Pylons apps. I then started to convert them into actual Pylons apps at my leisure, knowing that at least I had eliminated extra deployment steps and could manage everything in a consistent way.&lt;/p&gt;
&lt;p&gt;So that's the basic setup in a nutshell. In the rest of this series, we'll add the Pylons environment, controllers, Routes, Mako templates, logging, and SQLAlchemy.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
</description><guid>http://countergram.com/pylons-from-scratch-1</guid><pubDate>Sun, 23 Aug 2009 00:00:00 EST</pubDate></item><item><title>Create Pylons filters: middleware the configurable way</title><link>http://countergram.com/pylons-filters</link><description>&lt;div class="document"&gt;
&lt;p&gt;If you're familiar with the use of WSGI middleware in Python web applications, you know that it's a potentially powerful way to modify both requests and responses without changing any code in the WSGI application wrapped by the middleware. But without something like the &amp;quot;filters&amp;quot; of Paste Deploy, used by  Pylons and discussed here, the middleware must itself be instantiated in code.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;This article will show you how to package a WSGI middleware component so it can be added to your application using only configuration and no code, assuming you are using Pylons (or at least Paste Deploy).&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Here's a simple piece of WSGI middleware that adds a value to the WSGI environment. It's about as simple as middleware gets.&lt;/p&gt;
&lt;div class="code-header"&gt;modulename/__init__.py&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="python"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AddValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;object&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;__init__&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="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__call__&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="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;start_response&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;environ&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;start_response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;To use this middleware class as it is, we would have to instantiate it in code. For example, if &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;app&lt;/span&gt;&lt;/tt&gt; is an existing WSGI application, we could write &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;app&lt;/span&gt; &lt;span class="pre"&gt;=&lt;/span&gt; &lt;span class="pre"&gt;AddValue(app,&lt;/span&gt; &lt;span class="pre"&gt;'foo',&lt;/span&gt; &lt;span class="pre"&gt;4)&lt;/span&gt;&lt;/tt&gt;. Then, for the wrapped application, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;environ['foo']&lt;/span&gt;&lt;/tt&gt; would equal 4.&lt;/p&gt;
&lt;div class="section" id="creating-the-filter"&gt;
&lt;h2&gt;Creating the filter&lt;/h2&gt;
&lt;p&gt;Converting this middleware into a filter will allow it to be applied using configuration only, with no code changes. Filters are added to Pylons applications in the INI configuration file, and are themselves configurable. The downside is that &lt;strong&gt;middleware applied in this way can only be applied to the &amp;quot;outside&amp;quot; of an application&lt;/strong&gt;, not in between other middleware in its internal stack.&lt;/p&gt;
&lt;p&gt;Creating a filter is not hard at all, but can look confusing at first because there are three layers:&lt;/p&gt;
&lt;ol class="arabic simple"&gt;
&lt;li&gt;The middleware component itself (The &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;AddValue&lt;/span&gt;&lt;/tt&gt; class, defined above, which will remain unchanged)&lt;/li&gt;
&lt;li&gt;The filter function, which takes a WSGI application as an argument and returns a WSGI application that has had the middleware applied&lt;/li&gt;
&lt;li&gt;The filter factory function, which takes configuration as its arguments and returns the filter function.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;What you need to deploy a filter in practice is a filter factory function, in a package deployed as an egg, with extra lines in the package's ``setup.py`` to make it usable from a configuration file.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Here's a filter factory that applies our &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;AddValue&lt;/span&gt;&lt;/tt&gt; middleware class:&lt;/p&gt;
&lt;div class="code-header"&gt;modulename/__init__.py (cont.)&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="python"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;addvalue_filter_factory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;global_conf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&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;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;AddValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;filter&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;key&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;value&lt;/span&gt;&lt;/tt&gt; arguments here come from the INI configuration file. Since they are specified as arguments individually, they are required. To make them optional, we would rephrase the code like this:&lt;/p&gt;
&lt;div class="code-header"&gt;modulename/__init__.py (optional arguments)&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="python"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;addvalue_filter_factory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;global_conf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;app_conf&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;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;AddValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;app_conf&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;key&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;default&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="n"&gt;app_conf&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;value&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;defaultvalue&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;filter&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class="section" id="deployment"&gt;
&lt;h2&gt;Deployment&lt;/h2&gt;
&lt;p&gt;The &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;addvalue_filter_factory&lt;/span&gt;&lt;/tt&gt; function is not meant to be used directly. Instead, it should be part of a package deployed as an egg using &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;setuptools&lt;/span&gt;&lt;/tt&gt;, and the following should be placed in the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;setup.py&lt;/span&gt;&lt;/tt&gt;:&lt;/p&gt;
&lt;div class="code-header"&gt;setup.py (partial)&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="python"&gt;&lt;span class="n"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="c"&gt;# ...&lt;/span&gt;
    &lt;span class="n"&gt;entry_points&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;span class="s"&gt;    [paste.filter_factory]&lt;/span&gt;
&lt;span class="s"&gt;    addvalue = modulename:addvalue_filter_factory&lt;/span&gt;
&lt;span class="s"&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
    &lt;span class="c"&gt;# ...&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This sets up an entry point named &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;addvalue&lt;/span&gt;&lt;/tt&gt; that points to the function. Note that the punctuation between the module name and the function name is a colon, not a dot.&lt;/p&gt;
&lt;p&gt;Assuming this package is built as an egg and available on the Python path, the filter can now be used by adding something like this to the INI file for the Pylons application:&lt;/p&gt;
&lt;div class="code-header"&gt;development.ini (partial)&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="ini"&gt;&lt;span class="k"&gt;[app:main]&lt;/span&gt;
&lt;span class="c"&gt;# ...&lt;/span&gt;
&lt;span class="na"&gt;filter-with&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;myvalue&lt;/span&gt;

&lt;span class="k"&gt;[filter:myvalue]&lt;/span&gt;
&lt;span class="na"&gt;use&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;egg:modulename#addvalue&lt;/span&gt;
&lt;span class="na"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;foo&lt;/span&gt;
&lt;span class="na"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class="section" id="conclusion"&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;It seems like more work up front to create a filter rather than just a plain middleware component. For many types of middleware, however, the ability to easily drop in a component using configuration and no code is valuable. It can ease deployment, and makes the conditional application of middleware for different configurations easier.&lt;/p&gt;
&lt;p&gt;An example of a useful filter might be an authorization filter. Although it's generally more powerful to build authorization into an app, authorization could be added on top of an app that doesn't have any using a filter that assigns certain required roles to any part of the app under certain paths. The configuration might look something like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class="highlight" lang="ini"&gt;&lt;span class="k"&gt;[filter:auth]&lt;/span&gt;
&lt;span class="na"&gt;use&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;egg:myauth#rolebased&lt;/span&gt;
&lt;span class="na"&gt;/admin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;admin&lt;/span&gt;
&lt;span class="na"&gt;/edit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;writer,editor&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Implementation is left as an exercise for the reader!&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
</description><guid>http://countergram.com/pylons-filters</guid><pubDate>Thu, 20 Aug 2009 00:00:00 EST</pubDate></item><item><title>Rename your Pylons app without going mad</title><link>http://countergram.com/rename-pylons-app</link><description>&lt;div class="document"&gt;
&lt;p&gt;Eventually, it happens: You realize that the name you gave your Pylons app conflicts with another package, or just needs to be changed. Unfortunately, you've now got a bunch of references to the old name to change. Not only that, but they come in different contexts: import statements, expressions, configuration, strings, etc.&lt;/p&gt;
&lt;p&gt;Here's how to rename that Pylons app.&lt;/p&gt;
&lt;p&gt;First, and perhaps, most important, &lt;strong&gt;delete&lt;/strong&gt; the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;dist&lt;/span&gt;&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;build&lt;/span&gt;&lt;/tt&gt;, and any &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;.egg-info&lt;/span&gt;&lt;/tt&gt; directories in your project directory.&lt;/p&gt;
&lt;p&gt;If you leave these directories laying around, they can hold old information that references the previous name of your project, which will cause problems whether you are running your app from the project directory or installing it. Running &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;python&lt;/span&gt; &lt;span class="pre"&gt;setup.py&lt;/span&gt; &lt;span class="pre"&gt;clean&lt;/span&gt;&lt;/tt&gt; will not necessarily fix this problem, so delete the directories.&lt;/p&gt;
&lt;p&gt;Next, you need to start replacing references to your project in code and configuration files. But you can't simply do a global search-and-replace, because your project name might be used in multiple contexts; for example, maybe you named your project &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;foo&lt;/span&gt;&lt;/tt&gt; because it would be running on &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;foo.com&lt;/span&gt;&lt;/tt&gt;, and now you want it to be named &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;foo_site&lt;/span&gt;&lt;/tt&gt; to avoid a naming conflict. But a global search-and-replace could end up replacing &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;foo.com&lt;/span&gt;&lt;/tt&gt; with &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;foo_site.com&lt;/span&gt;&lt;/tt&gt; in your templates.&lt;/p&gt;
&lt;p&gt;Instead, try doing global search-and-replace on &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;from&lt;/span&gt; &lt;span class="pre"&gt;foo&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;import&lt;/span&gt; &lt;span class="pre"&gt;foo&lt;/span&gt;&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;Of course, you'll also need to change any references to &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;foo&lt;/span&gt;&lt;/tt&gt; in your own code. But here's a list of places in a stock Pylons app, created by &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;paster&lt;/span&gt; &lt;span class="pre"&gt;create&lt;/span&gt; &lt;span class="pre"&gt;-t&lt;/span&gt; &lt;span class="pre"&gt;pylons&lt;/span&gt;&lt;/tt&gt;, where your app name appears, &lt;strong&gt;other than import statements&lt;/strong&gt;:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;Config file (e.g. &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;development.ini&lt;/span&gt;&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;foo/config/deployment.ini_tmpl&lt;/span&gt;&lt;/tt&gt;): &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;use&lt;/span&gt;&lt;/tt&gt; under &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;[app:main]&lt;/span&gt;&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;keys&lt;/span&gt;&lt;/tt&gt; under &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;[loggers]&lt;/span&gt;&lt;/tt&gt;, and the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;[logger_foo]&lt;/span&gt;&lt;/tt&gt; section.&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;foo/config/environment.py&lt;/span&gt;&lt;/tt&gt;: &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;config.init_app&lt;/span&gt;&lt;/tt&gt; call, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;config['pylons.h']&lt;/span&gt;&lt;/tt&gt; assignment.&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;MANIFEST.in&lt;/span&gt;&lt;/tt&gt;&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;setup.py&lt;/span&gt;&lt;/tt&gt;: &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;name&lt;/span&gt;&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;package_data&lt;/span&gt;&lt;/tt&gt;, and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;entry_points&lt;/span&gt;&lt;/tt&gt; arguments.&lt;/li&gt;
&lt;li&gt;Documentation.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Finally, change the project directory name and module directory name, which are usually identical. In other words, if this is your project directory layout, you'll want to change both &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;foo&lt;/span&gt;&lt;/tt&gt; directory names:&lt;/p&gt;
&lt;pre&gt;&lt;code class="highlight" lang="text"&gt;foo
|-- foo
|   |-- __init__.py
|   |-- config
|   |-- controllers
|   |-- ...
|   `-- websetup.py
|-- development.ini
`-- setup.py
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Once you're pretty sure you've changed all references to &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;foo&lt;/span&gt;&lt;/tt&gt;, run &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;python&lt;/span&gt; &lt;span class="pre"&gt;setup.py&lt;/span&gt; &lt;span class="pre"&gt;egg_info&lt;/span&gt;&lt;/tt&gt; to regenerate the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;.egg-info&lt;/span&gt;&lt;/tt&gt; directory you deleted earlier.&lt;/p&gt;
&lt;p&gt;Also check your system's &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;site-packages&lt;/span&gt;&lt;/tt&gt; directory or other directories on your Python path, to make sure there is not still an installed version of the package with the previous name.&lt;/p&gt;
&lt;p&gt;Now you're ready to test. If all goes well, your app should run under its new name. If you see any &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;ImportError&lt;/span&gt;&lt;/tt&gt; exceptions or other unexpected errors, you missed one.&lt;/p&gt;
&lt;p&gt;Finally, run &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;python&lt;/span&gt; &lt;span class="pre"&gt;setup.py&lt;/span&gt; &lt;span class="pre"&gt;install&lt;/span&gt;&lt;/tt&gt; (unless you have a reason not to want your app in &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;site-packages&lt;/span&gt;&lt;/tt&gt;) to make sure the packaging works.&lt;/p&gt;
&lt;p&gt;Done.&lt;/p&gt;
&lt;/div&gt;
</description><guid>http://countergram.com/rename-pylons-app</guid><pubDate>Tue, 18 Aug 2009 00:00:00 EST</pubDate></item><item><title>GMP bignums vs. Python bignums: performance and code examples</title><link>http://countergram.com/c-extension-n-choose-k</link><description>&lt;div class="document"&gt;
&lt;p&gt;Interpreted languages such as Python are traditionally used in IO-bound applications where their lack of high CPU-bound performance does not matter. On the other hand, extensions written in C (or C++) can be used to implement computationally heavy operations that can be called from Python as if they were native functions.&lt;/p&gt;
&lt;p&gt;Of course, it's not quite that simple. As soon as data types and structures more complicated than simple integers, floats, and strings become involved, you have to consider both the implementation of the types in C and data interchange between the C layer and the high-level language, whether through serialization or through a data-access API. Furthermore, you might be surprised at what actually does, and does not, result in a gain in performance.&lt;/p&gt;
&lt;p&gt;In this article, I've implemented n-choose-k, or &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;choose(n,&lt;/span&gt; &lt;span class="pre"&gt;k)&lt;/span&gt;&lt;/tt&gt; for languages that don't support arbitrary infix operators, in Python and C. The choose function is useful in combinatorics and evaluates to the number of unordered subsets of size &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;k&lt;/span&gt;&lt;/tt&gt; that can be chosen from a set of size &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;n&lt;/span&gt;&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;Besides being practical, choose requires dividing factorials, which means very large numbers and lots of arithmetic operations. N-choose-k can be implemented in terms of factorials like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class="highlight" lang="text"&gt;choose(n, k) == factorial(n) / (factorial(k) * factorial(n - k))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Because k ≤ n in all normal cases, a small optimization is possible by simplifying &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;factorial(n)&lt;/span&gt; &lt;span class="pre"&gt;/&lt;/span&gt; &lt;span class="pre"&gt;factorial(k)&lt;/span&gt;&lt;/tt&gt; into a stopped factorial function. In other words, evaluate [n × (n - 1) × ... × k]. This saves 2k multiplications versus computing the full factorials of both n and k.&lt;/p&gt;
&lt;div class="section" id="pure-python-version"&gt;
&lt;h2&gt;Pure Python Version&lt;/h2&gt;
&lt;p&gt;Versions of the factorial and choose functions implemented in pure Python will serve as references; theoretically, the C version should improve on the performance of the Python version, or there's no point. Because Python has a 1,000-call recursion limit and does not optimize tail recursion, an iterative algorithm is best. The choice of 50,000 and 50 as parameters is arbitrary and will take a long enough time to execute that differences in performance between versions will be obvious.&lt;/p&gt;
&lt;div class="code-header"&gt;testpure.py&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="python"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stop&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;stop&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
        &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mf"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;choose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stop&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;k&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;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;__main__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="n"&gt;choose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;50000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class="section" id="the-first-c-version"&gt;
&lt;h2&gt;The First C Version&lt;/h2&gt;
&lt;p&gt;A naive version in C would duplicate the Python code above, using C &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;unsigned&lt;/span&gt; &lt;span class="pre"&gt;long&lt;/span&gt; &lt;span class="pre"&gt;long&lt;/span&gt;&lt;/tt&gt; arguments instead of Python objects. This would lead to an upper bound of 1.8e19, a value easily exceeded by the factorial function at n=21.&lt;/p&gt;
&lt;p&gt;In order to get around this limitation, we need a bignum or bigint library, which can represent arbitrary-precision integers. As it happens, Python intrinsically supports bignums in pure Python code, and also exposes a C API to its bignum objects. Perhaps we could use Python's bignum library while writing the algorithm in C for speed.&lt;/p&gt;
&lt;p&gt;Additionally, a &lt;a class="reference external" href="http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/"&gt;Pyrex&lt;/a&gt; wrapper, shown below the C code, will make it easy to generate a module usable from Python.&lt;/p&gt;
&lt;p&gt;The code below implements C functions for stopped factorial and choose, which use Python objects throughout. We'll call this &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;choose1.c&lt;/span&gt;&lt;/tt&gt;, which distinguishes it from the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;choose.c&lt;/span&gt;&lt;/tt&gt; that Pyrex will generate (and foreshadows that there will be a &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;choose2.c&lt;/span&gt;&lt;/tt&gt;.)&lt;/p&gt;
&lt;div class="code-header"&gt;choose1.c&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="c"&gt;&lt;span class="cp"&gt;#include &amp;lt;Python.h&amp;gt;&lt;/span&gt;

&lt;span class="n"&gt;PyObject&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;factorial1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;PyObject&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PyObject&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;stop&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;PyObject&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;dec&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PyLong_FromLong&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;PyObject&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PyLong_FromLong&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;PyObject&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;n1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PyNumber_Long&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;PyObject&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;placeholder&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;PyObject_Compare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stop&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;placeholder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PyNumber_Multiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;Py_DECREF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;placeholder&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;placeholder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PyNumber_Subtract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dec&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;Py_DECREF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;n1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;placeholder&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;Py_DECREF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dec&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;Py_DECREF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;PyObject&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;choose1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;PyObject&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PyObject&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;PyObject&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;zero&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PyLong_FromLong&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;PyObject&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;nk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PyNumber_Subtract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;PyObject&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;fact1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;PyObject&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;fact2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;zero&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;PyObject&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PyNumber_FloorDivide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fact1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fact2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;Py_DECREF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nk&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;Py_DECREF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;zero&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;Py_DECREF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fact1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;Py_DECREF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fact2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You might wonder why this code uses a placeholder value and does not use the available in-place arithmetic functions. This is because Python integers are immutable objects. Using &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;PyNumber_InPlaceMultiply&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;PyNumber_InPlaceSubtract&lt;/span&gt;&lt;/tt&gt; actually produces the same results as the above.&lt;/p&gt;
&lt;p&gt;Using immutable types has performance consequences. In calculating 50,000 choose 50, for instance, this code will instantiate and destroy about 199,850 objects inside the main loop of the factorial function (given that instances of the integers from -1 to 100 are kept in a static table and not instantiated dynamically). More on performance later.&lt;/p&gt;
&lt;p&gt;The following Pyrex source code and distutils configuration wrap the C code as a Python function called choose().&lt;/p&gt;
&lt;div class="code-header"&gt;choose.pyx&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="text"&gt;cdef extern from &amp;quot;choose1.c&amp;quot;:
    object _choose1 &amp;quot;choose1&amp;quot; (object n, object k)

def choose(n, k):
    return _choose1(n, k)
&lt;/code&gt;&lt;/pre&gt;&lt;div class="code-header"&gt;setup.py&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="python"&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;distutils.core&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;setup&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;distutils.extension&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Extension&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;Pyrex.Distutils&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;build_ext&lt;/span&gt;
&lt;span class="n"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;choose&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;ext_modules&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="n"&gt;Extension&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;choose&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;choose.pyx&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="n"&gt;cmdclass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;build_ext&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;build_ext&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Finally, the following code uses the library and will be the point of comparison to the pure Python version.&lt;/p&gt;
&lt;div class="code-header"&gt;testc.py&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="python"&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;__main__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;choose&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;choose&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="n"&gt;choose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;50000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class="section" id="performance-comparison"&gt;
&lt;h2&gt;Performance Comparison&lt;/h2&gt;
&lt;p&gt;There are numerous ways of comparing execution times. In this example, I'm using the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;time&lt;/span&gt;&lt;/tt&gt; utility rather than something in Python such as the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;timeit&lt;/span&gt;&lt;/tt&gt; module.&lt;/p&gt;
&lt;div class="code-header"&gt;time python testpure.py&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="text"&gt;284958500315333290867708487072990268397101930544468658
476216100935982755508148971449700622210078705183923286
686402942943816349032142836981589618876226813174803825
580124000

real    0m14.921s
user    0m9.211s
sys     0m4.151s
&lt;/code&gt;&lt;/pre&gt;&lt;div class="code-header"&gt;time python testc.py&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="text"&gt;284958500315333290867708487072990268397101930544468658
476216100935982755508148971449700622210078705183923286
686402942943816349032142836981589618876226813174803825
580124000

real    0m14.460s
user    0m9.146s
sys     0m4.000s
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;What a surprise! Accounting for normal variations in performance based on background CPU load, the two versions take effectively the same amount of time. It looks as though the overhead in the pure Python version is not, as you might assume, in the fact that Python is interpreted rather than compiled. By using Python objects and memory management, the C version runs just as slowly.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="replacing-python-bignums-with-gmp"&gt;
&lt;h2&gt;Replacing Python bignums with GMP&lt;/h2&gt;
&lt;p&gt;The &lt;a class="reference external" href="http://gmplib.org/"&gt;GNU Multiple Precision Arithmetic Library&lt;/a&gt; (GMP) is a bignum library that supports integers, fractions, and floating-point numbers, and is usable from C or C++.&lt;/p&gt;
&lt;p&gt;GMP allows true in-place arithmetic operations, which might make it faster than the Python version, which allocates and deallocates objects on every iteration of the main loop. It's also very easy to mix GMP's bignums and unsigned longs, as well as producing string representations of GMP bignums in different bases.&lt;/p&gt;
&lt;p&gt;Be sure to specify your host platform when building GMP. The first time I built it, I relied on &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;configure&lt;/span&gt;&lt;/tt&gt; to auto-detect everything, and it built a 64-bit library for my definitively 32-bit computer. Read the section in the manual on &lt;a class="reference external" href="http://gmplib.org/manual/Build-Options.html#Build-Options"&gt;GMP Build Options&lt;/a&gt; to figure out the right &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;host&lt;/span&gt;&lt;/tt&gt; argument to &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;configure&lt;/span&gt;&lt;/tt&gt;; for instance, mine on a 32-bit OS X system was &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;--host=i386-apple-darwin&lt;/span&gt;&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;The following code listings implement the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;factorial&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;choose&lt;/span&gt;&lt;/tt&gt; functions using GMP and update the Pyrex wrapper. While I won't try to duplicate the &lt;a class="reference external" href="http://gmplib.org/manual/"&gt;GMP Manual&lt;/a&gt; here, it's worth noting that its functions tend to operate on arguments rather than returning values, and variables need to be initialized and cleaned up.&lt;/p&gt;
&lt;p&gt;The prefixes used are &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;mi&lt;/span&gt;&lt;/tt&gt; for GMP multi-precision integer (distinct from the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;mpz&lt;/span&gt;&lt;/tt&gt; prefix used by the library), &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;pi&lt;/span&gt;&lt;/tt&gt; for Python integer, and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;ps&lt;/span&gt;&lt;/tt&gt; for Python string.&lt;/p&gt;
&lt;div class="code-header"&gt;choose2.c&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="c"&gt;&lt;span class="cp"&gt;#include &amp;lt;Python.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#include &amp;lt;gmp.h&amp;gt;&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;mi_factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mpz_t&lt;/span&gt; &lt;span class="n"&gt;mi_result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;stop&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;mpz_set_ui&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mi_result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n1&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;stop&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;mpz_mul_ui&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mi_result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mi_result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n1&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;PyObject&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;choose2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;mpz_t&lt;/span&gt; &lt;span class="n"&gt;mi_fact1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;mpz_t&lt;/span&gt; &lt;span class="n"&gt;mi_fact2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;mpz_t&lt;/span&gt; &lt;span class="n"&gt;mi_result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;PyObject&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;ps_result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;PyObject&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;pi_result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;mpz_init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mi_fact1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;mpz_init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mi_fact2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;mpz_init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mi_result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;mi_factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mi_fact1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;mi_factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mi_fact2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;mpz_cdiv_q&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mi_result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mi_fact1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mi_fact2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mpz_sizeinbase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mi_result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;mpz_get_str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mi_result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;ps_result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PyString_FromString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;pi_result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PyNumber_Long&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ps_result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;mpz_clear&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mi_fact1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;mpz_clear&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mi_fact2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;mpz_clear&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mi_result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;Py_DECREF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ps_result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;pi_result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;div class="code-header"&gt;choose.pyx (revised)&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="text"&gt;cdef extern from &amp;quot;choose1.c&amp;quot;:
    object _choose1 &amp;quot;choose1&amp;quot; (object n, object k)

cdef extern from &amp;quot;choose2.c&amp;quot;:
    object _choose2 &amp;quot;choose2&amp;quot; (unsigned long n, unsigned long k)

def choose(n, k):
    return _choose1(n, k)

def gmpchoose(n, k):
    return _choose2(n, k)
&lt;/code&gt;&lt;/pre&gt;&lt;div class="code-header"&gt;setup.py (revised)&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="python"&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;distutils.core&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;setup&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;distutils.extension&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Extension&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;Pyrex.Distutils&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;build_ext&lt;/span&gt;
&lt;span class="n"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;choose&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;ext_modules&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="n"&gt;Extension&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;choose&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;choose.pyx&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;libraries&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;gmp&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="n"&gt;cmdclass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;build_ext&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;build_ext&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;div class="code-header"&gt;testgmp.py&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="python"&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;__main__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;choose&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;gmpchoose&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="n"&gt;gmpchoose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;50000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And, finally, the result we've been waiting for:&lt;/p&gt;
&lt;div class="code-header"&gt;time python testgmp.py&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="text"&gt;284958500315333290867708487072990268397101930544468658
476216100935982755508148971449700622210078705183923286
686402942943816349032142836981589618876226813174803825
580124000

real    0m2.042s
user    0m1.993s
sys     0m0.033s
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Using the GMP library has cut execution time on this hardware down to around 2 seconds, from around 13-15 seconds.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="conclusion"&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;When implementing C extensions to speed up Python programs, there are more considerations than just &amp;quot;switch to compiled language, see speed increase.&amp;quot; Python provides many data types and structures that must be duplicated in the C program, and using the Python API heavily may lead to a slight or nonexistent gain in performance. Meanwhile, using other libraries may require interconversion (e.g. here, the GMP multi-precision integer is converted to a Python integer by way of a decimal string representation).&lt;/p&gt;
&lt;p&gt;For purely numeric functions that deal only in values that fit within standard C types, it's relatively easy to create large speedups. Once other types, such as strings and bignums, become involved, the work starts to build up. However, the reward can still be substantial: here, the final C extension using GMP resulted in an 86% reduction in execution time. The results above do not mean that GMP is more performant than Python bignums in general, but the difference in performance in this case is obvious.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
</description><guid>http://countergram.com/c-extension-n-choose-k</guid><pubDate>Sat, 18 Apr 2009 00:00:00 EST</pubDate></item><item><title>YouTube video embedding in reStructuredText</title><link>http://countergram.com/youtube-in-rst</link><description>&lt;div class="document"&gt;
&lt;p&gt;The reStructuredText markup language is used in a variety of ways, including as a documentation format for Python applications and a general markup language for web content, in the same vein as Textile or Markdown. It's also extensible in Python: by adding new functions as &amp;quot;directives&amp;quot; to the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;doctools&lt;/span&gt;&lt;/tt&gt; module, you can implement new commands. In this article, I'll show you how to implement a &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;youtube&lt;/span&gt;&lt;/tt&gt; directive so that a line like the following in your reStructured Text file will become an embedded YouTube video when it's rendered to HTML:&lt;/p&gt;
&lt;pre&gt;&lt;code class="highlight" lang="text"&gt;.. youtube:: &amp;lt;video-id&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The outline of what we're going to do is simple: create a &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;youtube&lt;/span&gt;&lt;/tt&gt; function that returns a document node list, then register this function with &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;docutils&lt;/span&gt;&lt;/tt&gt;' &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;directives&lt;/span&gt;&lt;/tt&gt; list. (By the way, I'm using the XHTML-valid approach that uses only an &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;object&lt;/span&gt;&lt;/tt&gt; element, not &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;embed&lt;/span&gt;&lt;/tt&gt;.) The specifics are a little more involved. The following code listing shows how I did it:&lt;/p&gt;
&lt;div class="code-header"&gt;youtube.py&lt;/div&gt;&lt;pre&gt;&lt;code class="highlight" lang="python"&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;docutils&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;nodes&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;docutils.parsers.rst&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;directives&lt;/span&gt;

&lt;span class="n"&gt;CODE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="s"&gt;&amp;lt;object type=&amp;quot;application/x-shockwave-flash&amp;quot;&lt;/span&gt;
&lt;span class="s"&gt;        width=&amp;quot;&lt;/span&gt;&lt;span class="si"&gt;%(width)s&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="s"&gt;        height=&amp;quot;&lt;/span&gt;&lt;span class="si"&gt;%(height)s&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="s"&gt;        class=&amp;quot;youtube-embed&amp;quot;&lt;/span&gt;
&lt;span class="s"&gt;        data=&amp;quot;http://www.youtube.com/v/&lt;/span&gt;&lt;span class="si"&gt;%(yid)s&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&amp;gt;&lt;/span&gt;
&lt;span class="s"&gt;    &amp;lt;param name=&amp;quot;movie&amp;quot; value=&amp;quot;http://www.youtube.com/v/&lt;/span&gt;&lt;span class="si"&gt;%(yid)s&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;span class="s"&gt;    &amp;lt;param name=&amp;quot;wmode&amp;quot; value=&amp;quot;transparent&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;%(extra)s&lt;/span&gt;&lt;span class="s"&gt;&lt;/span&gt;
&lt;span class="s"&gt;&amp;lt;/object&amp;gt;&lt;/span&gt;
&lt;span class="s"&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;

&lt;span class="n"&gt;PARAM&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;    &amp;lt;param name=&amp;quot;&lt;/span&gt;&lt;span class="si"&gt;%s&lt;/span&gt;&lt;span class="s"&gt;&amp;quot; value=&amp;quot;&lt;/span&gt;&lt;span class="si"&gt;%s&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;youtube&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lineno&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;contentOffset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;blockText&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stateMachine&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sd"&gt;&amp;quot;&amp;quot;&amp;quot; Restructured text extension for inserting youtube embedded videos &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mf"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="n"&gt;string_vars&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s"&gt;&amp;#39;yid&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="s"&gt;&amp;#39;width&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;425&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;&amp;#39;height&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;344&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;&amp;#39;extra&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;&amp;#39;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;extra_args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:]&lt;/span&gt; &lt;span class="c"&gt;# Because content[0] is ID&lt;/span&gt;
    &lt;span class="n"&gt;extra_args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;ea&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;=&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;ea&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;extra_args&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c"&gt;# key=value&lt;/span&gt;
    &lt;span class="n"&gt;extra_args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;ea&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;ea&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;extra_args&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ea&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mf"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c"&gt;# drop bad lines&lt;/span&gt;
    &lt;span class="n"&gt;extra_args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;extra_args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;width&amp;#39;&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;extra_args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;string_vars&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;width&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;extra_args&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;width&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;height&amp;#39;&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;extra_args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;string_vars&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;height&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;extra_args&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;height&amp;#39;&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;extra_args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;PARAM&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;extra_args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;extra_args&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;string_vars&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;extra&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;nodes&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;CODE&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string_vars&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;html&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;span class="n"&gt;youtube&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
&lt;span class="n"&gt;directives&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;register_directive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;youtube&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;youtube&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Much of this code is unnecessary if you only want to implement the single-line version of the command I used above. But this code also allows you to customize the width and height and add other &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;param&lt;/span&gt;&lt;/tt&gt; elements:&lt;/p&gt;
&lt;pre&gt;&lt;code class="highlight" lang="text"&gt;.. youtube:: asdfjkl
    width=600
    height=400
    someOtherParam=value
&lt;/code&gt;&lt;/pre&gt;&lt;div class="section" id="quick-code-walk-through"&gt;
&lt;h2&gt;Quick code walk-through&lt;/h2&gt;
&lt;ol class="arabic simple"&gt;
&lt;li&gt;The argument signature of the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;youtube&lt;/span&gt;&lt;/tt&gt; function is the same as you would use for any directive of this type (i.e. a block directive, as opposed to something inserted into running text.)&lt;/li&gt;
&lt;li&gt;The only argument actually used here is &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;content&lt;/span&gt;&lt;/tt&gt;, which would contain everything from &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;asdfjkl&lt;/span&gt;&lt;/tt&gt; through &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;400&lt;/span&gt;&lt;/tt&gt; in the above example.&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;content&lt;/span&gt;&lt;/tt&gt; is a list of lines.&lt;/li&gt;
&lt;li&gt;The code involving &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;extra_args&lt;/span&gt;&lt;/tt&gt; simply splits each line after the first on the equals sign and makes a dictionary out of the (key, value) pairs, treating width and height specially.&lt;/li&gt;
&lt;li&gt;The return value is a list of nodes. The &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;nodes.raw&lt;/span&gt;&lt;/tt&gt; function used to create the (single) node here takes three parameters: the raw source for the element, which is not really used in this case and can be left blank; the string to output; and the format, which in this case is HTML.&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;div class="section" id="how-to-render"&gt;
&lt;h2&gt;How to render&lt;/h2&gt;
&lt;p&gt;If you're using reStructured Text seriously on your web site, your content management system probably already has a renderer for it. But just in case, here's how to render it to HTML manually:&lt;/p&gt;
&lt;pre&gt;&lt;code class="highlight" lang="python"&gt;&lt;span class="n"&gt;source&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="s"&gt;This is some text.&lt;/span&gt;

&lt;span class="s"&gt;.. youtube:: 2A2XBoxtcUA&lt;/span&gt;

&lt;span class="s"&gt;This is some more text.&lt;/span&gt;
&lt;span class="s"&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;docutils.core&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;publish_parts&lt;/span&gt;

&lt;span class="n"&gt;doc_parts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;publish_parts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;settings_overrides&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;output_encoding&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;utf8&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="s"&gt;&amp;#39;initial_header_level&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;2&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;writer_name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;html&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="n"&gt;doc_parts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;html_body&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;initial_header_level&lt;/span&gt;&lt;/tt&gt; override is useful if you want your section headings to start as &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;h2&lt;/span&gt;&lt;/tt&gt; elements, rather than &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;h1&lt;/span&gt;&lt;/tt&gt; elements. On this site, I store the title attribute of each article separately from the body, and render it as a &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;h1&lt;/span&gt;&lt;/tt&gt; element separately. The reStructuredText documents begin with body text. If you place the title of your documents in reStructuredText, you will still need to use the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;initial_header_level&lt;/span&gt;&lt;/tt&gt; setting, because the default &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;doctools&lt;/span&gt;&lt;/tt&gt; behavior is to use &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;h1&lt;/span&gt;&lt;/tt&gt; elements for both the title and the top-level section headings.&lt;/p&gt;
&lt;p&gt;And that's it. Now embedding videos is a one-line statement, and you can make other helpful extensions using the same technique.&lt;/p&gt;
&lt;p&gt;Addendum: The Python source code in this article is under the terms of the &lt;a class="reference external" href="http://www.opensource.org/licenses/mit-license.php"&gt;MIT License&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
</description><guid>http://countergram.com/youtube-in-rst</guid><pubDate>Thu, 19 Mar 2009 00:00:00 EST</pubDate></item><item><title>WSGI decorators considered inconvenient</title><link>http://countergram.com/wsgi-decorators</link><description>&lt;div class="document"&gt;
&lt;p&gt;In the grand tradition of &amp;quot;considered harmful&amp;quot; essays, here's one on why it's not a great idea to use Python function decorators to wrap WSGI applications (e.g. for authentication/authorization). But unlike using &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;goto&lt;/span&gt;&lt;/tt&gt; to implement control structures, the mixture of decorators and WSGI is just inconvenient. In this essay, I'll explore the problem, show how it applies more generally to function decorators that work with arguments, and suggest a hackish solution if you don't want to avoid WSGI decorators altogether.&lt;/p&gt;
&lt;p&gt;WSGI applications are functions (well, callables) that expect two arguments &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;(environ,&lt;/span&gt; &lt;span class="pre"&gt;start_response)&lt;/span&gt;&lt;/tt&gt;: a dictionary of CGI-style and extension variables and a callback function. Pop quiz: in what position in a function's argument list is the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;environ&lt;/span&gt;&lt;/tt&gt; dict?&lt;/p&gt;
&lt;p&gt;The correct answer is &amp;quot;maybe 0 and maybe 1,&amp;quot; and this is the start of the problems with WSGI decorators. Specifically, when you're calling a WSGI function from normal code, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;environ&lt;/span&gt;&lt;/tt&gt; is in position 0; and when you're implementing a WSGI callable as an ordinary function, it's in position 0; but when you're implementing a WSGI callable as a method of a class, it's in position 1 because there's a &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;self&lt;/span&gt;&lt;/tt&gt; argument before it.&lt;/p&gt;
&lt;p&gt;When you wrap a bound method in another function, as is commonly done with WSGI middleware, there's no problem. Bound methods carry their &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;self&lt;/span&gt;&lt;/tt&gt; arguments with them, in their &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;im_self&lt;/span&gt;&lt;/tt&gt; attribute. The argument signature looks the same from the outside.&lt;/p&gt;
&lt;p&gt;In a decorator, though, you do have to worry about the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;self&lt;/span&gt;&lt;/tt&gt; argument. When you apply a decorator to a function inside a class, the decorator receives a reference to a function that has not become a method yet. In fact, only the final return value of the last decorator on the function will be &amp;quot;blessed&amp;quot; into an unbound method of the class, after the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;class&lt;/span&gt;&lt;/tt&gt; block closes. From the decorator you can't tell whether the function is destined to be a method or not.&lt;/p&gt;
&lt;p&gt;What this means is that, if your decorator expects a particular argument signature such as &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;(environ,&lt;/span&gt; &lt;span class="pre"&gt;start_response)&lt;/span&gt;&lt;/tt&gt;, whenever the decorator is applied to a method, the arguments will be shifted over one place. And you don't know that this has happened unless you (1) check the length of the argument list, assuming there are no optional arguments, or (2) check the types of the arguments.&lt;/p&gt;
&lt;p&gt;Our method-safe WSGI decorator might look like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class="highlight" lang="python"&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;functools&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;wsgi_decorator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nd"&gt;@functools&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wraps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;function&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;wrapper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mf"&gt;3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;start_response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:]&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;start_response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;
        &lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;mykey&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;my value&amp;quot;&lt;/span&gt; &lt;span class="c"&gt;# Do stuff&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;wrapper&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;By checking the length of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;*args&lt;/span&gt;&lt;/tt&gt;, we sidestep the problem of the potential &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;self&lt;/span&gt;&lt;/tt&gt; argument. You can see that the above decorator works through the following example:&lt;/p&gt;
&lt;pre&gt;&lt;code class="highlight" lang="python"&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;webtest&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;webob&lt;/span&gt;

&lt;span class="nd"&gt;@wsgi_decorator&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;app&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;start_response&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;webob&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;mykey&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;])(&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;start_response&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;C&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;object&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nd"&gt;@wsgi_decorator&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;app&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="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;start_response&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;webob&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;mykey&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;])(&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;start_response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="n"&gt;webtest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TestApp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;/&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="n"&gt;webtest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TestApp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;C&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;/&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This approach can be simplified with a simple utility function:&lt;/p&gt;
&lt;pre&gt;&lt;code class="highlight" lang="python"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;wsgi_args&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mf"&gt;3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mf"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mf"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The original function, however, must still be called with &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;*args&lt;/span&gt;&lt;/tt&gt;, not &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;environ,&lt;/span&gt; &lt;span class="pre"&gt;start_response&lt;/span&gt;&lt;/tt&gt;, because it will expect a &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;self&lt;/span&gt;&lt;/tt&gt; argument if it's a method.&lt;/p&gt;
&lt;p&gt;If you use decorators that ship with third-party WSGI modules, they might work with only functions, only modules, or both. Sometimes the only way to be sure is to check the source code.&lt;/p&gt;
&lt;p&gt;And unfortunately, other cases are not as simple as this. The example above only works because WSGI applications are expected to have exactly two regular arguments. If the expected argument signature involves optional arguments, it gets trickier. It may be a better idea to avoid such decorators.&lt;/p&gt;
&lt;/div&gt;
</description><guid>http://countergram.com/wsgi-decorators</guid><pubDate>Tue, 03 Mar 2009 00:00:00 EST</pubDate></item><item><title>Add bound methods to instantiated objects in Python</title><link>http://countergram.com/adding-bound-methods</link><description>&lt;div class="document"&gt;
&lt;p&gt;In Python, as in other dynamic languages, you can add new methods to a class after it's been defined. To do this in Python, you define a function that takes &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;self&lt;/span&gt;&lt;/tt&gt; as its first parameter, then assign that function to an attribute of the class:&lt;/p&gt;
&lt;pre&gt;&lt;code class="highlight" lang="python"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;C&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;object&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt; &lt;span class="c"&gt;# No methods... yet&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;foo&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="k"&gt;print&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;

&lt;span class="n"&gt;C&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;foo&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;When executing the final line, Python automagically wraps the function &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;foo&lt;/span&gt;&lt;/tt&gt; in an unbound method called &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;foo&lt;/span&gt;&lt;/tt&gt;, and assigns the unbound method to &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;C.foo&lt;/span&gt;&lt;/tt&gt;. (You need to be aware of this if you ever try to assign a function to an attribute of a class and want to use it as just a function: in that case, you would need to put it inside a data structure instead or use &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;C.foo.im_func&lt;/span&gt;&lt;/tt&gt; to get at the original function.)&lt;/p&gt;
&lt;p&gt;This principle is not at work when you are dealing with an instance of a class. If you assign a function to an attribute of an instance, it will not be converted into anything. The creation of a bound method must be done explicitly, like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class="highlight" lang="python"&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;types&lt;/span&gt;
&lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;C&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;types&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MethodType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__class__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In this example, invoking &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;MethodType&lt;/span&gt;&lt;/tt&gt; creates a new bound method object. Its parameters are a function, the instance to which the method should be bound, and the class (the last parameter could simply be &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;C&lt;/span&gt;&lt;/tt&gt;, but using &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;instance.__class__&lt;/span&gt;&lt;/tt&gt; allows you to use this approach when you don't have a direct reference to the class.)&lt;/p&gt;
&lt;/div&gt;
</description><guid>http://countergram.com/adding-bound-methods</guid><pubDate>Sun, 01 Mar 2009 00:00:00 EST</pubDate></item><item><title>Basic Formencode usage pattern</title><link>http://countergram.com/basic-formencode</link><description>&lt;div class="document"&gt;
&lt;p&gt;Here's a useful Python snippet that shows the basics of using &lt;a class="reference external" href="http://www.formencode.org/"&gt;Formencode&lt;/a&gt; to validate form submissions inside a typical web-application controller. There's a reusable pattern here that isn't totally clear from the official documentation. This is non-framework code. First, here's the setup (the definition part):&lt;/p&gt;
&lt;pre&gt;&lt;code class="highlight" lang="python"&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;formencode&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;validators&lt;/span&gt;

&lt;span class="c"&gt;# Declarative style for schemas&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyForm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;validators&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;not_empty&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;validators&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="n"&gt;resolve_domain&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;month&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;validators&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;not_empty&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;allow_extra_fields&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;

&lt;span class="c"&gt;# Adding to schemas after the fact&lt;/span&gt;
&lt;span class="n"&gt;MyForm&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;address&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;validators&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;not_empty&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now, here's what goes in your request handler, adapted as necessary to the particularities of your web application framework or architecture (i.e. the arguments passed to the request handler and the way of getting at POST arguments will be different depending on how your application is set up):&lt;/p&gt;
&lt;pre&gt;&lt;code class="highlight" lang="python"&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;formencode&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Invalid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;htmlfill&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;your_request_handler&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;errors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="c"&gt;# Put data access common to GET/POST requests here&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;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;POST&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;(&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;POST&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MyForm&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_python&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="c"&gt;# Put use of the data here&lt;/span&gt;
            &lt;span class="c"&gt;# Put a redirect to a GET-based confirmation page here&lt;/span&gt;
        &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;Invalid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;errors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;unpack_errors&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt; &lt;span class="c"&gt;# Put code for GET requests here&lt;/span&gt;
    &lt;span class="n"&gt;html&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;your_template&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="n"&gt;htmlfill&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The control flow works like this. If there's no post data, then &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;data&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;errors&lt;/span&gt;&lt;/tt&gt; will both be empty, and your template will be rendered with an empty form. If there's post data but it's in error, the errors will be displayed and the posted data will be pre-filled into the form for the user. And if the data validates, you should return a redirect to a GET-based page after data processing is complete.&lt;/p&gt;
&lt;/div&gt;
</description><guid>http://countergram.com/basic-formencode</guid><pubDate>Fri, 20 Feb 2009 00:00:00 EST</pubDate></item><item><title>Install Python Imaging Library (PIL) on Intel Mac</title><link>http://countergram.com/install-pil-intel-mac</link><description>&lt;div class="document"&gt;
&lt;p&gt;Recently I found the &lt;a class="reference external" href="http://www.pythonware.com/products/pil/"&gt;Python Imaging Library&lt;/a&gt; (PIL) would not install properly on my Intel Mac. The error message when trying to install PIL was:&lt;/p&gt;
&lt;pre&gt;&lt;code class="highlight" lang="text"&gt;ld: in /Developer/SDKs/MacOSX10.4u.sdk/usr/local/lib/libxml2.2.dylib,
file is not of required architecture for architecture ppc
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;It turned out that &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;gcc&lt;/span&gt;&lt;/tt&gt; was being called as &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;gcc&lt;/span&gt; &lt;span class="pre"&gt;-arch&lt;/span&gt; &lt;span class="pre"&gt;ppc&lt;/span&gt; &lt;span class="pre"&gt;-arch&lt;/span&gt; &lt;span class="pre"&gt;i386&lt;/span&gt;&lt;/tt&gt;, which is used to build a universal binary on OS X. Since the installed &lt;cite&gt;libxml2&lt;/cite&gt; was Intel-only, and &lt;cite&gt;setuptools&lt;/cite&gt; did not detect this, the PIL installation failed.&lt;/p&gt;
&lt;p&gt;Force &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;gcc&lt;/span&gt;&lt;/tt&gt; to build only an Intel binary when you install PIL, rather than a universal binary, by setting an environment variable:&lt;/p&gt;
&lt;pre&gt;&lt;code class="highlight" lang="text"&gt;sudo env ARCHFLAGS=&amp;quot;-arch i386&amp;quot; python setup.py install
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Since universal binaries are not needed on Intel Macs unless you are preparing them for distribution, this lets you install PIL normally.&lt;/p&gt;
&lt;/div&gt;
</description><guid>http://countergram.com/install-pil-intel-mac</guid><pubDate>Sun, 15 Feb 2009 00:00:00 EST</pubDate></item></channel></rss>
