<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <id>http://nvie.com/</id>
  <title>nvie.com</title>
  <updated>2026-02-19T00:00:00+00:00</updated>
  <author>
    <name>Vincent Driessen</name>
    <email>me@nvie.com</email>
    <uri>http://nvie.com/about/</uri>
  </author>
  <generator version="0.0.3">iron</generator>
  <entry>
    <id>https://nvie.com/posts/nspredicateeditor-tutorial/</id>
    <title>NSPredicateEditor tutorial</title>
    <updated>2009-06-20T00:00:00+00:00</updated>
    <content type="html">&lt;p&gt;Cocoa offers a nice visual editor for editing NSPredicate objects templates,
called NSPredicateEditor. The NSPredicateEditor can be set up using code or in
Interface Builder, which is preferable for simple use. The setup is fairly easy
once you know how to do it. In this tutorial, we’ll be building a simple
predicate editor example which shows the basic functionality of the predicate
editor.&lt;/p&gt;
&lt;h2 id="setting-up-the-appdelegate"&gt;Setting up the AppDelegate &lt;a href="#setting-up-the-appdelegate" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Begin by creating a new Xcode project (⌘⇧N). Name your project wisely
and create a new class in the Classes group, called AppDelegate.&lt;/p&gt;
&lt;p&gt;Switch to the header file and declare two IBOutlets for the main window and the
sheet on which we’re going to display the editor in a few minutes. Also, add
two IBActions called &lt;code&gt;-openEditor:&lt;/code&gt; and &lt;code&gt;-closeEditor:&lt;/code&gt;. Finally, add an ivar
that holds the NSPredicate we’re going to be editing.&lt;/p&gt;
&lt;p&gt;&lt;img alt="" src="/img/appdelegate1.png" /&gt;&lt;/p&gt;
&lt;p&gt;Next, we’re going to fire up Interface Builder to build the UI. Double click on
the MainMenu.xib file under the Resources group.&lt;/p&gt;
&lt;p&gt;Drag an NSObject object from the Library into the XIB and call it App Delegate.
Hit ⌘6 and make it a subclass of the AppDelegate class we just created.
Then, hook it up to the delegate property of the File’s Owner.&lt;/p&gt;
&lt;p&gt;&lt;img alt="" src="/img/hookup-appdelegate.png" /&gt;
&lt;img alt="" src="/img/choose-delegate.png" /&gt;&lt;/p&gt;
&lt;p&gt;Drag a new NSWindow to the XIB-file and call it Sheet. Make sure the checkbox
“Visible At Launch” is deselected or the sheet will not display properly at
runtime. Open the main window and add a NSButton and a NSTextView to it. To the
sheet window, drag a NSPredicateEditor and a NSButton.  They should look
somewhat like this now:&lt;/p&gt;
&lt;p&gt;&lt;img alt="" src="/img/Picture-5.png" /&gt;
&lt;img alt="" src="/img/Picture-4.png" /&gt;&lt;/p&gt;
&lt;p&gt;Now, we can hook up the outlets and actions as usual. Hook up the Edit
Predicate button on the main window to &lt;code&gt;-openEditor:&lt;/code&gt; and the OK button on the
sheet window to closeEditor:. Then hook up the mainWindow and sheet outlets of
the AppDelegate class to the respective NSWindow objects.&lt;/p&gt;
&lt;p class="centered"&gt;&lt;img alt="" src="/img/hookup-windows.png" /&gt;&lt;/p&gt;
&lt;h2 id="configure-the-nspredicateeditor"&gt;Configure the NSPredicateEditor &lt;a href="#configure-the-nspredicateeditor" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Once we have all of the connections between Xcode and Interface Builder set up,
we can continue to configure the predicate editor itself, which is actually
what this tutorial is all about. An NSPredicateEditor control uses a list of
NSPredicateEditorRowTemplate objects that can handle individual (simple)
NSPredicate objects. Combining these row templates enables the
NSPredicateEditor to edit compound predicates. There is no limitation to the
depth of nested compound predicates, although nesting too deep would not be
advisable from a usability perspective.&lt;/p&gt;
&lt;p&gt;In the edit window, click a few times until the “name contains” row template is
selected. In this row template, you define which key paths are supported.
Supported here means two things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;matching&lt;/strong&gt;—given an existing predicate with this key path in it on the
  left-hand side, this row template can be used to alter the predicate;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;generation&lt;/strong&gt;—when using the editor to create new predicates, adding a new
  rule for this key path will generate a predicate for this key path.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="" src="/img/Picture-10.png" /&gt;
&lt;img alt="" src="/img/Picture-13.png" /&gt;&lt;/p&gt;
&lt;h4 id="gotcha"&gt;Gotcha &lt;a href="#gotcha" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h4&gt;
&lt;p&gt;A small gotcha, at least one that initially put me on the wrong foot, is that
there is quite a difference between the rows that you see design-time in
Interface Builder and the rows that are available run-time. At design-time, you
define the NSPredicateEditorRowTemplate objects while at run-time you see
instances of them. Hence, the number of rows at design-time is the &lt;em&gt;number of
different row templates available&lt;/em&gt;. At run-time, however, the number of rows is
the number of &lt;em&gt;predicates within the compound predicate&lt;/em&gt; (which each has an
associated row template instance that handles it). Subtle difference.&lt;/p&gt;
&lt;p&gt;In short, in Interface Builder, &lt;em&gt;create a row template for &lt;em&gt;each type of match&lt;/em&gt;
that you want to allow&lt;/em&gt;. Typically, this means for each data type that you want
to support. In our example, we have the following setup:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Row template #1 is for all string matches. Here, we have defined it for the
  key paths “firstname”, “lastname”, “address.street” and “address.city”. They,
  per definition, have the same allowed operators. If we want to have an other
  set of operators for a specific key path, we need to define a separate row
  template for it.&lt;/li&gt;
&lt;li&gt;Row template #2 is for date matches, i.e. our “birthdate” key path.&lt;/li&gt;
&lt;li&gt;Row template #3 is for all integer matches, i.e. our “address.number” key
  path.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The result looks like this:&lt;/p&gt;
&lt;p class="centered"&gt;&lt;img alt="" src="/img/row-templates-setup.png" /&gt;&lt;/p&gt;
&lt;h2 id="using-bindings-to-connect-the-predicate-to-the-ui"&gt;Using bindings to connect the predicate to the UI &lt;a href="#using-bindings-to-connect-the-predicate-to-the-ui" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;img alt="" class="right" src="/img/bindings.png" /&gt;&lt;/p&gt;
&lt;p&gt;Next up, we simply connect both the text view from the main window and the
predicate editor from the sheet window to the predicate key path using Cocoa
bindings. In order to do so, select the NSPredicateEditor (first click the
control to select the scroll view, then click again to select the inner
NSPredicateEditor), hit ⌘4. Then, unfold the “Value” binding and hook it up to
the App Delegate’s “predicate” key path.&lt;/p&gt;
&lt;p&gt;Do the same for the text view in the main window, but this time hook it up to
the “predicate.description” key path (since only strings can be displayed in
a text view). When you do this, make sure that the text view is read-only,
since the description property of objects should never be set.&lt;/p&gt;
&lt;h2 id="writing-the-code-to-wrap-it-all-up"&gt;Writing the code to wrap it all up &lt;a href="#writing-the-code-to-wrap-it-all-up" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Finally, we have only a bit of code to write in our AppDelegate implementation,
so let’s go:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="c1"&gt;//&lt;/span&gt;
&lt;span class="c1"&gt;//  AppDelegate.m&lt;/span&gt;
&lt;span class="c1"&gt;//  PredicateEditorTest&lt;/span&gt;
&lt;span class="c1"&gt;//&lt;/span&gt;
&lt;span class="c1"&gt;//  Created by Vincent on 20-07-09.&lt;/span&gt;
&lt;span class="c1"&gt;//&lt;/span&gt;

&lt;span class="cp"&gt;#import &amp;quot;AppDelegate.h&amp;quot;&lt;/span&gt;

&lt;span class="cp"&gt;#define DEFAULT_PREDICATE @&amp;quot;(firstname = &amp;#39;John&amp;#39; AND lastname = &amp;#39;Doe&amp;#39;) &amp;quot; &lt;/span&gt;
&lt;span class="w"&gt;                    &lt;/span&gt;&lt;span class="s"&gt;@&amp;quot;OR birthdate &amp;gt; CAST(&amp;#39;01/01/1985&amp;#39;, &amp;#39;NSDate&amp;#39;) &amp;quot;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;
&lt;span class="w"&gt;                    &lt;/span&gt;&lt;span class="s"&gt;@&amp;quot;OR address.city = &amp;#39;Chicago&amp;#39; &amp;quot;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;
&lt;span class="w"&gt;                    &lt;/span&gt;&lt;span class="s"&gt;@&amp;quot;AND address.street != &amp;#39;Main Street&amp;#39; &amp;quot;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;
&lt;span class="w"&gt;                    &lt;/span&gt;&lt;span class="s"&gt;@&amp;quot;OR address.number &amp;gt; 1000&amp;quot;&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;

&lt;span class="k"&gt;@implementation&lt;/span&gt; &lt;span class="nc"&gt;AppDelegate&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;

&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;super&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;!=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="n"&gt;predicate&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="bp"&gt;NSPredicate&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;predicateWithFormat&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;DEFAULT_PREDICATE&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;retain&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;

&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;dealloc&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;predicate&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;release&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;super&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;dealloc&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;

&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;IBAction&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;openEditor:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nv"&gt;sender&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;NSApp&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;beginSheet&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;sheet&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="nl"&gt;modalForWindow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;mainWindow&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="nl"&gt;modalDelegate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nb"&gt;nil&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="nl"&gt;didEndSelector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="nl"&gt;contextInfo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nb"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;

&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;IBAction&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;closeEditor:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nv"&gt;sender&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;NSApp&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;endSheet&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;sheet&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;sheet&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;orderOut&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;

&lt;span class="k"&gt;@end&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In the &lt;code&gt;-init:&lt;/code&gt; method, we initialize the AppDelegate by setting and retaining
a reference to a rather complex default predicate. When the XIB is loaded at
run-time, the textbox shows exactly this predicate and it can be edited by
invoking the edit sheet.&lt;/p&gt;
&lt;p&gt;The actual implementation of the &lt;code&gt;-openEditor:&lt;/code&gt; and &lt;code&gt;-closeEditor:&lt;/code&gt; methods
aren’t too exciting.&lt;/p&gt;
&lt;h2 id="downloading-the-source"&gt;Downloading the source &lt;a href="#downloading-the-source" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;You can download the source code for this tutorial as an Xcode project here.&lt;/p&gt;
&lt;p class="centered"&gt;&lt;a href="/files/PredicateEditorTest.zip"&gt;&lt;img alt="" src="/img/zip@2x.png" style="max-width: 128px" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;a href="/files/PredicateEditorTest.zip"&gt;PredicateEditorTest.zip&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Have a blast!&lt;/p&gt;</content>
    <link href="https://nvie.com/posts/nspredicateeditor-tutorial/"/>
    <summary>Cocoa offers a nice visual editor for editing NSPredicate objects templates,
called NSPredicateEditor. The NSPredicateEditor can be set up using code or in
Interface Builder, which is preferable for simple use. The setup is fairly easy
once you know how to do it. In this tutorial, we’ll be building a simple
predicate editor example which shows the basic functionality of the predicate
editor.</summary>
    <published>2009-06-20T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://nvie.com/posts/nsmanagedobjectcontext-extensions/</id>
    <title>NSManagedObjectContext extensions</title>
    <updated>2009-06-22T00:00:00+00:00</updated>
    <content type="html">&lt;p&gt;The Core Data framework rules, and its API is really really powerful.  But
really, why does the Core Data API require us to write so much boilerplate
code? Simple things need to be simple.&lt;/p&gt;
&lt;p&gt;Why is the deletion of a managed object from the NSManagedObjectContext so
easy:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;deleteObject&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;someObject&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Compared to its creation:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="bp"&gt;NSEntityDescription&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;insertNewObjectForEntityForName&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;@&amp;quot;someObjectClassName&amp;quot;&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;                              &lt;/span&gt;&lt;span class="nl"&gt;inManagedObjectContext&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="extending-nsmanagedobjectcontext"&gt;Extending NSManagedObjectContext &lt;a href="#extending-nsmanagedobjectcontext" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Add the following category on NSManagedObjectContext to all of your Core Data
projects and your pains will be history.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;@implementation&lt;/span&gt; &lt;span class="bp"&gt;NSManagedObjectContext&lt;/span&gt;&lt;span class="nl"&gt;(NSManagedObjectContextConvenienceMethods)&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;

&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;newObject:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Class&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nv"&gt;entity&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="bp"&gt;NSEntityDescription&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;insertNewObjectForEntityForName&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;entity&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;                                        &lt;/span&gt;&lt;span class="nl"&gt;inManagedObjectContext&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;

&lt;span class="k"&gt;@end&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now, a call to create a new object is as easy as deleting it.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;newObject&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;someEntity&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;]];&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="further-enhancements-of-nsmanagedobject"&gt;Further enhancements of NSManagedObject &lt;a href="#further-enhancements-of-nsmanagedobject" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Matt Gallagher has written an &lt;a href="http://cocoawithlove.com/2008/03/core-data-one-line-fetch.html"&gt;excellent
article&lt;/a&gt; about
how to further enhance NSManagedObject for adding simple, one-line fetch
support. Be sure to check it out.&lt;/p&gt;</content>
    <link href="https://nvie.com/posts/nsmanagedobjectcontext-extensions/"/>
    <summary>The Core Data framework rules, and its API is really really powerful.  But
really, why does the Core Data API require us to write so much boilerplate
code? Simple things need to be simple.</summary>
    <published>2009-06-22T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://nvie.com/posts/automatically-generate-classes-for-your-core-data-data-model/</id>
    <title>Automatically generate classes for your Core Data data model</title>
    <updated>2009-06-30T00:00:00+00:00</updated>
    <content type="html">&lt;p&gt;When designing a Core Data data model for your Xcode projects, you can choose
to create Objective-C object wrappers for your entities, so that you can profit
from type-safe code. The normal, tedious, workflow for this is that you select
each entity from the model designer, select all of its attributes and
relationships, Ctrl-click it and from the contextual menu first select “Copy
Obj-C 2.0 Method Declarations To Clipboard”, paste it into the appropriate
class header file, then do the same thing for the method implementations in the
class implementation file. Waaaaaay too much work. Not to mention the manual
copy-pastes are really hard to keep in sync once you start adding functionality
to these class files, since you don’t want to overwrite those additions, but
you want to keep replacing everything else.&lt;/p&gt;
&lt;h2 id="meet-mogenerator"&gt;Meet mogenerator &lt;a href="#meet-mogenerator" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Fortunately, there is a great way for automating this process, using
mogenerator. The tool can be downloaded as a &lt;a href="http://aralbalkan.com/2152"&gt;DMG
installer&lt;/a&gt; (Aral Balkan’s blog mentions
a workaround for older Xcode versions, but for Xcode 3.1.3 it worked out of the
box for me), or you can checkout the sources from
&lt;a href="http://github.com/rentzsch/mogenerator/"&gt;github&lt;/a&gt; and build it yourself.&lt;/p&gt;
&lt;p&gt;The mogenerator command line tool eases this generation process by reading the
&lt;code&gt;*.xcdatamodel&lt;/code&gt; file and generating both class files and intermediate class
files for each entity. The intermediate classes (called &lt;em&gt;machine&lt;/em&gt; classes) are
continuously overwritten by subsequent regenerations, so you should never edit
the contents of these files. The actual model object classes (called &lt;em&gt;human&lt;/em&gt;
classes) inherit from those intermediate classes with a default empty
implementation, allowing for all manual extensions.&lt;/p&gt;
&lt;p&gt;For example, when you design a model with two entities Foo and Bar, mogenerator
can be invokes as follows:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;mogenerator -m MyDocument.xcdatamodel -M Entities -H Model
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The flag &lt;code&gt;-m&lt;/code&gt; sets the input model file, while &lt;code&gt;-M&lt;/code&gt; and &lt;code&gt;-H&lt;/code&gt; specify the output
directories where the machine and human classes should be generated
respectively.&lt;/p&gt;
&lt;p&gt;This does a few things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;In the Entities subdirectory, there will be generated header and
  implementation files for NSManagedObject subclasses called &lt;code&gt;_Foo&lt;/code&gt; and &lt;code&gt;_Bar&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;In the Model subdirectory, there will be generated classes called &lt;code&gt;Foo&lt;/code&gt; and
  &lt;code&gt;Bar&lt;/code&gt;—respective subclasses of &lt;code&gt;_Foo&lt;/code&gt; and &lt;code&gt;_Bar&lt;/code&gt;. These are only created if
  not available yet. Otherwise, they are left as is.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="wrapping-it-up"&gt;Wrapping it up &lt;a href="#wrapping-it-up" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The trick of how mogenerator works is that you can run the script as often as
you want. After every change in your model, you’ll want to re-run the
generation again to update the machine classes. You could easily leave Xcode,
switch over to Terminal and issue the command above. But you’ll get quite tired
of that after a few times.&lt;/p&gt;
&lt;p&gt;Therefore, I’ve written a custom user script that can be added to Xcode (see
figure), which does the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You can configure the output directories in the first lines of the script.
  There is no per-project configuration, so choose them as you would like to
  use them with all your projects;&lt;/li&gt;
&lt;li&gt;Mind that these generated files are not automatically included in your Xcode
  project. Drag them there once and ideally put the machine generated classes
  into a group under “Other resource”, so you never have to see them again.
  Whenever you add a new class to your model, new files will be generated, so
  again you must drag the new files to reference those, of course!&lt;/li&gt;
&lt;li&gt;The script can be run with any file in the project opened. It starts out with
  that file and walks up the directory tree to search for your Xcode project.
  If found, it executes all the rest from your project directory.  (Suggestions
  are welcome, I could not find a better implementation since a variable like
  &lt;code&gt;%%%{PBXProjectPath}%%%&lt;/code&gt; does not seem to exist.)&lt;/li&gt;
&lt;li&gt;It invokes mogenerator to generate all model classes for the project. It is
  smart enough to detect whether you are using Brian Webster’s
  &lt;a href="http://www.fatcatsoftware.com/blog/2008/per-object-ordered-relationships-using-core-data"&gt;BWOrderedManagedObject&lt;/a&gt;
  in your project. If so, your generated machine classes will inherit from
  BWOrderedManagedObject instead of NSManagedObject.&lt;/li&gt;
&lt;/ul&gt;
&lt;p class="centered"&gt;&lt;img alt="" src="/img/set-user-script.png" /&gt;&lt;/p&gt;
&lt;p&gt;To add this script to Xcode, open the menu Scripts (the icon) &amp;gt; Edit User
Scripts… Click the “+”-button on the bottom-left and select “New shell script”.
Set the values for Input, Directory, Output and Errors as in the screenshot
above, then copy-paste the script below into the code window.  Add a nice
keyboard shortcut to this action to top it off :-)  I’ve chosen ⌥⌘G for this.&lt;/p&gt;
&lt;p&gt;Please feel free to leave any comments if this helped you.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="ch"&gt;#!/bin/sh&lt;/span&gt;
&lt;span class="c1"&gt;#&lt;/span&gt;
&lt;span class="c1"&gt;# Automatic (re)generation of model classes for all *.xcdatamodel files.&lt;/span&gt;
&lt;span class="c1"&gt;# Written by Vincent Driessen&lt;/span&gt;
&lt;span class="c1"&gt;#&lt;/span&gt;
&lt;span class="c1"&gt;# You are free to use this script in any way.&lt;/span&gt;
&lt;span class="c1"&gt;# The original blog post is http://nvie.com/archives/263&lt;/span&gt;
&lt;span class="c1"&gt;#&lt;/span&gt;

&lt;span class="c1"&gt;# Define output directories&lt;/span&gt;
&lt;span class="nv"&gt;MACHINE_DIR&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Entities&amp;quot;&lt;/span&gt;
&lt;span class="nv"&gt;MODEL_DIR&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Model&amp;quot;&lt;/span&gt;

&lt;span class="c1"&gt;# Look for the Xcode project directory for this file&lt;/span&gt;
&lt;span class="nb"&gt;cd&lt;/span&gt; &lt;span class="sb"&gt;`&lt;/span&gt;dirname &lt;span class="s2"&gt;&amp;quot;%%%{PBXFilePath}%%%&amp;quot;&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="sb"&gt;`&lt;/span&gt;ls -d *.xcodeproj &lt;span class="m"&gt;2&lt;/span&gt;&amp;gt;/dev/null &lt;span class="p"&gt;|&lt;/span&gt; wc -l&lt;span class="sb"&gt;`&lt;/span&gt; -eq &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="nb"&gt;cd&lt;/span&gt; ..
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;`pwd`&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
        &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;No Xcode project found.&amp;quot;&lt;/span&gt;
        &lt;span class="nb"&gt;exit&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;fi&lt;/span&gt;
&lt;span class="k"&gt;done&lt;/span&gt;

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Project directory is `pwd`&amp;quot;&lt;/span&gt;

&lt;span class="c1"&gt;#&lt;/span&gt;
&lt;span class="c1"&gt;# Check to see whether the base class is just a default (NSManagedObject) or&lt;/span&gt;
&lt;span class="c1"&gt;# maybe Brian Webster&amp;#39;s excellent BWOrderedManagedObject.&lt;/span&gt;
&lt;span class="c1"&gt;# http://fatcatsoftware.com/blog/2008/per-object-ordered-relationships-using-core-data&lt;/span&gt;
&lt;span class="c1"&gt;#&lt;/span&gt;
&lt;span class="c1"&gt;# NOTE:&lt;/span&gt;
&lt;span class="c1"&gt;# The check really is quite arbitrary: if there exists a file called&lt;/span&gt;
&lt;span class="c1"&gt;# BWOrderedManagedObject.h somewhere below the project root directory, we&lt;/span&gt;
&lt;span class="c1"&gt;# assume that we want to use this as the base class for all generated classes.&lt;/span&gt;
&lt;span class="c1"&gt;#&lt;/span&gt;
&lt;span class="nv"&gt;EXTRA_FLAGS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="sb"&gt;`&lt;/span&gt;find . -name BWOrderedManagedObject.h &lt;span class="p"&gt;|&lt;/span&gt; wc -l&lt;span class="sb"&gt;`&lt;/span&gt; -gt &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
    &lt;span class="nv"&gt;EXTRA_FLAGS&lt;/span&gt;&lt;span class="o"&gt;+=&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;--base-class BWOrderedManagedObject&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;

&lt;span class="c1"&gt;# Generate the model classes using mogenerator&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; model &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="sb"&gt;`&lt;/span&gt;find . -name &lt;span class="s1"&gt;&amp;#39;*.xcdatamodel&amp;#39;&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
   &lt;span class="c1"&gt;# The output directories have to exist, so create them&lt;/span&gt;
   mkdir -p &lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;MACHINE_DIR&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;MODEL_DIR&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;
   mogenerator &lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;EXTRA_FLAGS&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; -m &lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;model&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt; -M &lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;MACHINE_DIR&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt; -H &lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;MODEL_DIR&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</content>
    <link href="https://nvie.com/posts/automatically-generate-classes-for-your-core-data-data-model/"/>
    <summary>When designing a Core Data data model for your Xcode projects, you can choose
to create Objective-C object wrappers for your entities, so that you can profit
from type-safe code. The normal, tedious, workflow for this is that you select
each entity from the model designer, select all of its attributes and
relationships, Ctrl-click it and from the contextual menu first select “Copy
Obj-C 2.0 Method Declarations To Clipboard”, paste it into the appropriate
class header file, then do the same thing for the method implementations in the
class implementation file. Waaaaaay too much work. Not to mention the manual
copy-pastes are really hard to keep in sync once you start adding functionality
to these class files, since you don’t want to overwrite those additions, but
you want to keep replacing everything else.</summary>
    <published>2009-06-30T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://nvie.com/posts/auto-generate-classes-for-your-core-data-data-model-revisited/</id>
    <title>Auto-generate classes for your Core Data data model, revisited</title>
    <updated>2009-09-19T00:00:00+00:00</updated>
    <content type="html">&lt;p&gt;A few months ago, I wrote about &lt;a href="/posts/automatically-generate-classes-for-your-core-data-data-model/"&gt;automatically generating classes for your Core
Data entities&lt;/a&gt; and how to automate Xcode using users scripts, such that,
when your model changed, you only needed to run your custom script again and
your intermediate model files would reflect the new situation.&lt;/p&gt;
&lt;p&gt;Well, the guys from the &lt;a href="http://github.com/rentzsch/mogenerator"&gt;mogenerator&lt;/a&gt; project have come up with
a far superior solution in the mean time. The newest version of mogenerator
comes with an Xcode plugin named Xmo’d, which monitors your &lt;code&gt;*.xcdatamodel&lt;/code&gt;
file for changes and, as soon as it changes, regenerates all of the neccessary
files.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;This means that there is officially no more reason not to use mogenerator.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;To set it up, download the installer package from their (improved) &lt;a href="http://rentzsch.github.com/mogenerator/"&gt;project
website&lt;/a&gt; and install it.  (Before
installing, please read the important release note about the renamed method
&lt;code&gt;+newInManagedObjectContext:&lt;/code&gt;.)&lt;/p&gt;
&lt;p&gt;When installed, all you need to do is Command-click your &lt;code&gt;*.xcdatamodel&lt;/code&gt; file,
click Get Info, switch to the Comments tab and add the string “xmod” to the
comment field. This is the trigger for Xmo’d to start (re)generating your
machine-classes (the underscored class files) when the data model changes.
Brilliant!&lt;/p&gt;
&lt;p&gt;&lt;img alt="" src="/img/comment-field.png" /&gt;&lt;/p&gt;
&lt;p&gt;Oh, the default location at which the generated files will be emitted, is in
a folder named after your project, right next to where your &lt;code&gt;*.xcdatamodel&lt;/code&gt;
already sits:&lt;/p&gt;
&lt;p&gt;&lt;img alt="" src="/img/emission-location.png" /&gt;&lt;/p&gt;
&lt;p&gt;Enjoy it and spread the word!&lt;/p&gt;</content>
    <link href="https://nvie.com/posts/auto-generate-classes-for-your-core-data-data-model-revisited/"/>
    <summary>A few months ago, I wrote about [automatically generating classes for your Core
Data entities][prev] and how to automate Xcode using users scripts, such that,
when your model changed, you only needed to run your custom script again and
your intermediate model files would reflect the new situation.</summary>
    <published>2009-09-19T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://nvie.com/posts/gitflow-01-released/</id>
    <title>gitflow 0.1 released</title>
    <updated>2010-01-26T00:00:00+00:00</updated>
    <content type="html">&lt;p&gt;After the overwhelming attention and feedback on the &lt;a href="/posts/a-successful-git-branching-model/"&gt;Git branching model
post&lt;/a&gt;, a general consensus was that
this workflow would benefit from some form of proper scriptability. The
workflow works seamlessly if you perform the steps involved manually, but hey…
manually is manually, really.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;UPDATE 2/4/2010&lt;/strong&gt;:
I recommend NOT USING this very early release, but to jump on the &lt;a href="http://github.com/nvie/gitflow/tree/develop"&gt;current
develop tip&lt;/a&gt;, which is much more mature.  Release 0.2 is coming very
soon.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;An assisting tool (dubbed &lt;code&gt;gitflow&lt;/code&gt;) was therefore created to provide simple,
high-level commands to adopt the workflow into your own software development
process. It’s free and it’s open source. Feel free to contribute to it if you
like.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Fork me on Github: &lt;a href="http://github.com/nvie/gitflow"&gt;http://github.com/nvie/gitflow&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Since this morning, the first working &lt;a href="http://github.com/nvie/gitflow/downloads"&gt;release
0.1&lt;/a&gt; was tagged, albeit very basic.&lt;/p&gt;
&lt;h2 id="a-quick-walkthrough"&gt;A quick walkthrough &lt;a href="#a-quick-walkthrough" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;gitflow&lt;/code&gt; script essentially features six subcommands: paired start/finish
commands for managing the different types of branches from the originating
article:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Feature branches:&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gitflow start feature &amp;lt;myfeature&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gitflow finish feature &amp;lt;myfeature&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Release branches:&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gitflow start release &amp;lt;version-id&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gitflow finish release@ &amp;lt;version-id&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Hotfix branches:&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gitflow start hotfix &amp;lt;version-id&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gitflow finish hotfix &amp;lt;version-id&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Each of these scripts exactly reports what actions were taken and what
follow-up actions are required by the user. This output will be polished in
future versions to improve the &lt;a href="http://en.wikipedia.org/wiki/User_experience_design"&gt;UX&lt;/a&gt; . An example output:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;gitflow finish feature foo
&lt;span class="go"&gt;Branches &amp;#39;develop&amp;#39; and &amp;#39;origin/develop&amp;#39; have diverged.&lt;/span&gt;
&lt;span class="go"&gt;And local branch &amp;#39;develop&amp;#39; is ahead of &amp;#39;origin/develop&amp;#39;.&lt;/span&gt;
&lt;span class="go"&gt;Switched to branch &amp;quot;develop&amp;quot;&lt;/span&gt;
&lt;span class="go"&gt;Your branch is ahead of &amp;#39;origin/develop&amp;#39; by 12 commits.&lt;/span&gt;
&lt;span class="go"&gt;Merge made by recursive.&lt;/span&gt;
&lt;span class="go"&gt; README |    2 ++&lt;/span&gt;
&lt;span class="go"&gt; 1 files changed, 2 insertions(+), 0 deletions(-)&lt;/span&gt;
&lt;span class="go"&gt;Deleted branch foo (cd3effb).&lt;/span&gt;

&lt;span class="go"&gt;Summary of actions:&lt;/span&gt;
&lt;span class="go"&gt;- The feature branch &amp;#39;foo&amp;#39; was merged into &amp;#39;develop&amp;#39;&lt;/span&gt;
&lt;span class="go"&gt;- Feature branch &amp;#39;foo&amp;#39; has been removed&lt;/span&gt;
&lt;span class="go"&gt;- You are now on branch &amp;#39;develop&amp;#39;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="limitations"&gt;Limitations &lt;a href="#limitations" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The script is very limited at the moment yet, but future versions will fix
that, too. Some of the main limitations:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Branch names (&lt;code&gt;master&lt;/code&gt;, &lt;code&gt;develop&lt;/code&gt;) and the remote repo name (&lt;code&gt;origin&lt;/code&gt;) are
  currently fixed.&lt;/li&gt;
&lt;li&gt;There is no support for dealing with merge conflicts yet.&lt;/li&gt;
&lt;li&gt;There is no support for &lt;code&gt;support-*&lt;/code&gt; branches.&lt;/li&gt;
&lt;li&gt;There is no documentation.&lt;/li&gt;
&lt;li&gt;There is no installer.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;However, as this post is written, some of the limitations are already taken
care of by community members. Power to the open source!&lt;/p&gt;</content>
    <link href="https://nvie.com/posts/gitflow-01-released/"/>
    <summary>After the overwhelming attention and feedback on the Git branching model
post, a general consensus was that this workflow would benefit from some
form of proper scriptability. This post proposes the initial version of
a tool I called git-flow.</summary>
    <published>2010-01-26T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://nvie.com/posts/an-upgrade-of-gitflow/</id>
    <title>An upgrade of gitflow</title>
    <updated>2010-03-04T00:00:00+00:00</updated>
    <content type="html">&lt;p&gt;Last week, I silently tagged &lt;a href="http://github.com/nvie/gitflow/tree/0.2"&gt;gitflow 0.2&lt;/a&gt;.  The most important changes
since 0.1 are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Order of arguments changed to have a more “gitish” subcommand structure. For
  example, you now say: &lt;code&gt;git flow feature start myfeature&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Better initializer. &lt;code&gt;git flow init&lt;/code&gt; now prompts interactively to set up
  a gitflow enabled repo.&lt;/li&gt;
&lt;li&gt;Added a command to list all feature/release/hotfix/support branches, e.g.:
  &lt;code&gt;git flow feature list&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Made all merge/rebase operations failsafe, providing a non-destructive
  workflow in case of merge conflicts.&lt;/li&gt;
&lt;li&gt;Easy diff’ing of all changes on a specific (or the current) feature branch:
  &lt;code&gt;git flow feature diff [feature]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Add support for feature branch rebasing: &lt;code&gt;git flow feature rebase&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Some subactions now take name prefixes as their arguments, for convenience.
  For example, if you have feature branches called “experimental”,
  “refactoring” and “feature-X”, you could say: &lt;code&gt;git flow feature finish
  ref&lt;/code&gt;&lt;br&gt; And gitflow will know you mean the “refactoring” feature branch.&lt;br&gt;
  These actions are: &lt;code&gt;finish&lt;/code&gt;, &lt;code&gt;diff&lt;/code&gt; and &lt;code&gt;rebase&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Much better overall sanity checking.&lt;/li&gt;
&lt;li&gt;Better portability (POSIX compliant code)&lt;/li&gt;
&lt;li&gt;Better (more portable) flag parsing using Kate Ward’s
  &lt;a href="http://code.google.com/p/shflags/"&gt;shFlags&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Improved installer. To install &lt;code&gt;git flow&lt;/code&gt; as a first-class Git subcommand,
  simply type: &lt;code&gt;sudo make install&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Major and minor bug fixes.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That’s all for now.&lt;/p&gt;</content>
    <link href="https://nvie.com/posts/an-upgrade-of-gitflow/"/>
    <summary>Last week, I silently tagged gitflow 0.2. These are the most important
changes since 0.1.</summary>
    <published>2010-03-04T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://nvie.com/posts/a-whole-new-blog/</id>
    <title>A whole new blog</title>
    <updated>2010-09-13T00:00:00+00:00</updated>
    <content type="html">&lt;p&gt;&lt;em&gt;Note:&lt;/em&gt; This blog post has been written in 2010, and a lot has changed since
then.  I’m not using any of this anymore.  The post is still available for
posterity, but do take this into account when reading this.&lt;/p&gt;
&lt;p&gt;Finally, I’ve made the move to a static blog engine! I’m using
&lt;a href="http://nanoc.stoneship.org/"&gt;nanoc&lt;/a&gt; now (bye bye WordPress). nanoc is a very
flexible and customizable static site generator, written by &lt;a href="http://twitter.com/ddfreyne"&gt;Denis
Defreyne&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;As with all static site generators, nanoc lets you write your source files in
a simple markup language. Out of the box, nanoc offers you the choice of using
Markdown, Textile, reStructuredText or plain HTML (with or without embedded
Ruby). In fact, nanoc is nothing more than a generator honoring a &lt;code&gt;Rules&lt;/code&gt;-file
that tells it how to compile, layout and route the site’s items.&lt;/p&gt;
&lt;h2 id="compiling-items"&gt;Compiling items &lt;a href="#compiling-items" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;An “item” is a file on your website. It can be any kind of file, like a web
site page (HTML), an image, a JavaScript or CSS file or an RSS feed.  During
the compile phase, you specify which sequential actions should be performed on
the &lt;em&gt;content&lt;/em&gt; of that item. These actions are called &lt;strong&gt;filters&lt;/strong&gt;. Some examples
of filters are an embedded ruby filter,
a &lt;a href="http://redcloth.org/"&gt;Textile-to-HTML&lt;/a&gt; converter,
a &lt;a href="http://lesscss.org/"&gt;less&lt;/a&gt; compiler, or minify
&lt;a href="http://code.google.com/p/rainpress/"&gt;CSS&lt;/a&gt;. Filters can be chained, for
example:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="n"&gt;compile&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;/static/css/*/&amp;#39;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
   &lt;span class="c1"&gt;# compress CSS :)&lt;/span&gt;
   &lt;span class="n"&gt;filter&lt;/span&gt; &lt;span class="ss"&gt;:less&lt;/span&gt;
   &lt;span class="n"&gt;filter&lt;/span&gt; &lt;span class="ss"&gt;:rainpress&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Which turns &lt;code&gt;.less&lt;/code&gt;-files into compressed CSS:&lt;/p&gt;
&lt;p&gt;&lt;img alt="" src="/img/less2css.png" /&gt;&lt;/p&gt;
&lt;p&gt;Any filter you can imagine, nanoc can handle. nanoc comes with a lot of filters
&lt;a href="http://nanoc.stoneship.org/docs/4-basic-concepts/#filters"&gt;out of the box&lt;/a&gt;,
but even &lt;a href="http://nanoc.stoneship.org/docs/5-advanced-concepts/#writing-filters"&gt;writing your
own&lt;/a&gt;
filters really is a piece of cake.&lt;/p&gt;
&lt;h2 id="routing-items"&gt;Routing items &lt;a href="#routing-items" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;After compiling (i.e. transforming content through filters) comes the routing
of the items. This is a means of assigning file names to compiled content.
nanoc calculates default files names from the input, but you can use this to
influence the default naming. A special case is where you set the route to
&lt;code&gt;Nil&lt;/code&gt; which doesn’t write the file at all. I use this to test draft posts
locally, like this (oh, did I mention the &lt;code&gt;Rules&lt;/code&gt; file is 100% Ruby?):&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="n"&gt;route&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;/posts/*/&amp;#39;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="vg"&gt;$include_drafts&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="vi"&gt;@item&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:published&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
      &lt;span class="s1"&gt;&amp;#39;/posts/&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="vi"&gt;@item&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;slug&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;/index.html&amp;#39;&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="laying-out-items"&gt;Laying out items &lt;a href="#laying-out-items" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Finally, layouts are applied. Layouts are kind of templates that can be used to
“frame” the item’s contents. This is typically used for HTML files only, but
isn’t limited to it. For example, the blog posts are compiled into (partial)
HTML, and the layout rules put the site’s container HTML around it, adding CSS
styling, jQuery scripts, the header, sidebars and footer and Google Analytics
tracking (these go for each page). There’s a special extra layout rule for blog
post pages, which additionally adds Disqus comments.&lt;/p&gt;
&lt;h2 id="summary"&gt;Summary &lt;a href="#summary" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Each build of this blog also automatically:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Converts &lt;a href="http://redcloth.org/"&gt;Textile&lt;/a&gt; content to HTML&lt;/li&gt;
&lt;li&gt;Highlights syntax using &lt;a href="http://pygments.org/"&gt;pygments&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Converts &lt;a href="http://lesscss.org/"&gt;less&lt;/a&gt; to CSS&lt;/li&gt;
&lt;li&gt;Minifies &lt;a href="http://code.google.com/p/rainpress/"&gt;CSS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Minifies &lt;a href="http://code.google.com/closure/compiler/"&gt;JavaScript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.graphicsmagick.org/convert.html"&gt;Downsizes&lt;/a&gt; source images&lt;/li&gt;
&lt;li&gt;Generates redirect pages for alternative (old-style) URL’s (for user that
  have existing bookmarks to old WordPress URL’s)&lt;/li&gt;
&lt;li&gt;Generates a new blog post &lt;a href="http://nanoc.stoneship.org/docs/api/3.1/Nanoc3/Helpers/Blogging.html"&gt;RSS
  feed&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In short, now nanoc is fully configured to my wishes, I can simply focus on
writing &lt;strong&gt;blog content&lt;/strong&gt;, without preparing image content (it is done
automatically), and without having to choose between either a “WYSIWYG” editor
or writing HTML manually. And I can do it in an offline fashion, too, which was
one my main complaints about WordPress.&lt;/p&gt;
&lt;p&gt;So I’m happy.&lt;/p&gt;
&lt;p&gt;Oh, and since I have been converting my blog anyway, I also created a new look
and feel for it. I hope you like it. Feel free to comment.&lt;/p&gt;</content>
    <link href="https://nvie.com/posts/a-whole-new-blog/"/>
    <summary>I've moved my blog to Nanoc.</summary>
    <published>2010-09-13T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://nvie.com/posts/how-i-boosted-my-vim/</id>
    <title>How I boosted my Vim</title>
    <updated>2010-09-23T00:00:00+00:00</updated>
    <content type="html">&lt;p&gt;A few weeks ago, I felt inspired by articles from &lt;a href="http://jeffkreeftmeijer.com/2010/stumbling-into-vim/"&gt;Jeff
Kreeftmeijer&lt;/a&gt; and &lt;a href="http://lucumr.pocoo.org/2010/7/29/sharing-vim-tricks"&gt;Armin
Ronacher&lt;/a&gt;. I took some
time to configure and fine-tune my Vim environment. A lot of new stuff made it
into my &lt;code&gt;.vimrc&lt;/code&gt; file and my &lt;code&gt;.vim&lt;/code&gt; directory. This blog post is a summary
describing what I’ve added and how I use it in my daily work.&lt;/p&gt;
&lt;p&gt;Before doing anything else, make sure you have the following line in your
&lt;code&gt;.vimrc&lt;/code&gt; file:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;quot; This must be first, because it changes other options as side effect&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;nocompatible&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="pathogen"&gt;Step 0: make the customization process easier &lt;a href="#pathogen" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Before starting configuring, it’s useful to install
&lt;a href="http://www.vim.org/scripts/script.php?script_id=2332"&gt;pathogen&lt;/a&gt;.  Plugins in
Vim are files that you drop in subdirectories of your &lt;code&gt;.vim/&lt;/code&gt; directory.  Many
plugins exist of only a single file that should be dropped in &lt;code&gt;.vim/plugin&lt;/code&gt;,
but some exist of multiple files. For example, they come with documentation, or
ship syntax files. In those cases, files need to be dropped into &lt;code&gt;.vim/doc&lt;/code&gt; and
&lt;code&gt;.vim/syntax&lt;/code&gt;. This makes it difficult to remove the plugin afterwards. After
installing pathogen, you can simply unzip a plugin distribution into
&lt;code&gt;.vim/bundle/myplugin&lt;/code&gt;, under which the required subdirectories are created.
Removing the plugin, then, is as simple as removing the &lt;code&gt;myplugin&lt;/code&gt; directory.&lt;/p&gt;
&lt;p&gt;So, download &lt;code&gt;pathogen.vim&lt;/code&gt;, move it into the &lt;code&gt;.vim/autoload&lt;/code&gt; directory (create
it if necessary) and add the following lines to your &lt;code&gt;.vimrc&lt;/code&gt;, to activate it:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;quot; Use pathogen to easily modify the runtime path to include all&lt;/span&gt;
&lt;span class="c"&gt;&amp;quot; plugins under the ~/.vim/bundle directory&lt;/span&gt;
&lt;span class="k"&gt;call&lt;/span&gt; pathogen#&lt;span class="k"&gt;helptags&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;call&lt;/span&gt; pathogen#runtime_append_all_bundles&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Next, I’ve remapped the leader key to &lt;code&gt;,&lt;/code&gt; (comma) instead of the default &lt;code&gt;\&lt;/code&gt;
(backslash), just because I like it better. Since in Vim’s default
configuration, almost every key is already mapped to a command, there needs to
be some sort of standard “free” key where you can place custom mappings under.
This is called the “mapleader”, and can be defined like this:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;quot; change the mapleader from \ to ,&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; mapleader&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;,&amp;quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Once that is done, this is a little tweak that is a time-saver while you’re
building up your &lt;code&gt;.vimrc&lt;/code&gt;. Here, we start using the leader key:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;quot; Quickly edit/reload the vimrc file&lt;/span&gt;
nmap &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;silent&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;leader&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;ev :&lt;span class="k"&gt;e&lt;/span&gt; $MYVIMRC&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;CR&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
nmap &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;silent&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;leader&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="k"&gt;sv&lt;/span&gt; :&lt;span class="k"&gt;so&lt;/span&gt; $MYVIMRC&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;CR&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This effectively maps the &lt;code&gt;,ev&lt;/code&gt; and &lt;code&gt;,sv&lt;/code&gt; keys to edit/reload &lt;code&gt;.vimrc&lt;/code&gt;.  (I got
this from &lt;a href="http://derekwyatt.org/"&gt;Derek Wyatt&lt;/a&gt;’s &lt;code&gt;.vimrc&lt;/code&gt; file.)&lt;/p&gt;
&lt;h2 id="change-vim-behaviour"&gt;Change Vim behaviour &lt;a href="#change-vim-behaviour" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;One particularly useful setting is &lt;code&gt;hidden&lt;/code&gt;. Its name isn’t too descriptive,
though. It &lt;em&gt;hides&lt;/em&gt; buffers instead of &lt;em&gt;closing&lt;/em&gt; them. This means that you can
have unwritten changes to a file and open a new file using &lt;code&gt;:e&lt;/code&gt;, without being
forced to write or undo your changes first. Also, undo buffers and marks are
preserved while the buffer is open. This is an absolute must-have.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;hidden&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;These are some of the most basic settings that you probably want to enable,
too:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;nowrap&lt;/span&gt;        &lt;span class="c"&gt;&amp;quot; don&amp;#39;t wrap lines&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;tabstop&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="m"&gt;4&lt;/span&gt;     &lt;span class="c"&gt;&amp;quot; a tab is four spaces&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;backspace&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;indent&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nb"&gt;eol&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="k"&gt;start&lt;/span&gt;
&lt;span class="c"&gt;                    &amp;quot; allow backspacing over everything in insert mode&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;autoindent&lt;/span&gt;    &lt;span class="c"&gt;&amp;quot; always set autoindenting on&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;copyindent&lt;/span&gt;    &lt;span class="c"&gt;&amp;quot; copy the previous indentation on autoindenting&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="k"&gt;number&lt;/span&gt;        &lt;span class="c"&gt;&amp;quot; always show line numbers&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;shiftwidth&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="m"&gt;4&lt;/span&gt;  &lt;span class="c"&gt;&amp;quot; number of spaces to use for autoindenting&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;shiftround&lt;/span&gt;    &lt;span class="c"&gt;&amp;quot; use multiple of shiftwidth when indenting with &amp;#39;&amp;lt;&amp;#39; and &amp;#39;&amp;gt;&amp;#39;&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;showmatch&lt;/span&gt;     &lt;span class="c"&gt;&amp;quot; set show matching parenthesis&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;ignorecase&lt;/span&gt;    &lt;span class="c"&gt;&amp;quot; ignore case when searching&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;smartcase&lt;/span&gt;     &lt;span class="c"&gt;&amp;quot; ignore case if search pattern is all lowercase,&lt;/span&gt;
&lt;span class="c"&gt;                    &amp;quot;    case-sensitive otherwise&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;smarttab&lt;/span&gt;      &lt;span class="c"&gt;&amp;quot; insert tabs on the start of a line according to&lt;/span&gt;
&lt;span class="c"&gt;                    &amp;quot;    shiftwidth, not tabstop&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;hlsearch&lt;/span&gt;      &lt;span class="c"&gt;&amp;quot; highlight search terms&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;incsearch&lt;/span&gt;     &lt;span class="c"&gt;&amp;quot; show search matches as you type&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;There is a lot more goodness in my
&lt;a href="http://github.com/nvie/vimrc/raw/main/vimrc"&gt;&lt;code&gt;.vimrc&lt;/code&gt;&lt;/a&gt; file, which is put in
there with a lot of love. I’ve commented most of it, too. Feel free to poke
around in it.&lt;/p&gt;
&lt;p&gt;Also, I like Vim to have a large undo buffer, a large history of commands,
ignore some file extensions when completing names by pressing Tab, and be
silent about invalid cursor moves and other errors.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="k"&gt;history&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="m"&gt;1000&lt;/span&gt;         &lt;span class="c"&gt;&amp;quot; remember more commands and search history&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;undolevels&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="m"&gt;1000&lt;/span&gt;      &lt;span class="c"&gt;&amp;quot; use many muchos levels of undo&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;wildignore&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;*.swp&lt;span class="p"&gt;,&lt;/span&gt;*.bak&lt;span class="p"&gt;,&lt;/span&gt;*.pyc&lt;span class="p"&gt;,&lt;/span&gt;*.class
&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;title&lt;/span&gt;                &lt;span class="c"&gt;&amp;quot; change the terminal&amp;#39;s title&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;visualbell&lt;/span&gt;           &lt;span class="c"&gt;&amp;quot; don&amp;#39;t beep&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;noerrorbells&lt;/span&gt;         &lt;span class="c"&gt;&amp;quot; don&amp;#39;t beep&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I don't like Vim to ever write a backup file.  I prefer &lt;a href="http://git-scm.com/"&gt;more modern
ways&lt;/a&gt; of protecting against data loss.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;nobackup&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;noswapfile&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;There have been some passionate responses about this in comments, so a warning
may be appropriate here. If you care about recovering after a Vim or terminal
emulator crash, or you often load huge files into memory, do &lt;strong&gt;not&lt;/strong&gt; disable
the swapfile. I personally save/commit so
&lt;a href="http://jeffkreeftmeijer.com/2010/git-your-act-together/#commit-all-the-fucking-time"&gt;often&lt;/a&gt;
that the swap file adds nothing. Sometimes I conciously kill a terminal
forcefully, and I only find the swap file recovery process annoying.&lt;/p&gt;
&lt;h2 id="use-file-type-plugins"&gt;Use file type plugins &lt;a href="#use-file-type-plugins" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Vim can detect file types (by their extension, or by peeking inside the file).
This enabled Vim to load plugins, settings or key mappings that are only useful
in the context of specific file types. For example, a Python syntax checker
plugin only makes sense in a Python file. Finally, indenting intelligence is
enabled based on the syntax rules for the file type.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;filetype&lt;/span&gt; plugin indent &lt;span class="k"&gt;on&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To set some file type specific settings, you can now use the following:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;autocmd &lt;span class="k"&gt;filetype&lt;/span&gt; python &lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;expandtab&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To remain compatible with older versions of Vim that do not have the &lt;code&gt;autocmd&lt;/code&gt;
functions, always wrap those functions inside a block like this:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; has&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;autocmd&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    ...
&lt;span class="k"&gt;endif&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="enable-syntax-highlighting"&gt;Enable syntax highlighting &lt;a href="#enable-syntax-highlighting" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Somewhat related to the file type plugins is the syntax highlighting of
different types of source files. Vim uses syntax definitions to highlight
source code. Syntax definitions simply declare where a function name starts,
which pieces are commented out and what are keywords. To color them, Vim uses
colorschemes. You can load custom color schemes by placing them in
&lt;code&gt;.vim/colors&lt;/code&gt;, then load them using the &lt;code&gt;colorscheme&lt;/code&gt; command. You have to try
what you like most. I like
&lt;a href="http://hcalves.deviantart.com/art/Mustang-Vim-Colorscheme-98974484"&gt;mustang&lt;/a&gt;
a lot.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &amp;amp;&lt;span class="nb"&gt;t_Co&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="m"&gt;256&lt;/span&gt; &lt;span class="p"&gt;||&lt;/span&gt; has&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;gui_running&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;colorscheme&lt;/span&gt; mustang
&lt;span class="k"&gt;endif&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &amp;amp;&lt;span class="nb"&gt;t_Co&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt; &lt;span class="p"&gt;||&lt;/span&gt; has&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;gui_running&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c"&gt;    &amp;quot; switch syntax highlighting on, when the terminal has colors&lt;/span&gt;
    &lt;span class="nb"&gt;syntax&lt;/span&gt; &lt;span class="k"&gt;on&lt;/span&gt;
&lt;span class="k"&gt;endif&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this case, mustang is only loaded if the terminal emulator Vim runs in
supports at least 256 colors (or if you use the GUI version of Vim).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Hint&lt;/strong&gt;:
If you’re using a terminal emulator that can show 256 colors, try setting
&lt;code&gt;TERM=xterm-256color&lt;/code&gt; in your terminal configuration or in your shell’s .rc
file.&lt;/p&gt;
&lt;h2 id="change-editing-behaviour"&gt;Change editing behaviour &lt;a href="#change-editing-behaviour" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;When you write a lot of code, you probably want to obey certain style rules.
In some programming languages (like Python), whitespace is important, so you
may not just swap tabs for spaces and even the number of spaces is important.&lt;/p&gt;
&lt;p&gt;Vim can highlight whitespaces for you in a convenient way:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;listchars&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="k"&gt;tab&lt;/span&gt;:&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;.&lt;span class="p"&gt;,&lt;/span&gt;trail:.&lt;span class="p"&gt;,&lt;/span&gt;extends:#&lt;span class="p"&gt;,&lt;/span&gt;nbsp:.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This line will make Vim set out tab characters, trailing whitespace and
invisible spaces visually, and additionally use the &lt;code&gt;#&lt;/code&gt; sign at the end of
lines to mark lines that extend off-screen. For more info, see &lt;code&gt;:h listchars&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In some files, like HTML and XML files, tabs are fine and showing them is
really annoying, you can disable them easily using an &lt;code&gt;autocmd&lt;/code&gt; declaration:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;autocmd &lt;span class="k"&gt;filetype&lt;/span&gt; html&lt;span class="p"&gt;,&lt;/span&gt;xml &lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;listchars&lt;/span&gt;&lt;span class="p"&gt;-=&lt;/span&gt;&lt;span class="k"&gt;tab&lt;/span&gt;:&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;One caveat when setting &lt;code&gt;listchars&lt;/code&gt;: if nothing happens, you have probably not
enabled &lt;code&gt;list&lt;/code&gt;, so try &lt;code&gt;:set list&lt;/code&gt;, too.&lt;/p&gt;
&lt;h2 id="pasting-large-amounts-of-text-into-vim"&gt;Pasting large amounts of text into Vim &lt;a href="#pasting-large-amounts-of-text-into-vim" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Every Vim user likes to enable auto-indenting of source code, so Vim can
intelligently position you cursor on the next line as you type. This has one
big ugly consequence however: when you paste text into your terminal-based Vim
with a right mouse click, Vim cannot know it is coming from a paste. To Vim, it
looks like text entered by someone who can type incredibly fast :) Since Vim
thinks this is regular key strokes, it applies all auto-indenting and
auto-expansion of defined abbreviations to the input, resulting in often
cascading indents of paragraphs.&lt;/p&gt;
&lt;p&gt;There is an easy option to prevent this, however. You can temporarily switch to
“paste mode”, simply by setting the following option:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;pastetoggle&lt;/span&gt;&lt;span class="p"&gt;=&amp;lt;&lt;/span&gt;F2&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then, when in insert mode, ready to paste, if you press &lt;code&gt;&amp;lt;F2&amp;gt;&lt;/code&gt;, Vim will switch
to paste mode, disabling all kinds of smartness and just pasting a whole buffer
of text. Then, you can disable paste mode again with another press of &lt;code&gt;&amp;lt;F2&amp;gt;&lt;/code&gt;.
Nice and simple. Compare paste mode disabled vs enabled:&lt;/p&gt;
&lt;p&gt;&lt;img alt="" class="left" src="/img/ugly-paste.png" style="margin-left: -40px" /&gt;
&lt;img alt="" class="right" src="/img/better-paste.png" style="margin-right: -40px" /&gt;&lt;/p&gt;
&lt;p style="clear: both"&gt;Another great trick I read in a &lt;a href="http://www.reddit.com/r/programming/comments/ddbuc/how_i_boosted_my_vim/c0zelsm"&gt;reddit
comment&lt;/a&gt;
is to use &lt;code&gt;&amp;lt;C-r&amp;gt;+&lt;/code&gt; to paste right from the OS paste board. Of course, this only
works when running Vim locally (i.e. not over an SSH connection).&lt;/p&gt;
&lt;h2 id="enable-the-mouse"&gt;Enable the mouse &lt;a href="#enable-the-mouse" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;While using the mouse is considered a deadly sin among Vim users, there &lt;em&gt;are&lt;/em&gt;
a few features about the mouse that can really come to your advantage.  Most
notably—scrolling. In fact, it’s the only thing I use it for.&lt;/p&gt;
&lt;p&gt;Also, if you are a rookie Vim user, setting this value will make your Vim
experience definitively feel more natural.&lt;/p&gt;
&lt;p&gt;To enable the mouse, use:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;mouse&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="k"&gt;a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;However, this comes at one big disadvantage: when you run Vim inside
a terminal, the terminal itself cannot control your mouse anymore.  Therefore,
you cannot select text anymore with the terminal (to copy it to the system
clipboard, for example).&lt;/p&gt;
&lt;p&gt;To be able to have the best of both worlds, I wrote this simple Vim plugin:
&lt;a href="http://github.com/nvie/vim-togglemouse"&gt;vim-togglemouse&lt;/a&gt;. It maps &lt;code&gt;&amp;lt;F12&amp;gt;&lt;/code&gt; to
toggle your mouse “focus” between Vim and the terminal.&lt;/p&gt;
&lt;p&gt;Small plugins like these are really useful, yet have the additional benefit of
lowering the barrier of learning the Vim scripting language. At the core, this
plugin exists of only one simple function:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt; s:ToggleMouse&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;!&lt;/span&gt;exists&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;s:old_mouse&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; s:old_mouse &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;a&amp;quot;&lt;/span&gt;
    &lt;span class="k"&gt;endif&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &amp;amp;&lt;span class="nb"&gt;mouse&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&amp;quot;&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &amp;amp;&lt;span class="nb"&gt;mouse&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; s:old_mouse
        echo &lt;span class="s2"&gt;&amp;quot;Mouse is for Vim (&amp;quot;&lt;/span&gt; . &amp;amp;&lt;span class="nb"&gt;mouse&lt;/span&gt; . &lt;span class="s2"&gt;&amp;quot;)&amp;quot;&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; s:old_mouse &lt;span class="p"&gt;=&lt;/span&gt; &amp;amp;&lt;span class="nb"&gt;mouse&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &amp;amp;&lt;span class="nb"&gt;mouse&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&amp;quot;&lt;/span&gt;
        echo &lt;span class="s2"&gt;&amp;quot;Mouse is for terminal&amp;quot;&lt;/span&gt;
    &lt;span class="k"&gt;endif&lt;/span&gt;
&lt;span class="k"&gt;endfunction&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="get-efficient-shortcut-mappings"&gt;Get efficient: shortcut mappings &lt;a href="#get-efficient-shortcut-mappings" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The following trick is a really small one, but a super-efficient one, since it
strips off two full keystrokes from almost every Vim command:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nb"&gt;nnoremap&lt;/span&gt; ; :
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;For example, to save a file, you type &lt;code&gt;:w&lt;/code&gt; normally, which means:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Press and hold Shift&lt;/li&gt;
&lt;li&gt;Press &lt;code&gt;;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Release the Shift key&lt;/li&gt;
&lt;li&gt;Press &lt;code&gt;w&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Press Return&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This trick strips off steps 1 and 3 for &lt;strong&gt;each&lt;/strong&gt; Vim command. It takes some
times for your muscle memory to get used to this new &lt;code&gt;;w&lt;/code&gt; command, but once you
use it, you don’t want to go back!&lt;/p&gt;
&lt;p&gt;I also find this key binding very useful, since I like to reformat paragraph
text often. Just set your cursor inside a paragraph and press &lt;code&gt;Q&lt;/code&gt; (or select
a visual block and press &lt;code&gt;Q&lt;/code&gt;).&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;quot; Use Q for formatting the current paragraph (or selection)&lt;/span&gt;
vmap Q gq
nmap Q gqap
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you are still getting used to Vim and want to force yourself to stop using
the arrow keys, add this:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;map &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;up&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;nop&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
map &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;down&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;nop&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
map &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;left&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;nop&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
map &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;right&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;nop&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you like long lines with line wrapping enabled, this solves the problem that
pressing down jumpes your cursor “over” the current line to the next line. It
changes behaviour so that it jumps to the next row in the editor (much more
natural):&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nb"&gt;nnoremap&lt;/span&gt; &lt;span class="k"&gt;j&lt;/span&gt; gj
&lt;span class="nb"&gt;nnoremap&lt;/span&gt; &lt;span class="k"&gt;k&lt;/span&gt; gk
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;When you start to use Vim more professionally, you want to work with multiple
windows open. Navigating requires you to press &lt;code&gt;C-w&lt;/code&gt; first, then a navigation
command (h, j, k, l). This makes it easier to navigate focus through windows:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;quot; Easy window navigation&lt;/span&gt;
map &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;C&lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="k"&gt;h&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;C&lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="k"&gt;w&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="k"&gt;h&lt;/span&gt;
map &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;C&lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="k"&gt;j&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;C&lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="k"&gt;w&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="k"&gt;j&lt;/span&gt;
map &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;C&lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="k"&gt;k&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;C&lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="k"&gt;w&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="k"&gt;k&lt;/span&gt;
map &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;C&lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="k"&gt;l&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;C&lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="k"&gt;w&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="k"&gt;l&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Tired of clearing highlighted searches by searching for “ldsfhjkhgakjks”? Use
this:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;nmap &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;silent&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt;/ :&lt;span class="k"&gt;nohlsearch&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;CR&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I used to have it mapped to &lt;code&gt;:let&lt;/code&gt;/=“”&lt;CR&gt;&lt;code&gt;, but some users kindly pointed out
that it is better to use&lt;/code&gt;:nohlsearch@, because it keeps the search history
intact.&lt;/p&gt;
&lt;p&gt;It clears the search buffer when you press &lt;code&gt;,/&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Finally, a trick by &lt;a href="http://forrst.com/posts/Use_w_to_sudo_write_a_file_with_Vim-uAN"&gt;Steve
Losh&lt;/a&gt; for when
you forgot to &lt;code&gt;sudo&lt;/code&gt; before editing a file that requires root privileges
(typically &lt;code&gt;/etc/hosts&lt;/code&gt;). This lets you use &lt;code&gt;w!!&lt;/code&gt; to do that &lt;strong&gt;after&lt;/strong&gt; you
opened the file already:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;cmap &lt;span class="k"&gt;w&lt;/span&gt;&lt;span class="p"&gt;!!&lt;/span&gt; &lt;span class="k"&gt;w&lt;/span&gt; &lt;span class="p"&gt;!&lt;/span&gt;sudo tee % &lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="sr"&gt;/dev/&lt;/span&gt;null
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="other-cool-plugins"&gt;Other cool plugins &lt;a href="#other-cool-plugins" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In order to make the article not any more longer than it already is, here’s
a list of other plugins that are really worth checking out (I use each of them
regularly):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href="http://www.vim.org/scripts/script.php?script_id=1408"&gt;localrc&lt;/a&gt;: lets you
  load specific Vim settings for any file in the same directory (or
  a subdirectory thereof). Comes in super handy for project-wide settings.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href="http://kien.github.io/ctrlp.vim/"&gt;CtrlP&lt;/a&gt;: lets you open files or switch
  buffers quickly using fuzzy search.  I'd highly recommend it.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="other-resources"&gt;Other resources &lt;a href="#other-resources" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Some of the resources from where I have collected inspiration for my &lt;code&gt;.vimrc&lt;/code&gt;
file, plugins, and tricks:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://vimcasts.org/"&gt;Vimcasts&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://lococast.net/"&gt;Lococast&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://vimeo.com/user1690209/videos"&gt;Derek Wyatt’s videos&lt;/a&gt; (on Vimeo)&lt;/li&gt;
&lt;li&gt;Steve Losh blogged about &lt;a href="http://stevelosh.com/blog/2010/09/coming-home-to-vim/"&gt;moving back to
  Vim&lt;/a&gt; and has some
  great tips and tricks.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I hope you like these tips.  You can have a look at my full Vim configuration
in my &lt;a href="http://github.com/nvie/vimrc"&gt;Github repo&lt;/a&gt;.&lt;/p&gt;</content>
    <link href="https://nvie.com/posts/how-i-boosted-my-vim/"/>
    <summary>Where I lay out the recent changed I made to my Vim setup.</summary>
    <published>2010-09-14T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://nvie.com/posts/a-git-flow-screencast/</id>
    <title>A git-flow screencast</title>
    <updated>2011-05-25T00:00:00+00:00</updated>
    <content type="html">&lt;p&gt;Mr. &lt;a href="http://www.davebock.com/"&gt;Dave Bock&lt;/a&gt; of Code Sherpa’s put together a nice
screencast demonstrating a few of the most important git-flow features on their
&lt;a href="http://www.codesherpas.com/portfolio/publications"&gt;publications&lt;/a&gt; page.&lt;/p&gt;
&lt;p class="centered"&gt;&lt;a href="http://codesherpas.com/screencasts/on_the_path_gitflow.mov"&gt;&lt;img alt="" src="/img/screencast-still.png" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Many thanks to Dave for creating this!&lt;/p&gt;</content>
    <link href="https://nvie.com/posts/a-git-flow-screencast/"/>
    <summary>Mr. [Dave Bock](http://www.davebock.com/) of Code Sherpa’s put together a nice
screencast demonstrating a few of the most important git-flow features on their
[publications](http://www.codesherpas.com/portfolio/publications) page.</summary>
    <published>2011-01-27T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://nvie.com/posts/chords-lyrics/</id>
    <title>Chords + Lyrics</title>
    <updated>2011-10-22T00:00:00+00:00</updated>
    <content type="html">&lt;p&gt;It’s been quite a while since I took the time to update this blog. Many things
have happened in the meanwhile, though. The most important happening for me is
that I launched an iPad app and I founded a company called &lt;a href="http://www.3rdcloud.com"&gt;3rd Cloud&lt;/a&gt; last
week.&lt;/p&gt;
&lt;h2 id="hello-chords-lyrics"&gt;Hello, Chords + Lyrics! &lt;a href="#hello-chords-lyrics" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;An annoying problem amateur musicians might be familiar with is that chords or
tablature websites all look very differently, format their song data in various
formats, and oftentimes are just plain ugly. Oh, and they’re generally paved
with ads, too. So to scratch our own itches, I teamed up with
&lt;a href="http://twitter.com/jr00n"&gt;@jr00n&lt;/a&gt; from
&lt;a href="http://www.studiowolff.nl"&gt;StudioWolff&lt;/a&gt; to create &lt;a href="http://www.chordsandlyricsapp.com"&gt;Chords + Lyrics&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Chords + Lyrics is a simple music manager for your iPad that allows you to
easily import songs and lyrics from your favorite chords website (that means:
&lt;em&gt;any&lt;/em&gt; website—it recognizes the chords semantically):&lt;/p&gt;
&lt;p class="centered"&gt;&lt;img alt="" src="/img/showcase3.png" /&gt;&lt;/p&gt;
&lt;p&gt;Once imported, the chords become editable objects in the form of bubbles, which
makes it easy for you to edit or finetune the imported songs.  A carefully
selected choice of fonts is used to create a readable and uniform look and feel
for your song’s chords and lyrics:&lt;/p&gt;
&lt;p class="centered"&gt;&lt;img alt="" src="/img/showcase1.png" /&gt;&lt;/p&gt;
&lt;p&gt;Furthermore, once the editing is done, you can simply take Chords + Lyrics on
stage with your band or solo performance and leave your stack of sheet music at
home. When the device is rotated into landscape orientation, the user interface
transforms into a big music stand that arranges the songs of choice as a stack
of virtual music sheets in the order of your playlist:&lt;/p&gt;
&lt;p class="centered"&gt;&lt;img alt="" src="/img/showcase4.png" /&gt;&lt;/p&gt;
&lt;p&gt;You can get a sneak peak of the app by watching this video:&lt;/p&gt;
&lt;iframe width="560" height="420" src="https://www.youtube.com/embed/pG51bemU0ok?rel=0&amp;amp;hd=1" frameborder="0" allowfullscreen&gt;
&lt;/iframe&gt;

&lt;p&gt;The app is sold at $5.99. Check us out in the &lt;a href="http://itunes.apple.com/us/app/chords-lyrics/id462295346?ls=1&amp;amp;mt=8"&gt;App Store&lt;/a&gt;.&lt;/p&gt;
&lt;p class="centered"&gt;&lt;a href="http://itunes.apple.com/us/app/chords-lyrics/id462295346?ls=1&amp;amp;mt=8"&gt;&lt;img alt="" src="/img/appstore_badge.png" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="the-role-of-appsterdam"&gt;The Role of Appsterdam &lt;a href="#the-role-of-appsterdam" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Jeroen and I met during &lt;a href="http://www.iosdevcamp.org/"&gt;iOS Dev Camp&lt;/a&gt; last March
and immediately were excited with the idea for Chords + Lyrics. We even
received the &lt;em&gt;Most Likely to Succeed&lt;/em&gt; award from &lt;a href="http://twitter.com/dom"&gt;Dom
Sogolla&lt;/a&gt; and &lt;a href="http://twitter.com/bmf"&gt;Mike Lee&lt;/a&gt; at the
end of those two days, which got us even more excited about the project.&lt;/p&gt;
&lt;p&gt;We started hacking away at it in our spare time for the next few months.  While
at the same time, by some kind of lucky coincidence, the same Mike Lee started
building an awesome developer community for app developers:
&lt;a href="http://www.appsterdam.rs"&gt;Appsterdam&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Many of our thanks therefore go out to all of the Appsterdammers, in particular
Mike and &lt;a href="http://twitter.com/judykitteh"&gt;Judy&lt;/a&gt;, for inspiring us at the moments
we got stuck and for the helpful pieces of advice we got from them. Without the
Appsterdam community, the project may have never seen the light of day, or be
much less awesome.&lt;/p&gt;
&lt;p&gt;Let this be the first of many apps!&lt;/p&gt;</content>
    <link href="https://nvie.com/posts/chords-lyrics/"/>
    <summary>I released my first iPad app to import/manage chords and lyrics.</summary>
    <published>2011-10-05T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://nvie.com/posts/introducing-times/</id>
    <title>Introducing Times</title>
    <updated>2012-02-08T00:00:00+00:00</updated>
    <content type="html">&lt;p&gt;Lately I’ve been getting sick of working with datetimes and timezones in
Python. The standard library offers many different conversion routines, but
does not prescribe a best practice way to deal with them. Luckily, Armin
Ronacher did in his article &lt;a href="http://lucumr.pocoo.org/2011/7/15/eppur-si-muove/"&gt;Dealing with Timezones in
Python&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The summary is to never ever work with local datetimes. When a local datetime
is input, immediately convert it to universal time and only ever store or
calculate with those. Only when &lt;em&gt;presenting&lt;/em&gt; datetimes to the end user, convert
them to local time again.&lt;/p&gt;
&lt;p&gt;This seems simple enough, alright. But to actually &lt;em&gt;do&lt;/em&gt; it in Python, you still
have to think about how to implement it correctly. Every. Single.  Time.
&lt;code&gt;pytz&lt;/code&gt; does help a bit here, but it still isn’t trivial. It should be.&lt;/p&gt;
&lt;p&gt;Meet &lt;a href="https://github.com/nvie/times"&gt;&lt;code&gt;Times&lt;/code&gt;&lt;/a&gt;, a very small Python library to
deal with conversions from universal to local timezones and vice versa.  It’s
focused on simplicity and opinionated about what is good practice.&lt;/p&gt;
&lt;h2 id="example-use"&gt;Example use &lt;a href="#example-use" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Imagine you’re building a web app that allows your users to set an alarm. Say
that someone in the Netherlands sets an alarm to 9:30 am. You can use &lt;code&gt;times&lt;/code&gt;
to simplify this:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;times&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;datetime&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;local_time&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2012&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;universal_time&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;times&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_universal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;local_time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;Europe/Amsterdam&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;universal_time&lt;/span&gt;
&lt;span class="go"&gt;datetime.datetime(2012, 2, 3, 8, 30)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now, this &lt;code&gt;universal_time&lt;/code&gt; variable is safe to store or calculate with.&lt;/p&gt;
&lt;p&gt;Once you want to show this date to the user again, simply format it for the
given timezone:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;times&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;universal_time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;Europe/Amsterdam&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="go"&gt;&amp;#39;2012-02-03 09:30:00+0100&amp;#39;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If your app allows users to share alerts, it is just as easy to present the
alert date to an end user in New Zealand as well:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;times&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;universal_time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;Pacific/Auckland&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="go"&gt;&amp;#39;2012-02-03 21:30:00+1300&amp;#39;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="current-time"&gt;Current time &lt;a href="#current-time" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;If you ever need to record the current time, you can use&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;times&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="go"&gt;datetime.datetime(2012, 2, 2, 16, 4, 40, 283090)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Which is actually just an alias to &lt;code&gt;datetime.datetime.utcnow()&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id="converting-from-other-sources"&gt;Converting from other sources &lt;a href="#converting-from-other-sources" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I’ve added the ability to create universal times from two other sources: UNIX
timestamps and date strings. To use any of these, simply pass them to the
&lt;code&gt;to_universal&lt;/code&gt; function, like so:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="go"&gt;1328729274.982&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;times&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_universal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;1328729274.982&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="go"&gt;datetime.datetime(2012, 2, 8, 19, 27, 54, 982000)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Note that UNIX timestamps &lt;em&gt;must&lt;/em&gt; be in UTC (which the output of &lt;code&gt;time.time()&lt;/code&gt;
is). Local UNIX timestamps are not accepted.&lt;/p&gt;
&lt;p&gt;To create universal times from string representations, &lt;code&gt;Times&lt;/code&gt; uses the
advanced parser from the &lt;code&gt;python-dateutil&lt;/code&gt; library. Time zones are
automatically recognized if such info is encoded in the string representation.
In any other case, you are required to provide it explicitly. Two examples to
illustrate both variants:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="c1"&gt;# Timezone-aware date formats don&amp;#39;t require a source timezone&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;date_str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;2012-02-08 19:27:54+0100&amp;#39;&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;times&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_universal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;date_str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="go"&gt;datetime.datetime(2012, 2, 8, 18, 27, 54)&lt;/span&gt;

&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="c1"&gt;# Timezone-less date formats require an explicit source timezone&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;date_str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;2012-02-08 19:27:54&amp;#39;&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;times&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_universal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;date_str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;Asia/Singapore&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="go"&gt;datetime.datetime(2012, 2, 8, 11, 27, 54)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="installing"&gt;Installing &lt;a href="#installing" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;Times&lt;/code&gt; is on PyPI (&lt;a href="http://pypi.python.org/pypi/times"&gt;link&lt;/a&gt;), so just &lt;code&gt;pip
install times&lt;/code&gt; to use it.&lt;/p&gt;
&lt;p&gt;Of course, you can &lt;a href="http://github.com/nvie/times"&gt;fork me on GitHub&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;As usual, &lt;code&gt;Times&lt;/code&gt; is licensed under the liberal terms of the BSD license.&lt;/p&gt;</content>
    <link href="https://nvie.com/posts/introducing-times/"/>
    <summary>Lately I’ve been getting sick of working with datetimes and timezones in
Python. The standard library offers many different conversion routines, but
does not prescribe a best practice way to deal with them. Luckily, Armin
Ronacher did in his article [Dealing with Timezones in
Python](http://lucumr.pocoo.org/2011/7/15/eppur-si-muove/).</summary>
    <published>2012-02-02T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://nvie.com/posts/vim-flake8-flake8-for-vim/</id>
    <title>vim-flake8: Flake8 for Vim</title>
    <updated>2012-02-14T00:00:00+00:00</updated>
    <content type="html">&lt;p&gt;Just a quick post to let you know that I discarded my &lt;code&gt;vim-pep8&lt;/code&gt; and
&lt;code&gt;vim-pyflakes&lt;/code&gt; Vim plugins yesterday in favor of
&lt;a href="https://github.com/nvie/vim-flake8"&gt;vim-flake8&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;As you may know, PyFlakes is a static analysis tool that lets you catch static
programming errors when you write them, not when you run into them at runtime.
And &lt;code&gt;pep8&lt;/code&gt; is a Python style checking tool that enforces
&lt;a href="http://www.python.org/dev/peps/pep-0008/"&gt;PEP8&lt;/a&gt; guidelines on your code.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://pypi.python.org/pypi/flake8"&gt;Flake8&lt;/a&gt;, though, seems to be a much better
option to use these days. It integrates both of PEP8 and PyFlakes and even
combines it with a cyclomatic complexity checker (which is irrelevant for the
Vim plugin, by the way). To install Flake8, simply use:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;pip install flake8
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;After installing the plugin in Vim, you can add the following command to your
&lt;code&gt;.vimrc&lt;/code&gt; file to have it executed after every save of a Python source file.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;autocmd &lt;span class="nb"&gt;BufWritePost&lt;/span&gt; *.&lt;span class="k"&gt;py&lt;/span&gt; &lt;span class="k"&gt;call&lt;/span&gt; Flake8&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To avoid specific error messages from being reported, put a &lt;code&gt;# noqa&lt;/code&gt; comment at
the end of that line.&lt;/p&gt;
&lt;h2 id="installation"&gt;Installation &lt;a href="#installation" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Assuming you already use &lt;a href="https://github.com/tpope/vim-pathogen"&gt;vim-pathogen&lt;/a&gt;
(which you really should), you can simply install the plugin by cloning the
&lt;a href="https://github.com/nvie/vim-flake8"&gt;repository&lt;/a&gt; into the &lt;code&gt;~/.vim/bundle&lt;/code&gt;
folder.&lt;/p&gt;</content>
    <link href="https://nvie.com/posts/vim-flake8-flake8-for-vim/"/>
    <summary>Just a quick post to let you know that I discarded my `vim-pep8` and
`vim-pyflakes` Vim plugins yesterday in favor of
[vim-flake8](https://github.com/nvie/vim-flake8).</summary>
    <published>2012-02-14T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://nvie.com/posts/introducing-rq/</id>
    <title>Introducing RQ</title>
    <updated>2012-03-28T00:00:00+00:00</updated>
    <content type="html">&lt;p&gt;Today, I’m open sourcing a project that I’ve been working for the last few
months. It is a Python library to put work in the background, that you’d
typically use in a web context. It is designed to be simple to set up and use,
and be of help in almost any modern Python web stack.&lt;/p&gt;
&lt;p class="centered"&gt;&lt;img alt="" class="no-dark-bg" src="/img/rq-site@2x.png" style="max-width: 637px" /&gt;&lt;/p&gt;
&lt;h2 id="existing-solutions"&gt;Existing solutions &lt;a href="#existing-solutions" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Of course, there already exist a few solutions to this problem.
&lt;a href="http://celeryproject.org/"&gt;Celery&lt;/a&gt; (by the excellent
&lt;a href="http://twitter.com/#!/asksol"&gt;@asksol&lt;/a&gt;) is by far the most popular Python
framework for working with asynchronous tasks. It is agnostic about the
underlying queueing implementation, which is quite powerful, but also poses
a learning curve and requires a fair amount of setup.&lt;/p&gt;
&lt;p&gt;Don’t get me wrong—I think Celery is a great library. In fact, &lt;a href="https://github.com/ask/celery/commits/master?author=nvie"&gt;I’ve
contributed&lt;/a&gt; to
Celery myself in the past. My experiences are, however, that as your Python web
project grows, there comes this moment where you want to start offloading small
pieces of code into the background. Setting up Celery for these cases is
a substantial effort that isn’t done swiftly and might be holding you back.&lt;/p&gt;
&lt;p&gt;I wanted something simpler. Something that you’d use in &lt;em&gt;all&lt;/em&gt; of your Python
web projects, not only the big and serious ones.&lt;/p&gt;
&lt;h2 id="redis-as-a-broker"&gt;Redis as a broker &lt;a href="#redis-as-a-broker" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In many modern web stacks, chances are that you’re already using
&lt;a href="http://redis.io/"&gt;Redis&lt;/a&gt; (by &lt;a href="http://twitter.com/#!/antirez"&gt;@antirez&lt;/a&gt;).
Besides being a kick-ass key value store, Redis also provides semantics to
build a perfect queue implementation. The commands
&lt;a href="http://redis.io/commands/rpush"&gt;&lt;code&gt;RPUSH&lt;/code&gt;&lt;/a&gt;,
&lt;a href="http://redis.io/commands/lpop"&gt;&lt;code&gt;LPOP&lt;/code&gt;&lt;/a&gt; and
&lt;a href="http://redis.io/commands/blpop"&gt;&lt;code&gt;BLPOP&lt;/code&gt;&lt;/a&gt; are all it takes.&lt;/p&gt;
&lt;p&gt;Inspired by &lt;a href="https://github.com/resque/resque"&gt;Resque&lt;/a&gt; (by
&lt;a href="https://github.com/defunkt/resque"&gt;defunkt&lt;/a&gt;) and the simplicity of &lt;a href="http://flask.pocoo.org/snippets/73/"&gt;this Flask
snippet&lt;/a&gt; (by
&lt;a href="http://twitter.com/#!/mitsuhiko"&gt;@mitsuhiko&lt;/a&gt;), I’ve challenged myself to
imagine just how hard a job queue library really should be.&lt;/p&gt;
&lt;h2 id="introducing-rq"&gt;Introducing RQ &lt;a href="#introducing-rq" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I wanted a solution that was lightweight, easy to adopt, and easy to grasp. So
I devised a simple queueing library for Python, and dubbed it &lt;a href="https://github.com/nvie/rq"&gt;RQ&lt;/a&gt;. In
a nutshell, you define a job like you would any normal Python function.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;myfunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now, with RQ, it is ridiculously easy to put it in the background like this:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;rq&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;use_connection&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Queue&lt;/span&gt;

&lt;span class="c1"&gt;# Connect to Redis&lt;/span&gt;
&lt;span class="n"&gt;use_connection&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# Offload the &amp;quot;myfunc&amp;quot; invocation&lt;/span&gt;
&lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Queue&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myfunc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;318&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;62&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This puts the equivalent of &lt;code&gt;myfunc(318, 62)&lt;/code&gt; on the &lt;code&gt;default&lt;/code&gt; queue. Now, in
another shell, run a separate worker process to perform the actual work:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;rqworker
&lt;span class="go"&gt;12:46:56:&lt;/span&gt;
&lt;span class="go"&gt;12:46:56: *** Listening on default...&lt;/span&gt;
&lt;span class="go"&gt;12:47:35: default: mymodule.myfunc(318, 62) (38d9c157-e997-40e2-8d20-574a97ec5a99&lt;/span&gt;
&lt;span class="go"&gt;12:47:35: Job OK, result = 19716&lt;/span&gt;
&lt;span class="go"&gt;12:47:35:&lt;/span&gt;
&lt;span class="go"&gt;12:47:35: *** Listening on default...&lt;/span&gt;
&lt;span class="go"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To poll for the asynchronous result in the web backend, you can use:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myfunc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;318&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;62&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;return_value&lt;/span&gt;
&lt;span class="go"&gt;None&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;return_value&lt;/span&gt;
&lt;span class="go"&gt;19716&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Although I must admit that polling for job results through the &lt;code&gt;return_value&lt;/code&gt;
isn’t quite useful and probably won’t be a pattern that you’d use in your
day-to-day work. (I would certainly recommend against doing that, at least.)&lt;/p&gt;
&lt;p&gt;There’s extensive documentation available at: &lt;a href="http://nvie.github.com/rq"&gt;http://nvie.github.com/rq&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id="near-zero-configuration"&gt;Near-zero configuration &lt;a href="#near-zero-configuration" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;RQ was designed to be as easy as possible to start using it immediately inside
your Python web projects. You only need to pass it a Redis connection to use,
because I didn’t want it to create new connections implicitly.&lt;/p&gt;
&lt;p&gt;To use the default Redis connection (to &lt;code&gt;localhost:6379&lt;/code&gt;), you only have to do
this:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;rq&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;use_connection&lt;/span&gt;
&lt;span class="n"&gt;use_connection&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can reuse an existing Redis connection that you are already using and pass
it into RQ’s &lt;code&gt;use_connection&lt;/code&gt; function:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;redis&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;rq&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;use_connection&lt;/span&gt;

&lt;span class="n"&gt;my_connection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;redis&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Redis&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hostname&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;example.com&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;6379&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;use_connection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_connection&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;There are more advanced ways of connection management available however, &lt;a href="http://nvie.github.com/rq/docs/connections/"&gt;so
please pick your favorite&lt;/a&gt;. You
can safely mix your Redis data with RQ, as RQ prefixes all of its keys with
&lt;code&gt;rq:&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id="building-your-own-queueing-system"&gt;Building your own queueing system &lt;a href="#building-your-own-queueing-system" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;RQ offers functionality to put work on queues. It provides FIFO-semantics per
queue, but how many queues you create is up to you. For the simplest cases,
simply using the &lt;code&gt;default&lt;/code&gt; queue suffices already.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Queue&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;
&lt;span class="go"&gt;&amp;#39;default&amp;#39;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;But you can name your queues however you want:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Queue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;low&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Queue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;high&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;lo&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myfunc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;lo&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myfunc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;hi&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myfunc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;lo&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;
&lt;span class="go"&gt;2&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;hi&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;
&lt;span class="go"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Both queues are equally important to RQ. None of these has higher priority as
far as RQ is concerned. But when you start a worker, you are defining queue
priority by the order of the arguments:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;rqworker high low
&lt;span class="go"&gt;12:47:35:&lt;/span&gt;
&lt;span class="go"&gt;12:47:35: *** Listening on high, low...&lt;/span&gt;
&lt;span class="go"&gt;12:47:35: high: mymodule.myfunc(6, 7) (cc183988-a507-4623-b31a-f0338031b613)&lt;/span&gt;
&lt;span class="go"&gt;12:47:35: Job OK, result = 42&lt;/span&gt;
&lt;span class="go"&gt;12:47:35:&lt;/span&gt;
&lt;span class="go"&gt;12:47:35: *** Listening on high, low...&lt;/span&gt;
&lt;span class="go"&gt;12:47:35: low: mymodule.myfunc(2, 3) (95fe658e-b23d-4aff-9307-a55a0ee55650)&lt;/span&gt;
&lt;span class="go"&gt;12:47:36: Job OK, result = 6&lt;/span&gt;
&lt;span class="go"&gt;12:47:36:&lt;/span&gt;
&lt;span class="go"&gt;12:47:36: *** Listening on high, low...&lt;/span&gt;
&lt;span class="go"&gt;12:47:36: low: mymodule.myfunc(4, 5) (bfb89229-3ce4-463c-abf8-f19c2808cb7c)&lt;/span&gt;
&lt;span class="go"&gt;12:47:36: Job OK, result = 20&lt;/span&gt;
&lt;span class="go"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;First, all work on the &lt;code&gt;high&lt;/code&gt; queue is done (with FIFO semantics), then &lt;code&gt;low&lt;/code&gt;
is emptied. If meanwhile work is enqueued on &lt;code&gt;high&lt;/code&gt;, that work takes precedence
over the &lt;code&gt;low&lt;/code&gt; queue again after the currently running job is finished.&lt;/p&gt;
&lt;p&gt;No rocket science here, just what you’d expect.&lt;/p&gt;
&lt;h2 id="insight-over-performance"&gt;Insight over performance &lt;a href="#insight-over-performance" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;One of the things I missed most in other queueing systems is to have a decent
view of what’s going on in the system. For example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What queues exist?&lt;/li&gt;
&lt;li&gt;How many messages are on each queue?&lt;/li&gt;
&lt;li&gt;What workers are listening on what queues?&lt;/li&gt;
&lt;li&gt;Who’s idle or busy?&lt;/li&gt;
&lt;li&gt;What actual messages are on the queue?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;RQ provides an answer to all of these questions (except for the last one,
currently), via the &lt;code&gt;rqinfo&lt;/code&gt; tool.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;rqinfo
&lt;span class="go"&gt;high       |██████████████████████████ 20&lt;/span&gt;
&lt;span class="go"&gt;low        |██████████████ 12&lt;/span&gt;
&lt;span class="go"&gt;default    |█████████ 8&lt;/span&gt;
&lt;span class="go"&gt;3 queues, 45 jobs total&lt;/span&gt;

&lt;span class="go"&gt;Bricktop.19233 idle: low&lt;/span&gt;
&lt;span class="go"&gt;Bricktop.19232 idle: high, default, low&lt;/span&gt;
&lt;span class="go"&gt;Bricktop.18349 idle: default&lt;/span&gt;
&lt;span class="go"&gt;3 workers, 3 queues&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Showing only a subset of queues (including empty ones):&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;rqinfo high archive
&lt;span class="go"&gt;high       |██████████████████████████ 20&lt;/span&gt;
&lt;span class="go"&gt;archive    | 0&lt;/span&gt;
&lt;span class="go"&gt;2 queues, 20 jobs total&lt;/span&gt;

&lt;span class="go"&gt;Bricktop.19232 idle: high&lt;/span&gt;
&lt;span class="go"&gt;1 workers, 2 queues&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you want to parse the output of this script, you can specify the &lt;code&gt;--raw&lt;/code&gt;
flag to disable the fancy drawing. Example:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;rqinfo --raw
&lt;span class="go"&gt;queue high 20&lt;/span&gt;
&lt;span class="go"&gt;queue low 12&lt;/span&gt;
&lt;span class="go"&gt;queue default 8&lt;/span&gt;
&lt;span class="go"&gt;worker Bricktop.19233 idle low&lt;/span&gt;
&lt;span class="go"&gt;worker Bricktop.19232 idle high,default,low&lt;/span&gt;
&lt;span class="go"&gt;worker Bricktop.18349 idle default&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can also sort the same data by queue:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;rqinfo --by-queue
&lt;span class="go"&gt;high       |██████████████████████████ 20&lt;/span&gt;
&lt;span class="go"&gt;low        |██████████████ 12&lt;/span&gt;
&lt;span class="go"&gt;default    |█████████ 8&lt;/span&gt;
&lt;span class="go"&gt;3 queues, 45 jobs total&lt;/span&gt;

&lt;span class="go"&gt;high:    Bricktop.19232 (idle)&lt;/span&gt;
&lt;span class="go"&gt;low:     Bricktop.19233 (idle), Bricktop.19232 (idle)&lt;/span&gt;
&lt;span class="go"&gt;default: Bricktop.18349 (idle), Bricktop.19232 (idle)&lt;/span&gt;
&lt;span class="go"&gt;3 workers, 4 queues&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;By default, these monitoring commands autorefresh every 2.5 seconds, but you
can change the refresh interval if you want to. See the &lt;a href="http://nvie.github.com/rq/docs/monitoring/"&gt;monitoring
docs&lt;/a&gt; for more info.&lt;/p&gt;
&lt;h2 id="limitations"&gt;Limitations &lt;a href="#limitations" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;RQ does not try to solve all of your queueing needs. But its codebase is
relatively small and certainly not overly complex. Nonetheless, I think it will
be helpful for all of the most basic queueing needs that you’ll encounter
during Python web development.&lt;/p&gt;
&lt;p&gt;Of course, with all this also come some limitations:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It’s Python-only&lt;/li&gt;
&lt;li&gt;It’s Redis-only&lt;/li&gt;
&lt;li&gt;The workers are Unix-only&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="please-give-feedback"&gt;Please, give feedback &lt;a href="#please-give-feedback" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I’m using RQ for two and a half web projects I’ve worked on during the last few
months, and I am currently at the point where I’m satisfied enough to open the
curtains to the world. So you’re invited to play with it. I’m very curious to
hear your thoughts about this.&lt;/p&gt;
&lt;p&gt;If you’d like to contribute, please go &lt;a href="https://github.com/nvie/rq"&gt;fork me on GitHub&lt;/a&gt;.&lt;/p&gt;</content>
    <link href="https://nvie.com/posts/introducing-rq/"/>
    <summary>Today, I'm open sourcing a project that I've been working for the last few
months.  It is a Python library to put work in the background, that you'd
typically use in a web context.  It is designed to be simple to set up and
use, and be of help in almost any modern Python web stack.</summary>
    <published>2012-03-28T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://nvie.com/posts/open-sourcing-is-the-ultimate-isolation/</id>
    <title>Open Sourcing: the Ultimate Isolation</title>
    <updated>2012-09-09T00:00:00+00:00</updated>
    <content type="html">&lt;p&gt;Reflecting on how I build software lately, I noticed a pattern. I tend to write
libraries in absolute isolation, &lt;em&gt;as if they were open sourced&lt;/em&gt; and the world
is watching along.&lt;/p&gt;
&lt;p&gt;Let me try to explain why this works for me.&lt;/p&gt;
&lt;h2 id="where-theory-fails"&gt;Where Theory Fails &lt;a href="#where-theory-fails" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;“The difference between theory and practice is that, in theory, there is
none.”&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;We have all been schooled to isolate units of software into reusable
components. Software engineering literature refers to this as separation of
concerns since decades. It reduces the big problem into smaller non-overlapping
problems.&lt;/p&gt;
&lt;p&gt;We obviously try doing so, by putting related logic into modules, libraries and
whatnot. Yet, in practice, so many real world projects fail at their attempts
and end up evolving into something unnecessarily complicated.&lt;/p&gt;
&lt;p&gt;The main problem with this is that it becomes increasingly hard to comprehend
and reason about your software, not to mention the “increased fun” maintaining
it.&lt;/p&gt;
&lt;p&gt;Why is it so hard to actually achieve this in practice? Separating concerns
apparently is easier said than done.&lt;/p&gt;
&lt;h2 id="how-things-evolve"&gt;How Things Evolve &lt;a href="#how-things-evolve" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The need for a new library often arises when solving a larger problem top-down.
In the quest of solving a larger problem, you need to create a smaller
component first that is required to get to a fully working solution. This is
what most of our work as engineers is about—while solving a larger problem, we
run into bumps along the road. When we do, we stop, fix the bump, and continue
on our journey to solving the large problem.&lt;/p&gt;
&lt;p&gt;In our rush to arrive at our end destination, we want to fix any bumps as
quickly as possible. For many good reasons, mostly. We might have a deadline,
or we are afraid to get lost in details and lose focus on the bigger problem we
were actually solving.&lt;/p&gt;
&lt;p&gt;In short, we tend to see those bumps as unsolicited chores that are blocking us
and we want to spend as little time as possible at overcoming them. From
a quality perspective, however, this may not be the best route to take. So the
least you can do is create that &lt;em&gt;Technical Debt&lt;/em&gt; ticket, feel less guilty, and
move on :)&lt;/p&gt;
&lt;p&gt;I’ve never seen a project work any more glorious than this.&lt;/p&gt;
&lt;h2 id="step-back-breathe-accept-open-source"&gt;Step Back, Breathe, Accept, Open Source &lt;a href="#step-back-breathe-accept-open-source" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Running into these bumps sucks. You’re frustrated that you’re held up and are
continuously thinking: dammit, I don’t want to deal with this now. The reality
often is that you don’t have a choice.&lt;/p&gt;
&lt;p&gt;Instead, step back a few steps, take a deep breath, and accept that you’ll have
to spend more time on this problem than budgeted. This enables the mental rest
to make a good engineering decision without too much frustration emotion
involved.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What I like to do at this moment, is to start a new open source project.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Not necessarily a public one, but I do set it up like it is and &lt;em&gt;actually
consider&lt;/em&gt; it to be, or eventually become, open. I start out with a README
decribing the problem and the API I’d like it to have. And in the case of
Python, I also create a &lt;code&gt;setup.py&lt;/code&gt; so integrating this into the original
project is only a &lt;code&gt;pip install&lt;/code&gt; away.&lt;/p&gt;
&lt;p&gt;Then, just start implementing it.&lt;/p&gt;
&lt;p&gt;Let me try to highlight the benefits this approach provides.&lt;/p&gt;
&lt;h2 id="youre-more-likely-to-do-it-right"&gt;You’re More Likely To Do It Right &lt;a href="#youre-more-likely-to-do-it-right" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The pressure you get from pretending (or knowing) that many others will read
your code, pushes you to do it right. I’d ask myself continuously: would this
be an API that I’d show off to the outside world and be proud of? Could I truly
explain this API in a README so that people would understand? If not, I don’t
implement it like that and push harder.&lt;/p&gt;
&lt;p&gt;Many eyeballs make you feel more responsible. Writing stuff in private for
yourself, doesn’t.&lt;/p&gt;
&lt;h2 id="no-cheating"&gt;No Cheating &lt;a href="#no-cheating" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;A big difference between starting an actual new project, or developing it as
one-of-many internal libraries, is that it’s impossible to rely on other parts
of the end product. For example, that convenient project-specific helper
function you already wrote is easily included in a module, but not so much in
another project.&lt;/p&gt;
&lt;p&gt;In an open source project, you simply cannot cheat on yourself this way and
you’re forced to come up with a better solution. This might feel inconvenient
at first, but remember that it’s easy to write complex software and it takes
more care and dedication to write simpler software.&lt;/p&gt;
&lt;p&gt;As a way of visualising this approach: compare programming to electrical
engineering for a minute. Say you have to create a circuit board of some sort.&lt;/p&gt;
&lt;p class="centered"&gt;&lt;img alt="" class="no-dark-bg" src="/img/circuit-board.jpg" /&gt;&lt;/p&gt;
&lt;p&gt;The chip on this board is analogous to an open source software project. Its
internals are nicely abstracted away, the pins of the chip form its API, it’s
probably well-tested, well-documented and can be reused immediately. It is
physically impossible to connect to any of the internals of it—which is exactly
the point of abstraction.&lt;/p&gt;
&lt;p&gt;Looking at the circuit board, everything about this falls into place pretty
obviously.&lt;/p&gt;
&lt;p&gt;As programmers, we often fool ourselves that we’re &lt;em&gt;isolating&lt;/em&gt; logic into
modules/libraries, while in fact we’re merely &lt;em&gt;organising&lt;/em&gt; it. Modules will
oftentimes still contain project-specific dependencies. (As a good litmus test,
move that module to an empty directory and use it. If it breaks, it wasn’t
truly isolated.)&lt;/p&gt;
&lt;p&gt;The curse with programming is that it’s so easy to create these dependencies.
They are only one &lt;code&gt;import&lt;/code&gt; statement away. Developers live in a world where
that temptation continuously lurks.&lt;/p&gt;
&lt;p&gt;But by isolating code into a stand-alone project, you can remove this
temptation wholly, thereby reducing ways of cheating on yourself.&lt;/p&gt;
&lt;h2 id="simplicity-pays"&gt;Simplicity Pays &lt;a href="#simplicity-pays" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Another big benefit of truly isolating your libraries this way, is that you are
forced to think about its API. It’s the &lt;em&gt;only&lt;/em&gt; way of interacting with the
library after all. Doing this, you’ll naturally feel the urge to simplify.
Complex APIs lead to complicated documentation and complex tests. The opposite
applies, too, fortunately, and you’ll naturally be inclined to simplify.&lt;/p&gt;
&lt;p&gt;A concrete example of this: When you are hacking in a web environment, you most
likely have “the request” or “the DB connection” at your disposal any time.
When you put your library in a module, it’s easy for these to become implicit
dependencies of your library. Your library may pretty well work outside of
a request context, however, and in fact, the only thing you actually need from
the request could be a &lt;code&gt;User&lt;/code&gt; instance. When you build your library as
a separate project, these decisions fall into place effortlessly. In the end,
this makes your library more decoupled, more generic, and overall cleaner. And
as such, simpler.&lt;/p&gt;
&lt;p&gt;True Isolation™ is the ultimate catalyst of simplicity.&lt;/p&gt;
&lt;h2 id="sharing-can-pay-off-too"&gt;Sharing Can Pay Off, Too &lt;a href="#sharing-can-pay-off-too" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Even if you’re only using this technique privately to produce better software
for yourself, this pays off already in a technical sense.&lt;/p&gt;
&lt;p&gt;But open sourcing can also pay off in non-technical ways. When it fits your
company’s strategy, you now have the choice to actually publish your project at
any time, since it’s been written for the public from the beginning. If it
solves a common problem, others may like it and take interest in following it
or even contributing to it. This may open up a whole new world of users
providing feedback and improvements through code or documentation
contributions.&lt;/p&gt;
&lt;p&gt;Your company may come across as an interesting place to work for to talented
people. Your open source project can be your company’s banner. We’ve seen this
with companies like &lt;a href="http://joyent.com/"&gt;Joyent&lt;/a&gt; (of Node.js fame),
&lt;a href="http://www.10gen.com/"&gt;10gen&lt;/a&gt; (of MongoDB fame) and
&lt;a href="http://www.opscode.com/"&gt;Opscode&lt;/a&gt; (of Chef fame), just to name a few. Open
sourcing has been an important marketing value to these companies and they have
attracted many talented folks through their high-quality work.&lt;/p&gt;
&lt;p&gt;Just always remember that &lt;em&gt;simpler&lt;/em&gt; projects have a much lower barrier for
contributors, so these are more likely to receive patches. Which by itself is
another good reason to simplify your libraries :)&lt;/p&gt;
&lt;h2 id="how-i-built-rq"&gt;How I Built RQ &lt;a href="#how-i-built-rq" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Many of the things I created recently, I created this way. Back a few months
ago, I needed a super-simple solution to put work in the background. I was
working on a startup idea, which I was creating a proof of concept for. It was
a small Flask web app, and I used &lt;a href="http://flask.pocoo.org/snippets/73/"&gt;this
snippet&lt;/a&gt; initially to offload work to the
background. It did the work fine, but I soon needed it to do more, so I kept
tweaking it until it was no longer a snippet, but a library. Although it was
nicely organised in a directory, it was still tailored to the specific product
I was creating.&lt;/p&gt;
&lt;p&gt;This is where I decided to step back and started building that library like an
open source project, which became &lt;a href="http://python-rq.org/"&gt;RQ&lt;/a&gt;, of course.
After using it privately for about four months, its API kept changing quite
a bit, but its use became more general over time. I started reusing it for
other projects I was working on, until I considered it stable enough.
I believed it could be of help to other Python engineers, so I &lt;a href="/posts/introducing-rq/"&gt;decided to open
source RQ&lt;/a&gt; in March.&lt;/p&gt;
&lt;p&gt;Eventually I dropped the original startup idea, but I still have RQ. Had I not
open sourced it, it would now be buried with the rest of that project’s code.&lt;/p&gt;
&lt;p&gt;Open sourcing pays off. Even if you do it in private.&lt;/p&gt;</content>
    <link href="https://nvie.com/posts/open-sourcing-is-the-ultimate-isolation/"/>
    <summary>Some thoughts on how I like to write libraries as "open source" projects.</summary>
    <published>2012-09-09T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://nvie.com/posts/pin-your-packages/</id>
    <title>Pin Your Packages</title>
    <updated>2013-05-08T00:00:00+00:00</updated>
    <content type="html">&lt;p&gt;In building your Python application and its dependencies for production, you
want to make sure that your builds are predictable and deterministic.
Therefore, always pin your dependencies.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt;
A newer blog post about the future of pip-tools is available too: &lt;a href="/posts/better-package-management/"&gt;Better
Package Management&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id="pin-explicitly"&gt;Pin Explicitly &lt;a href="#pin-explicitly" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Don’t ever use these styles in &lt;code&gt;requirements.txt&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;lxml&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;lxml&amp;gt;=2.2.0&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;lxml&amp;gt;=2.2.0,&amp;lt;2.3.0&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Instead, pin them:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;lxml==2.3.4&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you don’t, you can never know what you’ll get when you run &lt;code&gt;pip install&lt;/code&gt;.
Even if you rebuild the env every time, you still can’t predict it. The outcome
relies on a) what’s currently installed, and b) what’s the current version on
PyPI.&lt;/p&gt;
&lt;p&gt;Eventually, all of your environments, and those of your team members, will run
out of sync. Worse even, this cannot be fixed by rerunning &lt;code&gt;pip install&lt;/code&gt;.  It’s
just waiting for bad things to happen in production.&lt;/p&gt;
&lt;p&gt;The only way of making your builds deterministic, is if you pin &lt;em&gt;every&lt;/em&gt; single
package dependency (even the dependency’s dependencies).&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;WARNING:&lt;/strong&gt;
Don’t pin by default when you’re building libraries! Only use pinning for end
products.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The biggest complaint from folks regarding explicit pinning is that you won’t
benefit from updates that way. Well, yes, you won’t. But think of it.  It’s
impossible to distinguish between a new release that fixes bugs, or one that
introduced them. You are leaving it up to coincidence. There is only one way to
retake control: &lt;em&gt;pin every dependency&lt;/em&gt;.&lt;/p&gt;
&lt;h2 id="check-for-updates-automatically"&gt;Check for Updates Automatically &lt;a href="#check-for-updates-automatically" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;So: we want to pin packages, but don’t want to let them become outdated.  The
solution: use a tool that can check for updates. This is exactly what I built
&lt;a href="https://github.com/nvie/pip-tools"&gt;pip-tools&lt;/a&gt; for.&lt;/p&gt;
&lt;p&gt;pip-tools is the collective name for two tools: &lt;code&gt;pip-review&lt;/code&gt; + &lt;code&gt;pip-dump&lt;/code&gt;&lt;/p&gt;
&lt;p class="centered"&gt;&lt;img alt="" src="/img/pip-tools.png" /&gt;&lt;/p&gt;
&lt;h2 id="pip-review"&gt;pip-review &lt;a href="#pip-review" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;It will check for available updates of all packages currently installed in your
environment, and report about them when available:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;pip-review
&lt;span class="go"&gt;requests==0.14.0 available (you have 0.13.2)&lt;/span&gt;
&lt;span class="go"&gt;redis==2.6.2 available (you have 2.4.9)&lt;/span&gt;
&lt;span class="go"&gt;rq==0.3.2 available (you have 0.3.0)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can also install them automatically:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;pip-review --auto
&lt;span class="go"&gt;... &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;or interactively decide whether you want to install each package:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;pip-review --interactive
&lt;span class="go"&gt;requests==0.14.0 available (you have 0.13.2)&lt;/span&gt;
&lt;span class="go"&gt;Upgrade now? [Y]es, [N]o, [A]ll, [Q]uit y&lt;/span&gt;
&lt;span class="go"&gt;... &lt;/span&gt;
&lt;span class="go"&gt;redis==2.6.2 available (you have 2.4.9)&lt;/span&gt;
&lt;span class="go"&gt;Upgrade now? [Y]es, [N]o, [A]ll, [Q]uit n&lt;/span&gt;
&lt;span class="go"&gt;rq==0.3.2 available (you have 0.3.0)&lt;/span&gt;
&lt;span class="go"&gt;Upgrade now? [Y]es, [N]o, [A]ll, [Q]uit y&lt;/span&gt;
&lt;span class="go"&gt;... &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It’s advisable to pick a fixed schedule to run &lt;code&gt;pip-review&lt;/code&gt;. For example, every
monday during a weekly standup meeting with your engineering team.  Make it
a point on the agenda. You discuss &lt;code&gt;pip-review&lt;/code&gt;’s output, inspect changelogs,
or just blindly upgrade them. The important part is that you do it explicitly.
You have the chance to run with the upgraded versions for a while in
a development environment, before pushing those versions to production.&lt;/p&gt;
&lt;h2 id="pip-dump"&gt;pip-dump &lt;a href="#pip-dump" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Whereas &lt;code&gt;pip-review&lt;/code&gt; solves the problem of how to check for updates of pinned
packages, &lt;code&gt;pip-dump&lt;/code&gt; focuses on the problem of how to dump those definitions
into requirements files, managed under version control.&lt;/p&gt;
&lt;p&gt;Typically, in Python apps, you include a &lt;code&gt;requirements.txt&lt;/code&gt; file in the root of
your project directory, and you run &lt;code&gt;pip freeze &amp;gt; requirements.txt&lt;/code&gt;
periodically. While this works for simple projects, this doesn’t scale.  Some
packages are installed for development, or personal, purposes only and you
don’t want to include those in &lt;code&gt;requirements.txt&lt;/code&gt;, going to production, or
visible to your other team members.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;pip-dump&lt;/code&gt; provides a smarter way to dump requirements. It understands the
convention of separating requirements into multiple files, following the naming
convention:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;requirements.txt&lt;/code&gt; is the main (and default) requirements file;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;dev-requirements.txt&lt;/code&gt;, or &lt;code&gt;test-requirements.txt&lt;/code&gt;, or actually,
   &lt;code&gt;*requirements.txt&lt;/code&gt;, are secundary dependencies.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When you have a &lt;code&gt;requirements.txt&lt;/code&gt; and &lt;code&gt;dev-requirements.txt&lt;/code&gt; file in your
project, with the following content:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;# requirements.txt
Flask==0.9

# dev-requirements.txt
ipython
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then simply running &lt;code&gt;pip-dump&lt;/code&gt; will result in the following output:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;# requirements.txt
Flask==0.9
Jinja2==2.6
Werkzeug==0.8.3

# dev-requirements.txt
ipython==0.13
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It keeps the files sorted for tidiness, and to reduce the chance of merge
conflicts in version control.&lt;/p&gt;
&lt;p&gt;You can even put packages in an optional file called &lt;code&gt;.pipignore&lt;/code&gt;. This is
useful if you want to keep some packages installed in your local environment,
but don’t want to have them reflected in your requirements files.&lt;/p&gt;
&lt;h2 id="contributing"&gt;Contributing &lt;a href="#contributing" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;pip-tools&lt;/code&gt; 0.x is relied on by many already on a daily/weekly basis. It’s
worth noting that we’re working on &lt;a href="/posts/better-package-management/"&gt;Better Package Management&lt;/a&gt; too,
which will be the future of &lt;code&gt;pip-tools&lt;/code&gt;. If you want to contribute, please
shout out.&lt;/p&gt;</content>
    <link href="https://nvie.com/posts/pin-your-packages/"/>
    <summary>Make your Python production deployments predictable and deterministic by
pinning your dependencies.</summary>
    <published>2012-09-26T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://nvie.com/posts/better-package-management/</id>
    <title>Better Package Management</title>
    <updated>2026-02-19T00:00:00+00:00</updated>
    <content type="html">&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt; (Feb 2026): This article is
really really old. You probably want to use &lt;code&gt;uv&lt;/code&gt;
today.&lt;br /&gt;
&lt;strong&gt;Update&lt;/strong&gt; (March 2019): The Python
packaging landscape has changed significantly
since I first wrote this post. Your choice
today is mostly between using
&lt;a href="https://github.com/jazzband/pip-tools"&gt;pip-tools&lt;/a&gt;
directly, using
&lt;a href="https://docs.pipenv.org/"&gt;Pipenv&lt;/a&gt; (which is
a Swiss army knife kind of tool that
internally relies on pip-tools), or newer
tooling like
&lt;a href="https://poetry.eustace.io/"&gt;Poetry&lt;/a&gt;. A good
post to help you decide which is the tool to
best fit your use case is
&lt;a href="https://hynek.me/articles/python-app-deps-2018/"&gt;Python Application Dependency Management in 2018&lt;/a&gt;
by &lt;a href="https://twitter.com/hynek"&gt;Hynek Schlawack&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;You are managing your Python packages using &lt;code&gt;pip&lt;/code&gt; and &lt;code&gt;requirements.txt&lt;/code&gt; spec
files already. Maybe, you are even &lt;a href="/posts/pin-your-packages/"&gt;pinning them&lt;/a&gt;
too—that’s awesome. But how do you keep your environments clean and fresh?&lt;/p&gt;
&lt;p&gt;Here’s what I think can be improved to the state of package management in
Python.&lt;/p&gt;
&lt;h2 id="virtue-1-declare-only-your-top-level-dependencies"&gt;Virtue 1: Declare only your top-level dependencies &lt;a href="#virtue-1-declare-only-your-top-level-dependencies" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Often, your project will only need a limited set of what I’ll call &lt;em&gt;top-level&lt;/em&gt;
package dependencies. A typical example is that you’ll depend on Django or
Flask. But just putting those names in an &lt;code&gt;requirements.txt&lt;/code&gt; file is inherently
dangerous and will bite you at some point. If you don’t see why, &lt;a href="/posts/pin-your-packages/"&gt;read this
post&lt;/a&gt; first.&lt;/p&gt;
&lt;p&gt;So now you’re pinning them. If your app needs Flask, this will typically be in
your &lt;code&gt;requirements.txt&lt;/code&gt; file:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;Flask==0.9
Jinja2==2.6
Werkzeug==0.8.3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Jinja2 and Werkzeug are in there, because Flask needs them. And since you don’t
want fate to decide which versions of Jinja2 and Werkzeug you’ll get when
deploying, you’re wisely pinning them.&lt;/p&gt;
&lt;p&gt;The problem with this is that over time your &lt;code&gt;requirements.txt&lt;/code&gt; file will
accumulate all kinds of dependencies, and in reality, it’s not unusual that
you’ll lose sight of which packages are still used, and which have become
stale.&lt;/p&gt;
&lt;p&gt;The following file is the result of depending on Flask and legit.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;async==0.6.1
clint==0.3.1
Flask==0.9
gitdb==0.5.4
GitPython==0.3.2.RC1
Jinja2==2.6
legit==0.1.1
smmap==0.8.2
Werkzeug==0.8.3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Looking at this, I’d have no clue what &lt;code&gt;smmap&lt;/code&gt; is, and why it’s needed in
there.&lt;/p&gt;
&lt;p&gt;Wouldn’t it be awesome to actually have a way of expressing only your top-level
dependencies in a file called &lt;code&gt;requirements.in&lt;/code&gt;, like this?&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;Flask&amp;gt;=0.9  # we use 0.9 features
legit       # any version will do for us
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And “compiling” that to an actual &lt;code&gt;requirements.txt&lt;/code&gt;:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;async==0.6.1  # required by legit==0.1.1
clint==0.3.1  # required by legit==0.1.1
Flask==0.9
gitdb==0.5.4  # required by legit==0.1.1
GitPython==0.3.2.RC1  # required by legit==0.1.1
Jinja2==2.6  # required by Flask==0.9
legit==0.1.1
smmap==0.8.2  # required by legit==0.1.1
Werkzeug==0.8.3  # required by Flask==0.9
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This tool exists, and is called &lt;code&gt;pip-compile&lt;/code&gt;. Check it out on the &lt;a href="https://github.com/nvie/pip-tools/tree/future"&gt;&lt;code&gt;future&lt;/code&gt;
branch&lt;/a&gt; of pip-tools.
(&lt;strong&gt;UPDATE&lt;/strong&gt; This is now the master branch, available since 1.0.)  I wrote this
together with &lt;a href="http://twitter.com/brutasse"&gt;Bruno Renié&lt;/a&gt; over the last few
months.&lt;/p&gt;
&lt;p&gt;Let’s elaborate on this a bit. The &lt;code&gt;.in&lt;/code&gt; file provides the file format that
you’d &lt;em&gt;actually&lt;/em&gt; would want to use and maintain as a developer, while the
result of compilation is the file that you want to use to build deterministic
(and thus predictable) envs.&lt;/p&gt;
&lt;p&gt;Note that there’s a fundamental difference here between “compiling” these &lt;code&gt;.in&lt;/code&gt;
files and compiling a file of source code: the result of the compilation itself
isn’t deterministic. This means that compiling your requirements may lead to
a different &lt;code&gt;requirements.txt&lt;/code&gt; file depending on the moment you run it—because
in the meantime some packages might have gotten updates in PyPI.&lt;/p&gt;
&lt;p&gt;The point is to freeze the specs. Exactly why you were pinning your
dependencies already.&lt;/p&gt;
&lt;p&gt;As a consequence, you should put both files under version control. This plays
well with PAAS providers like Heroku as well. The &lt;code&gt;.in&lt;/code&gt; file is only used for
your own maintenance convenience, while the &lt;code&gt;.txt&lt;/code&gt; file is actually used to
install to your env. The difference is, it’s now generated for you.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A Quick Note on Complex Dependencies&lt;/strong&gt;&lt;br /&gt;
We’ve created &lt;code&gt;pip-compile&lt;/code&gt; to be smart with respect to resolving complex
dependency trees. For example, Flask 0.9 depends on &lt;code&gt;Jinja2&amp;gt;=2.4&lt;/code&gt;. If another
package, say Foo, declared &lt;code&gt;Jinja2&amp;lt;2.6&lt;/code&gt;, you’ll end up having &lt;code&gt;Jinja2==2.5&lt;/code&gt;
in your compiled requirements. It can figure this out.  (Obviously, conflicts
can occur, in which case compilation will fail.)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id="virtue-2-have-your-envs-reflect-your-specs"&gt;Virtue 2: Have your envs reflect your specs &lt;a href="#virtue-2-have-your-envs-reflect-your-specs" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The next step, then, would be to rebuild your actual virtualenvs by having them
reflect &lt;em&gt;exactly&lt;/em&gt; what’s in your (compiled) spec file. Let’s replay the example
above.&lt;/p&gt;
&lt;p&gt;Recall that we have this in our &lt;code&gt;requirements.in&lt;/code&gt; file:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;Flask&amp;gt;=0.9
legit
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then we run &lt;code&gt;pip-compile&lt;/code&gt;, and get:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;async==0.6.1  # required by legit==0.1.1
clint==0.3.1  # required by legit==0.1.1
Flask==0.9
gitdb==0.5.4  # required by legit==0.1.1
GitPython==0.3.2.RC1  # required by legit==0.1.1
Jinja2==2.6  # required by Flask==0.9
legit==0.1.1
smmap==0.8.2  # required by legit==0.1.1
Werkzeug==0.8.3  # required by Flask==0.9
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now, to actually install that into our environment, we typically run:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;pip install -r requirements.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;But frankly, this isn’t enough. To actually reliably mimic the spec file, the
env might need to uninstall some packages as well. This can actually be very
important. Suppose you have a package that’s already installed in your env, say
&lt;code&gt;requests&lt;/code&gt;. Your code is using it, but you forgot to add it to
&lt;code&gt;requirements.txt&lt;/code&gt;. That way, running &lt;code&gt;pip install -r requirements.txt&lt;/code&gt; will
work fine, but deploying this code will break due to an ImportError.&lt;/p&gt;
&lt;p&gt;Meet &lt;code&gt;pip-sync&lt;/code&gt;. This tool will install all required packages into your env,
but will additionally &lt;em&gt;uninstall&lt;/em&gt; everything else in there. Combined with
&lt;code&gt;pip-compile&lt;/code&gt;, this makes for package management nirvana. Say you don’t need
&lt;code&gt;legit&lt;/code&gt; anymore, and want to remove it as a project dependency.&lt;/p&gt;
&lt;p&gt;First, remove that top-level dependency from the &lt;code&gt;.in&lt;/code&gt; file:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;Flask
# legit  # comment out, or remove
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then run &lt;code&gt;pip-compile&lt;/code&gt; to update the compiled spec file:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;Flask==0.9
Jinja2==2.6  # required by Flask==0.9
Werkzeug==0.8.3  # required by Flask==0.9
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The unused dependencies are removed automatically. Now we need to sync that
back to our actual env:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;pip-sync
&lt;span class="go"&gt;Uninstalling package async&lt;/span&gt;
&lt;span class="go"&gt;Uninstalling package clint&lt;/span&gt;
&lt;span class="go"&gt;Uninstalling package gitdb&lt;/span&gt;
&lt;span class="go"&gt;Uninstalling package GitPython&lt;/span&gt;
&lt;span class="go"&gt;Uninstalling package smmap&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This will now uninstall legit and all it’s dependencies from the virtualenv
(unless some other package would depend on them still). Your virtualenv is
crisp and clean.&lt;/p&gt;
&lt;p&gt;I would propose PAAS providers to adopt the use of &lt;code&gt;pip-sync&lt;/code&gt; over &lt;code&gt;pip install
-r requirements.txt&lt;/code&gt;, as environments are automatically cleaned up that way.&lt;/p&gt;
&lt;h2 id="project-context-and-roadmap"&gt;Project context and roadmap &lt;a href="#project-context-and-roadmap" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;As said, over the last few months, &lt;a href="http://twitter.com/brutasse"&gt;Bruno Renié&lt;/a&gt;
and myself have been working on a better version of the &lt;code&gt;pip-tools&lt;/code&gt; project—one
that would let us do exactly the above. We’ve not been very public about it,
but you might have noticed the &lt;a href="https://github.com/nvie/pip-tools/tree/future"&gt;&lt;code&gt;future&lt;/code&gt;
branch&lt;/a&gt;. Basically, this would
replace the existing &lt;code&gt;pip-dump&lt;/code&gt; command by something inherently more
manageable.&lt;/p&gt;
&lt;p&gt;I do solicit feedback on all this, so feel free to get in touch.&lt;/p&gt;</content>
    <link href="https://nvie.com/posts/better-package-management/"/>
    <summary>&gt; **Update** (Feb 2026): This article is
&gt; really really old. You probably want to use `uv`
&gt; today.  
&gt; **Update** (March 2019): The Python
&gt; packaging landscape has changed significantly
&gt; since I first wrote this post. Your choice
&gt; today is mostly between using
&gt; [pip-tools](https://github.com/jazzband/pip-tools)
&gt; directly, using
&gt; [Pipenv](https://docs.pipenv.org/) (which is
&gt; a Swiss army knife kind of tool that
&gt; internally relies on pip-tools), or newer
&gt; tooling like
&gt; [Poetry](https://poetry.eustace.io/). A good
&gt; post to help you decide which is the tool to
&gt; best fit your use case is
&gt; [Python Application Dependency Management in 2018](https://hynek.me/articles/python-app-deps-2018/)
&gt; by [Hynek Schlawack](https://twitter.com/hynek).</summary>
    <published>2013-05-08T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://nvie.com/posts/writing-a-cli-in-python-in-under-60-seconds/</id>
    <title>Writing a Command-Line Tool in Python</title>
    <updated>2014-09-24T00:00:00+00:00</updated>
    <content type="html">&lt;p&gt;I used to find writing command line tools tedious.  Not so much the writing of
the core of the tool itself, but all the peripheral stuff you had to do to
actually &lt;em&gt;finish&lt;/em&gt; one.&lt;/p&gt;
&lt;h3 id="the-language"&gt;The Language? &lt;a href="#the-language" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The first issue is to pick the language to implement it in: do I use Python,
which I'm intimitely familiar with, or a Unix shell script?  With shell
scripts, the syntax is pretty terrible, but the tool typically fits in a single
file and there's hardly any overhead running them.  On the other hand, making
sure the tool works under all circumstances can be tricky.  Shell scripts are
notorious for breaking when you feed them arguments with spaces.  The burden of
making sure you properly quote all the variable interpolations in the script is
on the programmer.  It's possible to do, just unnecessarily hard.&lt;/p&gt;
&lt;p&gt;On the other hand, Python is so much more expressive.  There are a ton of
libraries out there ready to use, and Python itself includes a lot of batteries
already in its standard library, of course.&lt;/p&gt;
&lt;h3 id="distribution"&gt;Distribution? &lt;a href="#distribution" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Python comes with its own set of problems, though.  Python runtime environments
are typically a mess, and I don't want to further pollute people's already
cluttered global Python environments.  With Python, installing a package is
typically just a &lt;code&gt;pip install &amp;lt;pkg&amp;gt;&lt;/code&gt; away, but it requires another tedious
step: writing a &lt;code&gt;setup.py&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;If it comes to distributing the script, a shell script may be much easier.
With shell scripts it's either a single file that needs to be copied somewhere.
Manually, or via a &lt;code&gt;make install&lt;/code&gt; command, which involves adding a &lt;code&gt;Makefile&lt;/code&gt;
and dealing with subtle differences for each Unix platform, not to even mention
trying to run it on Windows machines.&lt;/p&gt;
&lt;h3 id="argument-parsing"&gt;Argument Parsing? &lt;a href="#argument-parsing" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Each script will at some stage require some options or arguments.  How should
we do the argument parsing?  Do I use &lt;code&gt;getopt&lt;/code&gt; or &lt;code&gt;getopts&lt;/code&gt;?  Does it even
matter?  Can it take &lt;code&gt;--long-form-options&lt;/code&gt;?  Or do I resign myself to poor
man's arg parsing again?  The latter has too often become the default choice.&lt;/p&gt;
&lt;h2 id="standing-on-the-shoulders-of-giants"&gt;Standing on the Shoulders of Giants &lt;a href="#standing-on-the-shoulders-of-giants" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Lately, a few fantastic projects have taken away most of the tedious work
surrounding the building of command line tools, and almost make it trivial now.&lt;/p&gt;
&lt;h3 id="click"&gt;Click &lt;a href="#click" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;a href="http://click.pocoo.org"&gt;Click&lt;/a&gt; is a Python library written by &lt;a href="https://twitter.com/mitsuhiko"&gt;Armin Ronacher&lt;/a&gt; that
deals with all the handling of command line option and argument parsing and
comes with fantastic defaults.  This project is a great step towards more
consistent and standard CLI interfaces.  Besides solving the options and
argument parsing, it also has a ton of useful features packaged, like smart
colorized terminal output, file abstractions, subcommands, and rendering
progress bars.&lt;/p&gt;
&lt;p&gt;It solves the argument parsing problem.&lt;/p&gt;
&lt;h3 id="pipsi"&gt;pipsi &lt;a href="#pipsi" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Using &lt;a href="https://github.com/mitsuhiko/pipsi#readme"&gt;pipsi&lt;/a&gt; (also by Armin!), users can install any Python command
line script into an isolated Python runtime environment, so it solves the
global cluttered Python environment problem entirely.&lt;/p&gt;
&lt;h3 id="cookiecutter"&gt;Cookiecutter &lt;a href="#cookiecutter" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;a href="http://cookiecutter.readthedocs.org"&gt;Cookiecutter&lt;/a&gt; (by the awesome &lt;a href="https://twitter.com/audreyr"&gt;Audrey Roy Greenfield&lt;/a&gt;)
is a project generator, based on a predefined project template.  It will read
the template, ask the user a few questions to fill in the blanks, and generates
a new project for you.&lt;/p&gt;
&lt;h3 id="cookiecutter-python-cli"&gt;cookiecutter-python-cli &lt;a href="#cookiecutter-python-cli" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;a href="https://github.com/nvie/cookiecutter-python-cli"&gt;cookiecutter-python-cli&lt;/a&gt; is one such Cookiecutter template I wrote
that uses all of the above: it sports a predefined &lt;code&gt;setup.py&lt;/code&gt;, a package
structure that's extensible, and test cases and a test runner to get you
started.&lt;/p&gt;
&lt;h2 id="putting-it-together"&gt;Putting it Together &lt;a href="#putting-it-together" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Let's build a new high quality CLI in Python in under 60 seconds now.&lt;/p&gt;
&lt;p&gt;First, install &lt;a href="https://github.com/mitsuhiko/pipsi#readme"&gt;pipsi&lt;/a&gt; and follow its instructions:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;curl https://raw.githubusercontent.com/mitsuhiko/pipsi/master/get-pipsi.py &lt;span class="p"&gt;|&lt;/span&gt; python
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Next, using pipsi, install &lt;a href="http://cookiecutter.readthedocs.org"&gt;Cookiecutter&lt;/a&gt; in its own isolated
runtime environment:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;pipsi install cookiecutter
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now use Cookiecutter to create your brand new project, based on my CLI template:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; ~/Desktop
&lt;span class="gp"&gt;$ &lt;/span&gt;cookiecutter https://github.com/nvie/cookiecutter-python-cli.git
&lt;span class="go"&gt;Cloning into &amp;#39;cookiecutter-python-cli&amp;#39;...&lt;/span&gt;
&lt;span class="go"&gt;remote: Counting objects: 64, done.&lt;/span&gt;
&lt;span class="go"&gt;remote: Total 64 (delta 0), reused 0 (delta 0)&lt;/span&gt;
&lt;span class="go"&gt;Unpacking objects: 100% (64/64), done.&lt;/span&gt;
&lt;span class="go"&gt;Checking connectivity... done.&lt;/span&gt;
&lt;span class="go"&gt;full_name (default is &amp;quot;Vincent Driessen&amp;quot;)?&lt;/span&gt;
&lt;span class="go"&gt;email (default is &amp;quot;vincent@3rdcloud.com&amp;quot;)?&lt;/span&gt;
&lt;span class="go"&gt;github_username (default is &amp;quot;nvie&amp;quot;)?&lt;/span&gt;
&lt;span class="go"&gt;project_name (default is &amp;quot;My Tool&amp;quot;)?&lt;/span&gt;
&lt;span class="go"&gt;repo_name (default is &amp;quot;python-mytool&amp;quot;)?&lt;/span&gt;
&lt;span class="go"&gt;pypi_name (default is &amp;quot;mytool&amp;quot;)?&lt;/span&gt;
&lt;span class="go"&gt;script_name (default is &amp;quot;my-tool&amp;quot;)?&lt;/span&gt;
&lt;span class="go"&gt;package_name (default is &amp;quot;my_tool&amp;quot;)?&lt;/span&gt;
&lt;span class="go"&gt;project_short_description (default is &amp;quot;My Tool does one thing, and one thing well.&amp;quot;)?&lt;/span&gt;
&lt;span class="go"&gt;release_date (default is &amp;quot;2014-09-04&amp;quot;)?&lt;/span&gt;
&lt;span class="go"&gt;year (default is &amp;quot;2014&amp;quot;)?&lt;/span&gt;
&lt;span class="go"&gt;version (default is &amp;quot;0.1.0&amp;quot;)?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;When you're done, you'll have a project where you can run &lt;a href="https://testrun.org/tox"&gt;&lt;code&gt;tox&lt;/code&gt;&lt;/a&gt; to run
your test suite on all important Python versions.  If you don't need the test
cases, simply remove the &lt;code&gt;tests/&lt;/code&gt; directory.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; python-mytool/
&lt;span class="gp"&gt;$ &lt;/span&gt;tox
&lt;span class="go"&gt;...&lt;/span&gt;
&lt;span class="go"&gt;  py26: commands succeeded&lt;/span&gt;
&lt;span class="go"&gt;  py27: commands succeeded&lt;/span&gt;
&lt;span class="go"&gt;  py33: commands succeeded&lt;/span&gt;
&lt;span class="go"&gt;  py34: commands succeeded&lt;/span&gt;
&lt;span class="go"&gt;  pypy: commands succeeded&lt;/span&gt;
&lt;span class="go"&gt;  flake8: commands succeeded&lt;/span&gt;
&lt;span class="go"&gt;  congratulations :)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Let's install and run it without further modifications:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;pipsi install --editable .
&lt;span class="go"&gt;...&lt;/span&gt;
&lt;span class="gp"&gt;$ &lt;/span&gt;my-tool
&lt;span class="go"&gt;Hello, world.&lt;/span&gt;
&lt;span class="gp"&gt;$ &lt;/span&gt;my-tool --as-cowboy
&lt;span class="go"&gt;Howdy, world.&lt;/span&gt;
&lt;span class="gp"&gt;$ &lt;/span&gt;my-tool --as-cowboy Vincent
&lt;span class="go"&gt;Howdy, Vincent.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can edit the &lt;code&gt;setup.py&lt;/code&gt; to your liking.  The default provided version
should already work out of the box.  When you're done implementing your tool,
you can either upload it to PyPI or just keep it to yourself locally:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;pipsi install &amp;lt;pkgname&amp;gt;  &lt;span class="c1"&gt;# install from PyPI&lt;/span&gt;
&lt;span class="gp"&gt;$ &lt;/span&gt;pipsi install --editable ../path/to/project/dir   &lt;span class="c1"&gt;# install locally&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you have any improvements for this template, please submit &lt;a href="https://github.com/nvie/cookiecutter-python-cli/pulls"&gt;a pull
request&lt;/a&gt;.  Thanks!&lt;/p&gt;</content>
    <link href="https://nvie.com/posts/writing-a-cli-in-python-in-under-60-seconds/"/>
    <summary>I used to find writing command line tools tedious.  Not so much the writing of
the core of the tool itself, but all the peripheral stuff you had to do to
actually _finish_ one.</summary>
    <published>2014-09-24T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://nvie.com/posts/iterators-vs-generators/</id>
    <title>Iterables vs. Iterators vs. Generators</title>
    <updated>2014-09-25T00:00:00+00:00</updated>
    <content type="html">&lt;p&gt;Occasionally I've run into situations of confusion on the exact differences
between the following related concepts in Python:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a container&lt;/li&gt;
&lt;li&gt;an iterable&lt;/li&gt;
&lt;li&gt;an iterator&lt;/li&gt;
&lt;li&gt;a generator&lt;/li&gt;
&lt;li&gt;a generator expression&lt;/li&gt;
&lt;li&gt;a {list, set, dict} comprehension&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I'm writing this post as a pocket reference for later.&lt;/p&gt;
&lt;p class="centered"&gt;&lt;img alt="" src="/img/relationships.png" width="636" /&gt;&lt;/p&gt;
&lt;h2 id="containers"&gt;Containers &lt;a href="#containers" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Containers are data structures holding elements, and that support membership
tests.  They are data structures that live in memory, and typically hold all
their values in memory, too.  In Python, some well known examples are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;list&lt;/strong&gt;, deque, …&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;set&lt;/strong&gt;, frozensets, …&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;dict&lt;/strong&gt;, defaultdict, OrderedDict, Counter, …&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;tuple&lt;/strong&gt;, namedtuple, …&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;str&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Containers are easy to grasp, because you can think of them as real life
containers: a box, a cubboard, a house, a ship, etc.&lt;/p&gt;
&lt;p&gt;Technically, an object is a container when it can be asked whether it
&lt;u&gt;contains&lt;/u&gt; a certain element.  You can perform such membership tests on
lists, sets, or tuples alike:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;      &lt;span class="c1"&gt;# lists&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;      &lt;span class="c1"&gt;# sets&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;      &lt;span class="c1"&gt;# tuples&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Dict membership will check the keys:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;foo&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;bar&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;qux&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;foo&amp;#39;&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;  &lt;span class="c1"&gt;# &amp;#39;foo&amp;#39; is not a _key_ in the dict&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Finally you can ask a string if it "contains" a substring:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;foobar&amp;#39;&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;b&amp;#39;&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;x&amp;#39;&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;foo&amp;#39;&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;  &lt;span class="c1"&gt;# a string &amp;quot;contains&amp;quot; all its substrings&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The last example is a bit strange, but it shows how the container interface
renders the object opaque.  A string does not literally store copies of all of
its substrings in memory, but you can certainly use it that way.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt;&lt;br /&gt;
Even though most containers provide a way to produce every element they
contain, that ability does not make them a container but an iterable (we'll
get there in a minute).&lt;/p&gt;
&lt;p&gt;Not all containers are necessarily iterable.  An example of this is a &lt;a href="http://en.wikipedia.org/wiki/Bloom_filter"&gt;Bloom
filter&lt;/a&gt;.  Probabilistic data structures like this can be asked whether
they contain a &lt;em&gt;certain&lt;/em&gt; element, but they are unable to return their
individual elements.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id="iterables"&gt;Iterables &lt;a href="#iterables" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;As said, most containers are also iterable.  But many more things are iterable
as well.  Examples are open files, open sockets, etc.  Where containers are
typically finite, an iterable may just as well represent an infinite source of
data.&lt;/p&gt;
&lt;p&gt;An &lt;strong&gt;iterable&lt;/strong&gt; is any object, not necessarily a data structure, that can
return an &lt;strong&gt;iterator&lt;/strong&gt; (with the purpose of returning all of its elements).
That sounds a bit awkward, but there is an important difference between an
iterable and an iterator.  Take a look at this example:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;iter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;iter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="go"&gt;1&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="go"&gt;2&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="go"&gt;1&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="go"&gt;&amp;lt;class &amp;#39;list&amp;#39;&amp;gt;&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="go"&gt;&amp;lt;class &amp;#39;list_iterator&amp;#39;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here, &lt;code&gt;x&lt;/code&gt; is the iter&lt;u&gt;able&lt;/u&gt;, while &lt;code&gt;y&lt;/code&gt; and &lt;code&gt;z&lt;/code&gt; are two individual
instances of an iterat&lt;u&gt;or&lt;/u&gt;, producing values from the iterable &lt;code&gt;x&lt;/code&gt;.  Both
&lt;code&gt;y&lt;/code&gt; and &lt;code&gt;z&lt;/code&gt; hold state, as you can see from the example.  In this example, &lt;code&gt;x&lt;/code&gt;
is a data structure (a list), but that is not a requirement.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt;&lt;br /&gt;
Often, for pragmatic reasons, iterable classes will implement both
&lt;code&gt;__iter__()&lt;/code&gt; and &lt;code&gt;__next__()&lt;/code&gt; in the same class, and have &lt;code&gt;__iter__()&lt;/code&gt; return
&lt;code&gt;self&lt;/code&gt;, which makes the class both an iterable and its own iterator.  It is
perfectly fine to return a different object as the iterator, though.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Finally, when you write:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;elem&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is what actually happens:&lt;/p&gt;
&lt;p class="centered"&gt;&lt;img alt="" src="/img/iterable-vs-iterator.png" width="584" /&gt;&lt;/p&gt;
&lt;p&gt;When you disassemble this Python code, you can see the explicit call to
&lt;code&gt;GET_ITER&lt;/code&gt;, which is essentially like invoking &lt;code&gt;iter(x)&lt;/code&gt;.  The &lt;code&gt;FOR_ITER&lt;/code&gt; is an
instruction that will do the equivalent of calling &lt;code&gt;next()&lt;/code&gt; repeatedly to get
every element, but this does not show from the byte code instructions because
it's optimized for speed in the interpreter.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;dis&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;dis&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dis&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;for _ in x: pass&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="go"&gt;  1           0 SETUP_LOOP              14 (to 17)&lt;/span&gt;
&lt;span class="go"&gt;              3 LOAD_NAME                0 (x)&lt;/span&gt;
&lt;span class="go"&gt;              6 GET_ITER&lt;/span&gt;
&lt;span class="go"&gt;        &amp;gt;&amp;gt;    7 FOR_ITER                 6 (to 16)&lt;/span&gt;
&lt;span class="go"&gt;             10 STORE_NAME               1 (_)&lt;/span&gt;
&lt;span class="go"&gt;             13 JUMP_ABSOLUTE            7&lt;/span&gt;
&lt;span class="go"&gt;        &amp;gt;&amp;gt;   16 POP_BLOCK&lt;/span&gt;
&lt;span class="go"&gt;        &amp;gt;&amp;gt;   17 LOAD_CONST               0 (None)&lt;/span&gt;
&lt;span class="go"&gt;             20 RETURN_VALUE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="iterators"&gt;Iterators &lt;a href="#iterators" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;So what is an &lt;strong&gt;iterator&lt;/strong&gt; then?  It's a stateful helper object that will
produce the next value when you call &lt;code&gt;next()&lt;/code&gt; on it.  Any object that has
a &lt;code&gt;__next__()&lt;/code&gt; method is therefore an iterator.  How it produces a value is
irrelevant.&lt;/p&gt;
&lt;p&gt;So an iterator is a value factory.  Each time you ask it for "the next" value,
it knows how to compute it because it holds internal state.&lt;/p&gt;
&lt;p&gt;There are countless examples of iterators.  All of the &lt;code&gt;itertools&lt;/code&gt; functions
return iterators.  Some produce infinite sequences:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;itertools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="mi"&gt;13&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="mi"&gt;14&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Some produce infinite sequences from finite sequences:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;itertools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;cycle&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;colors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cycle&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;red&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;white&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;blue&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;colors&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="s1"&gt;&amp;#39;red&amp;#39;&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;colors&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="s1"&gt;&amp;#39;white&amp;#39;&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;colors&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="s1"&gt;&amp;#39;blue&amp;#39;&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;colors&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="s1"&gt;&amp;#39;red&amp;#39;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Some produce finite sequences from infinite sequences:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;itertools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;islice&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;colors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cycle&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;red&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;white&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;blue&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;  &lt;span class="c1"&gt;# infinite&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;limited&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;islice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;colors&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;            &lt;span class="c1"&gt;# finite&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;limited&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;                         &lt;span class="c1"&gt;# so safe to use for-loop on&lt;/span&gt;
&lt;span class="o"&gt;...&lt;/span&gt;     &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;red&lt;/span&gt;
&lt;span class="n"&gt;white&lt;/span&gt;
&lt;span class="n"&gt;blue&lt;/span&gt;
&lt;span class="n"&gt;red&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To get a better sense of the internals of an iterator, let's build an iterator
producing the Fibonacci numbers:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="fm"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="fm"&gt;__iter__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="fm"&gt;__next__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;        &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;curr&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prev&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;
&lt;span class="gp"&gt;...&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;islice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="go"&gt;[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Note that this class is both an iterable (because it sports an &lt;code&gt;__iter__()&lt;/code&gt;
method), and its own iterator (because it has a &lt;code&gt;__next__()&lt;/code&gt; method).&lt;/p&gt;
&lt;p&gt;The state inside this iterator is fully kept inside the &lt;code&gt;prev&lt;/code&gt; and &lt;code&gt;curr&lt;/code&gt;
instance variables, and are used for subsequent calls to the iterator.  Every
call to &lt;code&gt;next()&lt;/code&gt; does two important things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Modify its state for the next &lt;code&gt;next()&lt;/code&gt; call;&lt;/li&gt;
&lt;li&gt;Produce the result for the current call.&lt;/li&gt;
&lt;/ol&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Central idea: a lazy factory&lt;/strong&gt;&lt;br /&gt;
From the outside, the iterator is like a lazy factory that is idle until you
ask it for a value, which is when it starts to buzz and produce a single
value, after which it turns idle again.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id="generators"&gt;Generators &lt;a href="#generators" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Finally, we've arrived at our destination!  The generators are my absolute
favorite Python language feature.  A generator is a special kind of
iterator—the elegant kind.&lt;/p&gt;
&lt;p&gt;A generator allows you to write iterators much like the Fibonacci sequence
iterator example above, but in an elegant succinct syntax that avoids writing
classes with &lt;code&gt;__iter__()&lt;/code&gt; and &lt;code&gt;__next__()&lt;/code&gt; methods.&lt;/p&gt;
&lt;p&gt;Let's be explicit:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Any generator also is an iterator (not vice versa!);&lt;/li&gt;
&lt;li&gt;Any generator, therefore, is a factory that lazily produces values.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here is the same Fibonacci sequence factory, but written as a generator:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;    &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;        &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;curr&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;        &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;curr&lt;/span&gt;
&lt;span class="gp"&gt;...&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;islice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="go"&gt;[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Wow, isn't that elegant?  Notice the magic keyword that's responsible for the
beauty:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;yield&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Let's break down what happened here: first of all, take note that &lt;code&gt;fib&lt;/code&gt; is
defined as a normal Python function, nothing special.  Notice, however, that
there's no &lt;code&gt;return&lt;/code&gt; keyword inside the function body.  The return value of the
function will be a generator (read: an iterator, a factory, a stateful helper
object).&lt;/p&gt;
&lt;p&gt;Now when &lt;code&gt;f = fib()&lt;/code&gt; is called, the generator (the factory) is instantiated and
returned.  No code will be executed at this point: the generator starts in an
idle state initially.  To be explicit: the line &lt;code&gt;prev, curr = 0, 1&lt;/code&gt; is not
executed yet.&lt;/p&gt;
&lt;p&gt;Then, this generator instance is wrapped in an &lt;code&gt;islice()&lt;/code&gt;.  This is itself also
an iterator, so idle initially.  Nothing happens, still.&lt;/p&gt;
&lt;p&gt;Then, this iterator is wrapped in a &lt;code&gt;list()&lt;/code&gt;, which will consume all of its
arguments and build a list from it.  To do so, it will start calling &lt;code&gt;next()&lt;/code&gt;
on the &lt;code&gt;islice()&lt;/code&gt; instance, which in turn will start calling &lt;code&gt;next()&lt;/code&gt; on our
&lt;code&gt;f&lt;/code&gt; instance.&lt;/p&gt;
&lt;p&gt;But one step at a time.  On the first invocation, the code will finally run
a bit: &lt;code&gt;prev, curr = 0, 1&lt;/code&gt; gets executed, the &lt;code&gt;while True&lt;/code&gt; loop is entered, and
then it encounters the &lt;code&gt;yield curr&lt;/code&gt; statement.  It will produce the value
that's currently in the &lt;code&gt;curr&lt;/code&gt; variable and become idle again.&lt;/p&gt;
&lt;p&gt;This value is passed to the &lt;code&gt;islice()&lt;/code&gt; wrapper, which will produce it (because
it's not past the 10th value yet), and list can add the value &lt;code&gt;1&lt;/code&gt; to the list
now.&lt;/p&gt;
&lt;p&gt;Then, it asks &lt;code&gt;islice()&lt;/code&gt; for the next value, which will ask &lt;code&gt;f&lt;/code&gt; for the next
value, which will "unpause" &lt;code&gt;f&lt;/code&gt; from its previous state, resuming with the
statement &lt;code&gt;prev, curr = curr, prev + curr&lt;/code&gt;.  Then it re-enters the next
iteration of the &lt;code&gt;while&lt;/code&gt; loop, and hits the &lt;code&gt;yield curr&lt;/code&gt; statement, returning
the next value of &lt;code&gt;curr&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This happens until the output list is 10 elements long and when &lt;code&gt;list()&lt;/code&gt; asks
&lt;code&gt;islice()&lt;/code&gt; for the 11th value, &lt;code&gt;islice()&lt;/code&gt; will raise a &lt;code&gt;StopIteration&lt;/code&gt;
exception, indicating that the end has been reached, and list will return the
result: a list of 10 items, containing the first 10 Fibonacci numbers.  Notice
that the generator doesn't receive the 11th &lt;code&gt;next()&lt;/code&gt; call.  In fact, it will
not be used again, and will be garbage collected later.&lt;/p&gt;
&lt;h3 id="types-of-generators"&gt;Types of Generators &lt;a href="#types-of-generators" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;There are two types of generators in Python: generator &lt;strong&gt;functions&lt;/strong&gt; and
generator &lt;strong&gt;expressions&lt;/strong&gt;.  A generator function is any function in which the
keyword &lt;code&gt;yield&lt;/code&gt; appears in its body.  We just saw an example of that.  The
appearance of the keyword &lt;code&gt;yield&lt;/code&gt; is enough to make the function a generator
function.&lt;/p&gt;
&lt;p&gt;The other type of generators are the generator equivalent of a list
comprehension.  Its syntax is really elegant for a limited use case.&lt;/p&gt;
&lt;p&gt;Suppose you use this syntax to build a list of squares:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="go"&gt;[1, 4, 9, 16, 25, 36]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You could do the same thing with a set comprehension:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="go"&gt;{1, 4, 36, 9, 16, 25}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Or a dict comprehension:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="go"&gt;{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;But you can also use a generator expression (note: this is &lt;em&gt;not&lt;/em&gt; a tuple
comprehension):&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;lazy_squares&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;lazy_squares&lt;/span&gt;
&lt;span class="go"&gt;&amp;lt;generator object &amp;lt;genexpr&amp;gt; at 0x10d1f5510&amp;gt;&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lazy_squares&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="go"&gt;1&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lazy_squares&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="go"&gt;[4, 9, 16, 25, 36]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Note that, because we read the first value from &lt;code&gt;lazy_squares&lt;/code&gt; with &lt;code&gt;next()&lt;/code&gt;,
it's state is now at the "second" item, so when we consume it entirely by
calling &lt;code&gt;list()&lt;/code&gt;, that will only return the partial list of squares.  (This is
just to show the lazy behaviour.)  This is as much a generator (and thus, an
iterator) as the other examples above.&lt;/p&gt;
&lt;h2 id="summary"&gt;Summary &lt;a href="#summary" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Generators are an incredible powerful programming construct.  They allow you to
write streaming code with fewer intermediate variables and data structures.
Besides that, they are more memory and CPU efficient.  Finally, they tend to
require fewer lines of code, too.&lt;/p&gt;
&lt;p&gt;Tip to get started with generators: find places in your code where you do the
following:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;something&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And replace it by:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;iter_something&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;

&lt;span class="c1"&gt;# def something():  # Only if you really need a list structure&lt;/span&gt;
&lt;span class="c1"&gt;#     return list(iter_something())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</content>
    <link href="https://nvie.com/posts/iterators-vs-generators/"/>
    <summary>Occasionally I've run into situations of confusion on the exact differences
between the following related concepts in Python:</summary>
    <published>2014-09-25T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://nvie.com/posts/use-more-iterators/</id>
    <title>Use More Iterators</title>
    <updated>2014-10-01T00:00:00+00:00</updated>
    <content type="html">&lt;p&gt;One of my top favorite features of the Python programming language is
generators.  They are so useful, yet I don't encounter them often enough when
reading open source code.  In this post I hope to outline their simplest use
case and hope to encourage any readers to use them more often.&lt;/p&gt;
&lt;p&gt;This post assumes you know what a container and an iterator is.  I've explained
these concepts in a &lt;a href="/posts/iterators-vs-generators/"&gt;previous blog post&lt;/a&gt;.  In
a &lt;a href="/posts/thinking-in-streams/"&gt;follow-up&lt;/a&gt; post, I elaborate on what can be achieved with
thinking in streams a bit more.&lt;/p&gt;
&lt;h2 id="why"&gt;Why? &lt;a href="#why" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Why are iterators a good idea?  Code using iterators can avoid intermediate
variables, lead to shorter code, run lazily, consume less memory, run faster,
are composable, and are more beautiful.  In short: they are more elegant.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;"The moment you've made something iterable, you've done something magic with
your code.  As soon as something's iterable, you can feed it to &lt;code&gt;list()&lt;/code&gt;,
&lt;code&gt;set()&lt;/code&gt;, &lt;code&gt;sorted()&lt;/code&gt;, &lt;code&gt;min()&lt;/code&gt;, &lt;code&gt;max()&lt;/code&gt;, &lt;code&gt;heapify()&lt;/code&gt;, &lt;code&gt;sum()&lt;/code&gt;, ‥.  Many of the
tools in Python consume iterators."&lt;/p&gt;
&lt;p style="text-align: right"&gt;— Raymond Hettinger (&lt;a href="https://www.youtube.com/watch?v=OSGv2VnC0go#t=825"&gt;source&lt;/a&gt;)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Recently, Clojure added transducers to the language, which is a concept pretty
similar to generators in Python.  (I highly recommend watching &lt;a href="https://www.youtube.com/watch?v=6mTbuzafcII"&gt;Rich Hickey's
talk&lt;/a&gt; at Strange Loop 2014 where he introduces them.)&lt;/p&gt;
&lt;p&gt;In the video, he talks about "pouring" one collection into another, which
I think is a verb that very intuitively describes the nature of iterators in
relationship to datastructures.  I'm going to write about this idea in more
detail in a future blog post.&lt;/p&gt;
&lt;!-- TODO: link to future post --&gt;

&lt;h2 id="example"&gt;Example &lt;a href="#example" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Here's an example of a pattern commonly seen:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_lines&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;startswith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;#&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;

&lt;span class="n"&gt;lines&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;get_lines&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now look at the equivalent thing as a generator:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_lines&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;startswith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;#&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;

&lt;span class="n"&gt;lines&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;get_lines&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;!--
By the way, this generator is simple enough that you could even write it as
a generator expression:

  # Equivalent to the generator function
  get_lines = (line for line in f if not line.startswith('#'))
--&gt;

&lt;h2 id="the-benefits"&gt;The Benefits &lt;a href="#the-benefits" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Not much of a difference at first sight, but the benefits are pretty
substantial.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No bookkeeping.&lt;/strong&gt;  You don't have to create an empty list, append to it,
  and return it.  One more variable gone;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hardly consumes memory.&lt;/strong&gt;  No matter how large the input file is, the
  iterator version does not need to buffer the entire file in memory;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Works with infinite streams.&lt;/strong&gt;  The iterator version still works if &lt;code&gt;f&lt;/code&gt; is
  an infinite stream (i.e. stdin);&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Faster results.&lt;/strong&gt;  Results can be consumed immediately, not after the
  entire file is read;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Speed.&lt;/strong&gt;  The iterator version runs faster than building a list the naive
  way;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Composability.&lt;/strong&gt;  The caller gets to decide how it wants to use the result.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The last bullet is by far the most important one.  Let's dig in.&lt;/p&gt;
&lt;h2 id="composability"&gt;Composability &lt;a href="#composability" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Composability is key here.  Iterators are incredibly composable.  In the
example, a list is built explicitly.  What if the caller actually needs a set?
In practice, many people will either create a second, set-based version of the
same function, or simply wrap the call in a &lt;code&gt;set()&lt;/code&gt;.  Surely that works, but it
is a waste of resources.  Imagine the large file again.  First a list is built
from the entire file.  Then it's passed to &lt;code&gt;set()&lt;/code&gt; to build another collection
in memory.  Then the original list is garbage collected.&lt;/p&gt;
&lt;p&gt;With generators, the function just "emits" a stream of objects.  The caller
gets to decide &lt;em&gt;into&lt;/em&gt; what collection those objects gets poured.&lt;/p&gt;
&lt;p class="centered"&gt;&lt;img alt="" src="/img/generator-explained-by-your-finest-TAP@2x.jpg" width="545" /&gt;&lt;/p&gt;
&lt;p&gt;Want a set instead of a list?&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="n"&gt;uniq_lines&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;get_lines&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Want just the longest line from the file?  The file will be read entirely, but
at most two lines are kept in memory at all times:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="n"&gt;longest_line&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;get_lines&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Want just the first 10 lines from the file?  No more than 10 lines will be read
from the file, no matter how large it is:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;islice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;get_lines&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="loop-like-a-native"&gt;Loop Like a Native &lt;a href="#loop-like-a-native" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; At PyCon 2013, Ned Batchelder gave a great talk that perfectly
reflects what I tried to explain in this blog post.  You can watch it here,
I highly recommend it:&lt;/p&gt;
&lt;iframe width="560" height="420" style="display: block; margin-left: auto; margin-right: auto;"
        src="//www.youtube.com/embed/EnSu9hHGq5o" frameborder="0" allowfullscreen&gt;
&lt;/iframe&gt;

&lt;h2 id="summary"&gt;Summary &lt;a href="#summary" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Don't collect data in a result variable.  You can almost always avoid them.
You gain readability, speed, a smaller memory footprint, and composability in
return.&lt;/p&gt;</content>
    <link href="https://nvie.com/posts/use-more-iterators/"/>
    <summary>One of my top favorite features of the Python programming language is
generators.  They are so useful, yet I don't encounter them often enough when
reading open source code.  In this post I hope to outline their simplest use
case and hope to encourage any readers to use them more often.</summary>
    <published>2014-09-29T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://nvie.com/posts/modifying-deeply-nested-structures/</id>
    <title>Modifying Deeply-Nested Structures</title>
    <updated>2014-10-03T00:00:00+00:00</updated>
    <content type="html">&lt;p&gt;Yesterday, a friend asked me how I would solve a certain problem he was facing.
He did have a working solution, but felt like he could make it more generally
applicable.  Not shying away at a good challenge, I decided to take it and see
how I would solve it.  In this blog post you can read about my solution.&lt;/p&gt;
&lt;h2 id="the-problem"&gt;The Problem &lt;a href="#the-problem" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Consider this JSON document.  Return the same JSON document, but with the
points list sorted by stop time in descending order.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;timestamp&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1412282459&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;res&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;group&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;1&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;catlist&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;          &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;cat&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;1&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;          &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;start&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;none&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;          &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;stop&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;none&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;          &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;points&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;              &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;point&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;1&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;start&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;13.00&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;stop&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;13.35&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;              &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;point&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;2&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;start&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;11.00&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;stop&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;14.35&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;          &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; It's kept short for brevity: the actual document contained many
more items in each of the nested lists (so multiple groups, categories, and
points), but this snippet covers the overall structure.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id="analysis"&gt;Analysis &lt;a href="#analysis" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;This is an incredibly common task I'm sure any programmer with a sufficiently
long career has encountered in one shape or form.&lt;/p&gt;
&lt;p&gt;The simple, &lt;em&gt;ad hoc&lt;/em&gt;, solution to the problem above is:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sort_points&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;group&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;res&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;cat&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;group&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;catlist&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
            &lt;span class="n"&gt;cat&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;points&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cat&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;points&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;stop&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Some downsides in arbitrary order:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Not reusable.&lt;/strong&gt;  This function can only deal with dicts of the exact same
  structure.  It's required to know the key names and the type of data living
  at each nesting level.  Any other dict will make it choke;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Brittle.&lt;/strong&gt;  The algorithm itself needs to be changed when the document gets
  nested in another document, or when its structure should change.  Nest it in
  another dictionary, and you need an extra &lt;code&gt;for&lt;/code&gt; loop in there.  Pass it just
  a category, and you need to take out a &lt;code&gt;for&lt;/code&gt; loop;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Lacks abstraction.&lt;/strong&gt;  The algorithm mixes traversing the dict with
  modifying it—these should be two separate things.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Changes the dict in-place.&lt;/strong&gt;  There's no need to rely on using mutable data
  structures here.  It should work for dicts you cannot change, too.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="solution"&gt;Solution &lt;a href="#solution" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;How do we solve this more elegantly?&lt;/p&gt;
&lt;p&gt;The data itself isn't interesting in the slightest.  This is a problem of
structure only.  Let's try to break out the pieces that are &lt;em&gt;specific&lt;/em&gt; for this
problem and see if we can factor out a generic piece, taking the specific parts
as arguments.&lt;/p&gt;
&lt;p&gt;The three key tasks we need to perform:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;traverse the entire structure recursively;&lt;/li&gt;
&lt;li&gt;determine if we've arrived at a given designated path;&lt;/li&gt;
&lt;li&gt;change the nested structure at that location (using given function).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Note that these steps already show our function params.  We need a way of
specifying the "path" to drill down into, and specify what transformation to
apply to those targeted elements.&lt;/p&gt;
&lt;hr /&gt;
&lt;h4 id="traversal"&gt;Traversal &lt;a href="#traversal" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h4&gt;
&lt;p&gt;First and foremost, we need a way of traversing arbitrarily nested structures.
Given that this is only JSON data (so limited to strings, numbers, lists and
dicts), we can start with a recursive function that will walk the structure and
produce a new output which will essentially be a deep copy of the input:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;
    &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;elem&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;elem&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;  &lt;span class="c1"&gt;# no container, just values (str, int, float)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here are some examples:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="go"&gt;3&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;ohai&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="go"&gt;&amp;#39;ohai&amp;#39;&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;ohai&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;name&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;Vincent&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;age&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;}])&lt;/span&gt;
&lt;span class="go"&gt;[&amp;#39;ohai&amp;#39;, {&amp;#39;name&amp;#39;: &amp;#39;Vincent&amp;#39;, &amp;#39;age&amp;#39;: 32}]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;hr /&gt;
&lt;h4 id="path-matching"&gt;Path Matching &lt;a href="#path-matching" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h4&gt;
&lt;p&gt;To specify the target path into the structure, we need to come up with a syntax
that can express those, a mini-language.  Here's an example:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&amp;#39;res[].catlist[].points&amp;#39;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This notation clearly specifies the steps to take to drill down into the
structure to arrive at any nested element.  Note that each step is explicit:
it's either a step into a dict key (the string), or into a list (the empty
list).  This convenient string notation is of course just sugar for the
following:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;res&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;catlist&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;points&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;How can we use this structure?  Let's take the &lt;code&gt;traverse()&lt;/code&gt; function and change
it to keep a record of the paths it's traversing along as it traverses:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;
    &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;elem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;[[]])&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;elem&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;But we're not doing anyting with the &lt;code&gt;path&lt;/code&gt; we're tracking this way yet.&lt;/p&gt;
&lt;hr /&gt;
&lt;h4 id="applying-an-action"&gt;Applying an Action &lt;a href="#applying-an-action" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h4&gt;
&lt;p&gt;Now we need to use that path and in the interesting case perform an action.
One way is to add that to the &lt;code&gt;traverse()&lt;/code&gt; function, but it would do more than
just traversing that way.  Let's update the traverse function with a callback
argument that will get called for every node in the structure (every leaf, dict
entry, or list item).  This would fully decouple traversing the structure from
modifying it.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;callback&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                 &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;
    &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;elem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;[[]],&lt;/span&gt; &lt;span class="n"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                 &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;elem&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;callback&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;# if a callback is provided, call it to get the new value&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now the &lt;code&gt;traverse()&lt;/code&gt; function is really generic and can be used to replace any
node in the structure.  We can now implement our &lt;code&gt;traverse_modify()&lt;/code&gt; function
that will look for a specific node, and update it.  In this example, the
&lt;code&gt;transformer()&lt;/code&gt; function is our callback that will be invoked on every node in
the structure.  If the current path matches the target path, it will perform
the action.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;traverse_modify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target_path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;target_path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;to_path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target_path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# converts &amp;#39;foo.bar&amp;#39; to [&amp;#39;foo&amp;#39;, &amp;#39;bar&amp;#39;]&lt;/span&gt;

    &lt;span class="c1"&gt;# This will get called for every path/value in the structure&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;transformer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;target_path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;callback&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;transformer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="back-to-the-problem"&gt;Back to the Problem &lt;a href="#back-to-the-problem" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;There we have it: a generic, reusable function that abstracted out each
individual interaction: traversal, path matching, and action.  To solve our
original problem with it, now simply call:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;operator&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;itemgetter&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sort_points&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;points&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sd"&gt;&amp;quot;&amp;quot;&amp;quot;Will sort a list of points.&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;points&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;itemgetter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;stop&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="n"&gt;traverse_modify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;res[].catlist[].points&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sort_points&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here's the gist to the &lt;a href="https://gist.github.com/nvie/f304caf3b4f1ca4c3884"&gt;complete solution&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id="go-wild"&gt;Go Wild &lt;a href="#go-wild" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;This blog post was meant to provide some insight into a typical software
engineering approach to problem solving.  Breaking down a problem into pieces,
and abstracting out the essence is perhaps the most joyful thing to do as an
engineer.&lt;/p&gt;
&lt;p&gt;The constructed &lt;code&gt;traverse_modify()&lt;/code&gt; function could be written in many different
ways if you would like to give it more power.  Examples are supporting the
following path specs:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;qux[].(foo|bar)[]  # match both foo or bar
results[0..4]      # only apply action to first 4 results
foo.bar.*          # match any key value
foo.bar.**.qux     # match 0 or more levels of keys
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Another extension would be to allow it to take multiple target-path/action
combinations and apply them all in a single traversal.&lt;/p&gt;
&lt;p&gt;All of that is left as an exercise to the reader though, to keep this post
clear and concise.&lt;/p&gt;
&lt;h2 id="summary"&gt;Summary &lt;a href="#summary" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The fantastic thing is that, picking a good abstraction for the &lt;code&gt;traverse()&lt;/code&gt;
function opens a door to an entire world of possibilities to modify arbitrary
Python object structures.  In the end, you get more than you initially set out
for.&lt;/p&gt;
&lt;p&gt;If you build something cool based on this however, please let me know :)&lt;/p&gt;</content>
    <link href="https://nvie.com/posts/modifying-deeply-nested-structures/"/>
    <summary>Yesterday, a friend asked me how I would solve a certain problem he was facing.
He did have a working solution, but felt like he could make it more generally
applicable.  Not shying away at a good challenge, I decided to take it and see
how I would solve it.  In this blog post you can read about my solution.</summary>
    <published>2014-10-03T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://nvie.com/posts/thinking-in-streams/</id>
    <title>Thinking in streams</title>
    <updated>2015-03-25T00:00:00+00:00</updated>
    <content type="html">&lt;p&gt;In my previous post, &lt;a href="/posts/use-more-iterators/"&gt;Use More Iterators&lt;/a&gt;, I have outlined how
to harvest some low hanging fruit when it comes to converting your functions to
generators.  In this series of posts I want to take it to the next level and
introduce a few powerful constructs that can assist you when working with
streams.&lt;/p&gt;
&lt;!-- how I built Iron, a streaming build system, using generators --&gt;

&lt;h2 id="streams"&gt;Streams &lt;a href="#streams" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Previously, I've compared Python's generators to &lt;a href="/posts/iterators-vs-generators/#iterators"&gt;value factories&lt;/a&gt;
(producing values lazily) and talked about their
&lt;a href="/posts/use-more-iterators/#composability"&gt;composability&lt;/a&gt;.  I want to pay some more attention to these
concepts in this blog post.&lt;/p&gt;
&lt;p&gt;One particular concept that fits generators like a glove is to use them to
stream data.  Streams help you express solutions to many data manipulation
flows and processes elegantly.  Of course, this idea is not novel: the concept
of streams finds its roots in the early 60's (as all good CS ideas do).&lt;/p&gt;
&lt;p&gt;How do streams fit generators?  Since a generator is a function that returns
a "value factory", it's a natural component to act as a "node" in a network of
such generators.  Each such component takes input, does something with it, and
emits output.&lt;/p&gt;
&lt;h3 id="a-little-word-puzzle"&gt;A Little Word Puzzle &lt;a href="#a-little-word-puzzle" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Take a look at this example to generate a little word puzzle.  It generates
a list of the first 5 dictionary words of 20 characters or more that aren't
names, and hides their vowels:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;re&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;itertools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;islice&lt;/span&gt;

&lt;span class="n"&gt;vowels_re&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;[aeiouy]&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;all_words&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;/usr/share/dict/words&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;keep_long_words&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;iterable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;min_len&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;iterable&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;min_len&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;exclude_names&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;iterable&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;iterable&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;hide_vowels&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;iterable&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;iterable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;vowels_re&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;.&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;iterable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;islice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;iterable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;all_words&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;keep_long_words&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;exclude_names&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hide_vowels&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This will print the following list:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;.bd.m.n.h.st.r.ct.m.
.c.t.lm.th.lc.rb.n.l
.c.t.lph.n.lh.dr.z.n.
.m.n..c.t.ph.n.t.d.n.
.n.rch..nd.v.d..l.st
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="wrapwrapwrap"&gt;Wrap(Wrap(Wrap(…))) &lt;a href="#wrapwrapwrap" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The variable &lt;code&gt;stream&lt;/code&gt; is used to incrementally build up an entire stream
(network of stream processors).  It start with &lt;code&gt;all_words()&lt;/code&gt;, the generator
that emits all dictionary words from the dictionary file.&lt;/p&gt;
&lt;p&gt;Then with each further step, &lt;code&gt;stream&lt;/code&gt; is "wrapped" in another generator, which
is used to chain the generators together.  The emitted output of &lt;code&gt;all_words()&lt;/code&gt;
will now be consumed by the &lt;code&gt;keep_long_words()&lt;/code&gt; generator, emitting only the
words from the input stream that match the length criterium.&lt;/p&gt;
&lt;p&gt;We keep "wrapping" these with another filter step (&lt;code&gt;exclude_names()&lt;/code&gt;) and
a manipulation step (&lt;code&gt;hide_vowels()&lt;/code&gt;), and finally limit the list to return
maximally 5 items.&lt;/p&gt;
&lt;p&gt;As a result, the variable &lt;code&gt;stream&lt;/code&gt; is re-assigned a few times.  There is a nice
advantage to this approach: it avoids using any further variables, and allows
us to build up the entire stream line by line.  The order in which we build it
up, resembles the way the data flows.&lt;/p&gt;
&lt;p&gt;Lastly, you can comment out a line in the middle and the code still works.  If
you decide you &lt;em&gt;do&lt;/em&gt; want names in the result list, simply comment out this
line:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;all_words&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;keep_long_words&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# stream = exclude_names(stream)  # comment this out to skip this step&lt;/span&gt;
&lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hide_vowels&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And the stream will still be valid.  This is especially useful while trying out
your streams as you're still developing them streams.  Without using this
intermediate variable, the equivalent would look like this:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hide_vowels&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exclude_names&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;keep_long_words&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;all_words&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;))),&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This does the same thing, but is rather messy, and gets unreadable quickly.&lt;/p&gt;
&lt;h2 id="a-dsl-to-compose-streams"&gt;A DSL to Compose Streams &lt;a href="#a-dsl-to-compose-streams" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Putting together the pieces of the stream as we did above is relatively clunky.
There is a better way.  What if we could express the thing above like this?&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;all_words&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
          &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;keep_long_words&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
          &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;exclude_names&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
          &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;hide_vowels&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
          &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This syntax would have the best of both worlds: it allows you to elegantly
chain together generators using the &lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt; operator without using an intermediate
variable.  If &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt; are streams, then the result of &lt;code&gt;A &amp;gt;&amp;gt; B&lt;/code&gt; is the
composition of both streams, applying &lt;code&gt;A&lt;/code&gt; first to its input, then applying
&lt;code&gt;B&lt;/code&gt;:&lt;/p&gt;
&lt;p class="centered"&gt;&lt;img alt="" src="/img/composition@2x.png" width="488" /&gt;&lt;/p&gt;
&lt;p&gt;We can actually build this.  Let's define a &lt;code&gt;Task&lt;/code&gt;, a base class for each such
component that's chainable using the &lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt; operator.  It looks like this:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other_task&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;ChainedTask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other_task&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;inputs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="ne"&gt;NotImplementedError&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="fm"&gt;__iter__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;process&lt;/span&gt;&lt;span class="p"&gt;([])&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="fm"&gt;__rshift__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ChainedTask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="fm"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;task1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;task2&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;task1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;task1&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;task2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;task2&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;inputs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;task2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;task1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inputs&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;But how do we get from the generator functions of the example above to these
tasks that support the &lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt; operator?  We need to convert them to tasks by
implementing the &lt;code&gt;process()&lt;/code&gt; method.  Here's the &lt;code&gt;exclude_names()&lt;/code&gt; function
converted:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;exclude_names&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;inputs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;iterable&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And here is an example of converting a function with arguments.  The argument
moves to the constructor of the class:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;keep_long_words&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="fm"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;min_len&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;min_len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;min_len&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;iterable&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;iterable&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;min_len&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Another notable case is the "starting" generator: the one spitting out the
dictionary words.  As a generator function, this did not take any inputs, but
as a Task, it still receives the &lt;code&gt;inputs&lt;/code&gt; argument, but should ignore it.  This
way, we can treat it like any other task, and we'll see an example of how this
is useful later on:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;all_words&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;inputs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;  &lt;span class="c1"&gt;# inputs arg is ignored&lt;/span&gt;
        &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;/usr/share/dict/words&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="making-compositions"&gt;Making Compositions &lt;a href="#making-compositions" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Using this, we can now start to make some abstractions.  We can assign a series
of chained tasks to a variable and insert where we need it.  This can be used
to clarify what is going on in those steps, or for reusability of a component.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;puzzlify&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;keep_long_words&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;exclude_names&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;hide_vowels&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;all_words&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;puzzlify&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Note that calling &lt;code&gt;puzzlify()&lt;/code&gt; will return a &lt;code&gt;Task&lt;/code&gt; instance (the one that
chains together the three sub tasks).  Then, this task instance is further
chained into the larger example.  Also note that &lt;code&gt;puzzlify()&lt;/code&gt; itself is
a perpetual processor: there's no start or end defined by it.  The context it's
used in defined the start and the end.&lt;/p&gt;
&lt;p&gt;The chaining primitive allows you to craft complex data streams in an elegant
fashion.&lt;/p&gt;
&lt;h2 id="complex-operations"&gt;Complex Operations &lt;a href="#complex-operations" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The composition operation (&lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt;) isn't the only task common with streams.
Another one is the split-and-join operation.  In this scenario, you may want to
perform multiple operations on a single stream independent of each other.  This
can be achieved  using the &lt;code&gt;&amp;amp;&lt;/code&gt; operator:&lt;/p&gt;
&lt;p class="centered"&gt;&lt;img alt="" src="/img/split-and-join@2x.png" width="301" /&gt;&lt;/p&gt;
&lt;p&gt;This will split the inputs and feed copies of the inputs to both processes.
After applying each task, the results get merged back, exhausting &lt;code&gt;A&lt;/code&gt; first,
then &lt;code&gt;B&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Suppose we would want to filter our dictionary for words that are anagrams &lt;em&gt;or&lt;/em&gt;
contain the substring &lt;code&gt;anana&lt;/code&gt; (or both).  You could do it as follows:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;all_words&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
          &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;is_anagram&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;contains_anana&lt;/span&gt;&lt;span class="p"&gt;()))&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The real power of this split-and-join operator comes when you combine it to
perform different actions on each "side" of the split.  For example, if you
want to uppercase all of the anagrams, but lowercase all of the words
containing the "anana" substring:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;all_words&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
          &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;is_anagram&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;uppercase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
              &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;
              &lt;span class="n"&gt;contains_anana&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;lowercase&lt;/span&gt;&lt;span class="p"&gt;()))&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And here are the results:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;...
SOOLOOS
TEBBET
TEBET
TENET
TERRET
ULULU
YARAY
anana
ananaplas
ananaples
ananas
banana
bananaland
bananalander
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is all done in a single stream.&lt;/p&gt;
&lt;h2 id="why-and"&gt;Why &lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;amp;&lt;/code&gt;? &lt;a href="#why-and" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;You may ask why I did not follow the more familiar syntax that most shells
follow, using &lt;code&gt;A | B&lt;/code&gt;.  The reason is Python's operator precedence.&lt;/p&gt;
&lt;!--
Some of you may point out that this resembles golf.yes alongEffect we haveIn
fact we have actually used golf ourselves but weren't satisfied with them
they're setting system and the extensibility.

Dit lijkt verdacht veel op gulp.js.  Feitelijk hebben we dit ook zelf gebruikt
alleen aangezien we geen najaars de brommers zijn hebben we besloten omNaar de
wijze van experimentZelf een stream framework schrijvenAan.Dit is eigenlijk
begonnen als er eenExperiment om te kijken welke optiesNaar buiten 33 ons kan
bieden.Er is een beetje uit hand gelopen.
--&gt;</content>
    <link href="https://nvie.com/posts/thinking-in-streams/"/>
    <summary>In my previous post, [Use More Iterators][moar-iterators], I have outlined how
to harvest some low hanging fruit when it comes to converting your functions to
generators.  In this series of posts I want to take it to the next level and
introduce a few powerful constructs that can assist you when working with
streams.</summary>
    <published>2015-03-25T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://nvie.com/posts/beautiful-map/</id>
    <title>Beautiful map</title>
    <updated>2015-06-15T00:00:00+00:00</updated>
    <content type="html">&lt;p&gt;I was reading Igor Kalnitsky's blog post on why Python's &lt;a href="https://kalnytskyi.com/2015/06/14/mad-map/"&gt;&lt;code&gt;map()&lt;/code&gt; is
mad&lt;/a&gt;, and wanted to provide
a different perspective.  In fact, I would call the design of Python's &lt;code&gt;map()&lt;/code&gt;
beautiful instead.&lt;/p&gt;
&lt;p&gt;First off, what does &lt;code&gt;map(f, xs)&lt;/code&gt; represent mathematically in the first place?
It should invoke function &lt;code&gt;f(x)&lt;/code&gt; for every &lt;code&gt;x&lt;/code&gt; in &lt;code&gt;xs&lt;/code&gt;.  Functions, of course,
can take many arguments—single argument functions are just the simplest case.
So what would be reasonable to assume &lt;code&gt;map(f, xs, ys)&lt;/code&gt; would do?  In the blog
post, Igor suggests the behaviour should be to chain &lt;code&gt;xs&lt;/code&gt; and &lt;code&gt;ys&lt;/code&gt;, but chances
are they represent completely different things, so chaining them would lead to
a heterogenous collection of items.  Mathematically, you would expect the
function calls made to be &lt;code&gt;f(x1, y1)&lt;/code&gt;, &lt;code&gt;f(x2, y2)&lt;/code&gt;, ...&lt;/p&gt;
&lt;p&gt;Note that this is &lt;em&gt;different&lt;/em&gt; from &lt;code&gt;zip()&lt;/code&gt;'ing the function arguments.
A function &lt;code&gt;f&lt;/code&gt; with 2 arguments is different from a function &lt;code&gt;f&lt;/code&gt; with
1 argument, expecting a tuple.&lt;/p&gt;
&lt;p&gt;Compare:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;

&lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;a&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;b&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;c&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;    &lt;span class="c1"&gt;# [&amp;#39;a&amp;#39;, &amp;#39;bb&amp;#39;, &amp;#39;ccc&amp;#39;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;to&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pair&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pair&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;

&lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;zip&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;a&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;b&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;c&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt;  &lt;span class="c1"&gt;# [&amp;#39;a&amp;#39;, &amp;#39;bb&amp;#39;, &amp;#39;ccc&amp;#39;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The confusion around the items appearing to be zipped is caused by the implicit
behaviour in Python 2 when the first argument is &lt;code&gt;None&lt;/code&gt;.  I think it's handled
as a special case, which is unfortunate.  A more consistent behaviour would have been to&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="go"&gt;Python 2.7.9 (default, Dec 19 2014, 06:00:59)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;a&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="gt"&gt;Traceback (most recent call last):&lt;/span&gt;
  File &lt;span class="nb"&gt;&amp;quot;&amp;lt;stdin&amp;gt;&amp;quot;&lt;/span&gt;, line &lt;span class="m"&gt;1&lt;/span&gt;, in &lt;span class="n"&gt;&amp;lt;module&amp;gt;&lt;/span&gt;
&lt;span class="gr"&gt;TypeError&lt;/span&gt;: &lt;span class="n"&gt;&amp;lt;lambda&amp;gt;() takes exactly 1 argument (2 given)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;a&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="go"&gt;[(&amp;#39;a&amp;#39;, 1)]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The TypeError would have been the sane thing to do, since the identity function
should only ever take one argument.&lt;/p&gt;
&lt;p&gt;Therefore, my advice would be to never use the implicit &lt;code&gt;None&lt;/code&gt; as the first
argument.  It is broken under Python 3 anyway.&lt;/p&gt;
&lt;h2 id="to-zip-or-to-zip_longest"&gt;To &lt;code&gt;zip()&lt;/code&gt; or to &lt;code&gt;zip_longest()&lt;/code&gt;? &lt;a href="#to-zip-or-to-zip_longest" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The fact that the behaviour changed in Python 3 is unfortunate, but I think it
changed for the better.  The problem with &lt;code&gt;zip_longest()&lt;/code&gt;-like default
semantics is that it will only ever work with finite iterables.  If only one of
the given iterables is infinite, the map will be infinite too.  Now, perhaps
this is what you want, but in that case you should probably be explicit about
it anyway.  I think using &lt;code&gt;zip()&lt;/code&gt;-like semantics as the default makes perfect
sense.  It enables the following usage in Python 3:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;itertools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;a&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;b&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;c&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;    &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;...&lt;/span&gt;
&lt;span class="go"&gt;a&lt;/span&gt;
&lt;span class="go"&gt;bb&lt;/span&gt;
&lt;span class="go"&gt;ccc&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Compare this to Python 2's map behaviour, which would do:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;a&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;b&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;c&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;    &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;...&lt;/span&gt;
&lt;span class="go"&gt;a&lt;/span&gt;
&lt;span class="go"&gt;bb&lt;/span&gt;
&lt;span class="go"&gt;ccc&lt;/span&gt;
&lt;span class="gt"&gt;Traceback (most recent call last):&lt;/span&gt;
  File &lt;span class="nb"&gt;&amp;quot;&amp;lt;stdin&amp;gt;&amp;quot;&lt;/span&gt;, line &lt;span class="m"&gt;1&lt;/span&gt;, in &lt;span class="n"&gt;&amp;lt;module&amp;gt;&lt;/span&gt;
  File &lt;span class="nb"&gt;&amp;quot;&amp;lt;stdin&amp;gt;&amp;quot;&lt;/span&gt;, line &lt;span class="m"&gt;1&lt;/span&gt;, in &lt;span class="n"&gt;f&lt;/span&gt;
&lt;span class="gr"&gt;TypeError&lt;/span&gt;: &lt;span class="n"&gt;unsupported operand type(s) for *: &amp;#39;NoneType&amp;#39; and &amp;#39;int&amp;#39;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Because it tries to invoke &lt;code&gt;f(None, 4)&lt;/code&gt; the fourth time, which happens to fail.
If it would not fail, it would produce results infinitely.&lt;/p&gt;
&lt;p&gt;But what &lt;em&gt;if&lt;/em&gt; you actually want &lt;code&gt;zip_longest()&lt;/code&gt;-like behaviour?  Well, you can
either make all arguments be infinite iterables, or you can explicitly wrap
your arguments in a &lt;code&gt;zip_longest()&lt;/code&gt; wrapper, and pass that to &lt;code&gt;starmap()&lt;/code&gt;,
which will take an iterable of tuples and spread it over the arguments to
&lt;code&gt;f()&lt;/code&gt;, just like &lt;code&gt;map&lt;/code&gt;:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;itertools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;islice&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;starmap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;zip_longest&lt;/span&gt;
&lt;span class="go"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;starmap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;zip_longest&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;a&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;b&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;c&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;fillvalue&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;?&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;islice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;    &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;...&lt;/span&gt;
&lt;span class="go"&gt;a&lt;/span&gt;
&lt;span class="go"&gt;bb&lt;/span&gt;
&lt;span class="go"&gt;ccc&lt;/span&gt;
&lt;span class="go"&gt;????&lt;/span&gt;
&lt;span class="go"&gt;?????&lt;/span&gt;
&lt;span class="go"&gt;??????&lt;/span&gt;
&lt;span class="go"&gt;???????&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;As a bonus, you can pass in a fillvalue this way, instead of being stuck with
the assumption of &lt;code&gt;None&lt;/code&gt; (which could happen to be a valid value within the
iterable).&lt;/p&gt;
&lt;p&gt;However, personally, in this case, I'd prefer the following, more readable
version that avoids the &lt;code&gt;zip_longest()&lt;/code&gt; and &lt;code&gt;starmap()&lt;/code&gt; calls:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chain&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;a&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;b&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;c&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;?&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Note how you can thus make the map result infinite by simply making all
iterables infinite.  Consuming iterables until the first one is exhausted (so
&lt;code&gt;zip()&lt;/code&gt;-like), thus, is the sanest default behaviour, and the most beautiful of
the options.&lt;/p&gt;
&lt;h2 id="python-3-gets-it-right"&gt;Python 3 gets it right &lt;a href="#python-3-gets-it-right" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I'm glad that Python 3 changed &lt;code&gt;map()&lt;/code&gt; to be sane in every way:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It's made a lazy iterator, does not directly &lt;a href="/posts/use-more-iterators/#composability"&gt;produce
  a list&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;It disallows the ambiguous &lt;code&gt;None&lt;/code&gt; first argument&lt;/li&gt;
&lt;li&gt;It consumes the iterables until the first one is exhausted.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="bonus-your-own-zip"&gt;Bonus: your own &lt;code&gt;zip()&lt;/code&gt; &lt;a href="#bonus-your-own-zip" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Did you know you could express &lt;code&gt;zip()&lt;/code&gt; with &lt;code&gt;map()&lt;/code&gt;?  It's easy, now you know
the exact semantics:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;zip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;iterables&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;tuple&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;iterables&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</content>
    <link href="https://nvie.com/posts/beautiful-map/"/>
    <summary>I was reading Igor Kalnitsky's blog post on why Python's [`map()` is
mad](https://kalnytskyi.com/2015/06/14/mad-map/), and wanted to provide
a different perspective.  In fact, I would call the design of Python's `map()`
beautiful instead.</summary>
    <published>2015-06-15T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://nvie.com/posts/beautiful-code/</id>
    <title>Beautiful code</title>
    <updated>2015-06-16T00:00:00+00:00</updated>
    <content type="html">&lt;p&gt;When was the last time you looked at code someone else wrote?  (Debugging
doens't count!)  When did you &lt;em&gt;actually&lt;/em&gt; study it to learn from it?  Perhaps
even ponder over its beauty?&lt;/p&gt;
&lt;p&gt;It's surprising &lt;strong&gt;how uncommon it is in our industry to look at existing code
just to learn from it&lt;/strong&gt;.  In almost any other engineering or art field, people
constantly study the results of their peers.  Books on architecture are a great
example.  What makes a certain design so beautiful or effective?  Can I learn
something from it to make me a better engineer?  I feel &lt;strong&gt;we would benefit as
an industry if we would collectively take a little more time to reflect and
study&lt;/strong&gt;.  We should ask ourselves those question more often, and allocate study
time for it occasionally.&lt;/p&gt;
&lt;p class="right"&gt;&lt;img alt="The cover of Beautiful Code" height="300" src="/img/beautiful-code-cover@2x.jpg" /&gt;&lt;/p&gt;
&lt;p&gt;Last week, I ordered the book &lt;a href="http://shop.oreilly.com/product/9780596510046.do"&gt;Beautiful
Code&lt;/a&gt;, by Greg Wilson and
Andy Oram.  I would recommend this book to any fellow professional programmer.
(All of the book's proceeds go to Amnesty International.)&lt;/p&gt;
&lt;p&gt;The book's concept is simple: each of the 33 chapters is written by
a well-respected professional programmer who answers the question: &lt;em&gt;"What is
the most beautiful code you've ever seen?"&lt;/em&gt; after which they discuss
elaborately &lt;em&gt;why&lt;/em&gt; they think it's beautiful.&lt;/p&gt;
&lt;p&gt;Beauty, as it turns out, comes in many shapes and forms.  The topics in the
book vary from an elegant algorithm, to a design that lets all the surrounding
puzzle pieces fall into place perfectly.  From the cleverness of code
generators, to the expressiveness of a language construct.&lt;/p&gt;
&lt;p&gt;Naturally, some chapters have been more interesting than others.  But all of
them have left me with either:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a deep admiration for an elegant solution or cleverness;&lt;/li&gt;
&lt;li&gt;an interesting perspective on good design;&lt;/li&gt;
&lt;li&gt;an appreciation of seemingly banal things, or things I previously did not
  find beauty in;&lt;/li&gt;
&lt;li&gt;insight into how to articulate &lt;em&gt;why&lt;/em&gt; exactly something is beautiful (how
  meta)&lt;/li&gt;
&lt;/ul&gt;</content>
    <link href="https://nvie.com/posts/beautiful-code/"/>
    <summary>When was the last time you looked at code someone else wrote?  (Debugging
doens't count!)  When did you _actually_ study it to learn from it?  Perhaps
even ponder over its beauty?</summary>
    <published>2015-06-16T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://nvie.com/posts/pip-tools-10-released/</id>
    <title>pip-tools 1.0 released</title>
    <updated>2015-07-21T00:00:00+00:00</updated>
    <content type="html">&lt;p&gt;Yesterday, pip-tools version 1.0 was silently released, officially introducing
the &lt;strong&gt;pip-compile&lt;/strong&gt; and &lt;strong&gt;pip-sync&lt;/strong&gt; tools, and replacing the current
&lt;strong&gt;pip-dump&lt;/strong&gt; and &lt;strong&gt;pip-review&lt;/strong&gt; tools.&lt;/p&gt;
&lt;p&gt;I've blogged before about these ideas in &lt;a href="/posts/pin-your-packages"&gt;Pinning Your Packages&lt;/a&gt; and &lt;a href="/posts/better-package-management"&gt;Better
Package Management&lt;/a&gt;.  During the last year, I've been slowly working on the
future branch on the pip-tools repo, and have been using the new tools there.
The &lt;strong&gt;pip-sync&lt;/strong&gt; script was the only thing that was still delaying the release,
but since &lt;a href="https://github.com/hugopeixoto"&gt;Hugo Peixoto&lt;/a&gt; &lt;a href="https://github.com/nvie/pip-tools/pull/181"&gt;contributed&lt;/a&gt; this one recently,
it's now ready to switch over.&lt;/p&gt;
&lt;p&gt;So it's now time to switch over to the new tools if you've been using the old
ones.&lt;/p&gt;
&lt;p&gt;Old: pip-review, pip-dump&lt;br /&gt;
New: &lt;strong&gt;pip-compile&lt;/strong&gt;, &lt;strong&gt;pip-sync&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id="how-to-upgrade"&gt;How to upgrade &lt;a href="#how-to-upgrade" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;If you're using pip-tools 0.x, you'll notice that its main commands, pip-review
and pip-dump are gone.  Instead, you'll find two new commands, pip-compile and
pip-sync, which should allow you to do the same things, but arguably in a more
solid way.&lt;/p&gt;
&lt;p&gt;Typical usage:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;pip install pip-tools&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Record your top-level dependencies in &lt;code&gt;requirements.in&lt;/code&gt;.  Everything you
  &lt;em&gt;directly&lt;/em&gt; use in your source code should be a top-level dependency.&lt;/li&gt;
&lt;li&gt;Don't pin them—unless you want them pinned, of course.&lt;/li&gt;
&lt;li&gt;Put both &lt;code&gt;requirements.in&lt;/code&gt; and &lt;code&gt;requirements.txt&lt;/code&gt; under version control.&lt;/li&gt;
&lt;li&gt;Then, run pip-compile.  This will produce a &lt;code&gt;requirements.txt&lt;/code&gt; that pins the
  high-level requirements to the highest versions found on PyPI to match the
  given requirements.&lt;/li&gt;
&lt;li&gt;Using &lt;code&gt;pip-sync&lt;/code&gt; now will install/upgrade/uninstall everything so that your
  virtual env exactly matches what's in &lt;code&gt;requirements.txt&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For more information, see the
&lt;a href="https://github.com/nvie/pip-tools#readme"&gt;README&lt;/a&gt; of the new tools.&lt;/p&gt;
&lt;p&gt;Let me know how it works for you!&lt;/p&gt;</content>
    <link href="https://nvie.com/posts/pip-tools-10-released/"/>
    <summary>Yesterday, pip-tools version 1.0 was silently released, officially introducing
the **pip-compile** and **pip-sync** tools, and replacing the current
**pip-dump** and **pip-review** tools.</summary>
    <published>2015-07-21T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://nvie.com/posts/introducing-decoders/</id>
    <title>An intro to decoders</title>
    <updated>2018-09-25T00:00:00+00:00</updated>
    <content type="html">&lt;p&gt;Today, I’m thrilled to publicly announce a new open source project that we’ve been using at SimpleContacts in production for months:
&lt;a href="https://www.npmjs.com/package/decoders"&gt;&lt;strong&gt;decoders&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;To get started:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;$ npm install decoders
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here’s a quick example of what decoders can do:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;guard&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;optional&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kr"&gt;from&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;decoders&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;

&lt;span class="c1"&gt;// Define your decoder&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;decoder&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;optional&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;

&lt;span class="c1"&gt;// Build the runtime checker (&amp;quot;guard&amp;quot;) once&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;verify&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;guard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;decoder&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;

&lt;span class="c1"&gt;// Use it&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;unsafeData&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="c1"&gt;//                            ^^^^^^^^^^^^ Could be anything!&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;verify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;unsafeData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="c1"&gt;//    ^^^^ Guaranteed to be a person!&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;

&lt;span class="c1"&gt;// Now, Flow/TypeScript will _know_ the type of data!&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="c1"&gt;// string&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;// number | void&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="c1"&gt;//   ^^^^ 🎉 Type error! Property `city` is missing in `data`.&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="why"&gt;Why? &lt;a href="#why" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;When writing JavaScript programs (whether that’s for the server, or the
browser), one tool that has become indispensable for maintainable code bases is
a type checker like &lt;a href="https://flow.org/"&gt;Flow&lt;/a&gt; or &lt;a href="https://www.typescriptlang.org/"&gt;TypeScript&lt;/a&gt;.  &lt;strong&gt;Disclaimer: I’m mainly
a Flow user, but everything in this post also applies to TypeScript (= also
great).&lt;/strong&gt;  Using a static type checker makes making changes to large JS
possible in ways that weren’t possible before.&lt;/p&gt;
&lt;p&gt;One area where Flow (or TypeScript) coverage is typically hard to achieve is
when dealing with &lt;strong&gt;external data&lt;/strong&gt;.  Think any form of user input, an HTTP
request body, or even the results of a database query are “external” from your
app’s perspective.  How can we type those things?&lt;/p&gt;
&lt;p&gt;For example, say your app wants to do something with data coming in from a POST
request with some JSON body:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The type of &lt;code&gt;data&lt;/code&gt; here will be “&lt;code&gt;any&lt;/code&gt;”.  The reason is of course that we’re
dealing with a &lt;em&gt;static&lt;/em&gt; type checker.  So even though Flow will know that the
input to &lt;code&gt;JSON.parse()&lt;/code&gt; must be a string, it doesn’t know &lt;em&gt;which&lt;/em&gt; string and
the type of &lt;code&gt;JSON.parse()&lt;/code&gt;’s return value will be defined by the &lt;em&gt;value&lt;/em&gt; of the
string at runtime.  In other words, it could be anything.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="ow"&gt;typeof&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;42&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;              &lt;/span&gt;&lt;span class="c1"&gt;// number&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="ow"&gt;typeof&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;&amp;quot;hello&amp;quot;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;         &lt;/span&gt;&lt;span class="c1"&gt;// string&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="ow"&gt;typeof&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;{&amp;quot;name&amp;quot;: &amp;quot;Joe&amp;quot;}&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// object&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Statically, it’s impossible to know the return type.  That’s why Flow can only
define this type signature as:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;any&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Worse, even, is that using these &lt;code&gt;any&lt;/code&gt;-typed values may implicitly
(unknowingly) turn off type checking, even for code that’s type-safe otherwise.&lt;/p&gt;
&lt;p&gt;For example, if you could feed an implicitly-any value to a type-safe function
like:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;Hi, &amp;#39;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;!&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then Flow will just accept this, because &lt;code&gt;data&lt;/code&gt; is &lt;code&gt;any&lt;/code&gt;, and thus &lt;code&gt;data.name&lt;/code&gt;
is &lt;code&gt;any&lt;/code&gt;.  But of course this isn’t safe!  In this example, &lt;code&gt;data&lt;/code&gt; cannot and
should not be trusted.  Flow lets arbitrary values get passed into &lt;code&gt;greet()&lt;/code&gt;
anyway, despite its type annotation!&lt;/p&gt;
&lt;p&gt;Especially in real applications this puts a significant practical cap on Flow’s
usefulness.  &lt;strong&gt;Using &lt;code&gt;any&lt;/code&gt; (whether implicit or explicit) is completely unsafe,
and should be avoided like the plague.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id="decoders-to-the-rescue"&gt;Decoders to the Rescue &lt;a href="#decoders-to-the-rescue" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;How, then, can we statically type these seemingly dynamic beasts?  We can do so
if we change our perspective on the problem a little bit.&lt;/p&gt;
&lt;p&gt;Rather than trying to let Flow &lt;em&gt;infer&lt;/em&gt; the type of a dynamic expression (which
is impossible), what if we would have a way to instead &lt;em&gt;specify&lt;/em&gt; the type we
are expecting, and have an automatic type-check injected at runtime that will
verify those assumptions?  This way, Flow is able to know, statically, what the
runtime type will be.&lt;/p&gt;
&lt;p&gt;As you might have guessed, this is exactly what the &lt;a href="https://github.com/nvie/decoders/"&gt;decoders&lt;/a&gt; library
offers.&lt;/p&gt;
&lt;p&gt;You can use &lt;code&gt;decoders&lt;/code&gt;’ library of composable building blocks that allow you to
specify the shape of your expected output value:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Decoder&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kr"&gt;from&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;decoders&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;guard&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kr"&gt;from&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;decoders&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;

&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Point&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;pointDecoder&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;asPoint&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;guard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pointDecoder&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;p1&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Point&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;asPoint&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;42&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;123&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;p2&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Point&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;asPoint&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mf"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;There are a few interesting pieces to this example.&lt;/p&gt;
&lt;p&gt;First of all, you’ll notice the similarity between the &lt;code&gt;Point&lt;/code&gt; type, and the
structure of the decoder.&lt;/p&gt;
&lt;p&gt;Also note that, by wrapping any value in an &lt;code&gt;asPoint()&lt;/code&gt; call, Flow will
know—&lt;em&gt;statically&lt;/em&gt;—that &lt;code&gt;p1&lt;/code&gt; and &lt;code&gt;p2&lt;/code&gt; will be &lt;code&gt;Point&lt;/code&gt; instances.  And therefore
you get full type support in your editor like tab completion, and full Flow
type safety like you’re used to elsewhere.&lt;/p&gt;
&lt;p&gt;How?  Because if the data does not match the decoder’s description of the data,
the call to &lt;code&gt;verify()&lt;/code&gt; will &lt;strong&gt;throw a runtime error&lt;/strong&gt;.  This will be the case
in the unhappy path, for example:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;p3&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Point&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;asPoint&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;42&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="c1"&gt;//                ^^^^^^^^^^^^^^^^^^ Runtime error: Missing &amp;quot;y&amp;quot; key&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;p4&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Point&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;asPoint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;123&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="c1"&gt;//                ^^^^^^^^^^^^ Runtime error: Must be object&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="composition"&gt;Composition &lt;a href="#composition" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Decoders come with batteries included and these base decoders are designed to
be infinitely composable building blocks, which you can assemble into complex
custom decoders.&lt;/p&gt;
&lt;p&gt;The simplest decoder you can create are the scalar types: &lt;a href="https://github.com/nvie/decoders#number"&gt;&lt;code&gt;number&lt;/code&gt;&lt;/a&gt;,
&lt;a href="https://github.com/nvie/decoders#boolean"&gt;&lt;code&gt;boolean&lt;/code&gt;&lt;/a&gt;, and &lt;a href="https://github.com/nvie/decoders#string"&gt;&lt;code&gt;string&lt;/code&gt;&lt;/a&gt;.  From there, you can compose them
into higher order decoders like &lt;a href="https://github.com/nvie/decoders#object"&gt;&lt;code&gt;object()&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://github.com/nvie/decoders#array"&gt;&lt;code&gt;array()&lt;/code&gt;&lt;/a&gt;,
&lt;a href="https://github.com/nvie/decoders#optional"&gt;&lt;code&gt;optional()&lt;/code&gt;&lt;/a&gt;, or &lt;a href="https://github.com/nvie/decoders#nullable"&gt;&lt;code&gt;nullable()&lt;/code&gt;&lt;/a&gt; to create more complex
types.&lt;/p&gt;
&lt;p&gt;For example, starting with a decoder for &lt;code&gt;Point&lt;/code&gt;s:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;point&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In terms of types:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nx"&gt;point&lt;/span&gt;&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="c1"&gt;// Decoder&amp;lt;Point&amp;gt;&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;point&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="c1"&gt;// Decoder&amp;lt;Array&amp;lt;Point&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="nx"&gt;optional&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;point&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="c1"&gt;// Decoder&amp;lt;Point | void&amp;gt;&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="nx"&gt;nullable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;point&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="c1"&gt;// Decoder&amp;lt;Point | null&amp;gt;&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Decoders also comes with a special &lt;a href="https://github.com/nvie/decoders#regex"&gt;&lt;code&gt;regex()&lt;/code&gt;&lt;/a&gt; decoder which is like the
&lt;code&gt;string&lt;/code&gt; decoder, but will additionally perform a regex match and only allows
string values that match:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;hexcolor&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="sr"&gt;/^#[0-9a-f]{6}$/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;Must be hex color&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="c1"&gt;// Shown in error output&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can then reuse these new decoders above by composing them into a polygon
decoder.  Notice the reuse of the &lt;code&gt;hexcolor&lt;/code&gt; and the &lt;code&gt;point&lt;/code&gt; decoders here.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;polygon&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="nx"&gt;fill&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;hexcolor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="nx"&gt;stroke&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;optional&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hexcolor&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="nx"&gt;points&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;point&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can then reuse that complex definition in a list:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;polygons&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;polygon&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You get the point.  The final output type decoders of this type produce will
be:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nx"&gt;fill&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nx"&gt;stroke&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="ow"&gt;void&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nx"&gt;points&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Notice how the &lt;code&gt;fill&lt;/code&gt; and &lt;code&gt;stroke&lt;/code&gt; fields here end up as normal strings.
Statically, Flow only knows that they are going to be string values, but at
runtime, they will only contain hex color values that matched the regex.
(Decoders are therefore more expressive than the type system in describing what
values are allowed.)&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; It is not recommended to go fully overboard with this feature.
Decoders are best kept simple and straightforward, staying close to the
values they express, and not perform too much "magic" at runtime.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The best way to discover which ones are available, is to look at its &lt;a href="https://github.com/nvie/decoders/#api"&gt;reference
docs&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id="error-messages"&gt;Error messages &lt;a href="#error-messages" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Human readable and helpful error messages are considered important.  That’s why
decoders will always try to emit very readable error messages at runtime,
inlined right into the actual data.  An example of such a message would be:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;Decode error:
{
  &amp;quot;firstName&amp;quot;: &amp;quot;Vincent&amp;quot;,
  &amp;quot;age&amp;quot;: &amp;quot;37&amp;quot;,
         ^^^^
         Either:
         - Must be undefined
         - Must be number
}
^ Missing key &amp;quot;name&amp;quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is a complex error message, but optimized to be very readable to the human
eye when outputted to a console.&lt;/p&gt;
&lt;p&gt;The same error information can also be presented as a list of error messages
for outputting in API responses.  In this case, the input data isn't echoed
back as part of the error message:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;Value at key &amp;quot;age&amp;quot;: Either:\n- Must be undefined\n- Must be number&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;Missing key: &amp;quot;name&amp;quot;&amp;#39;&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;small&gt;(For those interested, this inline object annotation is performed by
&lt;a href="https://github.com/nvie/debrief.js"&gt;debrief.js&lt;/a&gt;.)&lt;/small&gt;&lt;/p&gt;
&lt;h2 id="decoders-vs-guards"&gt;Decoders vs Guards? &lt;a href="#decoders-vs-guards" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;When you have composed your decoder, it’s often useful to turn the outmost
decoder into a “guard”.  A “Guard” offers a slightly more convenient API, but
is very much like a decoder.  It’s also callable on unverified inputs, but it
will throw a runtime error if validation fails.  They are therefore typically
easier to work with: using the guard, you can focus on the happy path and
handle any validation errors in normal exception handling mechanism.&lt;/p&gt;
&lt;p&gt;Invoking the decoder directly on an input value will not throw a runtime error
and instead return a so called “decode result”.  A “decode result” is a value
that represents either an OK value or an Error, both of which you'll need to
“unpack” to do anything useful with it.&lt;/p&gt;
&lt;p&gt;For example, given this decoder definition:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;decoder&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;optional&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;verify&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;guard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;decoder&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;

&lt;span class="nx"&gt;decoder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;invalid data&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="c1"&gt;// Won&amp;#39;t throw&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="nx"&gt;verify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;invalid data&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="c1"&gt;// Throws&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you want to programmatically handle the decode result, you can use a decoder
directly and inspect the decode result.  If you're just interested in the data
and not in handling any decoding errors, use a guard.&lt;/p&gt;
&lt;p&gt;In terms of types:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Decoder&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;any&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;DecodeResult&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Guard&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;any&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;

&lt;span class="c1"&gt;// The guard() helper builds a guard for a decoder of the same type&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="nx"&gt;guard&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Decoder&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Guard&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;small&gt;(For those interested, the &lt;code&gt;DecodeResult&lt;/code&gt; type is powered by
&lt;a href="https://github.com/nvie/lemons.js#Result"&gt;lemons&lt;/a&gt;’ &lt;code&gt;Result&lt;/code&gt; type.)&lt;/small&gt;&lt;/p&gt;
&lt;h2 id="give-it-a-whirl"&gt;Give it a whirl! &lt;a href="#give-it-a-whirl" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Please try it out and let me know about your experiences.&lt;/p&gt;</content>
    <link href="https://nvie.com/posts/introducing-decoders/"/>
    <summary>Today, I’m thrilled to publicly announce a new open source project that we’ve been using at SimpleContacts in production for months:
[**decoders**](https://www.npmjs.com/package/decoders).</summary>
    <published>2018-09-25T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://nvie.com/posts/git-power-tools/</id>
    <title>Git power tools for daily use</title>
    <updated>2018-11-08T00:00:00+00:00</updated>
    <content type="html">&lt;p&gt;Every developer has their own favorite Git tricks they use daily.  Here are
some of my favorite ones I have been using for as long as I can remember.&lt;/p&gt;
&lt;p&gt;First of all, I should mention that most of these commands are bundled in my
&lt;a href="https://github.com/nvie/git-toolbelt#readme"&gt;git-toolbelt&lt;/a&gt; project.  If you like to use them, all you need to do
is install it like so:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;$ brew install nvie/tap/git-toolbelt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="quickly-opening-modified-files"&gt;Quickly opening modified files &lt;a href="#quickly-opening-modified-files" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;While working on a branch, I often find the need to re-open the files I was
working on.  The Git toolbelt project contains a command to show you all
locally modified files.  It will only report files that still exist locally, so
this overview won't include deleted files.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;$ git modified
controllers/foo.py
README.md
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is super useful to quickly open all locally modified files in your editor.
Definitely one of my most-used commands throughout the day:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;$ vim $(git modified)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;After quitting your editor, you can easily re-open the files you're working on
this way.&lt;/p&gt;
&lt;p&gt;To also include any files modified in the index (files that are &lt;code&gt;git add&lt;/code&gt;'ed
already), use the &lt;code&gt;-i&lt;/code&gt; flag:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;$ git modified -i
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can also pass it a commit SHA, which will open all files modified in that
commit:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;$ git modified HEAD~1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I have the following aliases &lt;a href="https://github.com/nvie/dotfiles/blob/ad229febfddc9aff47f0ea6a1539f936bf917042/.config/fish/aliases.fish#L163-L191"&gt;set up in my shell&lt;/a&gt;, for quickly opening
a specific set of files:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;vc&lt;/code&gt;: vim locally modified files (not indexed)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;vca&lt;/code&gt;: vim &lt;em&gt;all&lt;/em&gt; locally modified files (including the ones indexed)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;vch&lt;/code&gt;: vim files modified in the last commit (HEAD)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;vc HEAD~1&lt;/code&gt;: vim all files modified in the second-last commit&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="fixing-up-the-last-commit"&gt;Fixing up the last commit &lt;a href="#fixing-up-the-last-commit" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;You're probably familiar with &lt;code&gt;git commit --amend&lt;/code&gt; to incorporate the
currently-staged changes into the lastcommit, effectively rewriting the last
commit.  The toolbelt offers a similar command called &lt;code&gt;git fixup&lt;/code&gt;, which will
do the same, but without prompting for the commit message.  So it's like
a quicker version of &lt;code&gt;commit --amend&lt;/code&gt;.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;$ git fixup
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is a great way to build up a commit incrementally.  A very typical flow for me looks like:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;$ git add -p    # Pick bits to commit
$ git commit
$ git add -p    # Pick more bits
$ git fixup     # Add those to the last commit
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="emptying-the-last-commit"&gt;"Emptying" the last commit &lt;a href="#emptying-the-last-commit" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Sometimes I make a mistake and I accidentally commit too much, or something
I didn't intend to commit.  For example, an extra file I accidentally added, or
a patch within a file that I didn't want to include.  Here's how I fix that:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;$ git delouse
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This "empties" the last commit.  Think of "emptying" as keeping the commit
message and the author/date info, but "moving" all of its changes back into the
work tree.&lt;/p&gt;
&lt;p&gt;Technically:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Soft-resets the last commit, which means it will remove the last commit from
  the branch, and put back the contents of that commit into the work tree
  (basically reverting to the state right before committing).  File contents
  aren't changed by this, only the Git commit disappears.&lt;/li&gt;
&lt;li&gt;Add an empty commit with the same commit message and author details as the
  commit that was just removed.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;The net result of these actions are that it appears as if the last commit on
the branch is "emptied" back into your work tree.&lt;/strong&gt;   This command is
non-destructive, since all of your files remain untouched.  They are now just
local changes again.&lt;/p&gt;
&lt;p&gt;This allows re-adding all changes again.  Just use &lt;code&gt;git add -p&lt;/code&gt; to select the
bits to commit, and then &lt;code&gt;git fixup&lt;/code&gt; (see previous section) to keep changing
the last commit, effectively rebuilding it up from scratch.&lt;/p&gt;
&lt;p&gt;Because &lt;code&gt;git delouse&lt;/code&gt; kept the commit message and author information around in
that empty commit, the original commit info is never lost, and you don't have
to re-enter the commit message whenever you run &lt;code&gt;git fixup&lt;/code&gt;, which makes this
whole process super cheap.&lt;/p&gt;
&lt;p&gt;Typical flow:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;$ git commit -m &amp;#39;Add login screen&amp;#39;

# Oops! Checked in a secret key with that... let&amp;#39;s fix this mistake!
$ git delouse

# Retry adding stuff
$ git add -p   # This time, don&amp;#39;t add the secret key
$ git fixup    # Rewrites the previous commit
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And if you make a mistake, you can just run &lt;code&gt;git delouse&lt;/code&gt; again and start over,
as often as you want.  Since none of these commands destroy your local changes,
this allows you to carefully craft your commit contents without the risk of
losing any data.&lt;/p&gt;
&lt;h2 id="splitting-up-a-commit-into-pieces"&gt;Splitting up a commit into pieces &lt;a href="#splitting-up-a-commit-into-pieces" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;It's also a great way to split up a commit!  For example, suppose you are
adding a bugfix but you also renamed a variable to have clearer meaning.  When
submitting the code for review, you realize that the variable rename adds a lot
of noise to the actual change.  You may then decide it's a good idea to split
up this commit into two pieces: one that atomically just changes the variable
name everywhere, and one that fixes the bug.  You can then point to the bugfix
commit when asking for a code review.&lt;/p&gt;
&lt;p&gt;How would that work?&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;$ git commit -m &amp;#39;Bugfix for login screen&amp;#39;

# Oops, I should&amp;#39;ve split this one up. Let&amp;#39;s start over!
$ git delouse
$ git add -p     # Just pick the bugfix bits
$ git fixup
$ git add -p     # Now pick the var rename bits
$ git commit -m &amp;#39;Rename variable name to be clearer&amp;#39;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;These three commands have become indispensable in my day-to-day Git routine.
If you like it, let me know!&lt;/p&gt;</content>
    <link href="https://nvie.com/posts/git-power-tools/"/>
    <summary>Every developer has their own favorite Git tricks they use daily.  Here are
some of my favorite ones I have been using for as long as I can remember.</summary>
    <published>2018-11-08T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://nvie.com/posts/why-every-on-an-empty-list-is-true/</id>
    <title>Why .every() on an empty list is true</title>
    <updated>2021-12-06T00:00:00+00:00</updated>
    <content type="html">&lt;p&gt;We can all pretty intuitively understand how &lt;code&gt;.some()&lt;/code&gt; and &lt;code&gt;.every()&lt;/code&gt; predicate
expressions on lists work.&lt;/p&gt;
&lt;p&gt;Let's define a list of Simpsons:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;simpsons&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;Homer&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;39&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;Marge&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;37&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;Bart&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;13&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;Lisa&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;11&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;Maggie&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;4&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And a predicate to tell if someone is an adult:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;isAdult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;simpson&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;simpson&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;18&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then, these statements are pretty obvious:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nx"&gt;simpsons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isAdult&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="c1"&gt;// =&amp;gt; true, because Homer and Marge are adults&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="nx"&gt;simpsons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;every&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isAdult&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="c1"&gt;// =&amp;gt; false, because not everyone is an adult&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;But it's not immediately obvious why these are the defaults for an empty list:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="p"&gt;[].&lt;/span&gt;&lt;span class="nx"&gt;some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isAdult&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="c1"&gt;// =&amp;gt; false&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="p"&gt;[].&lt;/span&gt;&lt;span class="nx"&gt;every&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isAdult&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="c1"&gt;// =&amp;gt; true&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Every person in the empty list is an adult? That sounds weird when you say it.&lt;/p&gt;
&lt;p&gt;It doesn't matter what predicate you pass in here though, it will not affect
the result. In other words, every person in the empty list is also &lt;em&gt;&lt;em&gt;NOT&lt;/em&gt;&lt;/em&gt; an
adult 😉&lt;/p&gt;
&lt;p&gt;One intuition that can help with this, is to think of &lt;code&gt;.some()&lt;/code&gt; and &lt;code&gt;.every()&lt;/code&gt;
as chains of logical "or" and "and" expressions:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="c1"&gt;// .some() is a chain of &amp;quot;or&amp;quot;s&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="nx"&gt;isAdult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;homer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;||&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;isAdult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;marge&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;||&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;isAdult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bart&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;||&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;

&lt;span class="c1"&gt;// .every() is a chain of &amp;quot;and&amp;quot;s&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="nx"&gt;isAdult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;homer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;isAdult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;marge&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;isAdult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bart&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can tack a constant to the beginning of these chains in a way that keeps
them logically equivalent:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;||&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;isAdult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;homer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;||&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;isAdult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;marge&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;||&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;isAdult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bart&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;||&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;isAdult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;homer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;isAdult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;marge&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;isAdult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bart&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And that's why those are the defaults for the empty list!&lt;/p&gt;</content>
    <link href="https://nvie.com/posts/why-every-on-an-empty-list-is-true/"/>
    <summary>We can all pretty intuitively understand how `.some()` and `.every()` predicate
expressions on lists work.</summary>
    <published>2021-12-06T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://nvie.com/posts/a-successful-git-branching-model/</id>
    <title>A successful Git branching model</title>
    <updated>2010-01-05T00:00:00+00:00</updated>
    <content type="html">&lt;blockquote&gt;
&lt;h3 id="note-of-reflection-march-5-2020"&gt;Note of reflection &lt;small style="color: #666; font-weight: 300"&gt;&amp;middot; March 5, 2020&lt;/small&gt; &lt;a href="#note-of-reflection-march-5-2020" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;This model was conceived in 2010, now more than 10 years ago, and not very
long after Git itself came into being. In those 10 years, git-flow (the
branching model laid out in this article) has become hugely popular in many
a software team to the point where people have started treating it like
a standard of sorts — but unfortunately also as a dogma or panacea.&lt;/p&gt;
&lt;p&gt;During those 10 years, Git itself has taken the world by a storm, and the
most popular type of software that is being developed with Git is shifting
more towards web apps — at least in my filter bubble. Web apps are typically
continuously delivered, not rolled back, and you don't have to support
multiple versions of the software running in the wild.&lt;/p&gt;
&lt;p&gt;This is not the class of software that I had in mind when I wrote the blog
post 10 years ago. If your team is doing continuous delivery of software,
I would suggest to adopt a much simpler workflow (like &lt;a href="https://guides.github.com/introduction/flow/"&gt;GitHub
flow&lt;/a&gt;) instead of trying to
shoehorn git-flow into your team.&lt;/p&gt;
&lt;p&gt;If, however, you are building software that is explicitly versioned, or if
you need to support multiple versions of your software in the wild, then
git-flow may still be as good of a fit to your team as it has been to people
in the last 10 years. In that case, please read on.&lt;/p&gt;
&lt;p&gt;To conclude, always remember that panaceas don't exist. Consider your own
context. Don't be hating. Decide for yourself.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In this post I present the development model that I’ve introduced for some of
my projects (both at work and private) about a year ago, and which has turned
out to be very successful. I’ve been meaning to write about it for a while now,
but I’ve never really found the time to do so thoroughly, until now. I won’t
talk about any of the projects’ details, merely about the branching strategy
and release management.&lt;/p&gt;
&lt;figure class="centered" id="diagram"&gt;
  &lt;img src="/img/git-model@2x.png" width="575"&gt;
  &lt;figcaption&gt;Licensed under &lt;a class="secondary" href="https://creativecommons.org/licenses/by-sa/4.0/"&gt;Creative Commons BY-SA&lt;/a&gt;&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;h2 id="why-git"&gt;Why git? &lt;a href="#why-git" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;For a thorough discussion on the pros and cons of Git compared to centralized
source code control systems, see the
&lt;a href="http://git.or.cz/gitwiki/GitSvnComparsion"&gt;web&lt;/a&gt;. There are plenty of flame
wars going on there. As a developer, I prefer Git above all other tools around
today. Git really changed the way developers think of merging and branching.
From the classic CVS/Subversion world I came from, merging/branching has always
been considered a bit scary (“beware of merge conflicts, they bite you!”) and
something you only do every once in a while.&lt;/p&gt;
&lt;p&gt;But with Git, these actions are extremely cheap and simple, and they are
considered one of the core parts of your &lt;em&gt;daily&lt;/em&gt; workflow, really. For example,
in CVS/Subversion &lt;a href="http://svnbook.red-bean.com"&gt;books&lt;/a&gt;, branching and merging
is first discussed in the later chapters (for advanced users), while in
&lt;a href="http://book.git-scm.com"&gt;every&lt;/a&gt;
&lt;a href="http://pragprog.com/titles/tsgit/pragmatic-version-control-using-git"&gt;Git&lt;/a&gt;
&lt;a href="http://github.com/progit/progit"&gt;book&lt;/a&gt;, it’s already covered in chapter
3 (basics).&lt;/p&gt;
&lt;p&gt;As a consequence of its simplicity and repetitive nature, branching and merging
are no longer something to be afraid of. Version control tools are supposed to
assist in branching/merging more than anything else.&lt;/p&gt;
&lt;p&gt;Enough about the tools, let’s head onto the development model. The model that
I’m going to present here is essentially no more than a set of procedures that
every team member has to follow in order to come to a managed software
development process.&lt;/p&gt;
&lt;h2 id="decentralized-but-centralized"&gt;Decentralized but centralized &lt;a href="#decentralized-but-centralized" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The repository setup that we use and that works well with this branching model,
is that with a central “truth” repo. Note that this repo is only &lt;em&gt;considered&lt;/em&gt;
to be the central one (since Git is a DVCS, there is no such thing as a central
repo at a technical level). We will refer to this repo as &lt;code&gt;origin&lt;/code&gt;, since this
name is familiar to all Git users.&lt;/p&gt;
&lt;p class="centered"&gt;&lt;img alt="" src="/img/centr-decentr@2x.png" width="487" /&gt;&lt;/p&gt;
&lt;p&gt;Each developer pulls and pushes to origin. But besides the centralized
push-pull relationships, each developer may also pull changes from other peers
to form sub teams. For example, this might be useful to work together with two
or more developers on a big new feature, before pushing the work in progress to
&lt;code&gt;origin&lt;/code&gt; prematurely. In the figure above, there are subteams of Alice and Bob,
Alice and David, and Clair and David.&lt;/p&gt;
&lt;p&gt;Technically, this means nothing more than that Alice has defined a Git remote,
named &lt;code&gt;bob&lt;/code&gt;, pointing to Bob’s repository, and vice versa.&lt;/p&gt;
&lt;h2 id="the-main-branches"&gt;The main branches &lt;a href="#the-main-branches" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;img alt="" class="right" src="/img/main-branches@2x.png" width="267" /&gt;&lt;/p&gt;
&lt;p&gt;At the core, the development model is greatly inspired by existing models out
there. The central repo holds two main branches with an infinite lifetime:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;master&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;develop&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The &lt;code&gt;master&lt;/code&gt; branch at &lt;code&gt;origin&lt;/code&gt; should be familiar to every Git user. Parallel
to the &lt;code&gt;master&lt;/code&gt; branch, another branch exists called &lt;code&gt;develop&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;We consider &lt;code&gt;origin/master&lt;/code&gt; to be the main branch where the source code of
&lt;code&gt;HEAD&lt;/code&gt; always reflects a &lt;em&gt;production-ready&lt;/em&gt; state.&lt;/p&gt;
&lt;p&gt;We consider &lt;code&gt;origin/develop&lt;/code&gt; to be the main branch where the source code of
&lt;code&gt;HEAD&lt;/code&gt; always reflects a state with the latest delivered development changes
for the next release. Some would call this the “integration branch”. This is
where any automatic nightly builds are built from.&lt;/p&gt;
&lt;p&gt;When the source code in the &lt;code&gt;develop&lt;/code&gt; branch reaches a stable point and is
ready to be released, all of the changes should be merged back into &lt;code&gt;master&lt;/code&gt;
somehow and then tagged with a release number. How this is done in detail will
be discussed further on.&lt;/p&gt;
&lt;p&gt;Therefore, each time when changes are merged back into &lt;code&gt;master&lt;/code&gt;, this is a new
production release &lt;em&gt;by definition&lt;/em&gt;. We tend to be very strict at this, so that
theoretically, we could use a Git hook script to automatically build and
roll-out our software to our production servers everytime there was a commit on
&lt;code&gt;master&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id="supporting-branches"&gt;Supporting branches &lt;a href="#supporting-branches" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Next to the main branches &lt;code&gt;master&lt;/code&gt; and &lt;code&gt;develop&lt;/code&gt;, our development model uses
a variety of supporting branches to aid parallel development between team
members, ease tracking of features, prepare for production releases and to
assist in quickly fixing live production problems. Unlike the main branches,
these branches always have a limited life time, since they will be removed
eventually.&lt;/p&gt;
&lt;p&gt;The different types of branches we may use are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Feature branches&lt;/li&gt;
&lt;li&gt;Release branches&lt;/li&gt;
&lt;li&gt;Hotfix branches&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Each of these branches have a specific purpose and are bound to strict rules as
to which branches may be their originating branch and which branches must be
their merge targets. We will walk through them in a minute.&lt;/p&gt;
&lt;p&gt;By no means are these branches “special” from a technical perspective. The
branch types are categorized by how we &lt;em&gt;use&lt;/em&gt; them. They are of course plain old
Git branches.&lt;/p&gt;
&lt;h3 id="feature-branches"&gt;Feature branches &lt;a href="#feature-branches" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;img alt="" class="right" src="/img/fb@2x.png" width="133" /&gt;&lt;/p&gt;
&lt;dl&gt;
&lt;dt&gt;May branch off from:&lt;/dt&gt;
&lt;dd&gt;&lt;code&gt;develop&lt;/code&gt;&lt;/dd&gt;
&lt;dt&gt;Must merge back into:&lt;/dt&gt;
&lt;dd&gt;&lt;code&gt;develop&lt;/code&gt;&lt;/dd&gt;
&lt;dt&gt;Branch naming convention:&lt;/dt&gt;
&lt;dd&gt;anything except &lt;code&gt;master&lt;/code&gt;, &lt;code&gt;develop&lt;/code&gt;, &lt;code&gt;release-*&lt;/code&gt;, or &lt;code&gt;hotfix-*&lt;/code&gt;&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;Feature branches (or sometimes called topic branches) are used to develop new
features for the upcoming or a distant future release. When starting
development of a feature, the target release in which this feature will be
incorporated may well be unknown at that point. The essence of a feature branch
is that it exists as long as the feature is in development, but will eventually
be merged back into &lt;code&gt;develop&lt;/code&gt; (to definitely add the new feature to the
upcoming release) or discarded (in case of a disappointing experiment).&lt;/p&gt;
&lt;p&gt;Feature branches typically exist in developer repos only, not in &lt;code&gt;origin&lt;/code&gt;.&lt;/p&gt;
&lt;h4 id="creating-a-feature-branch"&gt;Creating a feature branch &lt;a href="#creating-a-feature-branch" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h4&gt;
&lt;p&gt;When starting work on a new feature, branch off from the &lt;code&gt;develop&lt;/code&gt; branch.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;git checkout -b myfeature develop
&lt;span class="go"&gt;Switched to a new branch &amp;quot;myfeature&amp;quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h4 id="incorporating-a-finished-feature-on-develop"&gt;Incorporating a finished feature on develop &lt;a href="#incorporating-a-finished-feature-on-develop" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h4&gt;
&lt;p&gt;Finished features may be merged into the &lt;code&gt;develop&lt;/code&gt; branch to definitely add
them to the upcoming release:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;git checkout develop
&lt;span class="go"&gt;Switched to branch &amp;#39;develop&amp;#39;&lt;/span&gt;
&lt;span class="gp"&gt;$ &lt;/span&gt;git merge --no-ff myfeature
&lt;span class="go"&gt;Updating ea1b82a..05e9557&lt;/span&gt;
&lt;span class="gp gp-VirtualEnv"&gt;(Summary of changes)&lt;/span&gt;
&lt;span class="gp"&gt;$ &lt;/span&gt;git branch -d myfeature
&lt;span class="go"&gt;Deleted branch myfeature (was 05e9557).&lt;/span&gt;
&lt;span class="gp"&gt;$ &lt;/span&gt;git push origin develop
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;--no-ff&lt;/code&gt; flag causes the merge to always create a new commit object, even
if the merge could be performed with a fast-forward. This avoids losing
information about the historical existence of a feature branch and groups
together all commits that together added the feature. Compare:&lt;/p&gt;
&lt;p class="centered"&gt;&lt;img alt="" src="/img/merge-without-ff@2x.png" width="478" /&gt;&lt;/p&gt;
&lt;p&gt;In the latter case, it is impossible to see from the Git history which of the
commit objects together have implemented a feature—you would have to manually
read all the log messages. Reverting a whole feature (i.e. a group of commits),
is a true headache in the latter situation, whereas it is easily done if the
&lt;code&gt;--no-ff&lt;/code&gt; flag was used.&lt;/p&gt;
&lt;p&gt;Yes, it will create a few more (empty) commit objects, but the gain is much
bigger than the cost.&lt;/p&gt;
&lt;h3 id="release-branches"&gt;Release branches &lt;a href="#release-branches" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h3&gt;
&lt;dl&gt;
&lt;dt&gt;May branch off from:&lt;/dt&gt;
&lt;dd&gt;&lt;code&gt;develop&lt;/code&gt;&lt;/dd&gt;
&lt;dt&gt;Must merge back into:&lt;/dt&gt;
&lt;dd&gt;&lt;code&gt;develop&lt;/code&gt; and &lt;code&gt;master&lt;/code&gt;&lt;/dd&gt;
&lt;dt&gt;Branch naming convention:&lt;/dt&gt;
&lt;dd&gt;&lt;code&gt;release-*&lt;/code&gt;&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;Release branches support preparation of a new production release. They allow
for last-minute dotting of i’s and crossing t’s. Furthermore, they allow for
minor bug fixes and preparing meta-data for a release (version number, build
dates, etc.). By doing all of this work on a release branch, the &lt;code&gt;develop&lt;/code&gt;
branch is cleared to receive features for the next big release.&lt;/p&gt;
&lt;p&gt;The key moment to branch off a new release branch from &lt;code&gt;develop&lt;/code&gt; is when
develop (almost) reflects the desired state of the new release. At least all
features that are targeted for the release-to-be-built must be merged in to
&lt;code&gt;develop&lt;/code&gt; at this point in time. All features targeted at future releases may
not—they must wait until after the release branch is branched off.&lt;/p&gt;
&lt;p&gt;It is exactly at the start of a release branch that the upcoming release gets
assigned a version number—not any earlier. Up until that moment, the &lt;code&gt;develop&lt;/code&gt;
branch reflected changes for the “next release”, but it is unclear whether that
“next release” will eventually become 0.3 or 1.0, until the release branch is
started. That decision is made on the start of the release branch and is
carried out by the project’s rules on version number bumping.&lt;/p&gt;
&lt;h4 id="creating-a-release-branch"&gt;Creating a release branch &lt;a href="#creating-a-release-branch" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h4&gt;
&lt;p&gt;Release branches are created from the &lt;code&gt;develop&lt;/code&gt; branch. For example, say
version 1.1.5 is the current production release and we have a big release
coming up. The state of &lt;code&gt;develop&lt;/code&gt; is ready for the “next release” and we have
decided that this will become version 1.2 (rather than 1.1.6 or 2.0). So we
branch off and give the release branch a name reflecting the new version
number:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;git checkout -b release-1.2 develop
&lt;span class="go"&gt;Switched to a new branch &amp;quot;release-1.2&amp;quot;&lt;/span&gt;
&lt;span class="gp"&gt;$ &lt;/span&gt;./bump-version.sh &lt;span class="m"&gt;1&lt;/span&gt;.2
&lt;span class="go"&gt;Files modified successfully, version bumped to 1.2.&lt;/span&gt;
&lt;span class="gp"&gt;$ &lt;/span&gt;git commit -a -m &lt;span class="s2"&gt;&amp;quot;Bumped version number to 1.2&amp;quot;&lt;/span&gt;
&lt;span class="go"&gt;[release-1.2 74d9424] Bumped version number to 1.2&lt;/span&gt;
&lt;span class="go"&gt;1 files changed, 1 insertions(+), 1 deletions(-)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;After creating a new branch and switching to it, we bump the version number.
Here, &lt;code&gt;bump-version.sh&lt;/code&gt; is a fictional shell script that changes some files in
the working copy to reflect the new version. (This can of course be a manual
change—the point being that &lt;em&gt;some&lt;/em&gt; files change.) Then, the bumped version
number is committed.&lt;/p&gt;
&lt;p&gt;This new branch may exist there for a while, until the release may be rolled
out definitely. During that time, bug fixes may be applied in this branch
(rather than on the &lt;code&gt;develop&lt;/code&gt; branch). Adding large new features here is
strictly prohibited. They must be merged into &lt;code&gt;develop&lt;/code&gt;, and therefore, wait
for the next big release.&lt;/p&gt;
&lt;h4 id="finishing-a-release-branch"&gt;Finishing a release branch &lt;a href="#finishing-a-release-branch" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h4&gt;
&lt;p&gt;When the state of the release branch is ready to become a real release, some
actions need to be carried out. First, the release branch is merged into
&lt;code&gt;master&lt;/code&gt; (since every commit on &lt;code&gt;master&lt;/code&gt; is a new release &lt;em&gt;by definition&lt;/em&gt;,
remember). Next, that commit on &lt;code&gt;master&lt;/code&gt; must be tagged for easy future
reference to this historical version. Finally, the changes made on the release
branch need to be merged back into &lt;code&gt;develop&lt;/code&gt;, so that future releases also
contain these bug fixes.&lt;/p&gt;
&lt;p&gt;The first two steps in Git:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;git checkout master
&lt;span class="go"&gt;Switched to branch &amp;#39;master&amp;#39;&lt;/span&gt;
&lt;span class="gp"&gt;$ &lt;/span&gt;git merge --no-ff release-1.2
&lt;span class="go"&gt;Merge made by recursive.&lt;/span&gt;
&lt;span class="gp gp-VirtualEnv"&gt;(Summary of changes)&lt;/span&gt;
&lt;span class="gp"&gt;$ &lt;/span&gt;git tag -a &lt;span class="m"&gt;1&lt;/span&gt;.2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The release is now done, and tagged for future reference.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Edit:&lt;/strong&gt; You might as well want to use the &lt;code&gt;-s&lt;/code&gt; or &lt;code&gt;-u &amp;lt;key&amp;gt;&lt;/code&gt; flags to sign
your tag cryptographically.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;To keep the changes made in the release branch, we need to merge those back
into &lt;code&gt;develop&lt;/code&gt;, though. In Git:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;git checkout develop
&lt;span class="go"&gt;Switched to branch &amp;#39;develop&amp;#39;&lt;/span&gt;
&lt;span class="gp"&gt;$ &lt;/span&gt;git merge --no-ff release-1.2
&lt;span class="go"&gt;Merge made by recursive.&lt;/span&gt;
&lt;span class="gp gp-VirtualEnv"&gt;(Summary of changes)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This step may well lead to a merge conflict (probably even, since we have
changed the version number). If so, fix it and commit.&lt;/p&gt;
&lt;p&gt;Now we are really done and the release branch may be removed, since we don’t
need it anymore:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;git branch -d release-1.2
&lt;span class="go"&gt;Deleted branch release-1.2 (was ff452fe).&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id="hotfix-branches"&gt;Hotfix branches &lt;a href="#hotfix-branches" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;img alt="" class="right" src="/img/hotfix-branches@2x.png" width="316" /&gt;&lt;/p&gt;
&lt;dl&gt;
&lt;dt&gt;May branch off from:&lt;/dt&gt;
&lt;dd&gt;&lt;code&gt;master&lt;/code&gt;&lt;/dd&gt;
&lt;dt&gt;Must merge back into:&lt;/dt&gt;
&lt;dd&gt;&lt;code&gt;develop&lt;/code&gt; and &lt;code&gt;master&lt;/code&gt;&lt;/dd&gt;
&lt;dt&gt;Branch naming convention:&lt;/dt&gt;
&lt;dd&gt;&lt;code&gt;hotfix-*&lt;/code&gt;&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;Hotfix branches are very much like release branches in that they are also meant
to prepare for a new production release, albeit unplanned. They arise from the
necessity to act immediately upon an undesired state of a live production
version. When a critical bug in a production version must be resolved
immediately, a hotfix branch may be branched off from the corresponding tag on
the master branch that marks the production version.&lt;/p&gt;
&lt;p&gt;The essence is that work of team members (on the &lt;code&gt;develop&lt;/code&gt; branch) can
continue, while another person is preparing a quick production fix.&lt;/p&gt;
&lt;h4 id="creating-the-hotfix-branch"&gt;Creating the hotfix branch &lt;a href="#creating-the-hotfix-branch" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h4&gt;
&lt;p&gt;Hotfix branches are created from the &lt;code&gt;master&lt;/code&gt; branch. For example, say version
1.2 is the current production release running live and causing troubles due to
a severe bug. But changes on &lt;code&gt;develop&lt;/code&gt; are yet unstable. We may then branch off
a hotfix branch and start fixing the problem:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;git checkout -b hotfix-1.2.1 master
&lt;span class="go"&gt;Switched to a new branch &amp;quot;hotfix-1.2.1&amp;quot;&lt;/span&gt;
&lt;span class="gp"&gt;$ &lt;/span&gt;./bump-version.sh &lt;span class="m"&gt;1&lt;/span&gt;.2.1
&lt;span class="go"&gt;Files modified successfully, version bumped to 1.2.1.&lt;/span&gt;
&lt;span class="gp"&gt;$ &lt;/span&gt;git commit -a -m &lt;span class="s2"&gt;&amp;quot;Bumped version number to 1.2.1&amp;quot;&lt;/span&gt;
&lt;span class="go"&gt;[hotfix-1.2.1 41e61bb] Bumped version number to 1.2.1&lt;/span&gt;
&lt;span class="go"&gt;1 files changed, 1 insertions(+), 1 deletions(-)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Don’t forget to bump the version number after branching off!&lt;/p&gt;
&lt;p&gt;Then, fix the bug and commit the fix in one or more separate commits.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;git commit -m &lt;span class="s2"&gt;&amp;quot;Fixed severe production problem&amp;quot;&lt;/span&gt;
&lt;span class="go"&gt;[hotfix-1.2.1 abbe5d6] Fixed severe production problem&lt;/span&gt;
&lt;span class="go"&gt;5 files changed, 32 insertions(+), 17 deletions(-)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h4 id="finishing-a-hotfix-branch"&gt;Finishing a hotfix branch &lt;a href="#finishing-a-hotfix-branch" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h4&gt;
&lt;p&gt;When finished, the bugfix needs to be merged back into &lt;code&gt;master&lt;/code&gt;, but also needs
to be merged back into &lt;code&gt;develop&lt;/code&gt;, in order to safeguard that the bugfix is
included in the next release as well. This is completely similar to how release
branches are finished.&lt;/p&gt;
&lt;p&gt;First, update &lt;code&gt;master&lt;/code&gt; and tag the release.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;git checkout master
&lt;span class="go"&gt;Switched to branch &amp;#39;master&amp;#39;&lt;/span&gt;
&lt;span class="gp"&gt;$ &lt;/span&gt;git merge --no-ff hotfix-1.2.1
&lt;span class="go"&gt;Merge made by recursive.&lt;/span&gt;
&lt;span class="gp gp-VirtualEnv"&gt;(Summary of changes)&lt;/span&gt;
&lt;span class="gp"&gt;$ &lt;/span&gt;git tag -a &lt;span class="m"&gt;1&lt;/span&gt;.2.1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Edit:&lt;/strong&gt; You might as well want to use the &lt;code&gt;-s&lt;/code&gt; or &lt;code&gt;-u &amp;lt;key&amp;gt;&lt;/code&gt; flags to sign
your tag cryptographically.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Next, include the bugfix in &lt;code&gt;develop&lt;/code&gt;, too:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;git checkout develop
&lt;span class="go"&gt;Switched to branch &amp;#39;develop&amp;#39;&lt;/span&gt;
&lt;span class="gp"&gt;$ &lt;/span&gt;git merge --no-ff hotfix-1.2.1
&lt;span class="go"&gt;Merge made by recursive.&lt;/span&gt;
&lt;span class="gp gp-VirtualEnv"&gt;(Summary of changes)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The one exception to the rule here is that, &lt;strong&gt;when a release branch currently
exists, the hotfix changes need to be merged into that release branch, instead
of &lt;code&gt;develop&lt;/code&gt;&lt;/strong&gt;. Back-merging the bugfix into the release branch will eventually
result in the bugfix being merged into &lt;code&gt;develop&lt;/code&gt; too, when the release branch
is finished. (If work in &lt;code&gt;develop&lt;/code&gt; immediately requires this bugfix and cannot
wait for the release branch to be finished, you may safely merge the bugfix
into &lt;code&gt;develop&lt;/code&gt; now already as well.)&lt;/p&gt;
&lt;p&gt;Finally, remove the temporary branch:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;git branch -d hotfix-1.2.1
&lt;span class="go"&gt;Deleted branch hotfix-1.2.1 (was abbe5d6).&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="summary"&gt;Summary &lt;a href="#summary" rel="bookmark" class="permalink"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;While there is nothing really shocking new to this branching model, the “big
picture” figure that this post began with has turned out to be tremendously
useful in our projects. It forms an elegant mental model that is easy to
comprehend and allows team members to develop a shared understanding of the
branching and releasing processes.&lt;/p&gt;
&lt;p&gt;A high-quality PDF version of the figure is provided here. Go ahead and hang it
on the wall for quick reference at any time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; And for anyone who requested it: here’s the
&lt;a href="https://www.dropbox.com/s/277ymr7n4f1fjhb/gitflow-model.src.key"&gt;gitflow-model.src.key&lt;/a&gt; of the main diagram image (Apple Keynote).&lt;/p&gt;
&lt;p class="centered"&gt;&lt;a href="/files/Git-branching-model.pdf"&gt;&lt;img alt="" src="/img/pdf@2x.png" width="128" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;a href="/files/Git-branching-model.pdf"&gt;Git-branching-model.pdf&lt;/a&gt;&lt;/p&gt;</content>
    <link href="https://nvie.com/posts/a-successful-git-branching-model/"/>
    <summary>In this post I present a Git branching strategy for developing and releasing version-based software.</summary>
    <published>2010-01-05T00:00:00+00:00</published>
  </entry>
  <entry>
    <id>https://nvie.com/posts/15-years-later/</id>
    <title>15+ years later, Microsoft morged my diagram</title>
    <updated>2026-02-18T00:00:00+00:00</updated>
    <content type="html">&lt;p&gt;A few days ago, people started tagging me on Bluesky and Hacker News about
a diagram on Microsoft's Learn portal. It looked... familiar.&lt;/p&gt;
&lt;p&gt;In 2010, I wrote &lt;a href="/posts/a-successful-git-branching-model/"&gt;A successful Git branching
model&lt;/a&gt; and created
a diagram to go with it. I designed that diagram in Apple Keynote, at the time
obsessing over the colors, the curves, and the layout until it clearly
communicated how branches relate to each other over time. I also published the
source file so others could build on it. That diagram has since spread
everywhere: in books, talks, blog posts, team wikis, and YouTube videos.
I never minded. That was the whole point: sharing knowledge and letting the
internet take it by storm!&lt;/p&gt;
&lt;p&gt;What I did not expect was for Microsoft, a trillion-dollar company, some 15+
years later, to apparently run it through an AI image generator and publish the
result on their official Learn portal, without any credit or link back to the
original.&lt;/p&gt;
&lt;p class="right"&gt;&lt;img alt="Close-up of the &amp;quot;continvoucly morged&amp;quot; text" height="300" src="/img/microsoft-continvoucly-morged-gitflow.png" /&gt;&lt;/p&gt;
&lt;p&gt;The AI rip-off was not just ugly. It was careless, blatantly amateuristic, and
lacking any ambition, to put it gently. Microsoft unworthy. The carefully
crafted visual language and layout of the original, the branch colors, the lane
design, the dot and bubble alignment that made the original so readable—all of
it had been muddled into a laughable form. Proper AI slop.&lt;/p&gt;
&lt;p&gt;Arrows missing and pointing in the wrong direction, and the obvious
"continvoucly morged" text quickly gave it away as a cheap AI artifact.&lt;/p&gt;
&lt;p&gt;It had the rough &lt;em&gt;shape&lt;/em&gt; of my diagram though. Enough actually so that people
recognized the original in it and started calling Microsoft out on it and
reaching out to me. That so many people were upset about this was really nice,
honestly. That, and "continvoucly morged" was a very fun meme—thank you,
internet! 😄&lt;/p&gt;
&lt;blockquote class="bluesky-embed" data-bluesky-uri="at://did:plc:n2k2qg5jevfifcmfinbsbt6e/app.bsky.feed.post/3meywr75ttc2v" data-bluesky-cid="bafyreid7zep5jusxiisa43sobwcycojd6i4x4yuxdyircbbmjv456plnri"&gt;&lt;p lang="nl"&gt;Oh god yes, Microsoft continvoucly morged my diagram there for sure 😬&lt;/p&gt;&amp;mdash; &lt;a href="https://bsky.app/profile/did:plc:n2k2qg5jevfifcmfinbsbt6e?ref_src=embed"&gt;Vincent Driessen (@nvie.com)&lt;/a&gt; &lt;a href="https://bsky.app/profile/did:plc:n2k2qg5jevfifcmfinbsbt6e/post/3meywr75ttc2v?ref_src=embed"&gt;2026-02-16T20:55:54.762Z&lt;/a&gt;&lt;/blockquote&gt;
&lt;script async src="https://embed.bsky.app/static/embed.js" charset="utf-8"&gt;&lt;/script&gt;

&lt;p&gt;Other than that, I find this whole thing mostly very saddening. Not because
some company used my diagram. As I said, it's been everywhere for 15 years and
I've always been fine with that. What's dispiriting is the (lack of) &lt;em&gt;process&lt;/em&gt;
and &lt;em&gt;care&lt;/em&gt;: take someone's carefully crafted work, run it through a machine to
wash off the fingerprints, and ship it as your own. This isn't a case of being
inspired by something and building on it. It's the opposite of that. It's
taking something that worked and making it worse. Is there even a goal here
beyond "generating content"?&lt;/p&gt;
&lt;p&gt;What's slightly worrying me is that this time around, the diagram was both
well-known enough and obviously AI-slop-y enough that it was easy to spot as
plagiarism. But we all know there will just be more and more content like this
that isn't so well-known or soon will get mutated or disguised in more advanced
ways that this plagiarism no longer will be recognizable as such.&lt;/p&gt;
&lt;p&gt;I don't need much here. A simple link back and attribution to the original
article would be a good start. I would also be interested in understanding how
this Learn page at Microsoft came to be, what the goals were here, and what the
process has been that led to the creation of this ugly asset, and how there
seemingly has not been any form of proof-reading for a document used as
a learning resource by many developers.&lt;/p&gt;
&lt;p&gt;Till next 'tim'.&lt;/p&gt;</content>
    <link href="https://nvie.com/posts/15-years-later/"/>
    <summary>How Microsoft continvoucly morged my Git branching diagram.</summary>
    <published>2026-02-18T00:00:00+00:00</published>
  </entry>
</feed>
