<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" xml:lang="en-US">
<title>Planet PJE</title>
<subtitle>All the PJE that's fit to blog</subtitle>
<link href="http://dirtsimple.org/index.html" type="text/html" rel="alternate" />

<updated>2011-11-22T05:05:23+0000</updated>
<id>tag:blogger.com,1999:blog-8674831</id>
<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/pje" /><feedburner:info uri="pje" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry>
    <title type="text">WSGI, Web Frameworks, and Requests: Explicit or Implicit?</title>
    <author>
        <name>PJE</name>
    </author>
    <link href="http://feedproxy.google.com/~r/pje/~3/SfhO-at8RDY/wsgi-web-frameworks-and-requests.html" type="text/html" rel="alternate" />
    <link href="http://www.blogger.com/comment.g?blogID=8674831&amp;postID=9217096005178432709" type="text/html" rel="comments" />
    <id>tag:blogger.com,1999:blog-8674831.post-9217096005178432709</id>
    
    <published>2011-08-02T14:47:38-0400</published>
    <updated>2011-08-02T17:46:52-0400</updated>
    <content xml:base="http://dirtsimple.org/2011/08/wsgi-web-frameworks-and-requests.html" type="html">&lt;p&gt;In Python web programming and frameworks, there is a constant juggling act that takes place between "explicit" and "implicit".&amp;nbsp;&amp;nbsp;Too explicit, and the code may get too verbose or unwieldy.&amp;nbsp; Too implicit, and the code may lose clarity or maintainability.&lt;/p&gt;
&lt;p&gt;And nowhere can this tension be more clearly seen, than in the area of "request" objects.&amp;nbsp; After all, nearly every web programming framework has some notion of a "request" at its core: usually some sort of object with an API.&lt;/p&gt;
&lt;p&gt;Now, as you may recall from my &lt;a href="http://dirtsimple.org/2011/08/is-wsgi-lite-library-or-protocol-and.html"&gt;previous article&lt;/a&gt;, the Web-SIG originally set out in 2003 to standardize a &lt;strong&gt;universal&lt;/strong&gt; "request" API for Python, but I diverted this effort towards a different sort of request API -- the WSGI "environ" object.&lt;/p&gt;
&lt;p&gt;Where web framework request APIs usually emphasize methods and properties, the WSGI "environ" object is just a big bag of data.&amp;nbsp; It doesn't &lt;em&gt;have&lt;/em&gt; any operations or properties.&lt;/p&gt;
&lt;p&gt;But the upside to this downside, is that the enviornment is &lt;em&gt;extensible&lt;/em&gt;, in a way that a request object is not.&amp;nbsp; You can add whatever you want to it, and you can call functions on it to do things that a request object would do with methods.&amp;nbsp; (Yay, freedom!)&lt;/p&gt;
&lt;p&gt;But the &lt;em&gt;new&lt;/em&gt; downside to that upside, is that if you want to use library functions on the environ instead of framework "request" object methods, you now have to pass the environ back into the library functions!&amp;nbsp; (Boo, hiss.)&lt;/p&gt;&lt;h4&gt;Binding To The Environment&lt;/h4&gt;
&lt;p&gt;So, WSGI-era web libraries (like WebOb and Werkzeug) tend to define their next-generation "request" objects as wrappers bound to the environ.&amp;nbsp; As Ian Bicking put it:&lt;/p&gt;
&lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt;
&lt;p&gt;"Everything WebOb does is basically functions on the environ"&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Of course, this isn't the &lt;em&gt;only&lt;/em&gt; strategy for managing request information.&amp;nbsp; Some web app frameworks dodge the argument-passing issue by using thread locals, or worse yet, global variables.&amp;nbsp; But they're &lt;em&gt;still&lt;/em&gt; trying to solve the same problem: connecting actions that a web application needs to perform, with some notion of the "current request".&lt;/p&gt;
&lt;p&gt;And in both cases, a key driver for the API design is brevity and ease-of-use (implicit) vs. clarity and consistency (explicit).&lt;/p&gt;
&lt;p&gt;On the explicit side, It's annoying to be constantly saying "foo = bar(environ, etc)", if only because it somehow looks less Pythonic than "foo = request.bar(etc)".&lt;/p&gt;
&lt;p&gt;So in effect, what we want in our frameworks is a way to (implicitly)&amp;nbsp;&lt;em&gt;bind&lt;/em&gt; operations to the "request", so that it isn't necessary to explicitly spell out the connection in every line of code.&amp;nbsp; (Even if we're still &lt;em&gt;explicitly&lt;/em&gt; referencing the request object.)&lt;/p&gt;
&lt;p&gt;In fact, we don't even want to have to include boilerplate like 'request = Request(environ)' at the top of our apps' code, and so we'd much rather have this binding take place &lt;strong&gt;outside&lt;/strong&gt; our code entirely.&lt;/p&gt;
&lt;p&gt;Now, &lt;em&gt;this&lt;/em&gt; is where things get really interesting!&amp;nbsp; In order to get rid of this boilerplate, web libraries and frameworks will usually do one of two things.&amp;nbsp; Either:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;They provide a decorator to change the calling signature while keeping external WSGI compliance (like WebOb), or
&lt;/li&gt;
&lt;li&gt;They ditch WSGI entirely and use a different calling signature
&amp;nbsp;(like Django)
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;And in either case, we're now more or less back where we started, pre-WSGI, as you are now writing code with a calling signature that's implicitly coupled to a specific library or framework.&lt;/p&gt;
&lt;p&gt;Sure, you get certain benefits in exchange for making this commitment, and you're less tightly coupled to libraries using option 1.&amp;nbsp; But it's still a pretty exclusive commitment.&amp;nbsp; If you want to use code from more than one library, you're going to have to write the boilerplate for each of them, except for whichever one you choose to be your "primary" - the main one that calls you and/or decorates your code.&lt;/p&gt;&lt;h4&gt;The Original Goal Of WSGI&lt;/h4&gt;
&lt;p&gt;Now, the original idea for WSGI (well, &lt;em&gt;my&lt;/em&gt; original idea, anyway) was that by letting "request" objects wrap the environ, and using "functions on the environ", we could get &lt;em&gt;out&lt;/em&gt; of this situation.&amp;nbsp; As I wrote in the original &lt;a href="http://www.python.org/dev/peps/pep-0333/#rationale-and-goals"&gt;PEP 333 rationale section&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt;
&lt;p&gt;"If middleware can be both simple and robust, and WSGI is widely available in servers and frameworks, it allows for the possibility of an entirely new kind of Python web application framework: one consisting of loosely-coupled WSGI middleware components.&lt;/p&gt;
&lt;p&gt;"Indeed, existing framework authors may even choose to refactor their frameworks' existing services to be provided in this way, becoming more like libraries used with WSGI, and less like monolithic frameworks. This would then allow application developers to choose "best-of-breed" components for specific functionality, rather than having to commit to all the pros and cons of a single framework."&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;But what I didn't understand then, was just how &lt;em&gt;annoying&lt;/em&gt; it is to have to explicitly pass the environ into every library function you wanted to use!&lt;/p&gt;
&lt;p&gt;(Actually, it's not just that it's annoying from a number-of-keystrokes point of view, it's also more foreign to a Python programmer's sensibilities.&amp;nbsp; We don't usually mind &lt;em&gt;receiving&lt;/em&gt; an explicit "self", but for some reason, we seem to hate &lt;strong&gt;sending&lt;/strong&gt; one!)&lt;/p&gt;
&lt;p&gt;And that (in a somewhat roundabout way) is how I ended up adding the experimental "binding" protocol to &lt;a href="http://dirtsimple.org/2011/08/is-wsgi-lite-library-or-protocol-and.html"&gt;WSGI Lite&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Specifically, what the binding protocol provides, is a way to &lt;em&gt;generically&lt;/em&gt; bind things to the environ dictionary, and pass them into your application's calling signature, while retaining WSGI compliance for any code that calls your function.&lt;/p&gt;
&lt;p&gt;In other words, the binding protocol is a way to make it so that you can use as many libraries, functions, or objects for your request as you want, without needing to pass an 'environ' parameter to them over and over.&lt;/p&gt;
&lt;p&gt;Now, in the simplest case, you can just use the binding protocol as a generic way to obtain any given library's request objects.&amp;nbsp; You can say, "my 'request' parameter maps to a WebOb request", for example.&lt;/p&gt;
&lt;p&gt;But the really &lt;em&gt;interesting&lt;/em&gt; cases come about, when you stop thinking in terms of "request" objects, and start thinking about what your application reallly &lt;em&gt;does&lt;/em&gt;.&lt;/p&gt;
&lt;h4&gt;The Meaning of "Lite"&lt;/h4&gt;
&lt;p&gt;For example, why not bind a session object to your function's 'session' argument?&amp;nbsp; Or maybe what you &lt;em&gt;really&lt;/em&gt; want is to just receive an authenticated user object in your 'user' parameter, and a cart object in your 'cart' parameter, instead of first getting a session, just so you can&amp;nbsp;&lt;em&gt;get&lt;/em&gt; to the user and cart.&lt;/p&gt;
&lt;p&gt;In other words, what if you made your &lt;strong&gt;application goals&lt;/strong&gt; more explicit?&lt;/p&gt;
&lt;p&gt;Now currently, getting access to such application-specific objects requires either painfully-verbose boilerplate off of a raw WSGI environment, or an increasingly tight coupling to an increasingly monolithic framework that does more of the work for you.&lt;/p&gt;
&lt;p&gt;But, with the Lite binding protocol, you can now represent &lt;em&gt;anything&lt;/em&gt; that's tied to "the current request", just by creating a callable object that takes an environment parameter.&lt;/p&gt;
&lt;p&gt;Which means you don't really need "request" objects any more in your main code, because you can simply arrange to be &lt;em&gt;called with whatever objects you need&lt;/em&gt;, to do the thing you're actually &lt;strong&gt;doing&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;And so your application code stops being about manipulating "web stuff", to focus more on whatever it is that your app actually &lt;em&gt;does&lt;/em&gt;...&amp;nbsp; while still being just a WSGI app from the point of view of its caller.&lt;/p&gt;
&lt;p&gt;(This by the way, is part of why I dubbed the concept "WSGI Lite", despite the fact that it adds new protocols to WSGI: it effectively lets you take most of the "WSGI" out of "WSGI applications".)
&lt;/p&gt;
&lt;h4&gt;The Great "Apps vs. Controllers" Debate&lt;/h4&gt;
&lt;p&gt;Now, if you look at how &lt;em&gt;non-&lt;/em&gt;WSGI-centric, "full-stack" frameworks (like Django, TurboGears, etc.) operate, they often have things they call "controllers": functions with more specialized signatures for doing this kind of "more app, less web" kind of stuff.&amp;nbsp; However, these frameworks tend to end up being very un-WSGI internally, because plain WSGI doesn't handle this sort of thing very well.&lt;/p&gt;
&lt;p&gt;However, with the WSGI Lite binding protocol, you can write controllers with whatever signature you like, while remaining "WSGI all the way down".&amp;nbsp; Anything you want as an argument, you can just create a binding rule for, which can be as simple as a string (to pull out an environ key) a short function that computes a value, or a tiny classmethod that returns an object wrapping the environ.&lt;/p&gt;
&lt;p&gt;And, if it's a callable (like a function or a method), it &lt;em&gt;too&lt;/em&gt; can use the binding protocol, and ask for its arguments to be calculated from the request.&lt;/p&gt;
&lt;p&gt;And that means that you can take, say, a generic binding rule that fetches a parsed form, and use it to write an application-specific binding rule that looks up something in a database.&lt;/p&gt;
&lt;p&gt;At which point, you can now write a controller that uses that binding rule to get something it needs as an argument.&lt;/p&gt;
&lt;h4&gt;Where All This Is Going&lt;/h4&gt;
&lt;p&gt;Now, if you look at where all this is going, you'll see that you're going to end up with a very small application body: just the code that actually &lt;em&gt;does&lt;/em&gt; things with the information that came in, and decides what to send back out.&lt;/p&gt;
&lt;p&gt;Something, in fact, that looks very much like a "controller" would in a non-WSGI, full-stack web framework...&amp;nbsp; yet isn't locked in to one particular full stack framework.&lt;/p&gt;
&lt;p&gt;Now, I don't know how clear any of the above was without code examples.&amp;nbsp; (Probably not very.)&amp;nbsp; But the endgame that I'm trying to describe, is a future in which both "full stack" and "WSGI-centric" frameworks use a &lt;em&gt;common protocol&lt;/em&gt; to provide their features to applications.&lt;/p&gt;
&lt;p&gt;And, more importantly, a future where &lt;strong&gt;full-stack features do not require &lt;em&gt;learning&lt;/em&gt;&amp;nbsp;a full stack framework&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;And where &lt;strong&gt;every application is its own framework&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;In effect, the binding protocol is a tool that allows every app to define its own embedded DSL: the set of high-level data objects and operations that it needs in order to do whatever it does.&lt;/p&gt;
&lt;p&gt;And these high-level, application-specific objects and operations are composed of lower-level, domain-generic objects and operations (such as form parsers and validators, URL parameter extractors, session and cookie managers, etc.), obtained from libraries or frameworks.&lt;/p&gt;
&lt;p&gt;And all of these objects are passed around via the environment and binding rules, while retaining WSGI Lite calling signatures...&amp;nbsp; making the entire thing "WSGI all the way down".&lt;/p&gt;
&lt;p&gt;And yet, the &lt;em&gt;code&lt;/em&gt; contained in those applications would not look like "WSGI" as we know it today.&amp;nbsp; For example:&lt;/p&gt;
&lt;pre&gt;
@lite(
    user = myapp.authorized_user,
    output = myapp.format_chooser,
)
def todo_list(environ, user, output):
    return output(user.todo_items())
&lt;/pre&gt;
&lt;p&gt;Or, perhaps the Python 3 version would look like this:&lt;/p&gt;
&lt;pre&gt;
@lite
def todo_list(
        environ,
        user:   myapp.authorized_user,
        output: myapp.format_chooser
    ):
    return output(user.todo_items())
&lt;/pre&gt;&lt;p&gt;Neither of these looks anything like "WSGI" code as we know it today - it's more like a full-stack framework's code.  But, where the bindings in a full-stack framework are &lt;em&gt;implicit&lt;/em&gt; (like automatically formatting the output with a template or turning it into JSON), all of the bindings here are &lt;strong&gt;explicit&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;   And not only is explicit better than implicit, but...&lt;/p&gt;
&lt;h4&gt;Readability Counts!&lt;/h4&gt;
&lt;p&gt;You can see right away, for example, that this app is using some sort of chooser to render the output in some request-determined format, and you can track down the relevant code, without having to first learn all of the implicit knowledge of a particular framework's construction.&lt;/p&gt;
&lt;p&gt;And, the &lt;em&gt;point&lt;/em&gt; of this app function is immediately obvious - it displays a user's todo list.  (Something that would otherwise be hidden under a pile of web I/O code, if this were written to plain WSGI or with a WSGI-centric library or framework.)&lt;/p&gt;
&lt;p&gt;And what this means is, if this approach becomes a focal point for Python web development, then being a Python web programmer would not be a matter of being a "Django developer" or "TurboGears developer" or "Pyramid Developer" or any &lt;em&gt;other&lt;/em&gt; sort of developer...&lt;/p&gt;
&lt;p&gt;Other than a &lt;em&gt;Python&lt;/em&gt; developer.&lt;/p&gt;
&lt;p&gt;Because any Python developer could pick this up, without having to have all the implicit, framework-specific knowledge already in their head.&lt;/p&gt;
&lt;p&gt;And hopefully, this will help get us to a situation  where, instead of people saying, "you should use Python for your web app because framework X is great"...&lt;/p&gt;
&lt;p&gt;People will say, "you should use Python for your web app because it lets you focus on what your application is really doing, and no matter what libraries you use, your code will be readable and maintainable, even by people who haven't used those libraries."&lt;/p&gt;
&lt;p&gt;Or maybe just, "you should use Python for your web app because &lt;em&gt;it's a great language for web development&lt;/em&gt;!"&lt;/p&gt;&lt;h4&gt;Plumbing The Pipe Dream&lt;/h4&gt;
&lt;p&gt;Now, is all that just a pipe dream?&lt;/p&gt;
&lt;p&gt;  Maybe so.  After all, there are still a &lt;strong&gt;lot&lt;/strong&gt; of hurdles between here and there!&lt;/p&gt;
&lt;p&gt;(For starters, I think that the actual binding protocol probably still needs some work!)&lt;/p&gt;
&lt;p&gt;But if you want to make a "pipe" dream real, you've got to start with the requirements for the &lt;strong&gt;plumbing&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;So right now, I'm collecting use cases from frameworks as I encounter them, to see what services the popular frameworks provide, and how they could be expressed as bindings.&lt;/p&gt;
&lt;p&gt;But I'm &lt;em&gt;also&lt;/em&gt; really interested in the problems that such frameworks have, in terms of how they currently communicate state, configuration, and other information to user code.  Are there any open issues the binding protocol could solve now, or could solve with some additions?&lt;/p&gt;
&lt;p&gt;Because that's what's really going to make the difference to adoption here. The authors of established libraries and frameworks aren't going to change things just beacuse I said this is a neat idea!&lt;/p&gt;
&lt;p&gt;But if we can make the protocol &lt;em&gt;solve&lt;/em&gt; some existing problems -- like helping to get rid of thread-local objects, for example -- then folks have another reason to get on board with a common protocol, besides it &lt;em&gt;being&lt;/em&gt; a common protocol.&lt;/p&gt;
&lt;p&gt;So, that's the interesting question that lies ahead:&lt;/p&gt;
&lt;p&gt;Do you have any warts in your current app, library, or framework that this might help you solve?  Or a feature you think it could help you add?&lt;/p&gt;
&lt;p&gt;Leave me a comment here, or drop me an email via the Web-SIG!&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/pje?a=SfhO-at8RDY:4x3w3-fOwC8:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/pje?a=SfhO-at8RDY:4x3w3-fOwC8:UT3xtbGYFzA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=UT3xtbGYFzA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/pje?a=SfhO-at8RDY:4x3w3-fOwC8:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
<feedburner:origLink>http://dirtsimple.org/2011/08/wsgi-web-frameworks-and-requests.html</feedburner:origLink></entry><entry>
    <title type="text">Is WSGI Lite a Library or a Protocol? (And Why You Should Care)</title>
    <author>
        <name>PJE</name>
    </author>
    <link href="http://feedproxy.google.com/~r/pje/~3/-xrv6pYqpIw/is-wsgi-lite-library-or-protocol-and.html" type="text/html" rel="alternate" />
    <link href="http://www.blogger.com/comment.g?blogID=8674831&amp;postID=3584372398539341275" type="text/html" rel="comments" />
    <id>tag:blogger.com,1999:blog-8674831.post-3584372398539341275</id>
    
    <published>2011-08-01T10:44:10-0400</published>
    <updated>2011-08-01T15:57:57-0400</updated>
    <content xml:base="http://dirtsimple.org/2011/08/is-wsgi-lite-library-or-protocol-and.html" type="html">&lt;p&gt;In retrospect, my article yesterday about WSGI Lite made a rather glaring mistake: instead of carefully laying out the background rationale and explaining where WSGI Lite fits in to today's Python world, I threw a bunch of links at people and went "Whee!&amp;nbsp; It's neat!"&lt;/p&gt;
&lt;p&gt;So, in hindsight, I should've expected reactions like "huh?"&amp;nbsp; "wha?" and "don't we already have WebOb and Werkzeug?"&lt;/p&gt;
&lt;p&gt;My bad, guys.&amp;nbsp; I totally failed to highlight the really crucial point about WSGI Lite, and that is the distinction between "wsgi_lite" (the proof-of-concept/future reference library) and "WSGI Lite" (the PEPpable protocol).&lt;/p&gt;
&lt;p&gt;See, in my mind, "wsgi_lite" the library is no more a competitor to WebOb and Werkzeug than the standard library's "wsgiref" package is a competitor to mod_wsgi: just because it has a server in there, doesn't mean it competes with servers!&lt;/p&gt;
&lt;p&gt;I think it's a pretty safe bet to say that most WSGI (protocol) code does not use wsgiref (library), except maybe indirectly via something else.&amp;nbsp; And the same thing may well end up being true of wsgi_lite (the library) and WSGI Lite (the protocol).&lt;/p&gt;
&lt;p&gt;Yeah, it's a little confusing.&amp;nbsp; I get that now.&amp;nbsp; When I was first writing the code, I called it "WSGI 2", and the decorators were "@wsgi2"&amp;nbsp;&amp;nbsp;and "@wsgi1", instead of "@lite" and "lighten()".&amp;nbsp; I was even having the decorators change the "wsgi.version" environment key from (1,0) to (2,0) and back.&lt;/p&gt;
&lt;p&gt;However, as the work progressed, the versioning didn't make a lot of sense to me, because in a sense, the core bits of the protocol weren't changing.&amp;nbsp; Instead, there were a handful of small protocols that, put together, make a new way of doing WSGI.&amp;nbsp; So I ended up deciding to call it "WSGI Lite", and dropped the version fudging.&lt;/p&gt;
&lt;p&gt;But if you look at what is happening with the actual underlying protocol, I really am proposing something like a WSGI 2 here, or probably more like a 1.1.&amp;nbsp; (Sort of.)&amp;nbsp; The key point is that it's a protocol that can work in &lt;em&gt;today's&lt;/em&gt; WSGI stacks, without needing a massive rewrite effort.&lt;/p&gt;
&lt;p&gt;Granted, this means that if you have some pet gripes with WSGI, then Lite may or may not be able to solve them.&amp;nbsp; A couple people have approached me privately about those issues, and I'd like to start hashing them out on the Web-SIG shortly.&lt;/p&gt;
&lt;p&gt;But in the meantime, I'd like to take the rest of this article to lay out just what (and &lt;strong&gt;why&lt;/strong&gt;) WSGI Lite, &lt;em&gt;the protocol&lt;/em&gt;, is.&amp;nbsp; (As opposed to wsgi_lite, the proof-of-concept implementation of the protocol.)&lt;/p&gt;&lt;h4&gt;Why A New Protocol?&lt;/h4&gt;
&lt;p&gt;Because WSGI &lt;em&gt;rots your brain&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Or, to put it less dramatically, it is damn near impossible to write correct WSGI middleware because there are too darn many things to think about.&lt;/p&gt;
&lt;p&gt;In the Reddit thread about Armin's article, one person posted a bunch of links to the various patches they had to do to a piece of WSGI code in order to make it work correctly with various corner cases in the protocol, as bugs cropped up in interaction with other WSGI code.&lt;/p&gt;
&lt;p&gt;And I took one quick look at one of those patches, and saw that it &lt;em&gt;still had bugs&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Granted, it was a resource-leak bug, but that's not the point.&amp;nbsp; It shouldn't be so frickin' easy to make that kind of mistake.&amp;nbsp; (And the author was not exactly a newbie to either WSGI or web programming.)&lt;/p&gt;
&lt;p&gt;And as I started writing my proof-of-concept (for what I originally thought of as "WSGI 2" rather than "WSGI Lite"), I discovered all kinds of &lt;strong&gt;other&lt;/strong&gt; mistakes that people could make in their middleware, that had never even &lt;em&gt;occurred&lt;/em&gt; to me before.&lt;/p&gt;
&lt;p&gt;Even Ian Bicking, author of WebOb, realized after reading the WSGI Lite docs that WebOb contained a latent bug I described there!&lt;/p&gt;
&lt;p&gt;So, something &lt;em&gt;has&lt;/em&gt; to be done.&amp;nbsp; WebOb and Werkzeug are great libraries, but if libraries could solve the problem, it would already be solved.&amp;nbsp; That's why wsgi_lite (the &lt;strong&gt;library&lt;/strong&gt;), is really just a test bed for WSGI Lite, (the &lt;strong&gt;protocol&lt;/strong&gt;).&lt;/p&gt;
&lt;p&gt;And the aim of WSGI Lite is not to solve all WSGI 1 problems, nor even the entire subset of WSGI 1 problems that can be addressed in a reasonably performant, backwards-compatible way using a pair of decorators.&lt;/p&gt;
&lt;p&gt;Rather, the aim is to eliminate certain key &lt;strong&gt;obstacles&lt;/strong&gt; to solving those problems.&lt;/p&gt;&lt;h4&gt;Protocols, WSGI, and Game Theory&lt;/h4&gt;
&lt;p&gt;Back when I first proposed the idea that became WSGI (late 2003), the goal of the Web-SIG was to define standard "request" and "response" objects for the standard library.&lt;/p&gt;
&lt;p&gt;So my counter-proposal to instead define a &lt;em&gt;protocol&lt;/em&gt;, and not actually put any &lt;em&gt;code&lt;/em&gt; for the protocol into the standard library, may have seemed a bit loopy to some folks.&amp;nbsp; Perhaps a bit like, "let's solve this problem by &lt;em&gt;not&lt;/em&gt; solving this problem!"&lt;/p&gt;
&lt;p&gt;But the reason that I did it -- and the reason it ended up working so well that damn near every dynamic language ends up more-or-less cloning WSGI these days -- is because of &lt;strong&gt;game theory&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Essentially, there was never any serious chance that a bunch of web framework developers with investment in existing APIs were ever going to get together and agree on the One True Request and One True Response: there were just too many differences in fundamental approaches, and way too much opportunity for bikeshedding.&lt;/p&gt;
&lt;p&gt;In game theory terms, you could say there was no &lt;a href="http://en.wikipedia.org/wiki/Focal_point_%28game_theory%29"&gt;Schelling Point&lt;/a&gt;.&amp;nbsp; As Wikipedia puts it:&lt;/p&gt;
&lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt;
&lt;p&gt;Consider a simple example: two people unable to communicate with each other are each shown a panel of four squares and asked to select one; if and only if they both select the &lt;i&gt;same&lt;/i&gt; one, they will each receive a prize. Three of the squares are blue and one is red. Assuming they each know nothing about the other player, but that they each do want to win the prize, then they will, reasonably, &lt;i&gt;both&lt;/i&gt; choose the red square. Of course, the red square is not in a sense a &lt;i&gt;better&lt;/i&gt; square; they could win by both choosing any square.&amp;nbsp;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In other words, in trying to design the One True Request and One True Response, there was no single obvious "square" to choose: everything was up for grabs, so nobody could win the "prize" (i.e., the benefits of having a One True anything in common).&lt;/p&gt;
&lt;p&gt;So what I did with my WSGI proposal was deliberately &lt;em&gt;create&lt;/em&gt; a Schelling Point: a single red square in a board full of blues.&lt;/p&gt;
&lt;p&gt;And the &lt;em&gt;way&lt;/em&gt; that I did it, was to specifically remove &lt;strong&gt;any&lt;/strong&gt; semblance of an API that would make WSGI look like another blue square.&lt;/p&gt;
&lt;p&gt;Voila: the Web-SIG was able to shift from discussions about what color to paint the bikeshed, to substantive discussions about the guts of HTTP and what requirements we had for interfacing with it.&lt;/p&gt;
&lt;p&gt;Now, notice that I'm &lt;em&gt;not&lt;/em&gt; saying that I came up with WSGI by myself and I was a genius.&amp;nbsp; What I'm saying is, I gave the Web-SIG something to &lt;em&gt;collaborate&lt;/em&gt; on, instead of something to &lt;em&gt;compete&lt;/em&gt; over.&lt;/p&gt;
&lt;p&gt;Let me repeat that: something to &lt;strong&gt;collaborate on&lt;/strong&gt;, instead of something to &lt;strong&gt;compete over&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;I could not have written the WSGI PEP by myself: I didn't have nearly enough information.&amp;nbsp; But the Web-SIG, in &lt;em&gt;collaboration&lt;/em&gt; mode, could.&lt;/p&gt;
&lt;p&gt;So what does all this have to do with WSGI Lite?&lt;/p&gt;
&lt;p&gt;Well, once again, the idea is to create a collaborative Schelling Point: a &lt;em&gt;protocol&lt;/em&gt;, rather than an API.&amp;nbsp; Because, once again, no one can agree on The One True WSGI Wrapper, when all we have are competing implementations with distinct APIs.&lt;/p&gt;
&lt;p&gt;Granted, I may have shot myself in the foot this time, by starting with a proof-of-concept library rather than a PEP explaining the protocols!&lt;/p&gt;
&lt;p&gt;Unfortunately, due to the nature of the requirements, I couldn't be &lt;strong&gt;sure&lt;/strong&gt; the protocols would work without prototyping an implementation first, and still can't be sure the protocols &lt;em&gt;really&lt;/em&gt; work without some community testing.&amp;nbsp; (And the shape of the protocols themselves evolved &lt;em&gt;considerably&lt;/em&gt; over the last three days of implementing, documenting, realizing something sucked, then fixing it and trying again!)&lt;/p&gt;
&lt;p&gt;But what are these protocols exactly?&amp;nbsp; What do they do, and why are they important?&lt;/p&gt;&lt;h4&gt;The First Protocol: Calling Convention&lt;/h4&gt;
&lt;p&gt;The WSGI Lite protocol consists of a few basic elements working together:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A revised calling convention and return protocol
&lt;/li&gt;
&lt;li&gt;A server API extension for resource closing
&lt;/li&gt;
&lt;li&gt;An "argument binding" protocol
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The first of these things is something that's been proposed for a long time, and there seems to be fairly widespread consensus that a Rack-style calling convention is a good idea.&amp;nbsp; WebOb, for example, already has some APIs that work on that calling convention, and I've never heard anybody saying that calling convention was &lt;em&gt;bad&lt;/em&gt;, or that the current WSGI convention is &lt;em&gt;better&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;(Actually, the closest thing I've seen to somebody saying that, would be in the Hacker News thread about yesterday's article: somebody thought that WSGI Lite forces async code to use greenlets.&amp;nbsp; But that's a mistake, because WSGI Lite only requires greenlets or threads for code that uses write().&amp;nbsp; WSGI Lite response bodies can still be produced just as asynchronously as a standard WSGI response body can.)&lt;/p&gt;
&lt;p&gt;Anyway, so, the first protocol is well-known to WSGIans, and largely uncontroversial, hence the "uhh..&amp;nbsp; don't we already have that?" reaction from some quarters.&amp;nbsp; What's been lacking is a co-ordinated way to &lt;em&gt;move forward&lt;/em&gt; on that.&lt;/p&gt;
&lt;p&gt;To put it another way, since that protocol lacks any "official" status or name, it's not really possible to use it as a Schelling Point of co-ordination between users and library authors.&amp;nbsp; Ian can't point to WebOb and say, "WebOb lets you use the [thingy] protocol", instead, he has to say, "WebOb is cool, you should use it."&amp;nbsp; Meanwhile, Armin is over there on the other side of the room, saying, "Werkzeug is cool, you should use it" too.&lt;/p&gt;
&lt;p&gt;Meanwhile, the poor user is left in the middle of the room, scratching his or her head and going, "Uh, so what should I use now?", with respect to any "enhanced WSGI" APIs.&lt;/p&gt;
&lt;p&gt;So, as far as this first sub-protocol is concerned, the ultimate point of WSGI Lite is going to be to nail down and "bless" a detailed and &lt;strong&gt;specific&lt;/strong&gt; flavor of the calling protocol, to provide that co-ordination point for libraries to say what they offer to people, and for people to make choices about using them.&lt;/p&gt;
&lt;p&gt;I seriously doubt that this is a very controversial proposal.&amp;nbsp; After all, many people have said they want this calling protocol, and some leading WSGIans (hm, that term even has the word "Ian" in it!) have actually implemented more or less that protocol in their libraries.&lt;/p&gt;
&lt;p&gt;What's more, people have been asking &lt;em&gt;me&lt;/em&gt; to do something about getting this protocol "out there", reflecting their subconscious realization that a Schelling Point is indeed &lt;strong&gt;needed&lt;/strong&gt; to do this, and that &lt;em&gt;I'm&lt;/em&gt; the most obvious "red square" for co-ordination where WSGI is concerned.&lt;/p&gt;
&lt;p&gt;So be it.&lt;/p&gt;
&lt;p&gt;That's why Armin's article finally pushed me to actually &lt;strong&gt;implement&lt;/strong&gt; something...&amp;nbsp; and that's when I discovered the need for the &lt;em&gt;other&lt;/em&gt; two sub-protocols in WSGI Lite.&lt;/p&gt;
&lt;h4&gt;The Second Protocol: Resource Management&lt;/h4&gt;
&lt;p&gt;See, as I was writing the decorators (called @wsgi2 and @wsgi1 at the time), I quickly began to notice that the "close()" part of WSGI was even more of a problem than I previously thought.&lt;/p&gt;
&lt;p&gt;I won't go into detail here about the specific problems, or the protocol itself, as they're both laid out in the README file for the wsgi_lite library.&amp;nbsp; Suffice to say here that under plain WSGI 1, resource closing is fragile because any one piece of middleware can inadvertently break the close() chain.&amp;nbsp; This is likely more of a problem for WSGI code running on non-refcounting Pythons, but it can cause headaches even on CPython.&lt;/p&gt;
&lt;p&gt;So, in order to solve that problem, I created a new resource closing protocol that allows applications to close multiple resources and to bypass broken WSGI 1 middleware.&lt;/p&gt;
&lt;p&gt;This, I also expect to be a fairly uncontroversial protocol proposal.&amp;nbsp; The problem it addresses is not widely understood, nor is there a big popular push for it, but it's an annoying little problem that can bite you in the butt and make debugging difficult, especially on "alternative implementations" of Python.&lt;/p&gt;
&lt;p&gt;However, as I began trying to &lt;em&gt;use&lt;/em&gt; this new protocol, and writing the early documentation for it, I discovered even &lt;strong&gt;more&lt;/strong&gt; problems with WSGI!&lt;/p&gt;
&lt;p&gt;Specifically, I noticed that it was damn hard to document my new closing protocol in such a way that it could actually be used correctly without having to learn even &lt;strong&gt;more&lt;/strong&gt; arbitrary rules about what to call when and where to fetch it from.&lt;/p&gt;
&lt;p&gt;Indeed, I ended up with something that looked &lt;em&gt;just as hard to get right&lt;/em&gt;, as WSGI middleware was in the first place!&lt;/p&gt;
&lt;p&gt;And when I looked at it more closely, I saw two things that were going on.&lt;/p&gt;
&lt;p&gt;The first, was that most people don't realize when you pass a WSGI environment to a WSGI app, it's &lt;strong&gt;not yours any more&lt;/strong&gt;.&amp;nbsp; The application is allowed to clear it, put junk in it, or whatever.&amp;nbsp; So you absolutely &lt;strong&gt;cannot&lt;/strong&gt; use that environment dictionary once you pass it on.&lt;/p&gt;
&lt;p&gt;And this put the closing protocol in a bit of a bind, because the closing protocol needed to be &lt;em&gt;called late&lt;/em&gt; in an app or piece of middleware, but &lt;em&gt;retrieved early&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;So, if you wrote the &lt;em&gt;natural&lt;/em&gt; thing, the &lt;strong&gt;obvious&lt;/strong&gt; thing, and pulled the closing key out of the environment at the point nearest where you were going to &lt;em&gt;use&lt;/em&gt; it, then your code would have a latent bug in it.&lt;/p&gt;
&lt;p&gt;And that's just &lt;strong&gt;evil&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;This is the point at which I realized just how much &lt;strong&gt;brain rot&lt;/strong&gt; the bare WSGI protocol has in it: there are &lt;strong&gt;lots&lt;/strong&gt; of little things like this that will bite you in the butt, &lt;em&gt;punishing&lt;/em&gt; you for doing the simple, obvious, straightforward thing.&lt;/p&gt;
&lt;p&gt;And so that's when I realized that I needed...&lt;/p&gt;&lt;h4&gt;The Third Protocol: Argument/Extension Binding&lt;/h4&gt;
&lt;p&gt;See, the new resource closing protocol I came up with is not the only WSGI environment extension out there -- there are lots of others.&amp;nbsp; But they share a few potential issues in &lt;strong&gt;common&lt;/strong&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Being pulled out of the environ at a point where they're no longer valid,
&lt;/li&gt;
&lt;li&gt;Having to write boilerplate to check for their existence, and fall back to something else, and
&lt;/li&gt;
&lt;li&gt;Mutually-incompatible decorators provided by libraries to fix problems 1 and 2!
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;That is, even if a library provides decorator support for its particular WSGI extension, you generally can't use more than &lt;strong&gt;one&lt;/strong&gt; of them at a time.&lt;/p&gt;
&lt;p&gt;And so, the argument/extension binding protocol fixes this by providing an &lt;strong&gt;argument-level&amp;nbsp;&lt;/strong&gt;decoration protocol, to replace function-level decoration as the way to solve problems 1 and 2 in existing libraries.&lt;/p&gt;
&lt;p&gt;The idea here is that instead of trying to use a session decorator from library 1 and an authentication decorator from library 2, you can just use a single decorator with two keyword arguments.&lt;/p&gt;
&lt;p&gt;This idea evolved gradually, as I first wrote a "@with_closing" decorator specifically to address the resource closing issue, and then noticed what having lots of decorators like that would lead to.&amp;nbsp; (And, sure enough, existing WSGI library wrappers have mutually-incompatible decorators for these purposes.)&lt;/p&gt;
&lt;p&gt;Anyway, the argument binding protocol is basically a way to map keyword arguments to things that are derived in some way from the request environment.&amp;nbsp; It could be a parsed form, a session, an authenticated user, a cart...&amp;nbsp; you name it, you can have it.&lt;/p&gt;
&lt;p&gt;In other words, the idea is once again to have a Schelling Point where libraries can be used &lt;em&gt;collaboratively&lt;/em&gt;, instead of having to compete for users.&amp;nbsp; It also makes it easier for individual users to write one-offs for their particular application.&amp;nbsp; Writing a WSGI Lite argument binding is a few lines of code over top of whatever kind of request-based object(s) you have in your application, and you can then use them anywhere.&lt;/p&gt;
&lt;p&gt;Or...&amp;nbsp; and this is the bigger point: you can then &lt;strong&gt;split out&lt;/strong&gt; your nifty cart or session or whatever, and make it available to other users as a library, &lt;em&gt;without&lt;/em&gt; needing to know dip about decorators.&lt;/p&gt;
&lt;p&gt;And, again, it's a co-ordination point, because you can say, "Here's my new session library - and it supports WSGI Lite argument binding."&amp;nbsp; The binding protocol becomes something that libraries have in common, allowing users to focus on functionality instead of screwing around with which color bikeshed the decorator is.&lt;/p&gt;
&lt;p&gt;Now, if you're not clear on the technical bits of what I'm on about here, the argument binding protocol is explained on the wsgi_lite homepage.&amp;nbsp; The basic idea, though, is that you can call @lite(keyword1=binding_rule1, keyword2=binding_rule2...), and bind your function's keyword arguments to objects like sessions, requests, carts, users, and arbitrary WSGI extensions.&amp;nbsp; The binding rules can be strings, callables, or sequences of the above, and the first rule that yields a result from the environment gets passed in to your function as a keyword argument.&amp;nbsp; And if no rule for that keyword yields a result, the keyword doesn't get passed to your function.&lt;/p&gt;
&lt;p&gt;So, this allows you to use normal Python function argument defaults to fall back on if you don't get the object you're looking for, &lt;em&gt;and&lt;/em&gt; it allows you to get a standard Python error when one of your arguments goes missing: you don't need to write code to check for the argument and raise your own error when it's missing.&lt;/p&gt;
&lt;p&gt;Under Python 3, it might be that the decorator could just use argument annotations to do the same thing (instead of duplicating argument names in the decorator) but I haven't tried that yet.&lt;/p&gt;
&lt;p&gt;The point, though, is that by defining a binding &lt;strong&gt;protocol&lt;/strong&gt;, you can use it in lots of different ways.&amp;nbsp; Given the protocol I've specified, you could go out there right &lt;em&gt;now&lt;/em&gt; and write yourself a Python 3 decorator that looks for binding rules in argument annotations, and applies them according to the rules of the binding protocol.&amp;nbsp; And users of your decorator would immediately be able to use anybody else's session, request, cart, or whatever other WSGI Lite argument bindings were out there, in their Python 3 argument annotations.&lt;/p&gt;
&lt;p&gt;Likewise, you can, right now, write yourself a binding for your session, request, cart, or whatever objects, and be assured that people will be able to use them with any decorator (or other tool) that uses "WSGI Lite binding rules".&lt;/p&gt;
&lt;p&gt;Even in tools that haven't been &lt;em&gt;thought of&lt;/em&gt; yet, let alone implemented.&lt;/p&gt;
&lt;p&gt;And that's the power of a &lt;strong&gt;protocol&lt;/strong&gt;, versus a mere library.&lt;/p&gt;
&lt;p&gt;Now, all in all, the argument binding sub-protocol is perhaps the most potentially-controversial part of the WSGI Lite protocol suite.&amp;nbsp; It's totally new, and as far as I know, unprecedented in the WSGI world.&amp;nbsp; And if you're the developer of a heavyweight WSGI library or framework, it might not seem very important to you.&lt;/p&gt;
&lt;p&gt;However, the point of it isn't to re-solve a problem you've already solved for your own library, or to replace your API.&amp;nbsp; Rather, it's a way to allow people to make &lt;em&gt;smaller libraries&lt;/em&gt;, by 1) shrinking the unit of reuse to the argument, rather than the decorator, and 2) lowering the entry barrier for a library to be written, by removing the need for a big API or a complex decorator.&lt;/p&gt;
&lt;p&gt;And of course, it also lets you add binding rules on top of your existing big library, to offer users an enticement or "gateway drug" to using the rest of your library.&amp;nbsp; You can, in effect, begin advertising your library as a &lt;em&gt;catalog of bindings&lt;/em&gt;, rather than trying to get people to drink &lt;strong&gt;all&lt;/strong&gt; of your library's cool-aid at once.&lt;/p&gt;
&lt;h4&gt;So, Where Do We Go From Here?&lt;/h4&gt;
&lt;p&gt;Well, at this point, the protocols are out there, but they don't have any "official" standing, except for my attempt at declaring them "red squares".&amp;nbsp; That is, they're potential points of co-ordination, and they have my backing as a potential "way forward" for the next-generation of WSGI.&lt;/p&gt;
&lt;p&gt;But this doesn't mean they won't &lt;em&gt;change&lt;/em&gt; between now and any real "official" status (like a PEP).&lt;/p&gt;
&lt;p&gt;My original effort at WSGI -- originally called "WCI" -- was not very much like WSGI at all.&amp;nbsp; The fundamental idea in WCI and WSGI was the same, sure, but the final implementation was very different.&lt;/p&gt;
&lt;p&gt;And the same thing might happen with WSGI Lite, too.&lt;/p&gt;
&lt;p&gt;Indeed, I've already gotten emails from a couple of big WSGIans about potential changes to WSGI Lite to fix other problems...&amp;nbsp; and so some things may well happen there.&lt;/p&gt;
&lt;p&gt;Mostly, though, what I want to do with WSGI Lite is create protocols that allow lightweight, &lt;em&gt;collaborative&lt;/em&gt; solutions to those problems.&lt;/p&gt;
&lt;p&gt;For example, rather than trying to fix all of the warts in "wsgi.input" in the core Lite protocol, I'd rather see some proposals for &lt;strong&gt;bindings&lt;/strong&gt; that people can use to fix those problems.&lt;/p&gt;
&lt;p&gt;Instead of us trying, yet again, to create the One True Input Object!&lt;/p&gt;
&lt;p&gt;Now, is that really possible with wsgi.input, or any of the other warts that people would like to see fixed in the "next generation" of WSGI?&lt;/p&gt;
&lt;p&gt;I don't know.&lt;/p&gt;
&lt;p&gt;But I think it's worth a shot at &lt;em&gt;finding out&lt;/em&gt;.&amp;nbsp; And if there are some clear wins to be had by tweaking the three Lite sub-protocols, or adding some others to the mix, I'm &lt;strong&gt;all ears&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;These are things that need to be hashed out a bit before the protocols are PEPpable, and yes, perhaps a bit of API bikeshedding may be needed as well.&lt;/p&gt;
&lt;p&gt;And you know what?&lt;/p&gt;
&lt;p&gt;I'm kind of looking forward to it.&lt;/p&gt;
&lt;p&gt;See you on the Web-SIG!&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/pje?a=-xrv6pYqpIw:NbMfo6iN-_Y:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/pje?a=-xrv6pYqpIw:NbMfo6iN-_Y:UT3xtbGYFzA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=UT3xtbGYFzA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/pje?a=-xrv6pYqpIw:NbMfo6iN-_Y:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
<feedburner:origLink>http://dirtsimple.org/2011/08/is-wsgi-lite-library-or-protocol-and.html</feedburner:origLink></entry><entry>
    <title type="text">WSGI Is Dead: Long Live WSGI Lite!</title>
    <author>
        <name>PJE</name>
    </author>
    <link href="http://feedproxy.google.com/~r/pje/~3/6gjGndnMEn8/wsgi-is-dead-long-live-wsgi-lite.html" type="text/html" rel="alternate" />
    <link href="http://www.blogger.com/comment.g?blogID=8674831&amp;postID=6410011968893245369" type="text/html" rel="comments" />
    <id>tag:blogger.com,1999:blog-8674831.post-6410011968893245369</id>
    
    <published>2011-07-31T20:58:53-0400</published>
    <updated>2011-08-01T15:57:26-0400</updated>
    <content xml:base="http://dirtsimple.org/2011/07/wsgi-is-dead-long-live-wsgi-lite.html" type="html">&lt;p&gt;Almost a decade ago, back when I first proposed the idea of WSGI to the Web-SIG, I had a rather idealistic vision of how WSGI could be a kind of "framework dissolver".&amp;nbsp; I envisioned a future in which everything was pluggable, and there would no longer be any reason to have monolithic application frameworks, because everything could be done with libraries, middleware, and decorators.&lt;/p&gt;
&lt;p&gt;Alas, that idealistic future didn't come to pass.&amp;nbsp; In fact, as far back as 2007, I had already noticed it wasn't working, and &lt;a href="http://dirtsimple.org/2007/02/wsgi-middleware-considered-harmful.html"&gt;proposed an idea for a WSGI 2 protocol&lt;/a&gt; that would resolve the problems...&amp;nbsp; and then proceeded to do nothing for the next few years.&amp;nbsp; (Well, I've been doing &lt;em&gt;other&lt;/em&gt; things, like working on setuptools, Chandler, and my own business.&amp;nbsp;&amp;nbsp;I just wasn't working on &lt;em&gt;web apps&lt;/em&gt; or WSGI!)&lt;/p&gt;
&lt;p&gt;Anyway, last week, Armin Ronacher wrote a great article on his blog called &lt;a href="http://lucumr.pocoo.org/2011/7/27/the-pluggable-pipedream/"&gt;WSGI and the Pluggable Pipe Dream&lt;/a&gt;, about this very topic.&amp;nbsp; If you haven't read it, I urge you to do so, as it provides in-depth coverage of many of WSGI's dark corners and design decisions that are not widely understood by people who weren't involved in the original design, or who haven't spent a lot of time working with it.&lt;/p&gt;&lt;p&gt;But I was a little disappointed with the end of the article, because Armin's build-up led me to believe he had a solution to the problems of dealing with crud like start_response, write, close, and all that in WSGI middleware.&amp;nbsp; But really, his claim ended up being that even if somebody invented something &lt;em&gt;better&lt;/em&gt; than WSGI, there would be no way to &lt;strong&gt;replace&lt;/strong&gt; it, because of all the investment in the existing protocol.&lt;/p&gt;
&lt;p&gt;So, I decided to &lt;strong&gt;do&lt;/strong&gt; something about that.&lt;/p&gt;&lt;p&gt;&lt;img style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height="300" alt="CHALLENGE ACCEPTED!" src="http://dirtsimple.org/2011/07/9163302.jpg" width="400" border="0" /&gt;&lt;/p&gt;
&lt;p&gt;Introducing &lt;a href="https://bitbucket.org/pje/wsgi_lite/"&gt;WSGI Lite&lt;/a&gt;, WSGI's new younger brother.&lt;/p&gt;
&lt;p&gt;WSGI Lite is a protocol that's basically the same thing as the "WSGI 2" calling convention I proposed four years ago, and pretty much the same as what other languages' versions of WSGI use.&amp;nbsp; There's no start_response, close, write, or exc_info to mess with, and I even threw in a &lt;em&gt;massively&lt;/em&gt; improved way to manage post-request resource release and cleanup operations.&lt;/p&gt;
&lt;p&gt;Now, if WSGI Lite were just a WSGI alternative, Armin's article would be right: nobody would use it, because it'd be in competition with WSGI, and we'd have to basically "&lt;strong&gt;Shut...&amp;nbsp; Down...&amp;nbsp; Everything&lt;/strong&gt;"&amp;nbsp; in order to replace it.&lt;/p&gt;
&lt;p&gt;But the WSGI Lite protocol is actually &lt;em&gt;backwards compatible&lt;/em&gt; with WSGI.&amp;nbsp; You can write code to the WSGI Lite API, and transparently interoperate with existing WSGI servers, apps, and middleware.&lt;/p&gt;
&lt;p&gt;Which means, you don't have to &lt;em&gt;replace&lt;/em&gt; anything; you can just start using it, wherever it's appropriate or useful to do so.&lt;/p&gt;
&lt;p&gt;All it takes, is two decorators: one to declare an app as being a "lite" app, and one to allow you to call standard WSGI apps using the "lite" calling protocol.&amp;nbsp; (And, as a special bonus, the decorator you use for new code can also automatically bind environment keys, session/request objects, or other cool things to your app or middleware's keyword arguments.&amp;nbsp; It's &lt;em&gt;tres chic&lt;/em&gt;.)&lt;/p&gt;
&lt;p&gt;I'm hoping that this will revitalize the "pluggable pipe dream", and make it a little less dream, a little more &lt;a href="http://svn.colorstudy.com/home/ianb/wsgi-tutorial/diagram-urldispatch.png"&gt;pipe&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;So&amp;nbsp;&lt;a href="http://pypi.python.org/pypi/wsgi_lite"&gt;try it out&lt;/a&gt;, and let me know what you think.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Update: on reflection, the above article is woefully inadequate to explain the actual rationale of the WSGI Lite protocol or its implementation, so I've written a&lt;/em&gt; &lt;a href="http://dirtsimple.org/2011/08/is-wsgi-lite-library-or-protocol-and.html"&gt;&lt;em&gt;follow-up piece&lt;/em&gt;&lt;/a&gt; &lt;em&gt;to cover that.&amp;nbsp; Check it out!&lt;/em&gt;&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/pje?a=6gjGndnMEn8:Knbpqla1sjc:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/pje?a=6gjGndnMEn8:Knbpqla1sjc:UT3xtbGYFzA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=UT3xtbGYFzA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/pje?a=6gjGndnMEn8:Knbpqla1sjc:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
<feedburner:origLink>http://dirtsimple.org/2011/07/wsgi-is-dead-long-live-wsgi-lite.html</feedburner:origLink></entry><entry>
    <title type="text">Building a Dream</title>
    <author>
        <name>PJE</name>
    </author>
    <link href="http://feedproxy.google.com/~r/pje/~3/BPEc46FktiU/building-dream.html" type="text/html" rel="alternate" />
    <link href="http://www.blogger.com/comment.g?blogID=8674831&amp;postID=6841252799885771557" type="text/html" rel="comments" />
    <id>tag:blogger.com,1999:blog-8674831.post-6841252799885771557</id>
    
    <published>2011-05-19T09:02:39-0400</published>
    <updated>2011-05-19T18:28:49-0400</updated>
    <content xml:base="http://dirtsimple.org/2011/05/building-dream.html" type="html">&lt;p&gt;Have you ever had something you wanted to achieve, that always seemed just out of reach?&lt;/p&gt;
&lt;p&gt;Every day, every month, every year, you think, "this is it, I'm finally going to do it..."&lt;/p&gt;
&lt;p&gt;And then you don't.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://dirtsimple.org/2011/05/building-dream.html#begin-post"&gt;Why is that?&lt;/a&gt;&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/pje?a=BPEc46FktiU:0ST5uhSP-B4:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/pje?a=BPEc46FktiU:0ST5uhSP-B4:UT3xtbGYFzA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=UT3xtbGYFzA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/pje?a=BPEc46FktiU:0ST5uhSP-B4:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
<feedburner:origLink>http://dirtsimple.org/2011/05/building-dream.html</feedburner:origLink></entry><entry>
    <title type="text">The simplest(?) way to do tree-based queries in SQL</title>
    <author>
        <name>PJE</name>
    </author>
    <link href="http://feedproxy.google.com/~r/pje/~3/M_LfPscU7gs/simplest-way-to-do-tree-based-queries.html" type="text/html" rel="alternate" />
    <link href="http://www.blogger.com/comment.g?blogID=8674831&amp;postID=4934975456648782670" type="text/html" rel="comments" />
    <id>tag:blogger.com,1999:blog-8674831.post-4934975456648782670</id>
    
    <published>2010-11-06T14:57:28-0400</published>
    <updated>2010-12-08T13:25:02-0500</updated>
    <content xml:base="http://dirtsimple.org/2010/11/simplest-way-to-do-tree-based-queries.html" type="html">&lt;p&gt;The other day I was looking around at "tree" model libraries for Django, and I noticed that there are an awful lot of algorithms people use to manage hierarchies in SQL out there, but the one that is simplest (at least in my view) seems to not be very widely known at all.&lt;/p&gt;
&lt;p&gt;There's the obvious way of simply giving records a "pointer" (foreign key) referring to their parent.&amp;nbsp; The upside is, it's easy and it's normalized: the data means exactly what it says and there's no duplication.&amp;nbsp; The downside is, you can't really retrieve an entire &lt;em&gt;tree&lt;/em&gt; from the database, without doing &lt;strong&gt;lots&lt;/strong&gt; of SQL queries.&amp;nbsp; Likewise if you need to be able to find all the children of X at any moment, or be able check within another query whether any of X's ancestors are Y.&amp;nbsp; It really doesn't work for that sort of thing at all.&lt;/p&gt;
&lt;p&gt;That's when people start coming up with all sorts of tricks, like storing a path string and using prefix searches (with BETWEEN or LIKE), or storing a beginning and end number (aka the &lt;a href="http://intelligent-enterprise.informationweek.com/001020/celko.jhtml"&gt;"nested sets" approach&lt;/a&gt;), or having a fixed maximum hierarchy depth and representing the materialized path using individual fields (used in Drupal's menu system).&lt;/p&gt;
&lt;p&gt;The advantage to these systems is that they don't take a lot of space, but the disadvantage is that nearly all the maintenance has to be done in the application -- often involving quite a few SQL queries!&amp;nbsp; So, in essence, these approaches trade lots of queries at retrieval time, for lots of queries at update and insert/delete time, and often some additional programming complexity.&lt;/p&gt;
&lt;p&gt;But I don't think I've ever seen an article on my &lt;em&gt;preferred&lt;/em&gt; way of managing hierarchies in SQL, and that's using what I call a &lt;strong&gt;closure table&lt;/strong&gt;.&lt;/p&gt;
&lt;h4&gt;Closure Tables&lt;/h4&gt;
&lt;p&gt;A closure table gives you the ability to do all the same sorts of "find me all children of X, to depth N" queries as any of the other methods, just by doing a join against the closure table.&lt;/p&gt;
&lt;p&gt;But the &lt;em&gt;killer&lt;/em&gt; feature of the closure table, is that to add or remove a parent-child relationship, you only need to run *one* SQL query -- a query so simple that even the least-powerful SQL databases can be configured to run it as a trigger on the main table!&lt;/p&gt;
&lt;p&gt;Let's take a look at how this works.&amp;nbsp; A closure table is simply a table that maintains the "transitive closure" of the parent-child relationships in the base table.&amp;nbsp; So, let's say you're modelling a directory structure, and you have a "directory" table, with a foreign key "parent_dir" pointing to each row's parent directory.&lt;/p&gt;
&lt;p&gt;With this structure, you can only query direct (depth 1) relationships, but by adding a "closure" table with fields for "parent", "child", and "depth", you can represent the hierarchy to whatever depth is present.&amp;nbsp; So, if directory C is a child of directory B, and directory B is a child of A, then the base table would look like this:&lt;/p&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;th&gt;id&lt;/th&gt;&lt;th&gt;parent_dir&lt;/th&gt;&lt;th&gt;name&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;A&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;B&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;C&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;And the closure table would look like this:&lt;/p&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;th&gt;parent&lt;/th&gt;&lt;th&gt;child&lt;/th&gt;&lt;th&gt;depth&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;In other words, the closure table says, "A is a child of itself at depth 0", "B is a child of A at depth 1", and "C is a child of A at depth 2".&amp;nbsp; The total number of records in the closure table is equal to the number of records in the base table, times the average depth of the tree(s) in the base table.&amp;nbsp; (That is, each base table row has a row in the closure table, plus one row for each of its parents.)&lt;/p&gt;&lt;h4&gt;Inserting Links&lt;/h4&gt;
&lt;p&gt;Now wait a minute, you may say.&amp;nbsp; This looks &lt;em&gt;way&lt;/em&gt; more complicated and tricky than a materialized path or even the nested sets thingy.&amp;nbsp;&amp;nbsp;I mean, how would you even &lt;em&gt;begin&lt;/em&gt; to write the code to maintain such a thing?&lt;/p&gt;
&lt;p&gt;Well, to make PARENT_ITEM a parent of CHILD_ITEM, the code looks like this:&lt;/p&gt;
&lt;pre&gt;
insert into closure(parent, child, depth)
select p.parent, c.child, p.depth+c.depth+1
  from closure p, closure c
 where p.child=PARENT_ITEM and c.parent=CHILD_ITEM
&lt;/pre&gt;
&lt;p&gt;In other words, it's something your average SQL database can do without breaking a sweat.  I mean, it's like baby SQL, for crying out loud.  Okay, so maybe &lt;em&gt;deliberately&lt;/em&gt; doing a &lt;a href="http://www.fluffycat.com/SQL/Cartesian-Joins/"&gt;cartesian product join&lt;/a&gt; isn't something you everyday, but every database can still &lt;strong&gt;do&lt;/strong&gt; it.&lt;/p&gt;
&lt;p&gt;And it's not even a particularly performance intensive query!  It basically just says, "make every parent of PARENT_ITEM (implicitly including itself) a parent of every child of CHILD_ITEM (again including itself), at the appropriate depth.&lt;/p&gt;
&lt;p&gt;The cost of doing this operation is directly proportional to the size of the subtree under CHILD_ITEM, so if you're adding a leaf node to the tree, this will simply insert parent entries for the new leaf, and nothing else.  If you're adding a new parent to an existing tree, on the other hand, then it will insert a new row for every node already in the subtree being "adopted".&lt;/p&gt;
&lt;p&gt;Of course, in other "materialized path" strategies, you're &lt;em&gt;still&lt;/em&gt; paying the same kinds of proportionate maintenance costs to update a deep tree, except that you're doing string manipulation or resetting a bunch of fields -- usually in code that then makes a bunch of SQL queries...  and gods help you if your tree is actually &lt;strong&gt;too big to fit in memory&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;(Even the nested sets model has similar update costs: you can certainly delegate most of the bulk operations to the database, but if you have a frequently-updated, very large tree, the nested sets approach will end up shuffling position pointers a &lt;em&gt;lot&lt;/em&gt; more frequently than the closure table will end up with new rows.)&lt;/p&gt;
&lt;p&gt;So how does this query actually work?  And how do you delete or update relationships?&lt;/p&gt;&lt;h4&gt;How This Works&lt;/h4&gt;
&lt;p&gt;Well, the real secret to making the closure table work is the rows with depth 0.  Without that little innovation, you'd actually have to do four separate inserts, or a giant union query  (yuck!)  to add everything you need for a new link.&lt;/p&gt;
&lt;p&gt;Let's start out with our tables in a "flat" structure, with nobody being a parent of anybody else, but with the (self,self,0) entries already in the closure table:&lt;/p&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;th&gt;id&lt;/th&gt;&lt;th&gt;parent_dir&lt;/th&gt;&lt;th&gt;name&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;A&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;B&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;C&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;th&gt;parent&lt;/th&gt;&lt;th&gt;child&lt;/th&gt;&lt;th&gt;depth&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Now, if we change the base table to make B into C's parent:&lt;/p&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;th&gt;id&lt;/th&gt;&lt;th&gt;parent_dir&lt;/th&gt;&lt;th&gt;name&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;A&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;B&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;C&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;And then run the SELECT part of the link-insertion query with a parent of 2 and a child of 3, we get:&lt;/p&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;th&gt;p.parent&lt;/th&gt;&lt;th&gt;c.child&lt;/th&gt;&lt;th&gt;p.depth+c.depth+1&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Resulting in a new closure table entry for the added link:&lt;/p&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;th&gt;parent&lt;/th&gt;&lt;th&gt;child&lt;/th&gt;&lt;th&gt;depth&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Okay, that seems easy enough.  But what if we now make B a child of A?&lt;/p&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;th&gt;id&lt;/th&gt;&lt;th&gt;parent_dir&lt;/th&gt;&lt;th&gt;name&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;A&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;B&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;C&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Now our SELECT (using PARENT_ITEM=1 and CHILD_ITEM=2) returns:&lt;/p&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;th&gt;p.parent&lt;/th&gt;&lt;th&gt;c.child&lt;/th&gt;&lt;th&gt;p.depth+c.depth+1&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;And our closure table now looks like this:&lt;/p&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;th&gt;parent&lt;/th&gt;&lt;th&gt;child&lt;/th&gt;&lt;th&gt;depth&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Voila!  Because the closure table contains correct information for every part of the tree that &lt;em&gt;already&lt;/em&gt; exists, our query can make use of that information to fill in the rest of the new information that &lt;em&gt;should&lt;/em&gt; exist.&lt;/p&gt;&lt;h4&gt;Removing Links&lt;/h4&gt;
&lt;p&gt;Removing a link works similarly, using a query like this:&lt;/p&gt;
&lt;pre&gt;
delete link
  from closure p, closure link, closure c
 where p.parent = link.parent and c.child = link.child
   and p.child=PARENT_ITEM    and c.parent=CHILD_ITEM
&lt;/pre&gt;
&lt;p&gt;This is the exact same query, just flipped so as to &lt;em&gt;delete&lt;/em&gt; the rows instead of inserting them.&lt;/p&gt;
&lt;p&gt;Now, imagine if you hooked these two queries up to triggers: you could simply do whatever operations you want on the base table, and let the database do the rest!&lt;/p&gt;
&lt;p&gt;Of course, you'd need a couple of extra actions when a row was inserted or deleted in the base table: at insert time, you need to insert the (self,self,0) entry in the closure table (followed by an optional link addition if the new row was inserted with a parent), and at deletion time, you need to delete both the self-link, and all the links that depend on it:&lt;/p&gt;
&lt;pre&gt;
delete link
  from closure p, closure link, closure c, closure to_delete
 where p.parent = link.parent      and c.child = link.child
   and p.child  = to_delete.parent and c.parent= to_delete.child
   and (to_delete.parent=DELETE_ITEM or to_delete.child=DELETE_ITEM)
   and to_delete.depth&amp;lt;2 
&lt;/pre&gt;
&lt;p&gt;Essentially, this is just a fancy way of doing the earlier link-removal operation for each of the target item's neighboring links -- i.e., ones with depth 0 or 1.  (The links with a greater depth are taken care of by the first few lines of the query.)&lt;/p&gt;
&lt;p&gt;So now, you have just a little bit of logic to do in three triggers for your base table, to get this set up, and presto!  No additional code needed.  Just work on the base table normally, and run any hiearchical queries through the closure table.  (And make sure you've got unique indexes on (parent, depth, child) and (child, parent, depth), so those queries can really &lt;em&gt;fly&lt;/em&gt;.)&lt;/p&gt;
&lt;h4&gt;So Why Aren't You Doing This Already?&lt;/h4&gt;
&lt;p&gt;Sure, I know, not every database supports triggers...  or do they?  Sure, maybe if you're on MySQL....  and it's still the 1990's!  &lt;em&gt;SQLite&lt;/em&gt; has triggers these days, for crying out loud.&lt;/p&gt;
&lt;p&gt;Your database library doesn't support them?  Okay, fine.  Write the equivalent of triggers in whatever your library &lt;em&gt;does&lt;/em&gt; support -- like a post-save signal in Django's case -- and enjoy.  (You can probably even set up your closure table as a many-to-many "through" table in Django's case, though I haven't actually tried that yet.)&lt;/p&gt;
&lt;p&gt;Really, there are only a few reasons not to use a closure table when hierarchical queries are required, and most of them boil down to, "I have a small tree (i.e.human-readable in its entirety) and I need to display the whole thing in hierarchical order using SORT BY".&lt;/p&gt;
&lt;p&gt;And even in the application I first used closure tables for (which had tens of thousands of items in various parent-child relationships), there were also some &lt;em&gt;smaller&lt;/em&gt; hierarchies that could've used a materialized path or nested sets approach, and if I were doing that application over, I'd have probably used them.&lt;/p&gt;
&lt;p&gt;But, if you have a huge set of hierarchical relationships (a large "forest" of large trees), and need to be able to query over the transitive closure (i.e., do "recursive" parent/child lookups from SQL), IMO a closure table is the only way to go.  (And if you're in an "enterprisey" environment where lots of programs touch the data, using triggers or stored procedures to maintain that closure table is a must!)&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/pje?a=M_LfPscU7gs:6-L8PGwEH5U:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/pje?a=M_LfPscU7gs:6-L8PGwEH5U:UT3xtbGYFzA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=UT3xtbGYFzA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/pje?a=M_LfPscU7gs:6-L8PGwEH5U:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
<feedburner:origLink>http://dirtsimple.org/2010/11/simplest-way-to-do-tree-based-queries.html</feedburner:origLink></entry><entry>
    <title type="text">Reclaiming This Space</title>
    <author>
        <name>PJE</name>
    </author>
    <link href="http://feedproxy.google.com/~r/pje/~3/uX3OzRncZb4/reclaiming-this-space.html" type="text/html" rel="alternate" />
    <link href="http://www.blogger.com/comment.g?blogID=8674831&amp;postID=1616757592967866193" type="text/html" rel="comments" />
    <id>tag:blogger.com,1999:blog-8674831.post-1616757592967866193</id>
    
    <published>2010-09-28T10:10:09-0400</published>
    <updated>2010-09-28T11:46:04-0400</updated>
    <content xml:base="http://dirtsimple.org/2010/09/reclaiming-this-space.html" type="html">&lt;p&gt;So, this is an article all about how, life got flipped turned upside down...&amp;nbsp; No, wait, nevermind.&amp;nbsp; Bad idea.&lt;/p&gt;
&lt;p&gt;Seriously though, I was about to try and put some preface onto this to take the sting off, so I can tell myself that people were warned and if they unsubscribe then that's their deal, not mine.&lt;/p&gt;
&lt;p&gt;So forget the preface, here's the deal: &lt;em&gt;I want my blog back&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Six years ago, I started this blog after being laid off and confronting a life-changing hurricane challenge.&amp;nbsp; Not to be a business, not to teach anybody anything, but just to &lt;strong&gt;say random stuff&lt;/strong&gt;.&amp;nbsp; Talk things out.&amp;nbsp; Explore new ideas, new lifeforms, and new ways of getting things done...&amp;nbsp; to boldly go where no split infinitives had gone before.&lt;/p&gt;
&lt;p&gt;And because nobody was reading the thing, and because it wasn't connected with a business, I felt free to rant about whatever I wanted, make stupid jokes about random stuff, and even &lt;a href="http://dirtsimple.org/2004/11/my-big-fat-obnoxious-parody.html"&gt;review funny reality shows&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;And most importantly, I didn't feel like something had to be &lt;strong&gt;awesome&lt;/strong&gt; before I posted it.&amp;nbsp; That I didn't need to have some kind of Important Point to make or Principle To Teach.&lt;/p&gt;
&lt;p&gt;Back then, this was just a place for me to type my thoughts into the computer, so I could see what I thought about something.&lt;/p&gt;
&lt;p&gt;Somewhere along the line though, I started getting &lt;strong&gt;popular&lt;/strong&gt;.&amp;nbsp; Or at least, people were paying &lt;em&gt;attention&lt;/em&gt; to me.&lt;/p&gt;
&lt;p&gt;And in retrospect, it's kind of surprising (and sad) how much I let that affect me.&amp;nbsp; Pretty soon I'd plugged that into half a dozen or so of my neuroses about Not Disappointing People, and needing to be Taken Seriously as an Important Person, and, well...&amp;nbsp; the actual &lt;em&gt;blogging&lt;/em&gt; part of things just kind of up and died.&lt;/p&gt;
&lt;p&gt;On top of that, I piled even more "shoulds" about how I should be doing marketing the way I learned in the classes I paid many thousands of dollars for, and trying to sell something in every post while being both Brief and Awesome at the same time.&lt;/p&gt;
&lt;p&gt;Which, rather than encouraging me to be either Brief or Awesome (let alone Selling), discouraged me from posting anything at all.&lt;/p&gt;
&lt;p&gt;Worse yet, it stopped me from even &lt;em&gt;writing things in the first place&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;When I started this, my blog was just how I thought things out, how I reflected on things.&amp;nbsp; So I didn't need a polished article to post: I just needed an idea (or a vague hint of one) to start typing with.&lt;/p&gt;
&lt;p&gt;But then later, with all my self-imposed demands, I'd actually interrogate myself into silence by wanting to &lt;em&gt;first&lt;/em&gt; know whether what I was writing was going to be an email to my list, a newsletter to my paying subscribers, a post for the blog, an article for one of my other websites, and what was I going to sell in it, and what was the main point going to be and....&lt;/p&gt;
&lt;p&gt;Enough!&lt;/p&gt;
&lt;p&gt;So these days, I've noticed that most of my best writing has been going into &lt;strong&gt;Mind Hackers' Guild&lt;/strong&gt; forum postings, and &lt;a href="http://lesswrong.com/user/pjeby"&gt;my rambling commentaries on LessWrong.com&lt;/a&gt;.&amp;nbsp; Because in neither of those places do I feel like I need to &lt;em&gt;already&lt;/em&gt; know where I'm going before I open up the window and Just Freaking &lt;em&gt;Type&lt;/em&gt; Something Already.&lt;/p&gt;
&lt;p&gt;Without having to &lt;em&gt;first&lt;/em&gt; make it into some sort of Life Changing Lesson Of Supreme Awesomeness.&lt;/p&gt;
&lt;p&gt;Because, you know, it's okay to just be &lt;em&gt;helpful&lt;/em&gt;.&amp;nbsp; Mildly amusing.&amp;nbsp; Or to even just be offering myself as a Minor Example Of What To Avoid.&lt;/p&gt;
&lt;p&gt;(Sorry about all the Capital Letter Phrases today; it seems to be a side-effect of reading lots of &lt;a href="http://www.fluentself.com/blog/"&gt;Fluent Self&lt;/a&gt; posts while I download and convert my Bloglines archives.)&lt;/p&gt;
&lt;p&gt;Sure, it's true that I still &lt;em&gt;want&lt;/em&gt; to be more than just slightly helpful or miildly amsuing.&amp;nbsp; I'm still totally into that whole&amp;nbsp;&lt;a href="http://dirtsimple.org/2010/02/ending-my-insight-addiction.html"&gt;insight thing&lt;/a&gt;, after all. Which is why this particular post has been trying to ramble sideways towards some sort of Actual Point, apart from just the bare facts of the situation, and my declared intent to reclaim this space.&lt;/p&gt;
&lt;p&gt;I wanted to also say something here about the specific neuroses I had, and how they made me not just want, but &lt;strong&gt;need&lt;/strong&gt; to be Serious and Important, not just here, but in my current work as a teacher of mind hacking things.&lt;/p&gt;
&lt;p&gt;How that need made me set ridiculously high goals for my work, to not only be 100% Right and True from a scientific standpoint, but to also have utterly perfect execution from a practical standpoint.&amp;nbsp; (Both of which &lt;em&gt;really&lt;/em&gt; meant, "good enough to not have anyone be able to criticize me, ever, without me having a good defense.")&lt;/p&gt;
&lt;p&gt;How that need made me believe I had to have the &lt;strong&gt;Ultimate Methods™&lt;/strong&gt;... &amp;nbsp;not only the perfect ways of changing minds and lives, but also the perfect ways of &lt;em&gt;teaching&lt;/em&gt; those perfect ways, with nothing less being suitable before I would allow myself to sell anything to anyone beyond the tiny circle of highly-motivated people who were willing to jump over all the arbitrary obstacles I put between them and the chance to give me money.&lt;/p&gt;
&lt;p&gt;Which of course, was all just bullshit.&lt;/p&gt;
&lt;p&gt;Because it not only kept my business in guilt-driven mediocrity, it also means that the stuff I &lt;em&gt;have&lt;/em&gt; developed isn't getting to a lot of people who need it.&lt;/p&gt;
&lt;p&gt;And every time I developed a newer technique that improved on earlier ones, I had to stop pushing or teaching the older (but usually easier-to-learn and easier-to-teach!) ones...&amp;nbsp; even though it's usually way &lt;em&gt;easier&lt;/em&gt; for people to learn the simple techniques first and then build up to the super-duper fix-everything ones.&lt;/p&gt;
&lt;p&gt;And then, after getting to a point late last year, where the super-duperest techniques are totally awesome and changing me and my wife and other people in ways I'd never dreamed of before, I just switched over to having to have the most perfect ways of describing, documenting, teaching, and promoting those techniques!&lt;/p&gt;
&lt;p&gt;But the hardest part of mindhacking is -- and perhaps always &lt;em&gt;will&lt;/em&gt; be -- seeing through your &lt;strong&gt;own&lt;/strong&gt; bullshit.&amp;nbsp; Seeing that what you're doing isn't really as necessary as you think it is.&amp;nbsp; That your so-called "musts" are in fact merely &lt;strong&gt;options&lt;/strong&gt;...&amp;nbsp; and piss-poor ones at that.&lt;/p&gt;
&lt;p&gt;And so, it doesn't matter if I &lt;em&gt;do&lt;/em&gt; end up creating the most marvelous methods of documenting and training the techniques themselves, because the hard part will &lt;em&gt;still&lt;/em&gt; be unique to the individual doing the learning.&lt;/p&gt;
&lt;p&gt;So, good enough is good enough.&lt;/p&gt;
&lt;p&gt;And that goes for this article, too, even though I'm still kinda feeling a little nagging pull inside, one that says, "But you haven't shared an Important Life Lesson, or explained a Powerful Principle Of Change yet!&amp;nbsp; You&amp;nbsp; haven't shown how you got rid of the neuroses, or explained how they arise...&amp;nbsp; you haven't..."&lt;/p&gt;
&lt;p&gt;Yeah, and I ain't gonna, either.&amp;nbsp; (At least, not in &lt;em&gt;this&lt;/em&gt; post.)&lt;/p&gt;
&lt;p&gt;Because good enough is good enough.&lt;/p&gt;
&lt;p&gt;And this is &lt;em&gt;my&lt;/em&gt; blog now.&lt;/p&gt;
&lt;p&gt;And it sure is nice to be back.&lt;/p&gt;
&lt;p&gt;Here's hoping you feel the same way.&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/pje?a=uX3OzRncZb4:ePvIPDkrIa8:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/pje?a=uX3OzRncZb4:ePvIPDkrIa8:UT3xtbGYFzA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=UT3xtbGYFzA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/pje?a=uX3OzRncZb4:ePvIPDkrIa8:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
<feedburner:origLink>http://dirtsimple.org/2010/09/reclaiming-this-space.html</feedburner:origLink></entry><entry>
    <title type="text">Simplifying prioritized methods in PEAK-Rules</title>
    <author>
        <name>PJE</name>
    </author>
    <link href="http://feedproxy.google.com/~r/pje/~3/vxLQs8m0vSI/simplifying-prioritized-methods-in-peak.html" type="text/html" rel="alternate" />
    <link href="http://www.blogger.com/comment.g?blogID=8674831&amp;postID=249148837079175194" type="text/html" rel="comments" />
    <id>tag:blogger.com,1999:blog-8674831.post-249148837079175194</id>
    
    <published>2010-08-10T17:39:14-0400</published>
    <updated>2010-08-10T21:02:44-0400</updated>
    <content xml:base="http://dirtsimple.org/2010/08/simplifying-prioritized-methods-in-peak.html" type="html">&lt;p&gt;Recently, I've been scouting around the web for examples of what people have been doing with &lt;a href="http://pypi.python.org/pypi/PEAK-Rules"&gt;PEAK-Rules&lt;/a&gt; (and the older RuleDispatch package) to get an idea of what else I should put in (if anything) before making the first official release.&lt;/p&gt;
&lt;p&gt;One of the interesting things I found was a package called &lt;a href="http://pypi.python.org/pypi/prioritized_methods"&gt;prioritized_methods&lt;/a&gt;, which advertises itself as a "prioritized" version of PEAK-Rules, and appears to have been used in the ToscaWidgets project at one point.&lt;/p&gt;
&lt;p&gt;Prioritized methods certainly seem like a useful idea, but I was a bit bothered by the specific implementation, because it showed just how weak PEAK-Rules' extensibility documentation is at this point.&lt;/p&gt;
&lt;p&gt;Really, it shouldn't be that &lt;strong&gt;hard&lt;/strong&gt; to implement manual method priorities in PEAK-Rules.&amp;nbsp; I mean, prioritized_methods is like 150 lines plus docstrings, it has to define several new method types and decorators to replace those in PEAK-Rules, and if you want to use it with a new method type of your own, you're already screwed by potential incompatibilities.&lt;/p&gt;
&lt;p&gt;In short, I clearly wasn't exposing a good enough API or providing good enough examples.&amp;nbsp; ;-)
&lt;/p&gt;
&lt;p&gt;So, there &lt;em&gt;had&lt;/em&gt; to be a better way, and in fact I immediately thought of one that ought to be doable in a dozen lines of code or so, that would make a perfect demo for PEAK-Rules' &lt;a href="http://peak.telecommunity.com/DevCenter/PEAK-Rules/Predicates"&gt;predicates module documentation&lt;/a&gt;:&lt;/p&gt;
&lt;pre&gt;
from peak.rules import implies, when
from peak.rules.criteria import Test
from peak.rules.predicates import Const, expressionSignature

class priority(int):
    """A simple priority"""

when(implies, (priority, priority))(lambda p1,p2: p1&amp;gt;p2)

@when(expressionSignature, 
      "isinstance(expr, Const) and "
      "isinstance(expr.value, priority)")
def test_for_priority(expr):
    return Test(None, expr.value)
&lt;/pre&gt;&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;What this code does is create an integer subclass called &lt;code&gt;priority&lt;/code&gt;, that can then be used in rule definitions, e.g.:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;@when(some_func, "isinstance(foo, Bar) and priority(1)")&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Then, between two otherwise-identical rules, the one with a priority will win over the one without, or the higher priority will win if both have priorities.&lt;/p&gt;
&lt;p&gt;All you have to do to use it, is import the &lt;code&gt;priority &lt;/code&gt; type into the modules where you want to use it.  No new decorators or special method types are needed, and it will continue to work with any &lt;strong&gt;new&lt;/strong&gt; method types added to PEAK-Rules or defined by third parties!&lt;/p&gt;
&lt;p&gt;Pretty neat, huh?&lt;/p&gt;
&lt;p&gt;There was just one downside to it, and that's that it didn't work.  :-(&lt;/p&gt;
&lt;p&gt;As it happens, PEAK-Rules' predicate dispatch engine barfed on using None as a test expression (in the &lt;code&gt;Test(None, expr.value)&lt;/code&gt; part), and I had to &lt;a href="http://svn.eby-sarna.com/PEAK-Rules/peak/rules/predicates.py?rev=2648&amp;amp;view=diff&amp;amp;r1=2648&amp;amp;r2=2647&amp;amp;p1=PEAK-Rules/peak/rules/predicates.py&amp;amp;p2=/PEAK-Rules/peak/rules/predicates.py"&gt;tweak a few lines&lt;/a&gt; to make it skip over indexing and code generation for tests on None.  But, once that was done, I was able to add a tested version of the above as a &lt;a href="http://peak.telecommunity.com/DevCenter/PEAK-Rules/Predicates#expression-to-predicate-conversion"&gt;doctest demo&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Anyway, if you're doing anything interesting with PEAK-Rules, or find yourself needing to extend it in some way, I'd love to &lt;a href="http://www.eby-sarna.com/mailman/listinfo/PEAK/"&gt;hear from you&lt;/a&gt;.  Right now, it's pretty easy for &lt;em&gt;me&lt;/em&gt; to add cool features like the one above, but I'm guessing that there are still some gaps in the &lt;a href="http://peak.telecommunity.com/DevCenter/PEAK-Rules"&gt;current documentation&lt;/a&gt; for anybody &lt;em&gt;else&lt;/em&gt; trying to implement nifty new features like the above.&lt;/p&gt;
&lt;p&gt;So, I'm especially interested in any problems you had doing extensions, as well as success stories.  (I'd really like to start firming up the extension APIs soon, as well as their docs!)&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/pje?a=vxLQs8m0vSI:QoMEEKu9Q_Q:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/pje?a=vxLQs8m0vSI:QoMEEKu9Q_Q:UT3xtbGYFzA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=UT3xtbGYFzA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/pje?a=vxLQs8m0vSI:QoMEEKu9Q_Q:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
<feedburner:origLink>http://dirtsimple.org/2010/08/simplifying-prioritized-methods-in-peak.html</feedburner:origLink></entry><entry>
    <title type="text">I Am A Complete Idiot</title>
    <author>
        <name>PJE</name>
    </author>
    <link href="http://feedproxy.google.com/~r/pje/~3/KS8hiWfb278/i-am-complete-idiot.html" type="text/html" rel="alternate" />
    
    
    <id>dirtsimple.org/2010/08/i-am-complete-idiot.html</id>
    <published>2010-08-04T21:38:12-0400</published>
    <updated>2010-08-04T21:50:20-0400</updated>
    <content xml:base="http://dirtsimple.org/2010/08/i-am-complete-idiot.html" type="html">&lt;p&gt;This is a total, absolute facepalm.&amp;nbsp; I cannot believe I have been this stupid.&lt;/p&gt;
&lt;p&gt;Today on Reddit, I saw a complaint about infrequent setuptools releases.&amp;nbsp; And I was like, "well, why don't you just install from SVN?"&lt;/p&gt;
&lt;p&gt;And then I thought to myself, you know, I really should release that thing more often, so that people can get the changes without needing to use SVN.&amp;nbsp; I mean, what if they don't have SVN installed, especially on Windows?&amp;nbsp; (Which the person posting the complaint was using.)&lt;/p&gt;
&lt;p&gt;So, I start thinking to myself, "well, how often could I release an update?&amp;nbsp; Could I do it every month or so?"&lt;/p&gt;
&lt;p&gt;And that's when I have the total facepalm.&lt;/p&gt;
&lt;p&gt;I mean, couldn't I just release &lt;strong&gt;snapshot&lt;/strong&gt; versions, like say, oh, I don't know...&amp;nbsp;&lt;em&gt;nightly&lt;/em&gt;?&lt;/p&gt;
&lt;p&gt;You know, just like I've been doing for all these &lt;a href="http://peak.telecommunity.com/snapshots/"&gt;other projects&lt;/a&gt;?&lt;/p&gt;
&lt;p&gt;For, like, the &lt;strong&gt;last five years&lt;/strong&gt;?&lt;/p&gt;
&lt;p&gt;Duh.&lt;/p&gt;
&lt;p&gt;I have now set up a snapshot script, and edited setuptools' PyPI entry to point to the snapshots directory.&amp;nbsp; So, as of now "easy_install -U setuptools" will update you to the latest snapshot of the 0.6 line, until I switch over to offering 0.7 alphas as snapshots.&lt;/p&gt;
&lt;p&gt;That is all.&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/pje?a=KS8hiWfb278:mtj3QYLOp0g:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/pje?a=KS8hiWfb278:mtj3QYLOp0g:UT3xtbGYFzA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=UT3xtbGYFzA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/pje?a=KS8hiWfb278:mtj3QYLOp0g:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
<feedburner:origLink>http://dirtsimple.org/2010/08/i-am-complete-idiot.html</feedburner:origLink></entry><entry>
    <title type="text">Ending My Insight Addiction</title>
    <author>
        <name>PJE</name>
    </author>
    <link href="http://feedproxy.google.com/~r/pje/~3/99biTWKO2jA/ending-my-insight-addiction.html" type="text/html" rel="alternate" />
    <link href="http://www.blogger.com/comment.g?blogID=8674831&amp;postID=2495795730320874364" type="text/html" rel="comments" />
    <id>tag:blogger.com,1999:blog-8674831.post-2495795730320874364</id>
    
    <published>2010-02-04T13:34:06-0500</published>
    <updated>2010-02-04T17:58:41-0500</updated>
    <content xml:base="http://dirtsimple.org/2010/02/ending-my-insight-addiction.html" type="html">&lt;p&gt;There's an old story that goes like this:&lt;/p&gt;
&lt;p&gt;Once upon a time, there was a boy who ate too much sugar.&amp;nbsp; His mother, who wanted him to stop, thought that if the boy wouldn't listen to her, then perhaps he would listen to his idol, Mahatma Gandhi.&lt;/p&gt;
&lt;p&gt;So she walked for many miles through the scorching heat to ask Gandhi, "Would you please tell my son to stop eating sugar?"&lt;/p&gt;
&lt;p&gt;Gandhi replied, "Bring your boy back in two weeks.&amp;nbsp; I will speak to him then."&lt;/p&gt;
&lt;p&gt;Confused, the mother left, then brought the boy back two weeks later.&lt;/p&gt;
&lt;p&gt;Gandhi looked the boy in the eye and said, "Stop eating sugar."&lt;/p&gt;
&lt;p&gt;The boy nodded, and promised to stop.&lt;/p&gt;
&lt;p&gt;His mother of course was grateful, but still confused.&amp;nbsp; "Why did you want me to bring him back in two weeks?&amp;nbsp; &lt;a href="http://dirtsimple.org/2010/02/ending-my-insight-addiction.html#begin-post"&gt;Couldn't you have said the same thing to him then?&lt;/a&gt;"&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/pje?a=99biTWKO2jA:CX0gZyvQbzc:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/pje?a=99biTWKO2jA:CX0gZyvQbzc:UT3xtbGYFzA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=UT3xtbGYFzA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/pje?a=99biTWKO2jA:CX0gZyvQbzc:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
<feedburner:origLink>http://dirtsimple.org/2010/02/ending-my-insight-addiction.html</feedburner:origLink></entry><entry>
    <title type="text">Don't use CGIHandler on Google App Engine</title>
    <author>
        <name>PJE</name>
    </author>
    <link href="http://feedproxy.google.com/~r/pje/~3/w1Gf0euS0ms/don-use-cgihandler-on-google-app-engine.html" type="text/html" rel="alternate" />
    <link href="http://www.blogger.com/comment.g?blogID=8674831&amp;postID=6344254560688720160" type="text/html" rel="comments" />
    <id>tag:blogger.com,1999:blog-8674831.post-6344254560688720160</id>
    
    <published>2010-02-04T11:35:10-0500</published>
    <updated>2010-03-12T20:01:55-0500</updated>
    <content xml:base="http://dirtsimple.org/2010/02/don-use-cgihandler-on-google-app-engine.html" type="html">&lt;p&gt;&lt;em&gt;Update (March 12th): &lt;/em&gt; &lt;a href="https://www.blogger.com/comment.g?blogID=8674831&amp;amp;postID=6344254560688720160#c2245241537453277541"&gt;&lt;em&gt;Guido says this problem only happens in the AppEngine SDK&lt;/em&gt;&lt;/a&gt;&lt;em&gt;, and doesn't happen in production.&amp;nbsp; "&lt;!--StartFragment --&gt;In production, wsgiref.handlers is imported in an earlier stage,&amp;nbsp; before the request-specific environment is set. So the scope of the problem is much smaller."&amp;nbsp; Yay!&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;/em&gt;It seems that an early documentation example recommended that people use &lt;code&gt;wsgiref.handlers.CGIHandler&lt;/code&gt; to run WSGI apps on Google App Engine, instead of the correctly-functioning &lt;code&gt;google.appengine.ext.webapp.util.run_wsgi_app()&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;If you are doing this in your application or your web framework, you have &lt;a href="http://code.google.com/p/googleappengine/issues/detail?id=2040"&gt;a potentially-exploitable security hole&lt;/a&gt; and you should &lt;strong&gt;fix it at once&lt;/strong&gt;.&amp;nbsp; (Or maybe not; see &lt;a href="https://www.blogger.com/comment.g?blogID=8674831&amp;amp;postID=6344254560688720160#c2245241537453277541"&gt;Guido's comment&lt;/a&gt;.)&lt;/p&gt;
&lt;p&gt;The specific problem is that one of CGIHandler's base classes caches a copy of os.environ, for non-CGI use cases, and this makes it possible for certain CGI variables to "leak" from the request that started the process, into every subsequent request.&lt;/p&gt;
&lt;p&gt;Of course, CGIHandler was never intended to be capable of handling long running processes like GAE, because CGI is not a long-running process.&amp;nbsp; The idea is that if you have a new kind of long-running process, you subclass&amp;nbsp;&lt;strong&gt;Base&lt;/strong&gt;CGIHandler for your specific use case.
&lt;/p&gt;
&lt;p&gt;See, in a "traditional" long-running web app protocol (like FastCGI), process startup is distinct from request handling.&amp;nbsp; Even if a FastCGI app is started because there's a request ready for processing, there is still a separation between application initialization and the actual request processing.&amp;nbsp; (And wsgiref tries to cache the "startup" os.environ, separate from the "request" os.environ.)&lt;/p&gt;
&lt;p&gt;App Engine, however, jams these two phases together, such that the "main" script is being re-run for each request, so there's no distinction between "startup" and "request".&amp;nbsp; This makes things convenient for people used to a CGI environment, but brings up problems for the CGIHandler, which expects that it will only be used once per process invocation, and so inherits a cached version of os.environ &lt;em&gt;that also contains request content&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The fix is straightforward: switch from using &lt;code&gt;wsgiref.handlers.CGIHandler&lt;/code&gt; to &lt;code&gt;google.appengine.ext.webapp.util.run_wsgi_app()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;However, if for some reason you can't do that, a quick monkeypatch fix is to add this line:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;CGIHandler.os_environ = {}&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;somewhere in your code before the first use of CGIHandler.&lt;/p&gt;
&lt;p&gt;It is possible that Google has already implemented the patch I provided them to fix this, but if so, the bug opened for App Engine is still open, more than two months later, and some of the documentation is &lt;em&gt;still&lt;/em&gt; &lt;a href="http://code.google.com/appengine/docs/python/taskqueue/overview.html#Using_Task_Queues_in_Python"&gt;recommending CGIHandler&lt;/a&gt;.&amp;nbsp; Don't know whether that means it's fixed and the docs are okay, or that it's unfixed and they're still recommending people use it.&lt;/p&gt;
&lt;p&gt;Either way, though, recommending CGIHandler for use in the GAE environment was &lt;em&gt;never&lt;/em&gt; a good idea, since GAE is not really CGI.&amp;nbsp; If it ain't CGI, &lt;strong&gt;don't use CGIHandler&lt;/strong&gt;.&amp;nbsp; Subclass BaseCGIHandler instead, and make a GAEHandler or AWSHandler or whatever, and take advantage of the branding opportunity provided thereby.&amp;nbsp; ;-)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/pje?a=w1Gf0euS0ms:j5ngUe5FCio:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/pje?a=w1Gf0euS0ms:j5ngUe5FCio:UT3xtbGYFzA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=UT3xtbGYFzA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/pje?a=w1Gf0euS0ms:j5ngUe5FCio:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/pje?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</content>
<feedburner:origLink>http://dirtsimple.org/2010/02/don-use-cgihandler-on-google-app-engine.html</feedburner:origLink></entry>
</feed>

