tag:blogger.com,1999:blog-54480194108730280212024-03-13T06:09:45.733-07:00Python and Web Development TutorJeffyehttp://www.blogger.com/profile/07633630042179070371noreply@blogger.comBlogger145125tag:blogger.com,1999:blog-5448019410873028021.post-46877190075915146422021-02-15T08:53:00.006-08:002021-02-15T08:53:43.646-08:00python Getting the class name of an instance?<p> <span style="background-color: white; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px;">How do I find out a name of class that created an instance of an object in Python if the function I am doing this from is the base class of which the class of the instance has been derived?</span></p><p><span style="color: #242729; font-family: Arial, Helvetica Neue, Helvetica, sans-serif;"><span style="background-color: white; font-size: 15px;"><b>solution 1:</b></span></span></p><p><code style="border-radius: 3px; border: 0px; box-sizing: inherit; color: #242729; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 2px 4px; vertical-align: baseline; white-space: pre-wrap;">type(x).__name__</code><span style="background-color: white; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px;"> will give you the name of the class, which I think is what you want.</span></p><pre class="lang-py s-code-block hljs python" style="border-radius: 5px; border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.30769; margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px; vertical-align: baseline; width: auto;"><code style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span class="hljs-meta" style="border: 0px; box-sizing: inherit; color: var(--highlight-keyword); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">>>> </span><span class="hljs-keyword" style="border: 0px; box-sizing: inherit; color: var(--highlight-keyword); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">import</span> itertools
<span class="hljs-meta" style="border: 0px; box-sizing: inherit; color: var(--highlight-keyword); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">>>> </span>x = itertools.count(<span class="hljs-number" style="border: 0px; box-sizing: inherit; color: var(--highlight-namespace); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">0</span>)
<span class="hljs-meta" style="border: 0px; box-sizing: inherit; color: var(--highlight-keyword); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">>>> </span><span class="hljs-built_in" style="border: 0px; box-sizing: inherit; color: var(--highlight-literal); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">type</span>(x).__name__
<span class="hljs-string" style="border: 0px; box-sizing: inherit; color: var(--highlight-variable); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">'count'</span></code></pre><p><span style="color: #242729; font-family: Arial, Helvetica Neue, Helvetica, sans-serif;"><span style="background-color: white; font-size: 15px;"><b>solution 2:</b></span></span></p><pre class="lang-py s-code-block hljs python" style="border-radius: 5px; border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.30769; margin-bottom: 0px; margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px; vertical-align: baseline; width: auto;"><code style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;">x.__class__.__name__</code></pre>Jeffyehttp://www.blogger.com/profile/07633630042179070371noreply@blogger.com1tag:blogger.com,1999:blog-5448019410873028021.post-44005130056512282712021-02-15T06:19:00.001-08:002021-02-15T06:19:43.841-08:00How to get a function name as a string in python?<p> <span style="background-color: white; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px;">In Python, how do I get a function name as a string, without calling the function?</span></p><pre class="lang-py s-code-block hljs python" style="border-radius: 5px; border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.30769; margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px; vertical-align: baseline; width: auto;"><code style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span class="hljs-function" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="hljs-keyword" style="border: 0px; box-sizing: inherit; color: var(--highlight-keyword); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">def</span> <span class="hljs-title" style="border: 0px; box-sizing: inherit; color: var(--highlight-literal); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">my_function</span>():</span>
<span class="hljs-keyword" style="border: 0px; box-sizing: inherit; color: var(--highlight-keyword); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">pass</span>
<span class="hljs-built_in" style="border: 0px; box-sizing: inherit; color: var(--highlight-literal); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">print</span> get_function_name_as_string(my_function) <span class="hljs-comment" style="border: 0px; box-sizing: inherit; color: var(--highlight-comment); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"># my_f</span></code>unction is not in quotes</pre><pre class="lang-py s-code-block hljs python" style="border-radius: 5px; border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.30769; margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px; vertical-align: baseline; width: auto;"><span style="background-color: white; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px; white-space: normal;">should output </span><code style="border-radius: 3px; border: 0px; box-sizing: inherit; color: #242729; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 2px 4px; vertical-align: baseline; white-space: pre-wrap;">"my_function"</code><span style="background-color: white; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px; white-space: normal;">.</span></pre>Jeffyehttp://www.blogger.com/profile/07633630042179070371noreply@blogger.com1tag:blogger.com,1999:blog-5448019410873028021.post-6616878289516019452021-02-15T03:39:00.001-08:002021-02-15T03:39:30.229-08:00How can i format a string using a dictionary in python 3<p> <span style="color: var(--highlight-color); font-family: inherit; font-size: 13px; font-style: inherit; font-variant-caps: inherit; font-variant-ligatures: inherit; font-weight: inherit; white-space: inherit;">geopoint = {</span><span class="hljs-string" style="border: 0px; box-sizing: inherit; color: var(--highlight-variable); font-family: inherit; font-size: 13px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;">'latitude'</span><span style="color: var(--highlight-color); font-family: inherit; font-size: 13px; font-style: inherit; font-variant-caps: inherit; font-variant-ligatures: inherit; font-weight: inherit; white-space: inherit;">:</span><span class="hljs-number" style="border: 0px; box-sizing: inherit; color: var(--highlight-namespace); font-family: inherit; font-size: 13px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;">41.123</span><span style="color: var(--highlight-color); font-family: inherit; font-size: 13px; font-style: inherit; font-variant-caps: inherit; font-variant-ligatures: inherit; font-weight: inherit; white-space: inherit;">,</span><span class="hljs-string" style="border: 0px; box-sizing: inherit; color: var(--highlight-variable); font-family: inherit; font-size: 13px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;">'longitude'</span><span style="color: var(--highlight-color); font-family: inherit; font-size: 13px; font-style: inherit; font-variant-caps: inherit; font-variant-ligatures: inherit; font-weight: inherit; white-space: inherit;">:</span><span class="hljs-number" style="border: 0px; box-sizing: inherit; color: var(--highlight-namespace); font-family: inherit; font-size: 13px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;">71.091</span><span style="color: var(--highlight-color); font-family: inherit; font-size: 13px; font-style: inherit; font-variant-caps: inherit; font-variant-ligatures: inherit; font-weight: inherit; white-space: inherit;">}</span></p><p><span style="color: var(--highlight-color); font-family: inherit; font-size: 13px; font-style: inherit; font-variant-caps: inherit; font-variant-ligatures: inherit; font-weight: inherit; white-space: inherit;">print(</span><span class="hljs-string" style="border: 0px; box-sizing: inherit; color: var(--highlight-variable); font-family: inherit; font-size: 13px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;">'{latitude} {longitude}'</span><span style="color: var(--highlight-color); font-family: inherit; font-size: 13px; font-style: inherit; font-variant-caps: inherit; font-variant-ligatures: inherit; font-weight: inherit; white-space: inherit;">.</span><span class="hljs-built_in" style="border: 0px; box-sizing: inherit; color: var(--highlight-literal); font-family: inherit; font-size: 13px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;">format</span><span style="color: var(--highlight-color); font-family: inherit; font-size: 13px; font-style: inherit; font-variant-caps: inherit; font-variant-ligatures: inherit; font-weight: inherit; white-space: inherit;">(**geopoint))</span></p><p><span style="color: var(--highlight-color); font-family: inherit; font-size: 13px; font-style: inherit; font-variant-caps: inherit; font-variant-ligatures: inherit; font-weight: inherit; white-space: inherit;"><br /></span></p><p><span style="color: var(--highlight-color); font-family: inherit; font-size: 13px; font-style: inherit; font-variant-caps: inherit; font-variant-ligatures: inherit; font-weight: inherit; white-space: inherit;">this should be good for you. </span></p><p><span style="color: var(--highlight-color); font-family: inherit; font-size: 13px; font-style: inherit; font-variant-caps: inherit; font-variant-ligatures: inherit; font-weight: inherit; white-space: inherit;">Or: </span></p><pre class="lang-py s-code-block hljs python" style="border-radius: 5px; border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.30769; margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px; vertical-align: baseline; width: auto;"><code style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;">d = <span class="hljs-built_in" style="border: 0px; box-sizing: inherit; color: var(--highlight-literal); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">dict</span>(foo=<span class="hljs-string" style="border: 0px; box-sizing: inherit; color: var(--highlight-variable); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">'x'</span>, bar=<span class="hljs-string" style="border: 0px; box-sizing: inherit; color: var(--highlight-variable); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">'y'</span>, baz=<span class="hljs-string" style="border: 0px; box-sizing: inherit; color: var(--highlight-variable); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">'z'</span>)</code></pre><pre class="lang-py s-code-block hljs python" style="border-radius: 5px; border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.30769; margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px; vertical-align: baseline; width: auto;"><code style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span class="hljs-string" style="border: 0px; box-sizing: inherit; color: var(--highlight-variable); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">'foo is {foo}, bar is {bar} and baz is {baz}'</span>.format_map(d)</code></pre><pre class="lang-py s-code-block hljs python" style="border-radius: 5px; border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.30769; margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px; vertical-align: baseline; width: auto;">or more complex case:</pre><pre class="lang-py s-code-block hljs python" style="border-radius: 5px; border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.30769; margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px; vertical-align: baseline; width: auto;"><pre class="lang-py s-code-block hljs python" style="border-radius: 5px; border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.30769; margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px; vertical-align: baseline; width: auto;"><code style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span class="hljs-meta" style="border: 0px; box-sizing: inherit; color: var(--highlight-keyword); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">>>> </span>p1 = {<span class="hljs-string" style="border: 0px; box-sizing: inherit; color: var(--highlight-variable); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">'latitude'</span>:<span class="hljs-number" style="border: 0px; box-sizing: inherit; color: var(--highlight-namespace); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">41.123</span>,<span class="hljs-string" style="border: 0px; box-sizing: inherit; color: var(--highlight-variable); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">'longitude'</span>:<span class="hljs-number" style="border: 0px; box-sizing: inherit; color: var(--highlight-namespace); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">71.091</span>}
<span class="hljs-meta" style="border: 0px; box-sizing: inherit; color: var(--highlight-keyword); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">>>> </span>p2 = {<span class="hljs-string" style="border: 0px; box-sizing: inherit; color: var(--highlight-variable); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">'latitude'</span>:<span class="hljs-number" style="border: 0px; box-sizing: inherit; color: var(--highlight-namespace); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">56.456</span>,<span class="hljs-string" style="border: 0px; box-sizing: inherit; color: var(--highlight-variable); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">'longitude'</span>:<span class="hljs-number" style="border: 0px; box-sizing: inherit; color: var(--highlight-namespace); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">23.456</span>}
<span class="hljs-meta" style="border: 0px; box-sizing: inherit; color: var(--highlight-keyword); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">>>> </span><span class="hljs-string" style="border: 0px; box-sizing: inherit; color: var(--highlight-variable); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">'{0[latitude]} {0[longitude]} - {1[latitude]} {1[longitude]}'</span>.<span class="hljs-built_in" style="border: 0px; box-sizing: inherit; color: var(--highlight-literal); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">format</span>(p1, p2)
<span class="hljs-string" style="border: 0px; box-sizing: inherit; color: var(--highlight-variable); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">'41.123 71.091 - 56.456 23.456'</span></code></pre></pre>Jeffyehttp://www.blogger.com/profile/07633630042179070371noreply@blogger.com0tag:blogger.com,1999:blog-5448019410873028021.post-16288210548536884442020-06-17T07:13:00.003-07:002020-06-17T07:14:07.978-07:00Pickle: TypeError: a bytes-like object is required, not 'str' (python 3 load python 2)<div style="background-color: white; border: 0px; box-sizing: inherit; clear: both; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1em; padding: 0px; vertical-align: baseline;">
You need to open the file in binary mode:</div>
<pre class="lang-py prettyprint prettyprinted" style="border-radius: 3px; border: 0px; box-sizing: inherit; color: #242729; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1em; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px 8px; vertical-align: baseline; width: auto;"><code style="border: 0px; box-sizing: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span class="pln" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">file </span><span class="pun" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> open</span><span class="pun" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="pln" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">fname</span><span class="pun" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">'rb'</span><span class="pun" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">)</span><span class="pln" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
response </span><span class="pun" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> pickle</span><span class="pun" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">load</span><span class="pun" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="pln" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">file</span><span class="pun" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">)</span><span class="pln" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
file</span><span class="pun" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">close</span><span class="pun" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">()</span></code></pre>
<div style="background-color: white; border: 0px; box-sizing: inherit; clear: both; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1em; padding: 0px; vertical-align: baseline;">
And when writing:</div>
<pre class="lang-py prettyprint prettyprinted" style="border-radius: 3px; border: 0px; box-sizing: inherit; color: #242729; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1em; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px 8px; vertical-align: baseline; width: auto;"><code style="border: 0px; box-sizing: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span class="pln" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">file </span><span class="pun" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> open</span><span class="pun" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="pln" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">fname</span><span class="pun" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">'wb'</span><span class="pun" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">)</span><span class="pln" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
pickle</span><span class="pun" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">dump</span><span class="pun" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="pln" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">response</span><span class="pun" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> file</span><span class="pun" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">)</span><span class="pln" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
file</span><span class="pun" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">close</span><span class="pun" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">()</span></code></pre>
<pre class="lang-py prettyprint prettyprinted" style="border-radius: 3px; border: 0px; box-sizing: inherit; color: #242729; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1em; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px 8px; vertical-align: baseline; width: auto;"><div style="background-color: white; border: 0px; box-sizing: inherit; clear: both; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1em; padding: 0px; vertical-align: baseline; white-space: normal;">
As an aside, you should use <code style="background-color: var(--black-075); border: 0px; box-sizing: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 1px 5px; vertical-align: baseline; white-space: pre-wrap;">with</code> to handle opening/closing files:</div>
<div style="background-color: white; border: 0px; box-sizing: inherit; clear: both; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1em; padding: 0px; vertical-align: baseline; white-space: normal;">
When reading:</div>
<pre class="lang-py prettyprint prettyprinted" style="border-radius: 3px; border: 0px; box-sizing: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1em; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px 8px; vertical-align: baseline; width: auto;"><code style="border: 0px; box-sizing: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span class="kwd" style="border: 0px; box-sizing: inherit; color: var(--blue-800); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">with</span><span class="pln" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> open</span><span class="pun" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="pln" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">fname</span><span class="pun" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="border: 0px; box-sizing: inherit; color: var(--red-800); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">'rb'</span><span class="pun" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">)</span><span class="pln" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="border: 0px; box-sizing: inherit; color: var(--blue-800); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">as</span><span class="pln" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> file</span><span class="pun" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
response </span><span class="pun" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> pickle</span><span class="pun" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">load</span><span class="pun" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="pln" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">file</span><span class="pun" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">)</span></code></pre>
<div style="background-color: white; border: 0px; box-sizing: inherit; clear: both; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1em; padding: 0px; vertical-align: baseline; white-space: normal;">
And when writing:</div>
<pre class="lang-py prettyprint prettyprinted" style="border-radius: 3px; border: 0px; box-sizing: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1em; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px 8px; vertical-align: baseline; width: auto;"><code style="border: 0px; box-sizing: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span class="kwd" style="border: 0px; box-sizing: inherit; color: var(--blue-800); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">with</span><span class="pln" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> open</span><span class="pun" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="pln" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">fname</span><span class="pun" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="border: 0px; box-sizing: inherit; color: var(--red-800); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">'wb'</span><span class="pun" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">)</span><span class="pln" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="border: 0px; box-sizing: inherit; color: var(--blue-800); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">as</span><span class="pln" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> file</span><span class="pun" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="pln" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
pickle</span><span class="pun" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">dump</span><span class="pun" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="pln" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">response</span><span class="pun" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> file</span><span class="pun" style="border: 0px; box-sizing: inherit; color: var(--black-750); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">)</span></code></pre>
</pre>
Jeffyehttp://www.blogger.com/profile/07633630042179070371noreply@blogger.com0tag:blogger.com,1999:blog-5448019410873028021.post-57452968224861342422020-03-22T05:13:00.002-07:002020-03-23T06:50:34.592-07:00Fluend Python pdf download<h3 class="post-title pdf" style="border: 0px; color: #404040; font-family: Raleway, Helvetica, Arial, sans-serif; font-size: 19px; font-weight: normal !important; margin: 5px 0px 10px; outline: 0px; padding: 0px; text-align: justify; vertical-align: baseline;">
Fluent Python is available for free download in PDF format</h3>
<div>
<h4 class="js-swp-splash-paper-preview-title swp-splash-paper-preview-title media-heading u-fontSerif u-fs24 u-mb0x" style="background-color: #e9e9e9; box-sizing: border-box; color: #333333; font-family: Georgia, serif !important; font-size: 24px !important; font-weight: 500; line-height: 1.45em; margin-bottom: 0px !important; margin-left: 0px; margin-right: 0px; margin-top: 0px;">
Fluent Python: Clear, Concise, and Effective Programming by Luciano Ramalho</h4>
</div>
<div>
No booshit. </div>
<div>
Dowload:</div>
<div>
<a href="https://www.dropbox.com/s/bubkxy62p9unwjq/Fluent%20Python.pdf?dl=0" rel="nofollow" target="_blank">Download link 1</a></div>
<div>
<a href="https://www.dropbox.com/s/r8m7po9isd8fqpy/ePUBw.COM%2B-%2B%E6%B5%81%E7%95%85%E7%9A%84Python.pdf?dl=0" rel="nofollow" target="_blank">Download link2 中文版</a></div>
<div>
<br /></div>
<div>
<br /></div>
Jeffyehttp://www.blogger.com/profile/07633630042179070371noreply@blogger.com0tag:blogger.com,1999:blog-5448019410873028021.post-23036381055844177392019-03-06T01:23:00.001-08:002019-03-06T01:23:19.120-08:00A good POS tagger in about 200 lines of Python<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
<strong>Update: For a fast and accurate text-processing from Python, </strong></div>
<div style="line-height: 1.5em; margin: 0.5em 1em 0.8em;">
<strong style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px;">from: </strong><span style="color: #333333; font-family: tahoma, verdana, sans-serif;"><span style="font-size: 12.16px;"><b>https://honnibal.wordpress.com/2013/09/11/a-good-part-of-speechpos-tagger-in-about-200-lines-of-python/</b></span></span></div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
<strong>see <a href="http://honnibal.github.io/spaCy" rel="nofollow" style="color: #226699; text-decoration-line: none;">http://honnibal.github.io/spaCy</a></strong></div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
Up-to-date knowledge about natural language processing is mostly locked away in academia. And academics are mostly pretty self-conscious when we write. We’re careful. We don’t want to stick our necks out too much. But under-confident recommendations suck, so here’s how to write a good <a href="https://en.wikipedia.org/wiki/Part-of-speech_tagging" style="color: #226699; font-weight: bold; text-decoration-line: none;" target="_blank">part-of-speech tagger</a>.</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
There are a tonne of <a href="http://aclweb.org/aclwiki/index.php?title=POS_Tagging_(State_of_the_art)" style="color: #226699; font-weight: bold; text-decoration-line: none;" target="_blank" title="roughly as accurate techniques for POS tagging">“best known techniques” for POS tagging</a>, and you should ignore the others and just use <a href="http://ciml.info/dl/v0_8/ciml-v0_8-ch03.pdf" style="color: #226699; font-weight: bold; text-decoration-line: none;" target="_blank" title="Averaged Perceptron (PDF)">Averaged Perceptron</a>.</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
You should use two tags of history, and features derived from the Brown word clusters distributed <a href="http://metaoptimize.com/projects/wordreprs/" style="color: #226699; font-weight: bold; text-decoration-line: none;" target="_blank" title="here">here</a>.</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
If you only need the tagger to work on carefully edited text, you should use case-sensitive features, but if you want a more robust tagger you should avoid them because they’ll make you over-fit to the conventions of your training domain. Instead, features that ask “how frequently is this word title-cased, in a large sample from the web?” work well. Then you can lower-case your comparatively tiny training corpus.</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
For efficiency, you should figure out which frequent words in your training data have unambiguous tags, so you don’t have to do anything but output their tags when they come up. About 50% of the words can be tagged that way.</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
And unless you really, really can’t do without an extra 0.1% of accuracy, you probably shouldn’t bother with any kind of search strategy — you should just use a greedy model.</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
If you do all that, you’ll find your tagger easy to write and understand, and an efficient Cython implementation will perform as follows on the standard evaluation, 130,000 words of text from the Wall Street Journal:</div>
<table class="tg-table-paper" style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px;"><tbody>
<tr><th>Tagger</th><th>Accuracy</th><th>Time (130k words)</th></tr>
<tr class="tg-even"><td><a href="https://github.com/syllog1sm/redshift/blob/develop/redshift/tagger.pyx#L60" style="color: #226699; font-weight: bold; text-decoration-line: none;" target="_blank">CyGreedyAP</a></td><td>97.1%</td><td>4s</td></tr>
</tbody></table>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
The 4s includes initialisation time — the actual per-token speed is high enough to be irrelevant; it won’t be your bottleneck.</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
It’s tempting to look at 97% accuracy and say something similar, but that’s not true. My parser is about 1% more accurate if the input has hand-labelled POS tags, and the taggers all perform much worse on out-of-domain data. Unfortunately accuracies have been fairly flat for the last ten years. That’s why my recommendation is to just use a simple and fast tagger that’s roughly as good.</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
The thing is though, it’s very common to see people using taggers that aren’t <em>anywhere near</em> that good! For an example of what a non-expert is likely to use, these were the two taggers wrapped by <a href="https://github.com/sloria/TextBlob" style="color: #226699; font-weight: bold; text-decoration-line: none;" title="TextBlob">TextBlob</a>, a new Python api that I think is quite neat:</div>
<table class="tg-table-paper" style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px;"><tbody>
<tr><th>Tagger</th><th>Accuracy</th><th>Time (130k words)</th></tr>
<tr class="tg-even"><td><a href="https://github.com/sloria/TextBlob/blob/master/text/nltk/tag/sequential.py#L488" style="color: #226699; font-weight: bold; text-decoration-line: none;">NLTK</a></td><td>94.0%</td><td>3m56s</td></tr>
<tr><td><a href="https://github.com/clips/pattern/blob/master/pattern/text/__init__.py#L913" style="color: #226699; font-weight: bold; text-decoration-line: none;">Pattern</a></td><td>93.5%</td><td>26s</td></tr>
</tbody></table>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
Both Pattern and NLTK are very robust and beautifully well documented, so the appeal of using them is obvious. But Pattern’s algorithms are pretty crappy, and NLTK carries tremendous baggage around in its implementation because of its massive framework, and double-duty as a teaching tool.</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
As a stand-alone tagger, my Cython implementation is needlessly complicated — it was written for my parser. So today I wrote a 200 line version of my recommended algorithm for TextBlob. It gets:</div>
<table class="tg-table-paper" style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px;"><tbody>
<tr><th>Tagger</th><th>Accuracy</th><th>Time (130k words)</th></tr>
<tr class="tg-even"><td><a href="https://github.com/sloria/textblob-aptagger/blob/master/textblob_aptagger/taggers.py" style="color: #226699; font-weight: bold; text-decoration-line: none;">PyGreedyAP</a></td><td>96.8%</td><td>12s</td></tr>
</tbody></table>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
I traded some accuracy and a lot of efficiency to keep the implementation simple. Here’s a far-too-brief description of how it works.</div>
<h2 style="border-bottom: 2px solid rgb(170, 187, 187); color: #555555; font-family: tahoma, verdana, sans-serif; font-size: 1.8em; font-weight: normal; letter-spacing: -1px; margin: 0px 0px 10px; padding: 0px;">
Averaged perceptron</h2>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
POS tagging is a “supervised learning problem”. You’re given a table of data, and you’re told that the values in the last column will be missing during run-time. You have to find correlations from the other columns to predict that value.</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
So for us, the missing column will be “part of speech at word <em>i</em>“. The predictor columns (features) will be things like “part of speech at word <em>i-1</em>“, “last three letters of word at <em>i+1</em>“, etc</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
First, here’s what prediction looks like at run-time:</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px;">
<div class="syntaxhighlighter python" id="highlighter_851830" style="background-color: white !important; font-size: 1em !important; margin: 1em 0px !important; overflow-x: auto !important; overflow-y: hidden !important; position: relative !important; width: 725.5px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 725px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="gutter" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(175, 175, 175) !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
1</div>
<div class="line number2 index1 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
2</div>
<div class="line number3 index2 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
3</div>
<div class="line number4 index3 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
4</div>
<div class="line number5 index4 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
5</div>
<div class="line number6 index5 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
6</div>
<div class="line number7 index6 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
7</div>
<div class="line number8 index7 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
8</div>
<div class="line number9 index8 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
9</div>
<div class="line number10 index9 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
10</div>
<div class="line number11 index10 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
11</div>
</td><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 689px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">def</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">predict(</code><code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">, features):</code></div>
<div class="line number2 index1 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 130, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'''Dot-product the features and current weights and return the best class.'''</code></div>
<div class="line number3 index2 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">scores </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">defaultdict(</code><code class="python functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">float</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">)</code></div>
<div class="line number4 index3 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">for</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">feat </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">in</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">features:</code></div>
<div class="line number5 index4 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">if</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">feat </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">not</code> <code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">in</code> <code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">.weights:</code></div>
<div class="line number6 index5 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">continue</code></div>
<div class="line number7 index6 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">weights </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code> <code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">.weights[feat]</code></div>
<div class="line number8 index7 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">for</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">clas, weight </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">in</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">weights.items():</code></div>
<div class="line number9 index8 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">scores[clas] </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">+</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">weight</code></div>
<div class="line number10 index9 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 130, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"># Do a secondary alphabetic sort, for stability</code></div>
<div class="line number11 index10 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">return</code> <code class="python functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">max</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">(</code><code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">.classes, key</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">lambda</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">clas: (scores[clas], clas))</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
Earlier I described the learning problem as a table, with one of the columns marked as missing-at-runtime. For NLP, our tables are always exceedingly sparse. You have columns like “word i-1=Parliament”, which is almost always 0. So our “weight vectors” can pretty much never be implemented as vectors. Map-types are good though — here we use dictionaries.</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
The input data, features, is a set with a member for every non-zero “column” in our “table” — every active feature. Usually this is actually a dictionary, to let you set values for the features. But here all my features are binary present-or-absent type deals.</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
The weights data-structure is a dictionary of dictionaries, that ultimately associates feature/class pairs with some weight. You want to structure it this way instead of the reverse because of <a href="https://en.wikipedia.org/wiki/Zipf's_law" style="color: #226699; font-weight: bold; text-decoration-line: none;" title="the way word frequencies are distributed">the way word frequencies are distributed</a>: most words are rare, frequent words are very frequent.</div>
<h3 style="color: #555555; font-family: tahoma, verdana, sans-serif; font-size: 1.4em; font-weight: normal; margin: 1em 0px 0.5em 0.5em; padding: 0px;">
Learning the weights</h3>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
Okay, so how do we get the values for the weights? We start with an empty weights dictionary, and iteratively do the following:</div>
<ol style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px;">
<li>Receive a new (features, POS-tag) pair</li>
<li>Guess the value of the POS tag given the current “weights” for the features</li>
<li>If guess is wrong, add +1 to the weights associated with the correct class for these features, and -1 to the weights for the predicted class.</li>
</ol>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
It’s one of the simplest learning algorithms. Whenever you make a mistake, increment the weights for the correct class, and penalise the weights that led to your false prediction. In code:</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px;">
<div class="syntaxhighlighter python" id="highlighter_720894" style="background-color: white !important; font-size: 1em !important; margin: 1em 0px !important; overflow-x: auto !important; overflow-y: hidden !important; position: relative !important; width: 725.5px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 725px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="gutter" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(175, 175, 175) !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
1</div>
<div class="line number2 index1 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
2</div>
<div class="line number3 index2 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
3</div>
<div class="line number4 index3 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
4</div>
<div class="line number5 index4 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
5</div>
<div class="line number6 index5 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
6</div>
<div class="line number7 index6 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
7</div>
<div class="line number8 index7 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
8</div>
<div class="line number9 index8 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
9</div>
</td><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 696px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">def</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">train(</code><code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">, nr_iter, examples):</code></div>
<div class="line number2 index1 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">for</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">i </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">in</code> <code class="python functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">range</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">(nr_iter):</code></div>
<div class="line number3 index2 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">for</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">features, true_tag </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">in</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">examples:</code></div>
<div class="line number4 index3 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">guess </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code> <code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">.predict(features)</code></div>
<div class="line number5 index4 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">if</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">guess !</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">true_tag:</code></div>
<div class="line number6 index5 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">for</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">f </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">in</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">features:</code></div>
<div class="line number7 index6 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">.weights[f][true_tag] </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">+</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code> <code class="python value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">1</code></div>
<div class="line number8 index7 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">.weights[f][guess] </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">-</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code> <code class="python value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">1</code></div>
<div class="line number9 index8 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">random.shuffle(examples)</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
If you iterate over the same example this way, the weights for the correct class would have to come out ahead, and you’d get the example right. If you think about what happens with two examples, you should be able to see that it will get them both right unless the features are identical. In general the algorithm will converge so long as the examples are linearly separable, although that doesn’t matter for our purpose.</div>
<h3 style="color: #555555; font-family: tahoma, verdana, sans-serif; font-size: 1.4em; font-weight: normal; margin: 1em 0px 0.5em 0.5em; padding: 0px;">
Averaging the weights</h3>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
We need to do one more thing to make the perceptron algorithm competitive. The problem with the algorithm so far is that if you train it twice on slightly different sets of examples, you end up with really different models. It doesn’t generalise that smartly. And the problem is really in the later iterations — if you let it run to convergence, it’ll pay lots of attention to the few examples it’s getting wrong, and mutate its whole model around them.</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
So, what we’re going to do is make the weights more `sticky’ — give the model less chance to ruin all its hard work in the later rounds. And we’re going to do that by returning the <em>averaged</em> weights, not the <em>final</em> weights.</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
I doubt there are many people who are convinced that’s the most obvious solution to the problem, but whatever. We’re not here to innovate, and this way is time tested on lots of problems. If you have another idea, run the experiments and tell us what you find. Actually I’d love to see more work on this, now that the averaged perceptron has become such a prominent learning algorithm in NLP.</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
Okay. So this averaging. How’s that going to work? Note that we don’t want to just average after each outer-loop iteration. We want the average of <em>all</em> the values — from the inner loop. So if we have 5,000 examples, and we train for 10 iterations, we’ll average across 50,000 values for each weight.</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
Obviously we’re not going to store all those intermediate values. Instead, we’ll track an accumulator for each weight, and divide it by the number of iterations at the end. Again: we want the average weight assigned to a feature/class pair during learning, so the key component we need is the total weight it was assigned. But we also want to be careful about how we compute that accumulator, too. On almost any instance, we’re going to see a tiny fraction of active feature/class pairs. All the other feature/class weights won’t change. So we shouldn’t have to go back and add the unchanged value to our accumulators anyway, like chumps.</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
Since we’re not chumps, we’ll make the obvious improvement. We’ll maintain <em>another</em> dictionary that tracks how long each weight has gone unchanged. Now when we do change a weight, we can do a fast-forwarded update to the accumulator, for all those iterations where it lay unchanged.</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
Here’s what a weight update looks like now that we have to maintain the totals and the time-stamps:</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px;">
<div class="syntaxhighlighter python" id="highlighter_724653" style="background-color: white !important; font-size: 1em !important; margin: 1em 0px !important; overflow-x: auto !important; overflow-y: hidden !important; position: relative !important; width: 725.5px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 725px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="gutter" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(175, 175, 175) !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
1</div>
<div class="line number2 index1 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
2</div>
<div class="line number3 index2 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
3</div>
<div class="line number4 index3 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
4</div>
<div class="line number5 index4 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
5</div>
<div class="line number6 index5 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
6</div>
<div class="line number7 index6 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
7</div>
<div class="line number8 index7 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
8</div>
<div class="line number9 index8 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
9</div>
<div class="line number10 index9 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
10</div>
<div class="line number11 index10 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
11</div>
</td><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 689px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">def</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">update(</code><code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">, truth, guess, features):</code></div>
<div class="line number2 index1 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">def</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">upd_feat(c, f, v):</code></div>
<div class="line number3 index2 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">nr_iters_at_this_weight </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code> <code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">.i </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">-</code> <code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">._timestamps[f][c]</code></div>
<div class="line number4 index3 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">._totals[f][c] </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">+</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">nr_iters_at_this_weight </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">*</code> <code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">.weights[f][c]</code></div>
<div class="line number5 index4 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">.weights[f][c] </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">+</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">v</code></div>
<div class="line number6 index5 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">._timestamps[f][c] </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code> <code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">.i</code></div>
<div class="line number7 index6 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
</div>
<div class="line number8 index7 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">.i </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">+</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code> <code class="python value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">1</code></div>
<div class="line number9 index8 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">for</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">f </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">in</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">features:</code></div>
<div class="line number10 index9 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">upd_feat(truth, f, </code><code class="python value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">1.0</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">)</code></div>
<div class="line number11 index10 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">upd_feat(guess, f, </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">-</code><code class="python value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">1.0</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">)</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<h2 style="border-bottom: 2px solid rgb(170, 187, 187); color: #555555; font-family: tahoma, verdana, sans-serif; font-size: 1.8em; font-weight: normal; letter-spacing: -1px; margin: 0px 0px 10px; padding: 0px;">
Features and pre-processing</h2>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
The POS tagging literature has tonnes of intricate features sensitive to case, punctuation, etc. They help on the standard test-set, which is from Wall Street Journal articles from the 1980s, but I don’t see how they’ll help us learn models that are useful on other text.</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
To help us learn a more general model, we’ll pre-process the data prior to feature extraction, as follows:</div>
<ul style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px;">
<li>All words are lower cased;</li>
<li>Digits in the range 1800-2100 are represented as !YEAR;</li>
<li>Other digit strings are represented as !DIGITS</li>
</ul>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
It would be better to have a module recognising dates, phone numbers, emails, hash-tags, etc. but that will have to be pushed back into the tokenization.</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
I played around with the features a little, and this seems to be a reasonable bang-for-buck configuration in terms of getting the development-data accuracy to 97% (where it typically converges anyway), and having a smaller memory foot-print:</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px;">
<div class="syntaxhighlighter python" id="highlighter_368763" style="background-color: white !important; font-size: 1em !important; margin: 1em 0px !important; overflow-x: auto !important; overflow-y: hidden !important; position: relative !important; width: 725.5px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 725px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="gutter" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(175, 175, 175) !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
1</div>
<div class="line number2 index1 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
2</div>
<div class="line number3 index2 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
3</div>
<div class="line number4 index3 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
4</div>
<div class="line number5 index4 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
5</div>
<div class="line number6 index5 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
6</div>
<div class="line number7 index6 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
7</div>
<div class="line number8 index7 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
8</div>
<div class="line number9 index8 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
9</div>
<div class="line number10 index9 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
10</div>
<div class="line number11 index10 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
11</div>
<div class="line number12 index11 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
12</div>
<div class="line number13 index12 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
13</div>
<div class="line number14 index13 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
14</div>
<div class="line number15 index14 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
15</div>
<div class="line number16 index15 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
16</div>
<div class="line number17 index16 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
17</div>
<div class="line number18 index17 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
18</div>
<div class="line number19 index18 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
19</div>
<div class="line number20 index19 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
20</div>
<div class="line number21 index20 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
21</div>
<div class="line number22 index21 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
22</div>
</td><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 689px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">def</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">_get_features(</code><code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">, i, word, context, prev, prev2):</code></div>
<div class="line number2 index1 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 130, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'''Map tokens-in-contexts into a feature representation, implemented as a</code></div>
<div class="line number3 index2 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 130, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">set. If the features change, a new model must be trained.'''</code></div>
<div class="line number4 index3 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">def</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">add(name, </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">*</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">args):</code></div>
<div class="line number5 index4 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">features.add(</code><code class="python string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'+'</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">.join((name,) </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">+</code> <code class="python functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">tuple</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">(args)))</code></div>
<div class="line number6 index5 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
</div>
<div class="line number7 index6 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">features </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code> <code class="python functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">set</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">()</code></div>
<div class="line number8 index7 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">add(</code><code class="python string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'bias'</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">) </code><code class="python comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 130, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"># This acts sort of like a prior</code></div>
<div class="line number9 index8 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">add(</code><code class="python string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'i suffix'</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">, word[</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">-</code><code class="python value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">3</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">:])</code></div>
<div class="line number10 index9 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">add(</code><code class="python string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'i pref1'</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">, word[</code><code class="python value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">0</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">])</code></div>
<div class="line number11 index10 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">add(</code><code class="python string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'i-1 tag'</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">, prev)</code></div>
<div class="line number12 index11 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">add(</code><code class="python string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'i-2 tag'</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">, prev2)</code></div>
<div class="line number13 index12 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">add(</code><code class="python string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'i tag+i-2 tag'</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">, prev, prev2)</code></div>
<div class="line number14 index13 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">add(</code><code class="python string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'i word'</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">, context[i])</code></div>
<div class="line number15 index14 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">add(</code><code class="python string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'i-1 tag+i word'</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">, prev, context[i])</code></div>
<div class="line number16 index15 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">add(</code><code class="python string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'i-1 word'</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">, context[i</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">-</code><code class="python value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">1</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">])</code></div>
<div class="line number17 index16 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">add(</code><code class="python string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'i-1 suffix'</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">, context[i</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">-</code><code class="python value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">1</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">][</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">-</code><code class="python value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">3</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">:])</code></div>
<div class="line number18 index17 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">add(</code><code class="python string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'i-2 word'</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">, context[i</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">-</code><code class="python value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">2</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">])</code></div>
<div class="line number19 index18 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">add(</code><code class="python string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'i+1 word'</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">, context[i</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">+</code><code class="python value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">1</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">])</code></div>
<div class="line number20 index19 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">add(</code><code class="python string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'i+1 suffix'</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">, context[i</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">+</code><code class="python value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">1</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">][</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">-</code><code class="python value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">3</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">:])</code></div>
<div class="line number21 index20 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">add(</code><code class="python string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'i+2 word'</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">, context[i</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">+</code><code class="python value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">2</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">])</code></div>
<div class="line number22 index21 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">return</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">features</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
I haven’t added any features from external data, such as case frequency statistics from the Google Web 1T corpus. I might add those later, but for now I figured I’d keep things simple.</div>
<h2 style="border-bottom: 2px solid rgb(170, 187, 187); color: #555555; font-family: tahoma, verdana, sans-serif; font-size: 1.8em; font-weight: normal; letter-spacing: -1px; margin: 0px 0px 10px; padding: 0px;">
What about search?</h2>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
The model I’ve recommended commits to its predictions on each word, and moves on to the next one. Those predictions are then used as features for the next word. There’s a potential problem here, but it turns out it doesn’t matter much. It’s easy to fix with beam-search, but I say it’s not really worth bothering. And it <em>definitely</em>doesn’t matter enough to adopt a slow and complicated algorithm like Conditional Random Fields.</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
Here’s the problem. The best indicator for the tag at position, say, 3 in a sentence is the word at position 3. But the next-best indicators are the tags at positions 2 and 4. So there’s a chicken-and-egg problem: we want the predictions for the surrounding words in hand before we commit to a prediction for the current word. Here’s an example where search might matter:</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
<em>Their management plan reforms worked</em></div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
Depending on just what you’ve learned from your training data, you can imagine making a different decision if you started at the left and moved right, conditioning on your previous decisions, than if you’d started at the right and moved left.</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
If that’s not obvious to you, think about it this way: “<em>worked</em>” is almost surely a verb, so if you tag “<em>reforms</em>” with that in hand, you’ll have a different idea of its tag than if you’d just come from “<em>plan</em>“, which you might have regarded as either a noun or a verb.</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
Search can only help you when you make a mistake. It can prevent that error from throwing off your subsequent decisions, or sometimes your future choices will correct the mistake. And that’s why for POS tagging, <em>search hardly matters</em>! Your model is so good straight-up that your past predictions are almost always true. So you really need the planets to align for search to matter at all.</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
And as we improve our taggers, search will matter less and less. Instead of search, what we should be caring about is multi-tagging. If we let the model be a bit uncertain, we can get over 99% accuracy assigning an average of 1.05 tags per word <a href="http://aclweb.org/anthology/P/P06/P06-1088.pdf" style="color: #226699; font-weight: bold; text-decoration-line: none;" target="_blank">(Vadas et al, ACL 2006)</a>. The averaged perceptron is rubbish at multi-tagging though. That’s its big weakness. You really want a probability distribution for that.</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
One caveat when doing greedy search, though. It’s very important that your training data model the fact that the history will be imperfect at run-time. Otherwise, it will be way over-reliant on the tag-history features. Because the Perceptron is iterative, this is very easy.</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
Here’s the training loop for the tagger:</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px;">
<div class="syntaxhighlighter python" id="highlighter_618875" style="background-color: white !important; font-size: 1em !important; margin: 1em 0px !important; overflow-x: auto !important; overflow-y: hidden !important; position: relative !important; width: 725.5px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 725px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="gutter" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(175, 175, 175) !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
1</div>
<div class="line number2 index1 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
2</div>
<div class="line number3 index2 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
3</div>
<div class="line number4 index3 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
4</div>
<div class="line number5 index4 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
5</div>
<div class="line number6 index5 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
6</div>
<div class="line number7 index6 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
7</div>
<div class="line number8 index7 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
8</div>
<div class="line number9 index8 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
9</div>
<div class="line number10 index9 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
10</div>
<div class="line number11 index10 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
11</div>
<div class="line number12 index11 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
12</div>
<div class="line number13 index12 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
13</div>
<div class="line number14 index13 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
14</div>
<div class="line number15 index14 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
15</div>
<div class="line number16 index15 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
16</div>
<div class="line number17 index16 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
17</div>
<div class="line number18 index17 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
18</div>
<div class="line number19 index18 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
19</div>
<div class="line number20 index19 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
20</div>
<div class="line number21 index20 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
21</div>
<div class="line number22 index21 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
22</div>
<div class="line number23 index22 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
23</div>
<div class="line number24 index23 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
24</div>
<div class="line number25 index24 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
25</div>
<div class="line number26 index25 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
26</div>
<div class="line number27 index26 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
27</div>
</td><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 689px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">def</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">train(</code><code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">, sentences, save_loc</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code><code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">None</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">, nr_iter</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code><code class="python value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">5</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">, quiet</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code><code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">False</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">):</code></div>
<div class="line number2 index1 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 130, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'''Train a model from sentences, and save it at save_loc. nr_iter</code></div>
<div class="line number3 index2 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 130, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">controls the number of Perceptron training iterations.'''</code></div>
<div class="line number4 index3 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">._make_tagdict(sentences, quiet</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">quiet)</code></div>
<div class="line number5 index4 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">.model.classes </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code> <code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">.classes</code></div>
<div class="line number6 index5 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">prev, prev2 </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">START</code></div>
<div class="line number7 index6 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">for</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">iter_ </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">in</code> <code class="python functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">range</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">(nr_iter):</code></div>
<div class="line number8 index7 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">c </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code> <code class="python value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">0</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">; n </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code> <code class="python value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">0</code></div>
<div class="line number9 index8 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">for</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">words, tags </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">in</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">sentences:</code></div>
<div class="line number10 index9 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">context </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">START </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">+</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">[</code><code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">._normalize(w) </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">for</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">w </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">in</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">words] </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">+</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">END</code></div>
<div class="line number11 index10 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">for</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">i, word </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">in</code> <code class="python functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">enumerate</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">(words):</code></div>
<div class="line number12 index11 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">guess </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code> <code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">.tagdict.get(word)</code></div>
<div class="line number13 index12 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">if</code> <code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">not</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">guess:</code></div>
<div class="line number14 index13 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">feats </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code> <code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">._get_features(i, word, context, prev, prev2)</code></div>
<div class="line number15 index14 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">guess </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code> <code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">.model.predict(feats)</code></div>
<div class="line number16 index15 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">.model.update(tags[i], guess, feats)</code></div>
<div class="line number17 index16 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 130, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"># Set the history features from the guesses, not the true tags</code></div>
<div class="line number18 index17 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">prev2 </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">prev; prev </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">guess</code></div>
<div class="line number19 index18 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">c </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">+</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">guess </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">tags[i]; n </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">+</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code> <code class="python value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">1</code></div>
<div class="line number20 index19 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">random.shuffle(sentences)</code></div>
<div class="line number21 index20 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">if</code> <code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">not</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">quiet:</code></div>
<div class="line number22 index21 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">print</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">(&quot;</code><code class="python functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">Iter</code> <code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">%</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">d: </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">%</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">d</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">/</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">%</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">d</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">=</code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">%</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">.</code><code class="python value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">3f</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">&quot; </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">%</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">(iter_, c, n, _pc(c, n)))</code></div>
<div class="line number23 index22 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">.model.average_weights()</code></div>
<div class="line number24 index23 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 130, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"># Pickle as a binary file</code></div>
<div class="line number25 index24 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">if</code> <code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">save_loc </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">is</code> <code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">not</code> <code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">None</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">:</code></div>
<div class="line number26 index25 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">cPickle.dump((</code><code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">.model.weights, </code><code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">.tagdict, </code><code class="python color1" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: gray !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">self</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">.classes),</code></div>
<div class="line number27 index26 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="python spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="python functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">open</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">(save_loc, </code><code class="python string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'wb'</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">), </code><code class="python keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">-</code><code class="python value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">1</code><code class="python plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">)</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
Unlike the previous snippets, this one’s literal — I tended to edit the previous ones to simplify. So if they have bugs, hopefully that’s why!</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
At the time of writing, I’m just finishing up the implementation before I submit a pull request to TextBlob. You can see the rest of the source here:</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
<a href="https://github.com/sloria/textblob-aptagger/blob/master/textblob_aptagger/taggers.py" rel="nofollow" style="color: #226699; font-weight: bold; text-decoration-line: none;">https://github.com/sloria/textblob-aptagger/blob/master/textblob_aptagger/taggers.py</a><br /><a href="https://github.com/sloria/textblob-aptagger/blob/master/textblob_aptagger/_perceptron.py" rel="nofollow" style="color: #226699; font-weight: bold; text-decoration-line: none;">https://github.com/sloria/textblob-aptagger/blob/master/textblob_aptagger/_perceptron.py</a></div>
<h2 style="border-bottom: 2px solid rgb(170, 187, 187); color: #555555; font-family: tahoma, verdana, sans-serif; font-size: 1.8em; font-weight: normal; letter-spacing: -1px; margin: 0px 0px 10px; padding: 0px;">
A final comparison…</h2>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
Over the years I’ve seen a lot of cynicism about the WSJ evaluation methodology. The claim is that we’ve just been meticulously over-fitting our methods to this data. Actually the evidence doesn’t really bear this out. Mostly, if a technique is clearly better on one evaluation, it improves others as well. Still, it’s very reasonable to want to know how these tools perform on other text. So I ran the unchanged models over two other sections from the OntoNotes corpus:</div>
<table class="tg-table-plain" style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px;"><tbody>
<tr><th>Tagger</th><th>WSJ</th><th>ABC</th><th>Web</th></tr>
<tr class="tg-even"><td>Pattern</td><td>93.5</td><td>90.7</td><td>88.1</td></tr>
<tr><td>NLTK</td><td>94.0</td><td>91.5</td><td>88.4</td></tr>
<tr class="tg-even"><td>PyGreedyAP</td><td>96.8</td><td>94.8</td><td>91.8</td></tr>
</tbody></table>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
The ABC section is broadcast news, Web is text from the web (blogs etc — I haven’t looked at the data much).</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
As you can see, the order of the systems is stable across the three comparisons, and the advantage of our Averaged Perceptron tagger over the other two is real enough. Actually the pattern tagger does very poorly on out-of-domain text. It mostly just looks up the words, so it’s very domain dependent. I hadn’t realised it before, but it’s obvious enough now that I think about it.</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
We can improve our score greatly by training on some of the foreign data. The technique described in this paper (<a href="http://acl.ldc.upenn.edu/P/P07/P07-1033.pdf" style="color: #226699; font-weight: bold; text-decoration-line: none;" title="Frustratingly easy domain adaptation">Daume III, 2007</a>) is the first thing I try when I have to do that.</div>
<div style="color: #333333; font-family: tahoma, verdana, sans-serif; font-size: 12.16px; line-height: 1.5em; margin: 0.5em 1em 0.8em;">
P.S. The format of these tables sucks. Probably I should move off <span class="skimlinks-unlinked">wordpress.com</span> to actual hosting, but in the meantime, any suggestions?</div>
Jeffyehttp://www.blogger.com/profile/07633630042179070371noreply@blogger.com0tag:blogger.com,1999:blog-5448019410873028021.post-89892354694643504362019-03-06T00:34:00.001-08:002019-03-06T00:34:17.630-08:00Python How to Send an Email Using smtplib + email<h1 class="entry-title" style="background-color: white; box-sizing: border-box; clear: both; color: #141412; font-family: Bitter, Georgia, serif; font-size: 48px; font-weight: normal; line-height: 1.3; margin: 0px 0px 5px;">
<div style="box-sizing: border-box; font-family: "Source Sans Pro", Helvetica, sans-serif; font-size: 16px; margin-bottom: 24px;">
in this article we will learn the following:</div>
<ul style="box-sizing: border-box; font-family: "Source Sans Pro", Helvetica, sans-serif; font-size: 16px; list-style-type: square; margin: 16px 0px; padding: 0px 0px 0px 40px;">
<li style="box-sizing: border-box;">The basics of emailing – kind of a rehash of the original article</li>
<li style="box-sizing: border-box;">How to send email using the TO, CC and BCC lines</li>
<li style="box-sizing: border-box;">How to send to multiple addresses at once</li>
<li style="box-sizing: border-box;">How to add an attachment and a body using the email module</li>
</ul>
<div style="box-sizing: border-box; font-family: "Source Sans Pro", Helvetica, sans-serif; font-size: 16px; margin-bottom: 24px;">
Let’s get started!<span id="more-3191" style="box-sizing: border-box;"></span></div>
</h1>
<h3 style="background-color: white; box-sizing: border-box; clear: both; color: #141412; font-family: Bitter, Georgia, serif; font-size: 22px; line-height: 1.3; margin: 22px 0px;">
How to Send an Email with Python with smtplib</h3>
<h1 class="entry-title" style="background-color: white; box-sizing: border-box; clear: both; color: #141412; font-family: Bitter, Georgia, serif; font-size: 48px; font-weight: normal; line-height: 1.3; margin: 0px 0px 5px;">
<div style="box-sizing: border-box; font-family: "Source Sans Pro", Helvetica, sans-serif; font-size: 16px; margin-bottom: 24px;">
</div>
<div style="box-sizing: border-box; font-family: "Source Sans Pro", Helvetica, sans-serif; font-size: 16px; margin-bottom: 24px;">
We will begin with a slightly modified version of the code from the original article. I noticed that I had forgotten to set up the HOST variable in the original, so this example will be a little more complete:</div>
<pre class="python" style="background: rgb(250, 250, 250); border: 1px dashed rgb(47, 111, 171); box-sizing: border-box; color: #666666; font-size: 14px; hyphens: none; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 0.5em; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">import</span> <span style="box-sizing: border-box; color: crimson;">smtplib</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">import</span> <span style="box-sizing: border-box; color: crimson;">string</span>
HOST = <span style="box-sizing: border-box; color: darkslateblue;">"mySMTP.server.com"</span>
SUBJECT = <span style="box-sizing: border-box; color: darkslateblue;">"Test email from Python"</span>
TO = <span style="box-sizing: border-box; color: darkslateblue;">"mike@someAddress.org"</span>
FROM = <span style="box-sizing: border-box; color: darkslateblue;">"python@mydomain.com"</span>
text = <span style="box-sizing: border-box; color: darkslateblue;">"Python rules them all!"</span>
BODY = <span style="box-sizing: border-box; color: crimson;">string</span>.<span style="box-sizing: border-box; color: black;">join</span><span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: black;">(</span>
<span style="box-sizing: border-box; color: darkslateblue;">"From: %s"</span> <span style="box-sizing: border-box; color: #66cc66;">%</span> FROM,
<span style="box-sizing: border-box; color: darkslateblue;">"To: %s"</span> <span style="box-sizing: border-box; color: #66cc66;">%</span> TO,
<span style="box-sizing: border-box; color: darkslateblue;">"Subject: %s"</span> <span style="box-sizing: border-box; color: #66cc66;">%</span> SUBJECT ,
<span style="box-sizing: border-box; color: darkslateblue;">""</span>,
text
<span style="box-sizing: border-box; color: black;">)</span>, <span style="box-sizing: border-box; color: darkslateblue;">"<span style="box-sizing: border-box; color: #000099; font-weight: bold;">\r</span><span style="box-sizing: border-box; color: #000099; font-weight: bold;">\n</span>"</span><span style="box-sizing: border-box; color: black;">)</span>
server = <span style="box-sizing: border-box; color: crimson;">smtplib</span>.<span style="box-sizing: border-box; color: black;">SMTP</span><span style="box-sizing: border-box; color: black;">(</span>HOST<span style="box-sizing: border-box; color: black;">)</span>
server.<span style="box-sizing: border-box; color: black;">sendmail</span><span style="box-sizing: border-box; color: black;">(</span>FROM, <span style="box-sizing: border-box; color: black;">[</span>TO<span style="box-sizing: border-box; color: black;">]</span>, BODY<span style="box-sizing: border-box; color: black;">)</span>
server.<span style="box-sizing: border-box; color: black;">quit</span><span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: black;">)</span></pre>
<div style="box-sizing: border-box; font-family: "Source Sans Pro", Helvetica, sans-serif; font-size: 16px; margin-bottom: 24px;">
You will note that this code doesn’t have a username or password in it. If your server requires authentication, then you’ll need to add the following code:</div>
<pre class="python" style="background: rgb(250, 250, 250); border: 1px dashed rgb(47, 111, 171); box-sizing: border-box; color: #666666; font-size: 14px; hyphens: none; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 0.5em; white-space: pre-wrap; word-wrap: break-word;">server.<span style="box-sizing: border-box; color: black;">login</span><span style="box-sizing: border-box; color: black;">(</span>username, password<span style="box-sizing: border-box; color: black;">)</span></pre>
<div style="box-sizing: border-box; font-family: "Source Sans Pro", Helvetica, sans-serif; font-size: 16px; margin-bottom: 24px;">
This should be added right after you create the <strong style="box-sizing: border-box;">server</strong> object. Normally, you would want to put this code into a function and call it with some of these parameters. You might even want to put some of this information into a config file. Let’s do that next.</div>
<pre class="python" style="background: rgb(250, 250, 250); border: 1px dashed rgb(47, 111, 171); box-sizing: border-box; color: #666666; font-size: 14px; hyphens: none; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 0.5em; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box; color: grey; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">def</span> send_email<span style="box-sizing: border-box; color: black;">(</span>host, subject, to_addr, from_addr, body_text<span style="box-sizing: border-box; color: black;">)</span>:
<span style="box-sizing: border-box; color: darkslateblue;">""</span><span style="box-sizing: border-box; color: darkslateblue;">"
Send an email
"</span><span style="box-sizing: border-box; color: darkslateblue;">""</span>
BODY = <span style="box-sizing: border-box; color: crimson;">string</span>.<span style="box-sizing: border-box; color: black;">join</span><span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: black;">(</span>
<span style="box-sizing: border-box; color: darkslateblue;">"From: %s"</span> <span style="box-sizing: border-box; color: #66cc66;">%</span> from_addr,
<span style="box-sizing: border-box; color: darkslateblue;">"To: %s"</span> <span style="box-sizing: border-box; color: #66cc66;">%</span> to_addr,
<span style="box-sizing: border-box; color: darkslateblue;">"Subject: %s"</span> <span style="box-sizing: border-box; color: #66cc66;">%</span> subject ,
<span style="box-sizing: border-box; color: darkslateblue;">""</span>,
body_text
<span style="box-sizing: border-box; color: black;">)</span>, <span style="box-sizing: border-box; color: darkslateblue;">"<span style="box-sizing: border-box; color: #000099; font-weight: bold;">\r</span><span style="box-sizing: border-box; color: #000099; font-weight: bold;">\n</span>"</span><span style="box-sizing: border-box; color: black;">)</span>
server = <span style="box-sizing: border-box; color: crimson;">smtplib</span>.<span style="box-sizing: border-box; color: black;">SMTP</span><span style="box-sizing: border-box; color: black;">(</span>host<span style="box-sizing: border-box; color: black;">)</span>
server.<span style="box-sizing: border-box; color: black;">sendmail</span><span style="box-sizing: border-box; color: black;">(</span>from_addr, <span style="box-sizing: border-box; color: black;">[</span>to_addr<span style="box-sizing: border-box; color: black;">]</span>, BODY<span style="box-sizing: border-box; color: black;">)</span>
server.<span style="box-sizing: border-box; color: black;">quit</span><span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: black;">)</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">if</span> __name__ == <span style="box-sizing: border-box; color: darkslateblue;">"__main__"</span>:
host = <span style="box-sizing: border-box; color: darkslateblue;">"mySMTP.server.com"</span>
subject = <span style="box-sizing: border-box; color: darkslateblue;">"Test email from Python"</span>
to_addr = <span style="box-sizing: border-box; color: darkslateblue;">"mike@someAddress.org"</span>
from_addr = <span style="box-sizing: border-box; color: darkslateblue;">"python@mydomain.com"</span>
body_text = <span style="box-sizing: border-box; color: darkslateblue;">"Python rules them all!"</span>
send_email<span style="box-sizing: border-box; color: black;">(</span>host, subject, to_addr, from_addr, body_text<span style="box-sizing: border-box; color: black;">)</span></pre>
<div style="box-sizing: border-box; font-family: "Source Sans Pro", Helvetica, sans-serif; font-size: 16px; margin-bottom: 24px;">
Now you can see how small the actual code is by just looking at the function itself. That’s 13 lines! And we could make it shorter if we didn’t put every item in the BODY on its own line, but it wouldn’t be as readable. Now we’ll add a config file to hold the server information and the from address. Why? Well in the work I do, we might use different email servers to send email or if the email server gets upgraded and the name changes, then we only need to change the config file rather than the code. The same thing could apply to the from address if our company was bought and merged into another. We’ll be using the configObj package instead of Python’s ConfigParser as I find configObj simpler. You should go on over to the <a href="https://pypi.python.org/pypi/configobj/" style="box-sizing: border-box; color: #bc360a; text-decoration-line: none;" target="_blank">Python Package Index</a> (PyPI) and download a copy if you don’t already have it.</div>
<div style="box-sizing: border-box; font-family: "Source Sans Pro", Helvetica, sans-serif; font-size: 16px; margin-bottom: 24px;">
Let’s take a look at the config file:</div>
<pre style="background: rgb(250, 250, 250); border: 1px dashed rgb(47, 111, 171); box-sizing: border-box; color: #666666; font-size: 14px; hyphens: none; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 0.5em; white-space: pre-wrap; word-wrap: break-word;">server = some.server.com
from_addr = python@mydomain.com</pre>
<div style="box-sizing: border-box; font-family: "Source Sans Pro", Helvetica, sans-serif; font-size: 16px; margin-bottom: 24px;">
That is a very simple config file. In it we have a section labeled <strong style="box-sizing: border-box;">smtp</strong> in which we have two items: server and from_addr. We’ll use configObj to read this file and turn it into a Python dictionary. Here’s the updated version of the code:</div>
<pre class="python" style="background: rgb(250, 250, 250); border: 1px dashed rgb(47, 111, 171); box-sizing: border-box; color: #666666; font-size: 14px; hyphens: none; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 0.5em; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">import</span> <span style="box-sizing: border-box; color: crimson;">os</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">import</span> <span style="box-sizing: border-box; color: crimson;">smtplib</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">import</span> <span style="box-sizing: border-box; color: crimson;">string</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">import</span> <span style="box-sizing: border-box; color: crimson;">sys</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">from</span> configobj <span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">import</span> ConfigObj
<span style="box-sizing: border-box; color: grey; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">def</span> send_email<span style="box-sizing: border-box; color: black;">(</span>subject, to_addr, body_text<span style="box-sizing: border-box; color: black;">)</span>:
<span style="box-sizing: border-box; color: darkslateblue;">""</span><span style="box-sizing: border-box; color: darkslateblue;">"
Send an email
"</span><span style="box-sizing: border-box; color: darkslateblue;">""</span>
base_path = <span style="box-sizing: border-box; color: crimson;">os</span>.<span style="box-sizing: border-box; color: black;">path</span>.<span style="box-sizing: border-box; color: black;">dirname</span><span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: crimson;">os</span>.<span style="box-sizing: border-box; color: black;">path</span>.<span style="box-sizing: border-box; color: black;">abspath</span><span style="box-sizing: border-box; color: black;">(</span>__file__<span style="box-sizing: border-box; color: black;">)</span><span style="box-sizing: border-box; color: black;">)</span>
config_path = <span style="box-sizing: border-box; color: crimson;">os</span>.<span style="box-sizing: border-box; color: black;">path</span>.<span style="box-sizing: border-box; color: black;">join</span><span style="box-sizing: border-box; color: black;">(</span>base_path, <span style="box-sizing: border-box; color: darkslateblue;">"config.ini"</span><span style="box-sizing: border-box; color: black;">)</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">if</span> <span style="box-sizing: border-box; color: crimson;">os</span>.<span style="box-sizing: border-box; color: black;">path</span>.<span style="box-sizing: border-box; color: black;">exists</span><span style="box-sizing: border-box; color: black;">(</span>config_path<span style="box-sizing: border-box; color: black;">)</span>:
cfg = ConfigObj<span style="box-sizing: border-box; color: black;">(</span>config_path<span style="box-sizing: border-box; color: black;">)</span>
cfg_dict = cfg.<span style="box-sizing: border-box; color: green;">dict</span><span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: black;">)</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">else</span>:
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">print</span> <span style="box-sizing: border-box; color: darkslateblue;">"Config not found! Exiting!"</span>
<span style="box-sizing: border-box; color: crimson;">sys</span>.<span style="box-sizing: border-box; color: black;">exit</span><span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: orangered;">1</span><span style="box-sizing: border-box; color: black;">)</span>
host = cfg_dict<span style="box-sizing: border-box; color: black;">[</span><span style="box-sizing: border-box; color: darkslateblue;">"smtp"</span><span style="box-sizing: border-box; color: black;">]</span><span style="box-sizing: border-box; color: black;">[</span><span style="box-sizing: border-box; color: darkslateblue;">"server"</span><span style="box-sizing: border-box; color: black;">]</span>
from_addr = cfg_dict<span style="box-sizing: border-box; color: black;">[</span><span style="box-sizing: border-box; color: darkslateblue;">"smtp"</span><span style="box-sizing: border-box; color: black;">]</span><span style="box-sizing: border-box; color: black;">[</span><span style="box-sizing: border-box; color: darkslateblue;">"from_addr"</span><span style="box-sizing: border-box; color: black;">]</span>
BODY = <span style="box-sizing: border-box; color: crimson;">string</span>.<span style="box-sizing: border-box; color: black;">join</span><span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: black;">(</span>
<span style="box-sizing: border-box; color: darkslateblue;">"From: %s"</span> <span style="box-sizing: border-box; color: #66cc66;">%</span> from_addr,
<span style="box-sizing: border-box; color: darkslateblue;">"To: %s"</span> <span style="box-sizing: border-box; color: #66cc66;">%</span> to_addr,
<span style="box-sizing: border-box; color: darkslateblue;">"Subject: %s"</span> <span style="box-sizing: border-box; color: #66cc66;">%</span> subject ,
<span style="box-sizing: border-box; color: darkslateblue;">""</span>,
body_text
<span style="box-sizing: border-box; color: black;">)</span>, <span style="box-sizing: border-box; color: darkslateblue;">"<span style="box-sizing: border-box; color: #000099; font-weight: bold;">\r</span><span style="box-sizing: border-box; color: #000099; font-weight: bold;">\n</span>"</span><span style="box-sizing: border-box; color: black;">)</span>
server = <span style="box-sizing: border-box; color: crimson;">smtplib</span>.<span style="box-sizing: border-box; color: black;">SMTP</span><span style="box-sizing: border-box; color: black;">(</span>host<span style="box-sizing: border-box; color: black;">)</span>
server.<span style="box-sizing: border-box; color: black;">sendmail</span><span style="box-sizing: border-box; color: black;">(</span>from_addr, <span style="box-sizing: border-box; color: black;">[</span>to_addr<span style="box-sizing: border-box; color: black;">]</span>, BODY<span style="box-sizing: border-box; color: black;">)</span>
server.<span style="box-sizing: border-box; color: black;">quit</span><span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: black;">)</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">if</span> __name__ == <span style="box-sizing: border-box; color: darkslateblue;">"__main__"</span>:
subject = <span style="box-sizing: border-box; color: darkslateblue;">"Test email from Python"</span>
to_addr = <span style="box-sizing: border-box; color: darkslateblue;">"mike@someAddress.org"</span>
body_text = <span style="box-sizing: border-box; color: darkslateblue;">"Python rules them all!"</span>
send_email<span style="box-sizing: border-box; color: black;">(</span>subject, to_addr, body_text<span style="box-sizing: border-box; color: black;">)</span></pre>
<div style="box-sizing: border-box; font-family: "Source Sans Pro", Helvetica, sans-serif; font-size: 16px; margin-bottom: 24px;">
We’ve added a little check to this code. We want to first grab the path that the script itself is in, which is what <strong style="box-sizing: border-box;">base_path</strong> represents. Next we combine that path with the file name to get a fully qualified path to the config file. We then check for the existence of that file. If it’s there, we create a dictionary and if it’s not, we print a message and exit the script. We should add an exception handler around the ConfigObj call just to be on the safe side though as the file could exist, but be corrupt or we might not have permission to open it and that will throw an exception. That will be a little project that you can attempt on your own. Anyway, let’s say that everything goes well and we get our dictionary. Now we can extract the host and from_addr information using normal dictionary syntax.</div>
<div style="box-sizing: border-box; font-family: "Source Sans Pro", Helvetica, sans-serif; font-size: 16px; margin-bottom: 24px;">
Now we’re ready to learn how to send multiple emails at the same time!</div>
</h1>
<h3 style="background-color: white; box-sizing: border-box; clear: both; color: #141412; font-family: Bitter, Georgia, serif; font-size: 22px; line-height: 1.3; margin: 22px 0px;">
How to Send Multiple Emails at Once</h3>
<h1 class="entry-title" style="background-color: white; box-sizing: border-box; clear: both; color: #141412; font-family: Bitter, Georgia, serif; font-size: 48px; font-weight: normal; line-height: 1.3; margin: 0px 0px 5px;">
<div style="box-sizing: border-box; font-family: "Source Sans Pro", Helvetica, sans-serif; font-size: 16px; margin-bottom: 24px;">
</div>
<div style="box-sizing: border-box; font-family: "Source Sans Pro", Helvetica, sans-serif; font-size: 16px; margin-bottom: 24px;">
If you do a search on the web on this topic, you’ll likely come across this StackOverflow <a href="http://stackoverflow.com/questions/6941811/send-email-to-multiple-recipients-from-txt-file-with-python-smtplib" style="box-sizing: border-box; color: #bc360a; text-decoration-line: none;" target="_blank">question</a> where we can learn for sending multiple emails via the smtplib module. Let’s modify our last example a little so we send multiple emails!</div>
<pre class="python" style="background: rgb(250, 250, 250); border: 1px dashed rgb(47, 111, 171); box-sizing: border-box; color: #666666; font-size: 14px; hyphens: none; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 0.5em; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">import</span> <span style="box-sizing: border-box; color: crimson;">os</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">import</span> <span style="box-sizing: border-box; color: crimson;">smtplib</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">import</span> <span style="box-sizing: border-box; color: crimson;">string</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">import</span> <span style="box-sizing: border-box; color: crimson;">sys</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">from</span> configobj <span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">import</span> ConfigObj
<span style="box-sizing: border-box; color: grey; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">def</span> send_email<span style="box-sizing: border-box; color: black;">(</span>subject, body_text, emails<span style="box-sizing: border-box; color: black;">)</span>:
<span style="box-sizing: border-box; color: darkslateblue;">""</span><span style="box-sizing: border-box; color: darkslateblue;">"
Send an email
"</span><span style="box-sizing: border-box; color: darkslateblue;">""</span>
base_path = <span style="box-sizing: border-box; color: crimson;">os</span>.<span style="box-sizing: border-box; color: black;">path</span>.<span style="box-sizing: border-box; color: black;">dirname</span><span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: crimson;">os</span>.<span style="box-sizing: border-box; color: black;">path</span>.<span style="box-sizing: border-box; color: black;">abspath</span><span style="box-sizing: border-box; color: black;">(</span>__file__<span style="box-sizing: border-box; color: black;">)</span><span style="box-sizing: border-box; color: black;">)</span>
config_path = <span style="box-sizing: border-box; color: crimson;">os</span>.<span style="box-sizing: border-box; color: black;">path</span>.<span style="box-sizing: border-box; color: black;">join</span><span style="box-sizing: border-box; color: black;">(</span>base_path, <span style="box-sizing: border-box; color: darkslateblue;">"config.ini"</span><span style="box-sizing: border-box; color: black;">)</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">if</span> <span style="box-sizing: border-box; color: crimson;">os</span>.<span style="box-sizing: border-box; color: black;">path</span>.<span style="box-sizing: border-box; color: black;">exists</span><span style="box-sizing: border-box; color: black;">(</span>config_path<span style="box-sizing: border-box; color: black;">)</span>:
cfg = ConfigObj<span style="box-sizing: border-box; color: black;">(</span>config_path<span style="box-sizing: border-box; color: black;">)</span>
cfg_dict = cfg.<span style="box-sizing: border-box; color: green;">dict</span><span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: black;">)</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">else</span>:
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">print</span> <span style="box-sizing: border-box; color: darkslateblue;">"Config not found! Exiting!"</span>
<span style="box-sizing: border-box; color: crimson;">sys</span>.<span style="box-sizing: border-box; color: black;">exit</span><span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: orangered;">1</span><span style="box-sizing: border-box; color: black;">)</span>
host = cfg_dict<span style="box-sizing: border-box; color: black;">[</span><span style="box-sizing: border-box; color: darkslateblue;">"smtp"</span><span style="box-sizing: border-box; color: black;">]</span><span style="box-sizing: border-box; color: black;">[</span><span style="box-sizing: border-box; color: darkslateblue;">"server"</span><span style="box-sizing: border-box; color: black;">]</span>
from_addr = cfg_dict<span style="box-sizing: border-box; color: black;">[</span><span style="box-sizing: border-box; color: darkslateblue;">"smtp"</span><span style="box-sizing: border-box; color: black;">]</span><span style="box-sizing: border-box; color: black;">[</span><span style="box-sizing: border-box; color: darkslateblue;">"from_addr"</span><span style="box-sizing: border-box; color: black;">]</span>
BODY = <span style="box-sizing: border-box; color: crimson;">string</span>.<span style="box-sizing: border-box; color: black;">join</span><span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: black;">(</span>
<span style="box-sizing: border-box; color: darkslateblue;">"From: %s"</span> <span style="box-sizing: border-box; color: #66cc66;">%</span> from_addr,
<span style="box-sizing: border-box; color: darkslateblue;">"To: %s"</span> <span style="box-sizing: border-box; color: #66cc66;">%</span> <span style="box-sizing: border-box; color: darkslateblue;">', '</span>.<span style="box-sizing: border-box; color: black;">join</span><span style="box-sizing: border-box; color: black;">(</span>emails<span style="box-sizing: border-box; color: black;">)</span>,
<span style="box-sizing: border-box; color: darkslateblue;">"Subject: %s"</span> <span style="box-sizing: border-box; color: #66cc66;">%</span> subject ,
<span style="box-sizing: border-box; color: darkslateblue;">""</span>,
body_text
<span style="box-sizing: border-box; color: black;">)</span>, <span style="box-sizing: border-box; color: darkslateblue;">"<span style="box-sizing: border-box; color: #000099; font-weight: bold;">\r</span><span style="box-sizing: border-box; color: #000099; font-weight: bold;">\n</span>"</span><span style="box-sizing: border-box; color: black;">)</span>
server = <span style="box-sizing: border-box; color: crimson;">smtplib</span>.<span style="box-sizing: border-box; color: black;">SMTP</span><span style="box-sizing: border-box; color: black;">(</span>host<span style="box-sizing: border-box; color: black;">)</span>
server.<span style="box-sizing: border-box; color: black;">sendmail</span><span style="box-sizing: border-box; color: black;">(</span>from_addr, emails, BODY<span style="box-sizing: border-box; color: black;">)</span>
server.<span style="box-sizing: border-box; color: black;">quit</span><span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: black;">)</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">if</span> __name__ == <span style="box-sizing: border-box; color: darkslateblue;">"__main__"</span>:
emails = <span style="box-sizing: border-box; color: black;">[</span><span style="box-sizing: border-box; color: darkslateblue;">"mike@example.org"</span>, <span style="box-sizing: border-box; color: darkslateblue;">"someone@gmail.com"</span><span style="box-sizing: border-box; color: black;">]</span>
subject = <span style="box-sizing: border-box; color: darkslateblue;">"Test email from Python"</span>
body_text = <span style="box-sizing: border-box; color: darkslateblue;">"Python rules them all!"</span>
send_email<span style="box-sizing: border-box; color: black;">(</span>subject, body_text, emails<span style="box-sizing: border-box; color: black;">)</span></pre>
<div style="box-sizing: border-box; font-family: "Source Sans Pro", Helvetica, sans-serif; font-size: 16px; margin-bottom: 24px;">
You’ll notice that in this example, we removed the <strong style="box-sizing: border-box;">to_addr</strong> parameter and added an <strong style="box-sizing: border-box;">emails</strong> parameter, which is a list of email addresses. To make this work, we need to create a comma-separated string in the <strong style="box-sizing: border-box;">To:</strong> portion of the BODY and also pass the email list to the sendmail method. Thus we do the following to create a simple comma separated string: <strong style="box-sizing: border-box;">‘, ‘.join(emails)</strong>. Simple, huh?</div>
<div style="box-sizing: border-box; font-family: "Source Sans Pro", Helvetica, sans-serif; font-size: 16px; margin-bottom: 24px;">
Now we just need to figure out how to send using the CC and BCC fields. Let’s create a new version of this code that supports that functionality!</div>
<pre class="python" style="background: rgb(250, 250, 250); border: 1px dashed rgb(47, 111, 171); box-sizing: border-box; color: #666666; font-size: 14px; hyphens: none; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 0.5em; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">import</span> <span style="box-sizing: border-box; color: crimson;">os</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">import</span> <span style="box-sizing: border-box; color: crimson;">smtplib</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">import</span> <span style="box-sizing: border-box; color: crimson;">string</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">import</span> <span style="box-sizing: border-box; color: crimson;">sys</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">from</span> configobj <span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">import</span> ConfigObj
<span style="box-sizing: border-box; color: grey; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">def</span> send_email<span style="box-sizing: border-box; color: black;">(</span>subject, body_text, to_emails, cc_emails, bcc_emails<span style="box-sizing: border-box; color: black;">)</span>:
<span style="box-sizing: border-box; color: darkslateblue;">""</span><span style="box-sizing: border-box; color: darkslateblue;">"
Send an email
"</span><span style="box-sizing: border-box; color: darkslateblue;">""</span>
base_path = <span style="box-sizing: border-box; color: crimson;">os</span>.<span style="box-sizing: border-box; color: black;">path</span>.<span style="box-sizing: border-box; color: black;">dirname</span><span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: crimson;">os</span>.<span style="box-sizing: border-box; color: black;">path</span>.<span style="box-sizing: border-box; color: black;">abspath</span><span style="box-sizing: border-box; color: black;">(</span>__file__<span style="box-sizing: border-box; color: black;">)</span><span style="box-sizing: border-box; color: black;">)</span>
config_path = <span style="box-sizing: border-box; color: crimson;">os</span>.<span style="box-sizing: border-box; color: black;">path</span>.<span style="box-sizing: border-box; color: black;">join</span><span style="box-sizing: border-box; color: black;">(</span>base_path, <span style="box-sizing: border-box; color: darkslateblue;">"config.ini"</span><span style="box-sizing: border-box; color: black;">)</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">if</span> <span style="box-sizing: border-box; color: crimson;">os</span>.<span style="box-sizing: border-box; color: black;">path</span>.<span style="box-sizing: border-box; color: black;">exists</span><span style="box-sizing: border-box; color: black;">(</span>config_path<span style="box-sizing: border-box; color: black;">)</span>:
cfg = ConfigObj<span style="box-sizing: border-box; color: black;">(</span>config_path<span style="box-sizing: border-box; color: black;">)</span>
cfg_dict = cfg.<span style="box-sizing: border-box; color: green;">dict</span><span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: black;">)</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">else</span>:
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">print</span> <span style="box-sizing: border-box; color: darkslateblue;">"Config not found! Exiting!"</span>
<span style="box-sizing: border-box; color: crimson;">sys</span>.<span style="box-sizing: border-box; color: black;">exit</span><span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: orangered;">1</span><span style="box-sizing: border-box; color: black;">)</span>
host = cfg_dict<span style="box-sizing: border-box; color: black;">[</span><span style="box-sizing: border-box; color: darkslateblue;">"smtp"</span><span style="box-sizing: border-box; color: black;">]</span><span style="box-sizing: border-box; color: black;">[</span><span style="box-sizing: border-box; color: darkslateblue;">"server"</span><span style="box-sizing: border-box; color: black;">]</span>
from_addr = cfg_dict<span style="box-sizing: border-box; color: black;">[</span><span style="box-sizing: border-box; color: darkslateblue;">"smtp"</span><span style="box-sizing: border-box; color: black;">]</span><span style="box-sizing: border-box; color: black;">[</span><span style="box-sizing: border-box; color: darkslateblue;">"from_addr"</span><span style="box-sizing: border-box; color: black;">]</span>
BODY = <span style="box-sizing: border-box; color: crimson;">string</span>.<span style="box-sizing: border-box; color: black;">join</span><span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: black;">(</span>
<span style="box-sizing: border-box; color: darkslateblue;">"From: %s"</span> <span style="box-sizing: border-box; color: #66cc66;">%</span> from_addr,
<span style="box-sizing: border-box; color: darkslateblue;">"To: %s"</span> <span style="box-sizing: border-box; color: #66cc66;">%</span> <span style="box-sizing: border-box; color: darkslateblue;">', '</span>.<span style="box-sizing: border-box; color: black;">join</span><span style="box-sizing: border-box; color: black;">(</span>to_emails<span style="box-sizing: border-box; color: black;">)</span>,
<span style="box-sizing: border-box; color: darkslateblue;">"CC: %s"</span> <span style="box-sizing: border-box; color: #66cc66;">%</span> <span style="box-sizing: border-box; color: darkslateblue;">', '</span>.<span style="box-sizing: border-box; color: black;">join</span><span style="box-sizing: border-box; color: black;">(</span>cc_emails<span style="box-sizing: border-box; color: black;">)</span>,
<span style="box-sizing: border-box; color: darkslateblue;">"BCC: %s"</span> <span style="box-sizing: border-box; color: #66cc66;">%</span> <span style="box-sizing: border-box; color: darkslateblue;">', '</span>.<span style="box-sizing: border-box; color: black;">join</span><span style="box-sizing: border-box; color: black;">(</span>bcc_emails<span style="box-sizing: border-box; color: black;">)</span>,
<span style="box-sizing: border-box; color: darkslateblue;">"Subject: %s"</span> <span style="box-sizing: border-box; color: #66cc66;">%</span> subject ,
<span style="box-sizing: border-box; color: darkslateblue;">""</span>,
body_text
<span style="box-sizing: border-box; color: black;">)</span>, <span style="box-sizing: border-box; color: darkslateblue;">"<span style="box-sizing: border-box; color: #000099; font-weight: bold;">\r</span><span style="box-sizing: border-box; color: #000099; font-weight: bold;">\n</span>"</span><span style="box-sizing: border-box; color: black;">)</span>
emails = to_emails + cc_emails + bcc_emails
server = <span style="box-sizing: border-box; color: crimson;">smtplib</span>.<span style="box-sizing: border-box; color: black;">SMTP</span><span style="box-sizing: border-box; color: black;">(</span>host<span style="box-sizing: border-box; color: black;">)</span>
server.<span style="box-sizing: border-box; color: black;">sendmail</span><span style="box-sizing: border-box; color: black;">(</span>from_addr, emails, BODY<span style="box-sizing: border-box; color: black;">)</span>
server.<span style="box-sizing: border-box; color: black;">quit</span><span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: black;">)</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">if</span> __name__ == <span style="box-sizing: border-box; color: darkslateblue;">"__main__"</span>:
emails = <span style="box-sizing: border-box; color: black;">[</span><span style="box-sizing: border-box; color: darkslateblue;">"mike@somewhere.org"</span><span style="box-sizing: border-box; color: black;">]</span>
cc_emails = <span style="box-sizing: border-box; color: black;">[</span><span style="box-sizing: border-box; color: darkslateblue;">"someone@gmail.com"</span><span style="box-sizing: border-box; color: black;">]</span>
bcc_emails = <span style="box-sizing: border-box; color: black;">[</span><span style="box-sizing: border-box; color: darkslateblue;">"schmuck@newtel.net"</span><span style="box-sizing: border-box; color: black;">]</span>
subject = <span style="box-sizing: border-box; color: darkslateblue;">"Test email from Python"</span>
body_text = <span style="box-sizing: border-box; color: darkslateblue;">"Python rules them all!"</span>
send_email<span style="box-sizing: border-box; color: black;">(</span>subject, body_text, emails, cc_emails, bcc_emails<span style="box-sizing: border-box; color: black;">)</span></pre>
<div style="box-sizing: border-box; font-family: "Source Sans Pro", Helvetica, sans-serif; font-size: 16px; margin-bottom: 24px;">
In this code, we pass in 3 lists, each with one email address a piece. We create the CC and BCC fields exactly the same as before, but we also need to combine the 3 lists into one so we can pass the combined list to the sendmail() method. There is some talk on <a href="http://stackoverflow.com/questions/771907/python-how-to-store-a-draft-email-with-bcc-recipients-to-exchange-server-via-im" style="box-sizing: border-box; color: #bc360a; text-decoration-line: none;" target="_blank">StackOverflow </a>that some email clients may handle the BCC field in odd ways that allow the recipient to see the BCC list via the email headers. I am unable to confirm this behavior, but I do know that Gmail successfully strips the BCC information from the email header. I haven’t found a client that doesn’t, but if you have, feel free to let us know in the comments.</div>
<div style="box-sizing: border-box; font-family: "Source Sans Pro", Helvetica, sans-serif; font-size: 16px; margin-bottom: 24px;">
Now we’re ready to move on to using Python’s email module!</div>
</h1>
<h3 style="background-color: white; box-sizing: border-box; clear: both; color: #141412; font-family: Bitter, Georgia, serif; font-size: 22px; line-height: 1.3; margin: 22px 0px;">
Sending Email Attachments with Python</h3>
<h1 class="entry-title" style="background-color: white; box-sizing: border-box; clear: both; color: #141412; font-family: Bitter, Georgia, serif; font-size: 48px; font-weight: normal; line-height: 1.3; margin: 0px 0px 5px;">
<div style="box-sizing: border-box; font-family: "Source Sans Pro", Helvetica, sans-serif; font-size: 16px; margin-bottom: 24px;">
</div>
<div style="box-sizing: border-box; font-family: "Source Sans Pro", Helvetica, sans-serif; font-size: 16px; margin-bottom: 24px;">
Now we’ll take what we learned from the previous section and mix it together with the Python email module. The email module makes adding attachments extremely easy. Here’s the code:</div>
<pre class="python" style="background: rgb(250, 250, 250); border: 1px dashed rgb(47, 111, 171); box-sizing: border-box; color: #666666; font-size: 14px; hyphens: none; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 0.5em; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">import</span> <span style="box-sizing: border-box; color: crimson;">os</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">import</span> <span style="box-sizing: border-box; color: crimson;">smtplib</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">import</span> <span style="box-sizing: border-box; color: crimson;">string</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">import</span> <span style="box-sizing: border-box; color: crimson;">sys</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">from</span> configobj <span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">import</span> ConfigObj
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">from</span> <span style="box-sizing: border-box; color: crimson;">email</span> <span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">import</span> Encoders
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">from</span> <span style="box-sizing: border-box; color: crimson;">email</span>.<span style="box-sizing: border-box; color: black;">mime</span>.<span style="box-sizing: border-box; color: black;">text</span> <span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">import</span> MIMEText
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">from</span> <span style="box-sizing: border-box; color: crimson;">email</span>.<span style="box-sizing: border-box; color: black;">MIMEBase</span> <span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">import</span> MIMEBase
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">from</span> <span style="box-sizing: border-box; color: crimson;">email</span>.<span style="box-sizing: border-box; color: black;">MIMEMultipart</span> <span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">import</span> MIMEMultipart
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">from</span> <span style="box-sizing: border-box; color: crimson;">email</span>.<span style="box-sizing: border-box; color: black;">Utils</span> <span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">import</span> formatdate
<span style="box-sizing: border-box; color: grey; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">def</span> send_email_with_attachment<span style="box-sizing: border-box; color: black;">(</span>subject, body_text, to_emails,
cc_emails, bcc_emails, file_to_attach<span style="box-sizing: border-box; color: black;">)</span>:
<span style="box-sizing: border-box; color: darkslateblue;">""</span><span style="box-sizing: border-box; color: darkslateblue;">"
Send an email with an attachment
"</span><span style="box-sizing: border-box; color: darkslateblue;">""</span>
base_path = <span style="box-sizing: border-box; color: crimson;">os</span>.<span style="box-sizing: border-box; color: black;">path</span>.<span style="box-sizing: border-box; color: black;">dirname</span><span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: crimson;">os</span>.<span style="box-sizing: border-box; color: black;">path</span>.<span style="box-sizing: border-box; color: black;">abspath</span><span style="box-sizing: border-box; color: black;">(</span>__file__<span style="box-sizing: border-box; color: black;">)</span><span style="box-sizing: border-box; color: black;">)</span>
config_path = <span style="box-sizing: border-box; color: crimson;">os</span>.<span style="box-sizing: border-box; color: black;">path</span>.<span style="box-sizing: border-box; color: black;">join</span><span style="box-sizing: border-box; color: black;">(</span>base_path, <span style="box-sizing: border-box; color: darkslateblue;">"config.ini"</span><span style="box-sizing: border-box; color: black;">)</span>
header = <span style="box-sizing: border-box; color: darkslateblue;">'Content-Disposition'</span>, <span style="box-sizing: border-box; color: darkslateblue;">'attachment; filename="%s"'</span> <span style="box-sizing: border-box; color: #66cc66;">%</span> file_to_attach
<span style="box-sizing: border-box; color: grey; font-style: italic;"># get the config</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">if</span> <span style="box-sizing: border-box; color: crimson;">os</span>.<span style="box-sizing: border-box; color: black;">path</span>.<span style="box-sizing: border-box; color: black;">exists</span><span style="box-sizing: border-box; color: black;">(</span>config_path<span style="box-sizing: border-box; color: black;">)</span>:
cfg = ConfigObj<span style="box-sizing: border-box; color: black;">(</span>config_path<span style="box-sizing: border-box; color: black;">)</span>
cfg_dict = cfg.<span style="box-sizing: border-box; color: green;">dict</span><span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: black;">)</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">else</span>:
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">print</span> <span style="box-sizing: border-box; color: darkslateblue;">"Config not found! Exiting!"</span>
<span style="box-sizing: border-box; color: crimson;">sys</span>.<span style="box-sizing: border-box; color: black;">exit</span><span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: orangered;">1</span><span style="box-sizing: border-box; color: black;">)</span>
<span style="box-sizing: border-box; color: grey; font-style: italic;"># extract server and from_addr from config</span>
host = cfg_dict<span style="box-sizing: border-box; color: black;">[</span><span style="box-sizing: border-box; color: darkslateblue;">"smtp"</span><span style="box-sizing: border-box; color: black;">]</span><span style="box-sizing: border-box; color: black;">[</span><span style="box-sizing: border-box; color: darkslateblue;">"server"</span><span style="box-sizing: border-box; color: black;">]</span>
from_addr = cfg_dict<span style="box-sizing: border-box; color: black;">[</span><span style="box-sizing: border-box; color: darkslateblue;">"smtp"</span><span style="box-sizing: border-box; color: black;">]</span><span style="box-sizing: border-box; color: black;">[</span><span style="box-sizing: border-box; color: darkslateblue;">"from_addr"</span><span style="box-sizing: border-box; color: black;">]</span>
<span style="box-sizing: border-box; color: grey; font-style: italic;"># create the message</span>
msg = MIMEMultipart<span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: black;">)</span>
msg<span style="box-sizing: border-box; color: black;">[</span><span style="box-sizing: border-box; color: darkslateblue;">"From"</span><span style="box-sizing: border-box; color: black;">]</span> = from_addr
msg<span style="box-sizing: border-box; color: black;">[</span><span style="box-sizing: border-box; color: darkslateblue;">"Subject"</span><span style="box-sizing: border-box; color: black;">]</span> = subject
msg<span style="box-sizing: border-box; color: black;">[</span><span style="box-sizing: border-box; color: darkslateblue;">"Date"</span><span style="box-sizing: border-box; color: black;">]</span> = formatdate<span style="box-sizing: border-box; color: black;">(</span>localtime=<span style="box-sizing: border-box; color: green;">True</span><span style="box-sizing: border-box; color: black;">)</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">if</span> body_text:
msg.<span style="box-sizing: border-box; color: black;">attach</span><span style="box-sizing: border-box; color: black;">(</span> MIMEText<span style="box-sizing: border-box; color: black;">(</span>body_text<span style="box-sizing: border-box; color: black;">)</span> <span style="box-sizing: border-box; color: black;">)</span>
msg<span style="box-sizing: border-box; color: black;">[</span><span style="box-sizing: border-box; color: darkslateblue;">"To"</span><span style="box-sizing: border-box; color: black;">]</span> = <span style="box-sizing: border-box; color: darkslateblue;">', '</span>.<span style="box-sizing: border-box; color: black;">join</span><span style="box-sizing: border-box; color: black;">(</span>to_emails<span style="box-sizing: border-box; color: black;">)</span>
msg<span style="box-sizing: border-box; color: black;">[</span><span style="box-sizing: border-box; color: darkslateblue;">"cc"</span><span style="box-sizing: border-box; color: black;">]</span> = <span style="box-sizing: border-box; color: darkslateblue;">', '</span>.<span style="box-sizing: border-box; color: black;">join</span><span style="box-sizing: border-box; color: black;">(</span>cc_emails<span style="box-sizing: border-box; color: black;">)</span>
attachment = MIMEBase<span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: darkslateblue;">'application'</span>, <span style="box-sizing: border-box; color: darkslateblue;">"octet-stream"</span><span style="box-sizing: border-box; color: black;">)</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">try</span>:
with <span style="box-sizing: border-box; color: green;">open</span><span style="box-sizing: border-box; color: black;">(</span>file_to_attach, <span style="box-sizing: border-box; color: darkslateblue;">"rb"</span><span style="box-sizing: border-box; color: black;">)</span> as fh:
data = fh.<span style="box-sizing: border-box; color: black;">read</span><span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: black;">)</span>
attachment.<span style="box-sizing: border-box; color: black;">set_payload</span><span style="box-sizing: border-box; color: black;">(</span> data <span style="box-sizing: border-box; color: black;">)</span>
Encoders.<span style="box-sizing: border-box; color: black;">encode_base64</span><span style="box-sizing: border-box; color: black;">(</span>attachment<span style="box-sizing: border-box; color: black;">)</span>
attachment.<span style="box-sizing: border-box; color: black;">add_header</span><span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: #66cc66;">*</span>header<span style="box-sizing: border-box; color: black;">)</span>
msg.<span style="box-sizing: border-box; color: black;">attach</span><span style="box-sizing: border-box; color: black;">(</span>attachment<span style="box-sizing: border-box; color: black;">)</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">except</span> <span style="box-sizing: border-box; color: green;">IOError</span>:
msg = <span style="box-sizing: border-box; color: darkslateblue;">"Error opening attachment file %s"</span> <span style="box-sizing: border-box; color: #66cc66;">%</span> file_to_attach
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">print</span> msg
<span style="box-sizing: border-box; color: crimson;">sys</span>.<span style="box-sizing: border-box; color: black;">exit</span><span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: orangered;">1</span><span style="box-sizing: border-box; color: black;">)</span>
emails = to_emails + cc_emails
server = <span style="box-sizing: border-box; color: crimson;">smtplib</span>.<span style="box-sizing: border-box; color: black;">SMTP</span><span style="box-sizing: border-box; color: black;">(</span>host<span style="box-sizing: border-box; color: black;">)</span>
server.<span style="box-sizing: border-box; color: black;">sendmail</span><span style="box-sizing: border-box; color: black;">(</span>from_addr, emails, msg.<span style="box-sizing: border-box; color: black;">as_string</span><span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: black;">)</span><span style="box-sizing: border-box; color: black;">)</span>
server.<span style="box-sizing: border-box; color: black;">quit</span><span style="box-sizing: border-box; color: black;">(</span><span style="box-sizing: border-box; color: black;">)</span>
<span style="box-sizing: border-box; color: #ff7700; font-weight: bold;">if</span> __name__ == <span style="box-sizing: border-box; color: darkslateblue;">"__main__"</span>:
emails = <span style="box-sizing: border-box; color: black;">[</span><span style="box-sizing: border-box; color: darkslateblue;">"mike@somewhere.org"</span>, <span style="box-sizing: border-box; color: darkslateblue;">"nedry@jp.net"</span><span style="box-sizing: border-box; color: black;">]</span>
cc_emails = <span style="box-sizing: border-box; color: black;">[</span><span style="box-sizing: border-box; color: darkslateblue;">"someone@gmail.com"</span><span style="box-sizing: border-box; color: black;">]</span>
bcc_emails = <span style="box-sizing: border-box; color: black;">[</span><span style="box-sizing: border-box; color: darkslateblue;">"anonymous@circe.org"</span><span style="box-sizing: border-box; color: black;">]</span>
subject = <span style="box-sizing: border-box; color: darkslateblue;">"Test email with attachment from Python"</span>
body_text = <span style="box-sizing: border-box; color: darkslateblue;">"This email contains an attachment!"</span>
path = <span style="box-sizing: border-box; color: darkslateblue;">"/path/to/some/file"</span>
send_email_with_attachment<span style="box-sizing: border-box; color: black;">(</span>subject, body_text, emails,
cc_emails, bcc_emails, path<span style="box-sizing: border-box; color: black;">)</span></pre>
<div style="box-sizing: border-box; font-family: "Source Sans Pro", Helvetica, sans-serif; font-size: 16px; margin-bottom: 24px;">
Here we have renamed our function and added a new argument, <strong style="box-sizing: border-box;">file_to_attach</strong>. We also need to add a header and create a <strong style="box-sizing: border-box;">MIMEMultipart</strong> object. The header could be created any time before we add the attachment. We add elements to the MIMEMultipart object (msg) like we would keys to a dictionary. You’ll note that we have to use the email module’s formatdate method to insert the properly formatted date. To add the body of the message, we need to create an instance of <strong style="box-sizing: border-box;">MIMEText</strong>. If you’re paying attention, you’ll see that we didn’t add the BCC information, but you could easily do so by following the conventions in the code above. Next we add the attachment. We wrap it in an exception handler and use the <strong style="box-sizing: border-box;">with </strong>statement to extract the file and place it in our <strong style="box-sizing: border-box;">MIMEBase</strong> object. Finally we add it to the msg variable and we send it out. Notice that we have to convert the msg to a string in the sendmail method.</div>
</h1>
<h3 style="background-color: white; box-sizing: border-box; clear: both; color: #141412; font-family: Bitter, Georgia, serif; font-size: 22px; line-height: 1.3; margin: 22px 0px;">
Wrapping Up</h3>
<h1 class="entry-title" style="background-color: white; box-sizing: border-box; clear: both; color: #141412; font-family: Bitter, Georgia, serif; font-size: 48px; font-weight: normal; line-height: 1.3; margin: 0px 0px 5px;">
<div style="box-sizing: border-box; font-family: "Source Sans Pro", Helvetica, sans-serif; font-size: 16px; margin-bottom: 24px;">
</div>
<div style="box-sizing: border-box; font-family: "Source Sans Pro", Helvetica, sans-serif; font-size: 16px; margin-bottom: 24px;">
Now you know how to send out emails with Python. For those of you that like mini projects, you should go back and add additional error handling around the <strong style="box-sizing: border-box;">server.sendmail</strong> portion of the code in case something odd happens during the process, such as an SMTPAuthenticationError or SMTPConnectError. We could also beef up the error handling during the attachment of the file to catch other errors. Finally, we may want take those various lists of emails and create one normalized list that has removed duplicates. This is especially important if we are reading a list of email addresses from a file.</div>
<div style="box-sizing: border-box; font-family: "Source Sans Pro", Helvetica, sans-serif; font-size: 16px; margin-bottom: 24px;">
Also note that our from address is fake. We can spoof emails using Python and other programming languages, but that is very bad etiquette and possibly illegal depending on where you live. You have been warned! Use your knowledge wisely and enjoy Python for fun and profit!</div>
</h1>
Jeffyehttp://www.blogger.com/profile/07633630042179070371noreply@blogger.com0tag:blogger.com,1999:blog-5448019410873028021.post-83770242183975167212019-03-06T00:25:00.003-08:002019-03-06T00:25:48.755-08:00 Basic Text Analysis with Command Line Tools in Linux<h3 style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.5em; margin: 0px 0px 1em; outline: 0px; padding: 0px; vertical-align: baseline;">
Introduction</h3>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.167em; line-height: 1.5em; margin-bottom: 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;">
In the Linux and Unix operating systems, everything is treated as a file. Whenever possible, those files are stored as human- and machine-readable text files. As a result, Linux contains a large number of tools that are specialized for working with texts. Here we will use a few of these tools to explore a textual source.</div>
<h3 style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.5em; margin: 0px 0px 1em; outline: 0px; padding: 0px; vertical-align: baseline;">
Downloading a text</h3>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.167em; line-height: 1.5em; margin-bottom: 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;">
Our first task is to obtain a sample text to analyze. We will be working with a nineteenth-century book from the Internet Archive: <a href="http://archive.org/details/thestoriesmother05792gut" style="background: 0px 0px; border: 0px; color: #ff8a00; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;">Jane Andrews, <em style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">The Stories Mother Nature Told Her Children</em> (1888, 1894)</a>. Since this text is part of the Project Gutenberg collection, it was typed in by humans, rather than being scanned and OCRed by machine. This greatly reduces the number of textual errors we expect to find in it. To download the file, we will use the <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">wget</strong> command, which needs a URL. We don’t want to give the program the URL that we use to read the file in our browser, because if we do the file that we download will have HTML markup tags in it. Instead, we want the raw text file, which is located at</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 12px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<div class="syntaxhighlighter nogutter plain" id="highlighter_130908" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: 0px 0px; background-repeat: initial; background-size: initial; border: 0px; font-size: 1em !important; margin: 1em 0px !important; outline: 0px; overflow-x: auto !important; overflow-y: hidden !important; padding: 0px; position: relative !important; vertical-align: baseline; width: 459.984px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-collapse: collapse; border-radius: 0px !important; border-spacing: 0px; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 475px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 475px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><a href="http://archive.org/download/thestoriesmother05792gut/stmtn10.txt" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-decoration-line: none; top: auto !important; vertical-align: baseline !important; width: auto !important;">http://archive.org/download/thestoriesmother05792gut/stmtn10.txt</a></code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.167em; line-height: 1.5em; margin-bottom: 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;">
First we download the file with <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">wget</strong>, then we use the <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">ls</strong> command (list directory contents) to make sure that we have a local copy.</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 12px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<div class="syntaxhighlighter bash" id="highlighter_324179" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: 0px 0px; background-repeat: initial; background-size: initial; border: 0px; font-size: 1em !important; margin: 1em 0px !important; outline: 0px; overflow-x: auto !important; overflow-y: hidden !important; padding: 0px; position: relative !important; vertical-align: baseline; width: 459.984px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-collapse: collapse; border-radius: 0px !important; border-spacing: 0px; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 552px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="gutter" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(175, 175, 175) !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
1</div>
<div class="line number2 index1 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
2</div>
</td><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 523px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">wget http:</code><code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">//archive</code><code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">.org</code><code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">/download/thestoriesmother05792gut/stmtn10</code><code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">.txt</code></div>
<div class="line number2 index1 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="bash functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">ls</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<h3 style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.5em; margin: 0px 0px 1em; outline: 0px; padding: 0px; vertical-align: baseline;">
Our first view of the text</h3>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.167em; line-height: 1.5em; margin-bottom: 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;">
The Linux <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">file</strong> command allows us to confirm that we have downloaded a text file. When we type</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 12px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<div class="syntaxhighlighter bash" id="highlighter_594446" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: 0px 0px; background-repeat: initial; background-size: initial; border: 0px; font-size: 1em !important; margin: 1em 0px !important; outline: 0px; overflow-x: auto !important; overflow-y: hidden !important; padding: 0px; position: relative !important; vertical-align: baseline; width: 459.984px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-collapse: collapse; border-radius: 0px !important; border-spacing: 0px; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 459px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="gutter" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(175, 175, 175) !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
1</div>
</td><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 430px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="bash functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">file</code> <code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">stmtn10.txt</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.167em; line-height: 1.5em; margin-bottom: 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;">
the computer responds with</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 12px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<div class="syntaxhighlighter nogutter plain" id="highlighter_121000" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: 0px 0px; background-repeat: initial; background-size: initial; border: 0px; font-size: 1em !important; margin: 1em 0px !important; outline: 0px; overflow-x: auto !important; overflow-y: hidden !important; padding: 0px; position: relative !important; vertical-align: baseline; width: 459.984px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-collapse: collapse; border-radius: 0px !important; border-spacing: 0px; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 459px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 459px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">stmtn10.txt: C source, ASCII text, with CRLF line terminators</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.167em; line-height: 1.5em; margin-bottom: 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;">
The output of the <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">file</strong> command confirms that this is an ASCII text (which we expect), guesses that it is some code in the <em style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">C</em> programming language (which is incorrect) and tells us that the ends of the lines in the file are coded with both a carriage return and a line feed. This is standard for Windows computers. Linux and OS X expect the ends of lines in an ASCII text file to be coded only with a line feed. If we want to move text files between operating systems, this is one thing we have to pay attention to. Later we will learn one method to convert the line endings from CRLF to LF, but for now we can leave the file as it is.</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.167em; line-height: 1.5em; margin-bottom: 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;">
[UPDATE 2014. The <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">file</strong> command no longer mistakenly identifies the file as <em style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">C</em> code.]</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.167em; line-height: 1.5em; margin-bottom: 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;">
The <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">head</strong> and <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">tail</strong> commands show us the first few and last few lines of the file respectively.</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 12px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<div class="syntaxhighlighter bash" id="highlighter_965821" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: 0px 0px; background-repeat: initial; background-size: initial; border: 0px; font-size: 1em !important; margin: 1em 0px !important; outline: 0px; overflow-x: auto !important; overflow-y: hidden !important; padding: 0px; position: relative !important; vertical-align: baseline; width: 459.984px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-collapse: collapse; border-radius: 0px !important; border-spacing: 0px; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 459px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="gutter" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(175, 175, 175) !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
1</div>
</td><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 430px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="bash functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">head</code> <code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">stmtn10.txt</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 12px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<div class="syntaxhighlighter nogutter plain" id="highlighter_474557" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: 0px 0px; background-repeat: initial; background-size: initial; border: 0px; font-size: 1em !important; margin: 1em 0px !important; outline: 0px; overflow-x: auto !important; overflow-y: hidden !important; padding: 0px; position: relative !important; vertical-align: baseline; width: 459.984px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-collapse: collapse; border-radius: 0px !important; border-spacing: 0px; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 547px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 547px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">The Project Gutenberg EBook of The Stories Mother Nature Told Her Children</code></div>
<div class="line number2 index1 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">by Jane Andrews</code></div>
<div class="line number3 index2 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
</div>
<div class="line number4 index3 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">Copyright laws are changing all over the world. Be sure to check the</code></div>
<div class="line number5 index4 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">copyright laws for your country before downloading or redistributing</code></div>
<div class="line number6 index5 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">this or any other Project Gutenberg eBook.</code></div>
<div class="line number7 index6 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
</div>
<div class="line number8 index7 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">This header should be the first thing seen when viewing this Project</code></div>
<div class="line number9 index8 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">Gutenberg file. Please do not remove it. Do not change or edit the</code></div>
<div class="line number10 index9 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">header without written permission.</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 12px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<div class="syntaxhighlighter bash" id="highlighter_567801" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: 0px 0px; background-repeat: initial; background-size: initial; border: 0px; font-size: 1em !important; margin: 1em 0px !important; outline: 0px; overflow-x: auto !important; overflow-y: hidden !important; padding: 0px; position: relative !important; vertical-align: baseline; width: 459.984px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-collapse: collapse; border-radius: 0px !important; border-spacing: 0px; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 459px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="gutter" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(175, 175, 175) !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
1</div>
</td><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 430px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="bash functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">tail</code> <code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">stmtn10.txt</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 12px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<div class="syntaxhighlighter nogutter plain" id="highlighter_162255" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: 0px 0px; background-repeat: initial; background-size: initial; border: 0px; font-size: 1em !important; margin: 1em 0px !important; outline: 0px; overflow-x: auto !important; overflow-y: hidden !important; padding: 0px; position: relative !important; vertical-align: baseline; width: 459.984px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-collapse: collapse; border-radius: 0px !important; border-spacing: 0px; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 497px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 497px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">[Portions of this eBook's header and trailer may be reprinted only</code></div>
<div class="line number2 index1 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">when distributed free of all fees. Copyright (C) 2001, 2002 by</code></div>
<div class="line number3 index2 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">Michael S. Hart. Project Gutenberg is a TradeMark and may not be</code></div>
<div class="line number4 index3 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">used in any sales of Project Gutenberg eBooks or other materials be</code></div>
<div class="line number5 index4 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">they hardware or software or any other related product without</code></div>
<div class="line number6 index5 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">express permission.]</code></div>
<div class="line number7 index6 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
</div>
<div class="line number8 index7 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">*END THE SMALL PRINT! FOR PUBLIC DOMAIN EBOOKS*Ver.02/11/02*END*</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.167em; line-height: 1.5em; margin-bottom: 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;">
As we can see, the Project Gutenberg text includes some material in the header and footer which we will probably want to remove so we can analyze the source itself. Before modifying files, it is usually a good idea to make a copy of the original. We can do this with the <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">cp</strong> command, then use the <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">ls</strong> command to make sure we now have two copies of the file.</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 12px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<div class="syntaxhighlighter bash" id="highlighter_129141" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: 0px 0px; background-repeat: initial; background-size: initial; border: 0px; font-size: 1em !important; margin: 1em 0px !important; outline: 0px; overflow-x: auto !important; overflow-y: hidden !important; padding: 0px; position: relative !important; vertical-align: baseline; width: 459.984px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-collapse: collapse; border-radius: 0px !important; border-spacing: 0px; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 459px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="gutter" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(175, 175, 175) !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
1</div>
<div class="line number2 index1 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
2</div>
</td><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 430px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="bash functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">cp</code> <code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">stmtn10.txt stmtn10-backup.txt</code></div>
<div class="line number2 index1 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="bash functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">ls</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.167em; line-height: 1.5em; margin-bottom: 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;">
In order to have a look at the whole file, we can use the <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">less</strong> command. Once we run the following statement, we will be able to use the arrow keys to move up and down in the file one line at a time (or the j and k keys); the page up and page down keys to jump by pages (or the f and b keys); and the forward slash key to search for something (try typing <em style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">/giant</em>for example and then press the n key to see the next match). Press the q key to exit from viewing the file with <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">less</strong>.</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 12px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<div class="syntaxhighlighter bash" id="highlighter_179410" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: 0px 0px; background-repeat: initial; background-size: initial; border: 0px; font-size: 1em !important; margin: 1em 0px !important; outline: 0px; overflow-x: auto !important; overflow-y: hidden !important; padding: 0px; position: relative !important; vertical-align: baseline; width: 459.984px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-collapse: collapse; border-radius: 0px !important; border-spacing: 0px; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 459px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="gutter" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(175, 175, 175) !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
1</div>
</td><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 430px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="bash functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">less</code> <code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">-N stmtn10.txt</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<h3 style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.5em; margin: 0px 0px 1em; outline: 0px; padding: 0px; vertical-align: baseline;">
Trimming the header and footer</h3>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.167em; line-height: 1.5em; margin-bottom: 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;">
In the above case, we used the option -N to tell the <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">less</strong> command that we wanted it to include line numbers at the beginning of each line. (Try running the <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">less</strong> command without that option to see the difference.) Using the line numbers, we can see that the Project Gutenberg header runs from Line 1 to Line 40 inclusive, and that the footer runs from Line 2206 to Line 2525 inclusive. To create a copy of the text that has the header and footer removed, we can use the Linux stream editor <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">sed</strong>. We have to start with the footer, because if we removed the header first it would change the line numbering for the rest of the file.</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 12px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<div class="syntaxhighlighter bash" id="highlighter_336675" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: 0px 0px; background-repeat: initial; background-size: initial; border: 0px; font-size: 1em !important; margin: 1em 0px !important; outline: 0px; overflow-x: auto !important; overflow-y: hidden !important; padding: 0px; position: relative !important; vertical-align: baseline; width: 459.984px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-collapse: collapse; border-radius: 0px !important; border-spacing: 0px; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 459px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="gutter" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(175, 175, 175) !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
1</div>
</td><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 430px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="bash functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">sed</code> <code class="bash string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'2206,2525d'</code> <code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">stmtn10.txt > stmtn10-nofooter.txt</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.167em; line-height: 1.5em; margin-bottom: 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;">
This command tells <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">sed</strong> to delete all of the material between lines 2206 and 2525 and output the results to a file called <em style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">stmtn10-nofooter.txt</em>. You can use <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">less</strong> to confirm that this new file still contains the Project Gutenberg header but not the footer. We can now trim the header from this file to create another version with no header or footer. We will call this file <em style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">stmtn10-trimmed.txt</em>. Use <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">less</strong> to confirm that it looks the way it should. While you are using <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">less</strong> to view a file, you can use the g key to jump to the top of the file and the shift-g to jump to the bottom.</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 12px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<div class="syntaxhighlighter bash" id="highlighter_479946" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: 0px 0px; background-repeat: initial; background-size: initial; border: 0px; font-size: 1em !important; margin: 1em 0px !important; outline: 0px; overflow-x: auto !important; overflow-y: hidden !important; padding: 0px; position: relative !important; vertical-align: baseline; width: 459.984px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-collapse: collapse; border-radius: 0px !important; border-spacing: 0px; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 459px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="gutter" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(175, 175, 175) !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
1</div>
</td><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 430px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="bash functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">sed</code> <code class="bash string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'1,40d'</code> <code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">stmtn10-nofooter.txt > stmtn10-trimmed.txt</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.167em; line-height: 1.5em; margin-bottom: 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;">
Use the <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">ls</strong> command to confirm that you now have four files, <em style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">stmtn10-backup.txt</em>, <em style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">stmtn10-nofooter.txt</em>, <em style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">stmtn10-trimmed.txt</em> and <em style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">stmtn10.txt</em>.</div>
<h3 style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.5em; margin: 0px 0px 1em; outline: 0px; padding: 0px; vertical-align: baseline;">
A few basic statistics</h3>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.167em; line-height: 1.5em; margin-bottom: 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;">
We can use the <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">wc</strong> command to find out how many lines (-l option) and how many characters (-m) our file has. Running the following shows us that the answer is 2165 lines and 121038 characters.</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 12px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<div class="syntaxhighlighter bash" id="highlighter_697347" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: 0px 0px; background-repeat: initial; background-size: initial; border: 0px; font-size: 1em !important; margin: 1em 0px !important; outline: 0px; overflow-x: auto !important; overflow-y: hidden !important; padding: 0px; position: relative !important; vertical-align: baseline; width: 459.984px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-collapse: collapse; border-radius: 0px !important; border-spacing: 0px; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 459px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="gutter" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(175, 175, 175) !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
1</div>
<div class="line number2 index1 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
2</div>
</td><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 430px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="bash functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">wc</code> <code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">-l stmtn10-trimmed.txt</code></div>
<div class="line number2 index1 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="bash functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">wc</code> <code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">-m stmtn10-trimmed.txt</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<h3 style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.5em; margin: 0px 0px 1em; outline: 0px; padding: 0px; vertical-align: baseline;">
Finding patterns</h3>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.167em; line-height: 1.5em; margin-bottom: 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;">
Linux has a very powerful pattern-matching command called <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">grep</strong>, which we will use frequently. At its most basic, <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">grep</strong> returns lines in a file which match a pattern. The command below shows us lines which contain the word <em style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">giant</em>. The -n option asks grep to include line numbers. Note that this pattern is case sensitive, and will not match <em style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Giant</em>.</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 12px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<div class="syntaxhighlighter bash" id="highlighter_58802" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: 0px 0px; background-repeat: initial; background-size: initial; border: 0px; font-size: 1em !important; margin: 1em 0px !important; outline: 0px; overflow-x: auto !important; overflow-y: hidden !important; padding: 0px; position: relative !important; vertical-align: baseline; width: 459.984px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-collapse: collapse; border-radius: 0px !important; border-spacing: 0px; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 459px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="gutter" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(175, 175, 175) !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
1</div>
</td><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 430px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="bash functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">grep</code> <code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">-n </code><code class="bash string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">"giant"</code> <code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">stmtn10-trimmed.txt</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 12px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<div class="syntaxhighlighter nogutter plain" id="highlighter_175148" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: 0px 0px; background-repeat: initial; background-size: initial; border: 0px; font-size: 1em !important; margin: 1em 0px !important; outline: 0px; overflow-x: auto !important; overflow-y: hidden !important; padding: 0px; position: relative !important; vertical-align: baseline; width: 459.984px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-collapse: collapse; border-radius: 0px !important; border-spacing: 0px; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 569px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 569px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">1115:Do you believe in giants? No, do you say? Well, listen to my story,</code></div>
<div class="line number2 index1 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">1138:to admit that to do it needed a giant's strength, and so they deserve</code></div>
<div class="line number3 index2 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">1214:giants think of doing. We have not long to wait before we shall see, and</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.167em; line-height: 1.5em; margin-bottom: 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;">
What if we wanted to find both capitalized and lowercase versions of the word? In the following command, we tell <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">grep</strong> that we want to use an extended set of possible patterns (the -E option) and show us line numbers (the -n option). The pattern itself says to match something that starts either with a capital <em style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">G</em> or a lowercase <em style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">g</em>, followed by lowercase <em style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">iant</em>.</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 12px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<div class="syntaxhighlighter bash" id="highlighter_672277" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: 0px 0px; background-repeat: initial; background-size: initial; border: 0px; font-size: 1em !important; margin: 1em 0px !important; outline: 0px; overflow-x: auto !important; overflow-y: hidden !important; padding: 0px; position: relative !important; vertical-align: baseline; width: 459.984px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-collapse: collapse; border-radius: 0px !important; border-spacing: 0px; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 459px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="gutter" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(175, 175, 175) !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
1</div>
</td><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 430px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="bash functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">grep</code> <code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">-E -n </code><code class="bash string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">"(G|g)iant"</code> <code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">stmtn10-trimmed.txt</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<h3 style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.5em; margin: 0px 0px 1em; outline: 0px; padding: 0px; vertical-align: baseline;">
Creating a standardized version of the text</h3>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.167em; line-height: 1.5em; margin-bottom: 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;">
When we are analyzing the words in a text, it is usually convenient to create a standardized version that eliminates whitespace and punctuation and converts all characters to lowercase. We will use the <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">tr</strong> command to translate and delete characters of our trimmed text, to create a standardized version. First we delete all punctuation, using the -d option and a special pattern which matches punctuation characters. Note that in this case the <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">tr</strong> command requires that we use the redirection operators to specify both the input file (<strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><</strong>) and the output file (<strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">></strong>). You can use the <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">less</strong> command to confirm that the punctuation has been removed.</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 12px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<div class="syntaxhighlighter bash" id="highlighter_781331" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: 0px 0px; background-repeat: initial; background-size: initial; border: 0px; font-size: 1em !important; margin: 1em 0px !important; outline: 0px; overflow-x: auto !important; overflow-y: hidden !important; padding: 0px; position: relative !important; vertical-align: baseline; width: 459.984px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-collapse: collapse; border-radius: 0px !important; border-spacing: 0px; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 480px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="gutter" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(175, 175, 175) !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
1</div>
</td><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 451px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="bash functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">tr</code> <code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">-d [:punct:] < stmtn10-trimmed.txt > stmtn10-nopunct.txt</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.167em; line-height: 1.5em; margin-bottom: 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;">
The next step is to use <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">tr</strong> to convert all characters to lowercase. Once again, use the <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">less</strong> command to confirm that the changes have been made.</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 12px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<div class="syntaxhighlighter bash" id="highlighter_855121" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: 0px 0px; background-repeat: initial; background-size: initial; border: 0px; font-size: 1em !important; margin: 1em 0px !important; outline: 0px; overflow-x: auto !important; overflow-y: hidden !important; padding: 0px; position: relative !important; vertical-align: baseline; width: 459.984px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-collapse: collapse; border-radius: 0px !important; border-spacing: 0px; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 545px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="gutter" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(175, 175, 175) !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
1</div>
</td><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 516px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="bash functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">tr</code> <code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">[:upper:] [:lower:] < stmtn10-nopunct.txt > stmtn10-lowercase.txt</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.167em; line-height: 1.5em; margin-bottom: 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;">
Finally, we will use the <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">tr</strong> command to convert all of the Windows CRLF line endings to the LF line endings that characterize Linux and OS X files. If we don’t do this, the spurious carriage return characters will interfere with our frequency counts.</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 12px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<div class="syntaxhighlighter bash" id="highlighter_572536" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: 0px 0px; background-repeat: initial; background-size: initial; border: 0px; font-size: 1em !important; margin: 1em 0px !important; outline: 0px; overflow-x: auto !important; overflow-y: hidden !important; padding: 0px; position: relative !important; vertical-align: baseline; width: 459.984px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-collapse: collapse; border-radius: 0px !important; border-spacing: 0px; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 487px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="gutter" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(175, 175, 175) !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
1</div>
</td><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 458px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="bash functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">tr</code> <code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">-d </code><code class="bash string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'\r'</code> <code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">< stmtn10-lowercase.txt > stmtn10-lowercaself.txt</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<h3 style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.5em; margin: 0px 0px 1em; outline: 0px; padding: 0px; vertical-align: baseline;">
Counting word frequencies</h3>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.167em; line-height: 1.5em; margin-bottom: 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;">
The first step in counting word frequencies is use the <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">tr</strong> command to translate each blank space into an end-of-line character (or <em style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">newline</em>, represented by \n). This gives us a file where each word is on its own line. Confirm this using the <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">less</strong> or <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">head</strong> command on <em style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">stmtn10-oneword.txt</em>.</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 12px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<div class="syntaxhighlighter bash" id="highlighter_986430" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: 0px 0px; background-repeat: initial; background-size: initial; border: 0px; font-size: 1em !important; margin: 1em 0px !important; outline: 0px; overflow-x: auto !important; overflow-y: hidden !important; padding: 0px; position: relative !important; vertical-align: baseline; width: 459.984px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-collapse: collapse; border-radius: 0px !important; border-spacing: 0px; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 480px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="gutter" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(175, 175, 175) !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
1</div>
</td><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 451px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="bash functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">tr</code> <code class="bash string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">' '</code> <code class="bash string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'\n'</code> <code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">< stmtn10-lowercaself.txt > stmtn10-oneword.txt</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.167em; line-height: 1.5em; margin-bottom: 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;">
The next step is to sort that file so the words are in alphabetical order, and so that if a given word appears a number of times, these are listed one after another. Once again, use the <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">less</strong> command to look at the resulting file. Note that there are many blank lines at the beginning of this file, but if you page down you start to see the words: a lot of copies of <em style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">a</em>, followed by one copy of <em style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">abashed</em>, one of <em style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">ability</em>, and so on.</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 12px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<div class="syntaxhighlighter bash" id="highlighter_796053" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: 0px 0px; background-repeat: initial; background-size: initial; border: 0px; font-size: 1em !important; margin: 1em 0px !important; outline: 0px; overflow-x: auto !important; overflow-y: hidden !important; padding: 0px; position: relative !important; vertical-align: baseline; width: 459.984px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-collapse: collapse; border-radius: 0px !important; border-spacing: 0px; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 459px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="gutter" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(175, 175, 175) !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
1</div>
</td><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 430px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="bash functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">sort</code> <code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">stmtn10-oneword.txt > stmtn10-onewordsort.txt</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.167em; line-height: 1.5em; margin-bottom: 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;">
Now we use the <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">uniq</strong> command with the -c option to count the number of repetitions of each line. This will give us a file where the words are listed alphabetically, each preceded by its frequency. We use the <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">head</strong>command to look at the first few lines of our word frequency file.</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 12px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<div class="syntaxhighlighter bash" id="highlighter_634041" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: 0px 0px; background-repeat: initial; background-size: initial; border: 0px; font-size: 1em !important; margin: 1em 0px !important; outline: 0px; overflow-x: auto !important; overflow-y: hidden !important; padding: 0px; position: relative !important; vertical-align: baseline; width: 459.984px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-collapse: collapse; border-radius: 0px !important; border-spacing: 0px; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 459px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="gutter" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(175, 175, 175) !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
1</div>
<div class="line number2 index1 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
2</div>
</td><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 430px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="bash functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">uniq</code> <code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">-c stmtn10-onewordsort.txt > stmtn10-wordfreq.txt</code></div>
<div class="line number2 index1 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="bash functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">head</code> <code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">stmtn10-wordfreq.txt</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 12px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<div class="syntaxhighlighter nogutter plain" id="highlighter_642226" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: 0px 0px; background-repeat: initial; background-size: initial; border: 0px; font-size: 1em !important; margin: 1em 0px !important; outline: 0px; overflow-x: auto !important; overflow-y: hidden !important; padding: 0px; position: relative !important; vertical-align: baseline; width: 459.984px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-collapse: collapse; border-radius: 0px !important; border-spacing: 0px; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 459px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 459px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">358</code></div>
<div class="line number2 index1 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">1 1861</code></div>
<div class="line number3 index2 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">1 1865</code></div>
<div class="line number4 index3 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">1 1888</code></div>
<div class="line number5 index4 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">1 1894</code></div>
<div class="line number6 index5 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">426 a</code></div>
<div class="line number7 index6 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">1 abashed</code></div>
<div class="line number8 index7 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">1 ability</code></div>
<div class="line number9 index8 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">4 able</code></div>
<div class="line number10 index9 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="plain spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="plain plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">44 about</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<h3 style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.5em; margin: 0px 0px 1em; outline: 0px; padding: 0px; vertical-align: baseline;">
Pipelines</h3>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.167em; line-height: 1.5em; margin-bottom: 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;">
When using the <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">tr</strong> command, we saw that it is possible to tell a Linux command where it is getting its input from and where it is sending its output to. It is also possible to arrange commands in a <em style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">pipeline</em> so that the output of one stage feeds into the input of the next. To do this, we use the <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">pipe</strong> operator (<strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">|</strong>). For example, we can create a pipeline to go from our lowercase file (with Linux LF endings) to word frequencies directly, as shown below. This way we don’t create a bunch of intermediate files if we don’t want to. You can use the <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">less</strong> command to confirm that <em style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">stmtn10-wordfreq.txt</em> and <em style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">stmtn10-wordfreq2.txt</em> look the same.</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 12px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<div class="syntaxhighlighter bash" id="highlighter_151268" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: 0px 0px; background-repeat: initial; background-size: initial; border: 0px; font-size: 1em !important; margin: 1em 0px !important; outline: 0px; overflow-x: auto !important; overflow-y: hidden !important; padding: 0px; position: relative !important; vertical-align: baseline; width: 459.984px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-collapse: collapse; border-radius: 0px !important; border-spacing: 0px; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 617px;"><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="gutter" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(175, 175, 175) !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(108, 226, 108) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
1</div>
</td><td class="code" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 588px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="bash functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">tr</code> <code class="bash string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">' '</code> <code class="bash string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'\n'</code> <code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">< stmtn10-lowercaself.txt | </code><code class="bash functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">sort</code> <code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">| </code><code class="bash functions" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(255, 20, 147) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">uniq</code> <code class="bash plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">-c > stmtn10-wordfreq2.txt</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<div style="background: 0px 0px rgb(255, 255, 255); border: 0px; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 1.167em; line-height: 1.5em; margin-bottom: 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;">
When we use <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">less</strong> to look at one of our word frequency files, we can search for a particular term with the forward slash. Trying <em style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">/giant</em>, for example, shows us that there are sixteen instances of the word <em style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">giants</em> in our text. Spend some time exploring the original text and the word frequency file with <strong style="background: 0px 0px; border: 0px; font-size: 14.004px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">less</strong>.</div>
<div class="sharedaddy sd-like-enabled" id="jp-post-flair" style="background: 0px 0px rgb(255, 255, 255); border: 0px; clear: both; color: #516064; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif; font-size: 12px; margin: 0px; outline: 0px; padding: 0.5em 0px 0px; vertical-align: baseline;">
<div class="jp-relatedposts" id="jp-relatedposts" style="background: 0px 0px; border: 0px; clear: both; margin: 1em 0px; outline: 0px; padding: 1em 0px 0px; position: relative; vertical-align: baseline;">
</div>
</div>
Jeffyehttp://www.blogger.com/profile/07633630042179070371noreply@blogger.com0tag:blogger.com,1999:blog-5448019410873028021.post-71558788041883277202015-10-07T08:28:00.003-07:002015-10-07T08:28:40.434-07:00vim plugins to usePlugin 'gmarik/Vundle.vim'<br />
Plugin 'jedi-vim'<br />
"<br />
Plugin 'tpope/vim-fugitive'<br />
Plugin 'L9'<br />
Plugin 'The-NERD-tree'<br />
" Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}<br />
" " Avoid a name conflict with L9<br />
" Plugin 'user/L9', {'name': 'newL9'}<br />
<br />
Plugin 'The-NERD-Commenter'<br />
Plugin 'grep.vim'<br />
Plugin 'FuzzyFinder'<br />
Plugin 'matchit.zip'<br />
" " Plugin 'SuperTab'<br />
Plugin 'surround.vim'<br />
Plugin 'Command-T'<br />
Plugin 'pydoc.vim'<br />
Plugin 'pep8'<br />
Plugin 'Yggdroot/indentLine'<br />
<br />
Plugin 'godlygeek/tabular'<br />
Plugin 'plasticboy/vim-markdown'<br />
<div>
<br /></div>
Jeffyehttp://www.blogger.com/profile/07633630042179070371noreply@blogger.com1tag:blogger.com,1999:blog-5448019410873028021.post-20926982534638087222015-08-23T21:02:00.001-07:002015-08-23T21:02:13.072-07:00How-To Guide for Descriptors<h1 class="title" style="text-align: center;">
How-To Guide for Descriptors</h1>
<table class="docinfo" frame="void" rules="none" style="margin: 2em 4em;"><colgroup><col class="docinfo-name"></col><col class="docinfo-content"></col></colgroup><tbody valign="top">
<tr><td class="docinfo-name" style="font-weight: bold; padding-left: 0.5em; padding-right: 0.5em; text-align: right; vertical-align: baseline;">Author: </td><td style="padding-left: 0.5em; padding-right: 0.5em; vertical-align: baseline;">Raymond Hettinger</td></tr>
<tr><td class="docinfo-name" style="font-weight: bold; padding-left: 0.5em; padding-right: 0.5em; text-align: right; vertical-align: baseline;">Contact: </td><td style="padding-left: 0.5em; padding-right: 0.5em; vertical-align: baseline;"><python at rcn dot com></td></tr>
<tr><td class="docinfo-name" style="font-weight: bold; padding-left: 0.5em; padding-right: 0.5em; text-align: right; vertical-align: baseline;">Copyright: </td><td style="padding-left: 0.5em; padding-right: 0.5em; vertical-align: baseline;">Copyright (c) 2003, 2004 Python Software Foundation. All rights reserved.</td></tr>
</tbody></table>
<div class="contents topic" id="contents" name="contents" style="margin: 2em;">
<div class="topic-title" style="font-weight: bold;">
From Link: http://users.rcn.com/python/download/Descriptor.htm</div>
<div class="topic-title" style="font-weight: bold;">
Contents</div>
<ul class="simple" style="margin-bottom: 1em;">
<li id="id1" name="id1"><a class="reference" href="http://users.rcn.com/python/download/Descriptor.htm#abstract">Abstract</a></li>
<li id="id2" name="id2"><a class="reference" href="http://users.rcn.com/python/download/Descriptor.htm#definition-and-introduction">Definition and Introduction</a></li>
<li id="id3" name="id3"><a class="reference" href="http://users.rcn.com/python/download/Descriptor.htm#descriptor-protocol">Descriptor Protocol</a></li>
<li id="id4" name="id4"><a class="reference" href="http://users.rcn.com/python/download/Descriptor.htm#invoking-descriptors">Invoking Descriptors</a></li>
<li id="id5" name="id5"><a class="reference" href="http://users.rcn.com/python/download/Descriptor.htm#descriptor-example">Descriptor Example</a></li>
<li id="id6" name="id6"><a class="reference" href="http://users.rcn.com/python/download/Descriptor.htm#properties">Properties</a></li>
<li id="id7" name="id7"><a class="reference" href="http://users.rcn.com/python/download/Descriptor.htm#functions-and-methods">Functions and Methods</a></li>
<li id="id8" name="id8"><a class="reference" href="http://users.rcn.com/python/download/Descriptor.htm#static-methods-and-class-methods">Static Methods and Class Methods</a></li>
</ul>
</div>
<div class="section" id="abstract" name="abstract">
<h1>
<a class="toc-backref" href="http://users.rcn.com/python/download/Descriptor.htm#id1" style="color: black; text-decoration: none;">Abstract</a></h1>
Defines descriptors, summarizes the protocol, and shows how descriptors are called. Examines a custom descriptor and several built-in python descriptors including functions, properties, static methods, and class methods. Shows how each works by giving a pure Python equivalent and a sample application.<br />
Learning about descriptors not only provides access to a larger toolset, it creates a deeper understanding of how Python works and an appreciation for the elegance of its design.</div>
<div class="section" id="definition-and-introduction" name="definition-and-introduction">
<h1>
<a class="toc-backref" href="http://users.rcn.com/python/download/Descriptor.htm#id2" style="color: black; text-decoration: none;">Definition and Introduction</a></h1>
In general, a descriptor is an object attribute with "binding behavior", one whose attribute access has been overridden by methods in the descriptor protocol. Those methods are <tt class="literal">__get__</tt>, <tt class="literal">__set__</tt>, and <tt class="literal">__delete__</tt>. If any of those methods are defined for an object, it is said to be a descriptor.<br />
The default behavior for attribute access is to get, set, or delete the attribute from an object's dictionary. For instance, <tt class="literal">a.x</tt> has a lookup chain starting with <tt class="literal">a.__dict__['x']</tt>, then <tt class="literal">type(a).__dict__['x']</tt>, and continuing through the base classes of <tt class="literal">type(a)</tt>excluding metaclasses. If the looked-up value is an object defining one of the descriptor methods, then Python may override the default behavior and invoke the descriptor method instead. Where this occurs in the precedence chain depends on which descriptor methods were defined. Note that descriptors are only invoked for new style objects or classes (a class is new style if it inherits from<tt class="literal">object</tt> or <tt class="literal">type</tt>).<br />
Descriptors are a powerful, general purpose protocol. They are the mechanism behind properties, methods, static methods, class methods, and <tt class="literal">super()</tt>. They are used used throughout Python itself to implement the new style classes introduced in version 2.2. Descriptors simplify the underlying C-code and offer a flexible set of new tools for everyday Python programs.</div>
<div class="section" id="descriptor-protocol" name="descriptor-protocol">
<h1>
<a class="toc-backref" href="http://users.rcn.com/python/download/Descriptor.htm#id3" style="color: black; text-decoration: none;">Descriptor Protocol</a></h1>
<tt class="literal">descr.__get__(self, obj, type=None) --> value</tt><br />
<tt class="literal">descr.__set__(self, obj, value) --> None</tt><br />
<tt class="literal">descr.__delete__(self, obj) --> None</tt><br />
That is all there is to it. Define any of these methods and an object is considered a descriptor and can override default behavior upon being looked up as an attribute.<br />
If an object defines both <tt class="literal">__get__</tt> and <tt class="literal">__set__</tt>, it is considered a data descriptor. Descriptors that only define <tt class="literal">__get__</tt> are called non-data descriptors (they are typically used for methods but other uses are possible).<br />
Data and non-data descriptors differ in how overrides are calculated with respect to entries in an instance's dictionary. If an instance's dictionary has an entry with the same name as a data descriptor, the data descriptor takes precedence. If an instance's dictionary has an entry with the same name as a non-data descriptor, the dictionary entry takes precedence.<br />
To make a read-only data descriptor, define both <tt class="literal">__get__</tt> and <tt class="literal">__set__</tt> with the <tt class="literal">__set__</tt> raising an <tt class="literal">AttributeError</tt> when called. Defining the <tt class="literal">__set__</tt> method with an exception raising placeholder is enough to make it a data descriptor.</div>
<div class="section" id="invoking-descriptors" name="invoking-descriptors">
<h1>
<a class="toc-backref" href="http://users.rcn.com/python/download/Descriptor.htm#id4" style="color: black; text-decoration: none;">Invoking Descriptors</a></h1>
A descriptor can be called directly by its method name. For example, <tt class="literal">d.__get__(obj)</tt>.<br />
Alternatively, it is more common for a descriptor to be invoked automatically upon attribute access. For example, <tt class="literal">obj.d</tt> looks up <tt class="literal">d</tt>in the dictionary of <tt class="literal">obj</tt>. If <tt class="literal">d</tt> defines the method <tt class="literal">__get__</tt>, then <tt class="literal">d.__get__(obj)</tt> is invoked according to the precedence rules listed below.<br />
The details of invocation depend on whether <tt class="literal">obj</tt> is an object or a class. Either way, descriptors only work for new style objects and classes. A class is new style if it is a subclass of <tt class="literal">object</tt>.<br />
For objects, the machinery is in <tt class="literal">object.__getattribute__</tt> which transforms <tt class="literal">b.x</tt> into <tt class="literal">type(b).__dict__['x'].__get__(b, type(b))</tt>. The implementation works through a precedence chain that gives data descriptors priority over instance variables, instance variables priority over non-data descriptors, and assigns lowest priority to <tt class="literal">__getattr__</tt> if provided. The full C implementation can be found in <tt class="literal">PyObject_GenericGetAttr()</tt> in <tt class="literal">Objects/object.c</tt>.<br />
For classes, the machinery is in <tt class="literal">type.__getattribute__</tt> which transforms <tt class="literal">B.x</tt> into <tt class="literal">B.__dict__['x'].__get__(None, B)</tt>. In pure Python, it looks like:<br />
<pre class="literal-block" style="background-color: #eeeeee; margin-left: 2em; margin-right: 2em;">def __getattribute__(self, key):
"Emulate type_getattro() in Objects/typeobject.c"
v = object.__getattribute__(self, key)
if hasattr(v, '__get__'):
return v.__get__(None, self)
return v</pre>
The important points to remember are:<br />
<ul class="simple" style="margin-bottom: 1em;">
<li>descriptors are invoked by the <tt class="literal">__getattribute__</tt> method</li>
<li>overriding <tt class="literal">__getattribute__</tt> prevents automatic descriptor calls</li>
<li><tt class="literal">__getattribute__</tt> is only available with new style classes and objects</li>
<li><tt class="literal">object.__getattribute__</tt> and <tt class="literal">type.__getattribute__</tt> make different calls to <tt class="literal">__get__</tt>.</li>
<li>data descriptors always override instance dictionaries.</li>
<li>non-data descriptors may be overridden by instance dictionaries.</li>
</ul>
The object returned by <tt class="literal">super()</tt> also has a custom <tt class="literal">__getattribute__</tt> method for invoking descriptors. The call <tt class="literal">super(B, obj).m()</tt> searches <tt class="literal">obj.__class__.__mro__</tt> for the base class <tt class="literal">A</tt> immediately following <tt class="literal">B</tt> and then returns<tt class="literal">A.__dict__['m'].__get__(obj, A)</tt>. If not a descriptor, <tt class="literal">m</tt> is returned unchanged. If not in the dictionary, <tt class="literal">m</tt> reverts to a search using<tt class="literal">object.__getattribute__</tt>.<br />
Note, in Python 2.2, <tt class="literal">super(B, obj).m()</tt> would only invoke <tt class="literal">__get__</tt> if <tt class="literal">m</tt> was a data descriptor. In Python 2.3, non-data descriptors also get invoked unless an old-style class is involved. The implementation details are in <tt class="literal">super_getattro()</tt> in<tt class="literal">Objects/typeobject.c</tt> and a pure Python equivalent can be found in <a class="reference" href="http://www.python.org/2.2.3/descrintro.html#cooperation">Guido's Tutorial</a>.<br />
The details above show that the mechanism for descriptors is embedded in the <tt class="literal">__getattribute__()</tt> methods for <tt class="literal">object</tt>, <tt class="literal">type</tt>, and<tt class="literal">super</tt>. Classes inherit this machinery when they derive from <tt class="literal">object</tt> or if they have a meta-class providing similar functionality. Likewise, classes can turn-off descriptor invocation by overriding <tt class="literal">__getattribute__()</tt>.</div>
<div class="section" id="descriptor-example" name="descriptor-example">
<h1>
<a class="toc-backref" href="http://users.rcn.com/python/download/Descriptor.htm#id5" style="color: black; text-decoration: none;">Descriptor Example</a></h1>
The following code creates a class whose objects are data descriptors which print a message for each get or set. Overriding<tt class="literal">__getattribute__</tt> is alternate approach that could do this for every attribute. However, this descriptor is useful for monitoring just a few chosen attributes:<br />
<pre class="literal-block" style="background-color: #eeeeee; margin-left: 2em; margin-right: 2em;">class RevealAccess(object):
"""A data descriptor that sets and returns values
normally and prints a message logging their access.
"""
def __init__(self, initval=None, name='var'):
self.val = initval
self.name = name
def __get__(self, obj, objtype):
print 'Retrieving', self.name
return self.val
def __set__(self, obj, val):
print 'Updating' , self.name
self.val = val
>>> class MyClass(object):
x = RevealAccess(10, 'var "x"')
y = 5
>>> m = MyClass()
>>> m.x
Retrieving var "x"
10
>>> m.x = 20
Updating var "x"
>>> m.x
Retrieving var "x"
20
>>> m.y
5</pre>
The protocol is simple and offers exciting possibilities. Several use cases are so common that they have been packaged into individual function calls. Properties, bound and unbound methods, static methods, and class methods are all based on the descriptor protocol.</div>
<div class="section" id="properties" name="properties">
<h1>
<a class="toc-backref" href="http://users.rcn.com/python/download/Descriptor.htm#id6" style="color: black; text-decoration: none;">Properties</a></h1>
Calling <tt class="literal">property()</tt> is a succinct way of building a data descriptor that triggers function calls upon access to an attribute. Its signature is:<br />
<pre class="literal-block" style="background-color: #eeeeee; margin-left: 2em; margin-right: 2em;">property(fget=None, fset=None, fdel=None, doc=None) -> property attribute</pre>
The documentation shows a typical use to define a managed attribute <tt class="literal">x</tt>:<br />
<pre class="literal-block" style="background-color: #eeeeee; margin-left: 2em; margin-right: 2em;">class C(object):
def getx(self): return self.__x
def setx(self, value): self.__x = value
def delx(self): del self.__x
x = property(getx, setx, delx, "I'm the 'x' property.")</pre>
To see how <tt class="literal">property()</tt> is implemented in terms of the descriptor protocol, here is a pure Python equivalent:<br />
<pre class="literal-block" style="background-color: #eeeeee; margin-left: 2em; margin-right: 2em;">class Property(object):
"Emulate PyProperty_Type() in Objects/descrobject.c"
def __init__(self, fget=None, fset=None, fdel=None, doc=None):
self.fget = fget
self.fset = fset
self.fdel = fdel
self.__doc__ = doc
def __get__(self, obj, objtype=None):
if obj is None:
return self
if self.fget is None:
raise AttributeError, "unreadable attribute"
return self.fget(obj)
def __set__(self, obj, value):
if self.fset is None:
raise AttributeError, "can't set attribute"
self.fset(obj, value)
def __delete__(self, obj):
if self.fdel is None:
raise AttributeError, "can't delete attribute"
self.fdel(obj)</pre>
The <tt class="literal">property()</tt> builtin helps whenever a user interface has granted attribute access and then subsequent changes require the intervention of a method.<br />
For instance, a spreadsheet class may grant access to a cell value through <tt class="literal">Cell('b10').value</tt>. Subsequent improvements to the program require the cell to be recalculated on every access; however, the programmer does not want to affect existing client code accessing the attribute directly. The solution is to wrap access to the value attribute in a <tt class="literal">property()</tt> data descriptor:<br />
<pre class="literal-block" style="background-color: #eeeeee; margin-left: 2em; margin-right: 2em;">class Cell(object):
. . .
def getvalue(self, obj):
"Recalculate cell before returning value"
self.recalc()
return obj._value
value = property(getvalue)</pre>
</div>
<div class="section" id="functions-and-methods" name="functions-and-methods">
<h1>
<a class="toc-backref" href="http://users.rcn.com/python/download/Descriptor.htm#id7" style="color: black; text-decoration: none;">Functions and Methods</a></h1>
Python's object oriented features are built upon a function based environment. Using non-data descriptors, the two are merged seamlessly.<br />
Class dictionaries store methods as functions. In a class definition, methods are written using <tt class="literal">def</tt> and <tt class="literal">lambda</tt>, the usual tools for creating functions. The only difference from regular functions is that the first argument is reserved for the object instance. By Python convention, the instance reference is called <tt class="literal">self</tt> but may be called <tt class="literal">this</tt> or any other variable name.<br />
To support method calls, functions include the <tt class="literal">__get__</tt> method for binding methods during attribute access. This means that all functions are non-data descriptors which return bound or unbound methods depending whether they are invoked from an object or a class. In pure python, it works like this:<br />
<pre class="literal-block" style="background-color: #eeeeee; margin-left: 2em; margin-right: 2em;">class Function(object):
. . .
def __get__(self, obj, objtype=None):
"Simulate func_descr_get() in Objects/funcobject.c"
return types.MethodType(self, obj, objtype)</pre>
Running the interpreter shows how the function descriptor works in practice:<br />
<pre class="literal-block" style="background-color: #eeeeee; margin-left: 2em; margin-right: 2em;">>>> class D(object):
def f(self, x):
return x
>>> d = D()
>>> D.__dict__['f'] # Stored internally as a function
<function f at 0x00C45070>
>>> D.f # Get from a class becomes an unbound method
<unbound method D.f>
>>> d.f # Get from an instance becomes a bound method
<bound method D.f of <__main__.D object at 0x00B18C90>></pre>
The output suggests that bound and unbound methods are two different types. While they could have been implemented that way, the actual C implemention of <tt class="literal">PyMethod_Type</tt> in <tt class="literal">Objects/classobject.c</tt> is a single object with two different representations depending on whether the <tt class="literal">im_self</tt> field is set or is <tt class="literal">NULL</tt> (the C equivalent of <tt class="literal">None</tt>).<br />
Likewise, the effects of calling a method object depend on the <tt class="literal">im_self</tt> field. If set (meaning bound), the original function (stored in the <tt class="literal">im_func</tt> field) is called as expected with the first argument set to the instance. If unbound, all of the arguments are passed unchanged to the original function. The actual C implementation of <tt class="literal">instancemethod_call()</tt> is only slightly more complex in that it includes some type checking.</div>
<div class="section" id="static-methods-and-class-methods" name="static-methods-and-class-methods">
<h1>
<a class="toc-backref" href="http://users.rcn.com/python/download/Descriptor.htm#id8" style="color: black; text-decoration: none;">Static Methods and Class Methods</a></h1>
Non-data descriptors provide a simple mechanism for variations on the usual patterns of binding functions into methods.<br />
To recap, functions have a <tt class="literal">__get__</tt> method so that they can be converted to a method when accessed as attributes. The non-data descriptor transforms a <tt class="literal">obj.f(*args)</tt> call into <tt class="literal">f(obj, *args)</tt>. Calling <tt class="literal">klass.f(*args)</tt> becomes <tt class="literal">f(*args)</tt>.<br />
This chart summarizes the binding and its two most useful variants:<br />
<blockquote>
<table frame="border" rules="all" style="margin-bottom: 0.5em; margin-top: 0.5em;"><colgroup><col colwidth="24%"></col><col colwidth="20%"></col><col colwidth="31%"></col><col colwidth="25%"></col></colgroup><thead valign="bottom">
<tr><th style="padding-left: 0.5em; padding-right: 0.5em; vertical-align: baseline;">Transformation</th><th style="padding-left: 0.5em; padding-right: 0.5em; vertical-align: baseline;"> </th><th style="padding-left: 0.5em; padding-right: 0.5em; vertical-align: baseline;">Called from an Object</th><th style="padding-left: 0.5em; padding-right: 0.5em; vertical-align: baseline;">Called from a Class</th></tr>
</thead><tbody valign="top">
<tr><td rowspan="3" style="padding-left: 0.5em; padding-right: 0.5em; vertical-align: baseline;">Descriptor</td><td style="padding-left: 0.5em; padding-right: 0.5em; vertical-align: baseline;">function</td><td style="padding-left: 0.5em; padding-right: 0.5em; vertical-align: baseline;">f(obj, *args)</td><td style="padding-left: 0.5em; padding-right: 0.5em; vertical-align: baseline;">f(*args)</td></tr>
<tr><td style="padding-left: 0.5em; padding-right: 0.5em; vertical-align: baseline;">staticmethod</td><td style="padding-left: 0.5em; padding-right: 0.5em; vertical-align: baseline;">f(*args)</td><td style="padding-left: 0.5em; padding-right: 0.5em; vertical-align: baseline;">f(*args)</td></tr>
<tr><td style="padding-left: 0.5em; padding-right: 0.5em; vertical-align: baseline;">classmethod</td><td style="padding-left: 0.5em; padding-right: 0.5em; vertical-align: baseline;">f(type(obj), *args)</td><td style="padding-left: 0.5em; padding-right: 0.5em; vertical-align: baseline;">f(klass, *args)</td></tr>
</tbody></table>
</blockquote>
Static methods return the underlying function without changes. Calling either <tt class="literal">c.f</tt> or <tt class="literal">C.f</tt> is the equivalent of a direct lookup into<tt class="literal">object.__getattribute__(c, "f")</tt> or <tt class="literal">object.__getattribute__(C, "f")</tt>. As a result, the function becomes identically accessible from either an object or a class.<br />
Good candidates for static methods are methods that do not reference the <tt class="literal">self</tt> variable.<br />
For instance, a statistics package may include a container class for experimental data. The class provides normal methods for computing the average, mean, median, and other descriptive statistics that depend on the data. However, there may be useful functions which are conceptually related but do not depend on the data. For instance, <tt class="literal">erf(x)</tt> is handy conversion routine that comes up in statistical work but does not directly depend on a particular data set. It can be called either from an object or the class:<tt class="literal">s.erf(1.5) --> .9332</tt> or <tt class="literal">Sample.erf(1.5) --> .9332</tt>.<br />
Since staticmethods return the underlying function with no changes, the example calls are unexciting:<br />
<pre class="literal-block" style="background-color: #eeeeee; margin-left: 2em; margin-right: 2em;">>>> class E(object):
def f(x):
print x
f = staticmethod(f)
>>> print E.f(3)
3
>>> print E().f(3)
3</pre>
Using the non-data descriptor protocol, a pure Python version of <tt class="literal">staticmethod()</tt> would look like this:<br />
<pre class="literal-block" style="background-color: #eeeeee; margin-left: 2em; margin-right: 2em;">class StaticMethod(object):
"Emulate PyStaticMethod_Type() in Objects/funcobject.c"
def __init__(self, f):
self.f = f
def __get__(self, obj, objtype=None):
return self.f</pre>
Unlike static methods, class methods prepend the class reference to the argument list before calling the function. This format is the same for whether the caller is an object or a class:<br />
<pre class="literal-block" style="background-color: #eeeeee; margin-left: 2em; margin-right: 2em;">>>> class E(object):
def f(klass, x):
return klass.__name__, x
f = classmethod(f)
>>> print E.f(3)
('E', 3)
>>> print E().f(3)
('E', 3)</pre>
This behavior is useful whenever the function only needs to have a class reference and does not care about any underlying data. One use for classmethods is to create alternate class constructors. In Python 2.3, the classmethod <tt class="literal">dict.fromkeys()</tt> creates a new dictionary from a list of keys. The pure Python equivalent is:<br />
<pre class="literal-block" style="background-color: #eeeeee; margin-left: 2em; margin-right: 2em;">class Dict:
. . .
def fromkeys(klass, iterable, value=None):
"Emulate dict_fromkeys() in Objects/dictobject.c"
d = klass()
for key in iterable:
d[key] = value
return d
fromkeys = classmethod(fromkeys)</pre>
Now a new dictionary of unique keys can be constructed like this:<br />
<pre class="literal-block" style="background-color: #eeeeee; margin-left: 2em; margin-right: 2em;">>>> Dict.fromkeys('abracadabra')
{'a': None, 'r': None, 'b': None, 'c': None, 'd': None}</pre>
Using the non-data descriptor protocol, a pure Python version of <tt class="literal">classmethod()</tt> would look like this:<br />
<pre class="literal-block" style="background-color: #eeeeee; margin-left: 2em; margin-right: 2em;">class ClassMethod(object):
"Emulate PyClassMethod_Type() in Objects/funcobject.c"
def __init__(self, f):
self.f = f
def __get__(self, obj, klass=None):
if klass is None:
klass = type(obj)
def newfunc(*args):
return self.f(klass, *args)
return newfunc</pre>
</div>
Jeffyehttp://www.blogger.com/profile/07633630042179070371noreply@blogger.com0tag:blogger.com,1999:blog-5448019410873028021.post-15496534537719946972015-08-22T22:05:00.001-07:002015-08-22T22:05:20.469-07:00abc – Abstract Base Classes - Python Module<h1 style="background-color: white; border: 0px; color: #666666; font-family: Arial, Helvetica, sans-serif; font-size: 24px; font-weight: normal; line-height: 1.25em; margin: 0.9523em 0px 0px; padding: 0px;">
abc – Abstract Base Classes<a href="http://pymotw.com/2/abc/#module-abc" style="border: 0px; color: #c60f0f; font-size: 0.8em; line-height: 1.428571em; margin: 0px; padding: 0px 4px; text-decoration: none; visibility: hidden; word-break: break-all;" title="Permalink to this headline"></a></h1>
<table style="background-color: white; border: 0px; color: black; font-family: Helvetica, Arial, 'Droid Sans', sans-serif; font-size: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 19.9999942779541px;"><colgroup style="line-height: 1.428571em;"><col style="line-height: 1.428571em;"></col><col style="line-height: 1.428571em;"></col></colgroup><tbody style="line-height: 1.428571em;" valign="top">
<tr style="line-height: 1.428571em;"><th style="background-color: whitesmoke; border: 0px; line-height: 1.428571em; padding: 1px 8px;">Purpose:</th><td style="border: 0px; line-height: 1.428571em; padding: 1px 8px;">Define and use abstract base classes for API checks in your code.</td></tr>
<tr style="line-height: 1.428571em;"><th style="background-color: whitesmoke; border: 0px; line-height: 1.428571em; padding: 1px 8px;">Available In:</th><td style="border: 0px; line-height: 1.428571em; padding: 1px 8px;">2.6</td></tr>
</tbody></table>
<div style="background-color: white; border: 0px; font-family: Helvetica, Arial, 'Droid Sans', sans-serif; font-size: 14px; line-height: 19.9999942779541px; margin: 0px; padding: 0px;">
<h2 style="border-bottom-color: rgb(221, 221, 221); border-bottom-style: solid; border-width: 0px 0px 1px; color: #666666; font-family: Arial, Helvetica, sans-serif; font-size: 18px; line-height: 1.111em; margin: 1.111em 0px 0.5556em; padding: 0px;">
Why use Abstract Base Classes?<a href="http://pymotw.com/2/abc/#why-use-abstract-base-classes" style="border: 0px; color: #c60f0f; font-size: 0.8em; line-height: 1.428571em; margin: 0px; padding: 0px 4px; text-decoration: none; visibility: hidden; word-break: break-all;" title="Permalink to this headline"></a></h2>
<div style="border: 0px; line-height: 1.428571em; margin-bottom: 0.714285em; padding: 0px;">
Abstract base classes are a form of interface checking more strict than individual <tt style="background-color: #ecf0f3; font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">hasattr()</span></tt>checks for particular methods. By defining an abstract base class, you can define a common API for a set of subclasses. This capability is especially useful in situations where a third-party is going to provide implementations, such as with plugins to an application, but can also aid you when working on a large team or with a large code-base where keeping all classes in your head at the same time is difficult or not possible.</div>
</div>
<div style="background-color: white; border: 0px; font-family: Helvetica, Arial, 'Droid Sans', sans-serif; font-size: 14px; line-height: 19.9999942779541px; margin: 0px; padding: 0px;">
<h2 style="border-bottom-color: rgb(221, 221, 221); border-bottom-style: solid; border-width: 0px 0px 1px; color: #666666; font-family: Arial, Helvetica, sans-serif; font-size: 18px; line-height: 1.111em; margin: 1.111em 0px 0.5556em; padding: 0px;">
How ABCs Work<a href="http://pymotw.com/2/abc/#how-abcs-work" style="border: 0px; color: #c60f0f; font-size: 0.8em; line-height: 1.428571em; margin: 0px; padding: 0px 4px; text-decoration: none; visibility: hidden; word-break: break-all;" title="Permalink to this headline"></a></h2>
<div style="border: 0px; line-height: 1.428571em; margin-bottom: 0.714285em; padding: 0px;">
<a href="http://pymotw.com/2/abc/#module-abc" style="border: 0px; color: #cc6714; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none; word-break: break-all;" title="abc: Abstract Base Classes"><tt style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">abc</span></tt></a> works by marking methods of the base class as abstract, and then registering concrete classes as implementations of the abstract base. If your code requires a particular API, you can use <tt style="background-color: #ecf0f3; font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">issubclass()</span></tt> or <tt style="background-color: #ecf0f3; font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">isinstance()</span></tt> to check an object against the abstract class.</div>
<div style="border: 0px; line-height: 1.428571em; margin-bottom: 0.714285em; padding: 0px;">
Let’s start by defining an abstract base class to represent the API of a set of plugins for saving and loading data.</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px 2em; padding: 0px;">
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.8000011444092px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;" xml:space="preserve"><span style="line-height: 1.428571em;">import</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">abc</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">class</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">PluginBase</span><span style="line-height: 1.428571em;">(</span><span style="color: green; line-height: 1.428571em;">object</span><span style="line-height: 1.428571em;">):</span>
<span style="line-height: 1.428571em;">__metaclass__</span> <span style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">abc</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">ABCMeta</span>
<span style="color: #aa22ff; line-height: 1.428571em;">@abc.abstractmethod</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">def</span> <span style="color: blue; line-height: 1.428571em;">load</span><span style="line-height: 1.428571em;">(</span><span style="color: green; line-height: 1.428571em;">self</span><span style="line-height: 1.428571em;">,</span> <span style="color: green; line-height: 1.428571em;">input</span><span style="line-height: 1.428571em;">):</span>
<span style="color: #ba2121; font-style: italic; line-height: 1.428571em;">"""Retrieve data from the input source and return an object."""</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">return</span>
<span style="color: #aa22ff; line-height: 1.428571em;">@abc.abstractmethod</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">def</span> <span style="color: blue; line-height: 1.428571em;">save</span><span style="line-height: 1.428571em;">(</span><span style="color: green; line-height: 1.428571em;">self</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">output</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">data</span><span style="line-height: 1.428571em;">):</span>
<span style="color: #ba2121; font-style: italic; line-height: 1.428571em;">"""Save the data object to the output."""</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">return</span>
</pre>
</div>
</div>
</div>
<div style="background-color: white; border: 0px; font-family: Helvetica, Arial, 'Droid Sans', sans-serif; font-size: 14px; line-height: 19.9999942779541px; margin: 0px; padding: 0px;">
<h2 style="border-bottom-color: rgb(221, 221, 221); border-bottom-style: solid; border-width: 0px 0px 1px; color: #666666; font-family: Arial, Helvetica, sans-serif; font-size: 18px; line-height: 1.111em; margin: 1.111em 0px 0.5556em; padding: 0px;">
Registering a Concrete Class<a href="http://pymotw.com/2/abc/#registering-a-concrete-class" style="border: 0px; color: #c60f0f; font-size: 0.8em; line-height: 1.428571em; margin: 0px; padding: 0px 4px; text-decoration: none; visibility: hidden; word-break: break-all;" title="Permalink to this headline"></a></h2>
<div style="border: 0px; line-height: 1.428571em; margin-bottom: 0.714285em; padding: 0px;">
There are two ways to indicate that a concrete class implements an abstract: register the class with the abc or subclass directly from the abc.</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px 2em; padding: 0px;">
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.8000011444092px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;" xml:space="preserve"><span style="line-height: 1.428571em;">import</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">abc</span>
<span style="line-height: 1.428571em;">from</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">abc_base</span> <span style="line-height: 1.428571em;">import</span> <span style="line-height: 1.428571em;">PluginBase</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">class</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">RegisteredImplementation</span><span style="line-height: 1.428571em;">(</span><span style="color: green; line-height: 1.428571em;">object</span><span style="line-height: 1.428571em;">):</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">def</span> <span style="color: blue; line-height: 1.428571em;">load</span><span style="line-height: 1.428571em;">(</span><span style="color: green; line-height: 1.428571em;">self</span><span style="line-height: 1.428571em;">,</span> <span style="color: green; line-height: 1.428571em;">input</span><span style="line-height: 1.428571em;">):</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">return</span> <span style="color: green; line-height: 1.428571em;">input</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">read</span><span style="line-height: 1.428571em;">()</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">def</span> <span style="color: blue; line-height: 1.428571em;">save</span><span style="line-height: 1.428571em;">(</span><span style="color: green; line-height: 1.428571em;">self</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">output</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">data</span><span style="line-height: 1.428571em;">):</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">return</span> <span style="line-height: 1.428571em;">output</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">write</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">data</span><span style="line-height: 1.428571em;">)</span>
<span style="line-height: 1.428571em;">PluginBase</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">register</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">RegisteredImplementation</span><span style="line-height: 1.428571em;">)</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">if</span> <span style="line-height: 1.428571em;">__name__</span> <span style="color: #666666; line-height: 1.428571em;">==</span> <span style="color: #ba2121; line-height: 1.428571em;">'__main__'</span><span style="line-height: 1.428571em;">:</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">print</span> <span style="color: #ba2121; line-height: 1.428571em;">'Subclass:'</span><span style="line-height: 1.428571em;">,</span> <span style="color: green; line-height: 1.428571em;">issubclass</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">RegisteredImplementation</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">PluginBase</span><span style="line-height: 1.428571em;">)</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">print</span> <span style="color: #ba2121; line-height: 1.428571em;">'Instance:'</span><span style="line-height: 1.428571em;">,</span> <span style="color: green; line-height: 1.428571em;">isinstance</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">RegisteredImplementation</span><span style="line-height: 1.428571em;">(),</span> <span style="line-height: 1.428571em;">PluginBase</span><span style="line-height: 1.428571em;">)</span>
</pre>
</div>
</div>
<div style="border: 0px; line-height: 1.428571em; margin-bottom: 0.714285em; padding: 0px;">
In this example the <tt style="background-color: #ecf0f3; font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">PluginImplementation</span></tt> is not derived from <tt style="background-color: #ecf0f3; font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">PluginBase</span></tt>, but is registered as implementing the <tt style="background-color: #ecf0f3; font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">PluginBase</span></tt> API.</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px 2em; padding: 0px;">
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.8000011444092px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;" xml:space="preserve">$ python abc_register.py
Subclass: True
Instance: True
</pre>
</div>
</div>
</div>
<div style="background-color: white; border: 0px; font-family: Helvetica, Arial, 'Droid Sans', sans-serif; font-size: 14px; line-height: 19.9999942779541px; margin: 0px; padding: 0px;">
<h2 style="border-bottom-color: rgb(221, 221, 221); border-bottom-style: solid; border-width: 0px 0px 1px; color: #666666; font-family: Arial, Helvetica, sans-serif; font-size: 18px; line-height: 1.111em; margin: 1.111em 0px 0.5556em; padding: 0px;">
Implementation Through Subclassing<a href="http://pymotw.com/2/abc/#implementation-through-subclassing" style="border: 0px; color: #c60f0f; font-size: 0.8em; line-height: 1.428571em; margin: 0px; padding: 0px 4px; text-decoration: none; visibility: hidden; word-break: break-all;" title="Permalink to this headline"></a></h2>
<div style="border: 0px; line-height: 1.428571em; margin-bottom: 0.714285em; padding: 0px;">
By subclassing directly from the base, we can avoid the need to register the class explicitly.</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px 2em; padding: 0px;">
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.8000011444092px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;" xml:space="preserve"><span style="line-height: 1.428571em;">import</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">abc</span>
<span style="line-height: 1.428571em;">from</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">abc_base</span> <span style="line-height: 1.428571em;">import</span> <span style="line-height: 1.428571em;">PluginBase</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">class</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">SubclassImplementation</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">PluginBase</span><span style="line-height: 1.428571em;">):</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">def</span> <span style="color: blue; line-height: 1.428571em;">load</span><span style="line-height: 1.428571em;">(</span><span style="color: green; line-height: 1.428571em;">self</span><span style="line-height: 1.428571em;">,</span> <span style="color: green; line-height: 1.428571em;">input</span><span style="line-height: 1.428571em;">):</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">return</span> <span style="color: green; line-height: 1.428571em;">input</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">read</span><span style="line-height: 1.428571em;">()</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">def</span> <span style="color: blue; line-height: 1.428571em;">save</span><span style="line-height: 1.428571em;">(</span><span style="color: green; line-height: 1.428571em;">self</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">output</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">data</span><span style="line-height: 1.428571em;">):</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">return</span> <span style="line-height: 1.428571em;">output</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">write</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">data</span><span style="line-height: 1.428571em;">)</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">if</span> <span style="line-height: 1.428571em;">__name__</span> <span style="color: #666666; line-height: 1.428571em;">==</span> <span style="color: #ba2121; line-height: 1.428571em;">'__main__'</span><span style="line-height: 1.428571em;">:</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">print</span> <span style="color: #ba2121; line-height: 1.428571em;">'Subclass:'</span><span style="line-height: 1.428571em;">,</span> <span style="color: green; line-height: 1.428571em;">issubclass</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">SubclassImplementation</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">PluginBase</span><span style="line-height: 1.428571em;">)</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">print</span> <span style="color: #ba2121; line-height: 1.428571em;">'Instance:'</span><span style="line-height: 1.428571em;">,</span> <span style="color: green; line-height: 1.428571em;">isinstance</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">SubclassImplementation</span><span style="line-height: 1.428571em;">(),</span> <span style="line-height: 1.428571em;">PluginBase</span><span style="line-height: 1.428571em;">)</span>
</pre>
</div>
</div>
<div style="border: 0px; line-height: 1.428571em; margin-bottom: 0.714285em; padding: 0px;">
In this case the normal Python class management is used to recognize<tt style="background-color: #ecf0f3; font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">PluginImplementation</span></tt> as implementing the abstract <tt style="background-color: #ecf0f3; font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">PluginBase</span></tt>.</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px 2em; padding: 0px;">
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.8000011444092px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;" xml:space="preserve">$ python abc_subclass.py
Subclass: True
Instance: True
</pre>
</div>
</div>
<div style="border: 0px; line-height: 1.428571em; margin-bottom: 0.714285em; padding: 0px;">
A side-effect of using direct subclassing is it is possible to find all of the implementations of your plugin by asking the base class for the list of known classes derived from it (this is not an abc feature, all classes can do this).</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px 2em; padding: 0px;">
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.8000011444092px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;" xml:space="preserve"><span style="line-height: 1.428571em;">import</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">abc</span>
<span style="line-height: 1.428571em;">from</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">abc_base</span> <span style="line-height: 1.428571em;">import</span> <span style="line-height: 1.428571em;">PluginBase</span>
<span style="line-height: 1.428571em;">import</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">abc_subclass</span>
<span style="line-height: 1.428571em;">import</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">abc_register</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">for</span> <span style="line-height: 1.428571em;">sc</span> <span style="color: #aa22ff; font-weight: bold; line-height: 1.428571em;">in</span> <span style="line-height: 1.428571em;">PluginBase</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">__subclasses__</span><span style="line-height: 1.428571em;">():</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">print</span> <span style="line-height: 1.428571em;">sc</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">__name__</span>
</pre>
</div>
</div>
<div style="border: 0px; line-height: 1.428571em; margin-bottom: 0.714285em; padding: 0px;">
Notice that even though <tt style="background-color: #ecf0f3; font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">abc_register</span></tt> is imported, <tt style="background-color: #ecf0f3; font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">RegisteredImplementation</span></tt> is not among the list of subclasses because it is not actually derived from the base.</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px 2em; padding: 0px;">
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.8000011444092px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;" xml:space="preserve">$ python abc_find_subclasses.py
SubclassImplementation
</pre>
</div>
</div>
<div style="border: 0px; line-height: 1.428571em; margin-bottom: 0.714285em; padding: 0px;">
Dr. André Roberge <a href="http://us.pycon.org/2009/conference/schedule/event/47/" style="border: 0px; color: #cc6714; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none; word-break: break-all;">has described</a> using this capability to discover plugins by importing all of the modules in a directory dynamically and then looking at the subclass list to find the implementation classes.</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<h3 style="border: 0px; color: #444444; font-size: 1em; line-height: 1.428571em; margin: 1.4285em 0px 0.714285em; padding: 0px;">
Incomplete Implementations<a href="http://pymotw.com/2/abc/#incomplete-implementations" style="border: 0px; color: #c60f0f; font-size: 0.8em; line-height: 1.428571em; margin: 0px; padding: 0px 4px; text-decoration: none; visibility: hidden; word-break: break-all;" title="Permalink to this headline"></a></h3>
<div style="border: 0px; line-height: 1.428571em; margin-bottom: 0.714285em; padding: 0px;">
Another benefit of subclassing directly from your abstract base class is that the subclass cannot be instantiated unless it fully implements the abstract portion of the API. This can keep half-baked implementations from triggering unexpected errors at runtime.</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px 2em; padding: 0px;">
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.8000011444092px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;" xml:space="preserve"><span style="line-height: 1.428571em;">import</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">abc</span>
<span style="line-height: 1.428571em;">from</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">abc_base</span> <span style="line-height: 1.428571em;">import</span> <span style="line-height: 1.428571em;">PluginBase</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">class</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">IncompleteImplementation</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">PluginBase</span><span style="line-height: 1.428571em;">):</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">def</span> <span style="color: blue; line-height: 1.428571em;">save</span><span style="line-height: 1.428571em;">(</span><span style="color: green; line-height: 1.428571em;">self</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">output</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">data</span><span style="line-height: 1.428571em;">):</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">return</span> <span style="line-height: 1.428571em;">output</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">write</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">data</span><span style="line-height: 1.428571em;">)</span>
<span style="line-height: 1.428571em;">PluginBase</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">register</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">IncompleteImplementation</span><span style="line-height: 1.428571em;">)</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">if</span> <span style="line-height: 1.428571em;">__name__</span> <span style="color: #666666; line-height: 1.428571em;">==</span> <span style="color: #ba2121; line-height: 1.428571em;">'__main__'</span><span style="line-height: 1.428571em;">:</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">print</span> <span style="color: #ba2121; line-height: 1.428571em;">'Subclass:'</span><span style="line-height: 1.428571em;">,</span> <span style="color: green; line-height: 1.428571em;">issubclass</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">IncompleteImplementation</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">PluginBase</span><span style="line-height: 1.428571em;">)</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">print</span> <span style="color: #ba2121; line-height: 1.428571em;">'Instance:'</span><span style="line-height: 1.428571em;">,</span> <span style="color: green; line-height: 1.428571em;">isinstance</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">IncompleteImplementation</span><span style="line-height: 1.428571em;">(),</span> <span style="line-height: 1.428571em;">PluginBase</span><span style="line-height: 1.428571em;">)</span>
</pre>
</div>
</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px 2em; padding: 0px;">
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.8000011444092px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;" xml:space="preserve">$ python abc_incomplete.py
Subclass: True
Instance:
Traceback (most recent call last):
File "abc_incomplete.py", line 22, in <module>
print 'Instance:', isinstance(IncompleteImplementation(), PluginBase)
TypeError: Can't instantiate abstract class IncompleteImplementation with abstract methods load
</pre>
</div>
</div>
</div>
</div>
<div style="background-color: white; border: 0px; font-family: Helvetica, Arial, 'Droid Sans', sans-serif; font-size: 14px; line-height: 19.9999942779541px; margin: 0px; padding: 0px;">
<h2 style="border-bottom-color: rgb(221, 221, 221); border-bottom-style: solid; border-width: 0px 0px 1px; color: #666666; font-family: Arial, Helvetica, sans-serif; font-size: 18px; line-height: 1.111em; margin: 1.111em 0px 0.5556em; padding: 0px;">
Concrete Methods in ABCs<a href="http://pymotw.com/2/abc/#concrete-methods-in-abcs" style="border: 0px; color: #c60f0f; font-size: 0.8em; line-height: 1.428571em; margin: 0px; padding: 0px 4px; text-decoration: none; visibility: hidden; word-break: break-all;" title="Permalink to this headline"></a></h2>
<div style="border: 0px; line-height: 1.428571em; margin-bottom: 0.714285em; padding: 0px;">
Although a concrete class must provide an implementation of an abstract methods, the abstract base class can also provide an implementation that can be invoked via <tt style="background-color: #ecf0f3; font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">super()</span></tt>. This lets you re-use common logic by placing it in the base class, but force subclasses to provide an overriding method with (potentially) custom logic.</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px 2em; padding: 0px;">
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.8000011444092px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;" xml:space="preserve"><span style="line-height: 1.428571em;">import</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">abc</span>
<span style="line-height: 1.428571em;">from</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">cStringIO</span> <span style="line-height: 1.428571em;">import</span> <span style="line-height: 1.428571em;">StringIO</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">class</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">ABCWithConcreteImplementation</span><span style="line-height: 1.428571em;">(</span><span style="color: green; line-height: 1.428571em;">object</span><span style="line-height: 1.428571em;">):</span>
<span style="line-height: 1.428571em;">__metaclass__</span> <span style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">abc</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">ABCMeta</span>
<span style="color: #aa22ff; line-height: 1.428571em;">@abc.abstractmethod</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">def</span> <span style="color: blue; line-height: 1.428571em;">retrieve_values</span><span style="line-height: 1.428571em;">(</span><span style="color: green; line-height: 1.428571em;">self</span><span style="line-height: 1.428571em;">,</span> <span style="color: green; line-height: 1.428571em;">input</span><span style="line-height: 1.428571em;">):</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">print</span> <span style="color: #ba2121; line-height: 1.428571em;">'base class reading data'</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">return</span> <span style="color: green; line-height: 1.428571em;">input</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">read</span><span style="line-height: 1.428571em;">()</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">class</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">ConcreteOverride</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">ABCWithConcreteImplementation</span><span style="line-height: 1.428571em;">):</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">def</span> <span style="color: blue; line-height: 1.428571em;">retrieve_values</span><span style="line-height: 1.428571em;">(</span><span style="color: green; line-height: 1.428571em;">self</span><span style="line-height: 1.428571em;">,</span> <span style="color: green; line-height: 1.428571em;">input</span><span style="line-height: 1.428571em;">):</span>
<span style="line-height: 1.428571em;">base_data</span> <span style="color: #666666; line-height: 1.428571em;">=</span> <span style="color: green; line-height: 1.428571em;">super</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">ConcreteOverride</span><span style="line-height: 1.428571em;">,</span> <span style="color: green; line-height: 1.428571em;">self</span><span style="line-height: 1.428571em;">)</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">retrieve_values</span><span style="line-height: 1.428571em;">(</span><span style="color: green; line-height: 1.428571em;">input</span><span style="line-height: 1.428571em;">)</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">print</span> <span style="color: #ba2121; line-height: 1.428571em;">'subclass sorting data'</span>
<span style="line-height: 1.428571em;">response</span> <span style="color: #666666; line-height: 1.428571em;">=</span> <span style="color: green; line-height: 1.428571em;">sorted</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">base_data</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">splitlines</span><span style="line-height: 1.428571em;">())</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">return</span> <span style="line-height: 1.428571em;">response</span>
<span style="color: green; line-height: 1.428571em;">input</span> <span style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">StringIO</span><span style="line-height: 1.428571em;">(</span><span style="color: #ba2121; line-height: 1.428571em;">"""line one</span>
<span style="color: #ba2121; line-height: 1.428571em;">line two</span>
<span style="color: #ba2121; line-height: 1.428571em;">line three</span>
<span style="color: #ba2121; line-height: 1.428571em;">"""</span><span style="line-height: 1.428571em;">)</span>
<span style="line-height: 1.428571em;">reader</span> <span style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">ConcreteOverride</span><span style="line-height: 1.428571em;">()</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">print</span> <span style="line-height: 1.428571em;">reader</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">retrieve_values</span><span style="line-height: 1.428571em;">(</span><span style="color: green; line-height: 1.428571em;">input</span><span style="line-height: 1.428571em;">)</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">print</span>
</pre>
</div>
</div>
<div style="border: 0px; line-height: 1.428571em; margin-bottom: 0.714285em; padding: 0px;">
Since <tt style="background-color: #ecf0f3; font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">ABCWithConcreteImplementation</span></tt> is an abstract base class, it isn’t possible to instantiate it to use it directly. Subclasses <em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">must</em> provide an override for <tt style="background-color: #ecf0f3; font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">retrieve_values()</span></tt>, and in this case the concrete class massages the data before returning it at all.</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px 2em; padding: 0px;">
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.8000011444092px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;" xml:space="preserve">$ python abc_concrete_method.py
base class reading data
subclass sorting data
['line one', 'line three', 'line two']
</pre>
</div>
</div>
</div>
<div style="background-color: white; border: 0px; font-family: Helvetica, Arial, 'Droid Sans', sans-serif; font-size: 14px; line-height: 19.9999942779541px; margin: 0px; padding: 0px;">
<span style="line-height: 1.428571em;"></span><h2 style="border-bottom-color: rgb(221, 221, 221); border-bottom-style: solid; border-width: 0px 0px 1px; color: #666666; font-family: Arial, Helvetica, sans-serif; font-size: 18px; line-height: 1.111em; margin: 1.111em 0px 0.5556em; padding: 0px;">
Abstract Properties<a href="http://pymotw.com/2/abc/#abstract-properties" style="border: 0px; color: #c60f0f; font-size: 0.8em; line-height: 1.428571em; margin: 0px; padding: 0px 4px; text-decoration: none; visibility: hidden; word-break: break-all;" title="Permalink to this headline"></a></h2>
<div style="border: 0px; line-height: 1.428571em; margin-bottom: 0.714285em; padding: 0px;">
If your API specification includes attributes in addition to methods, you can require the attributes in concrete classes by defining them with <tt style="background-color: #ecf0f3; font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">@abstractproperty</span></tt>.</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px 2em; padding: 0px;">
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.8000011444092px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;" xml:space="preserve"><span style="line-height: 1.428571em;">import</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">abc</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">class</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">Base</span><span style="line-height: 1.428571em;">(</span><span style="color: green; line-height: 1.428571em;">object</span><span style="line-height: 1.428571em;">):</span>
<span style="line-height: 1.428571em;">__metaclass__</span> <span style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">abc</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">ABCMeta</span>
<span style="color: #aa22ff; line-height: 1.428571em;">@abc.abstractproperty</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">def</span> <span style="color: blue; line-height: 1.428571em;">value</span><span style="line-height: 1.428571em;">(</span><span style="color: green; line-height: 1.428571em;">self</span><span style="line-height: 1.428571em;">):</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">return</span> <span style="color: #ba2121; line-height: 1.428571em;">'Should never get here'</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">class</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">Implementation</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">Base</span><span style="line-height: 1.428571em;">):</span>
<span style="color: #aa22ff; line-height: 1.428571em;">@property</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">def</span> <span style="color: blue; line-height: 1.428571em;">value</span><span style="line-height: 1.428571em;">(</span><span style="color: green; line-height: 1.428571em;">self</span><span style="line-height: 1.428571em;">):</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">return</span> <span style="color: #ba2121; line-height: 1.428571em;">'concrete property'</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">try</span><span style="line-height: 1.428571em;">:</span>
<span style="line-height: 1.428571em;">b</span> <span style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">Base</span><span style="line-height: 1.428571em;">()</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">print</span> <span style="color: #ba2121; line-height: 1.428571em;">'Base.value:'</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">b</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">value</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">except</span> <span style="color: #d2413a; font-weight: bold; line-height: 1.428571em;">Exception</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">err</span><span style="line-height: 1.428571em;">:</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">print</span> <span style="color: #ba2121; line-height: 1.428571em;">'ERROR:'</span><span style="line-height: 1.428571em;">,</span> <span style="color: green; line-height: 1.428571em;">str</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">err</span><span style="line-height: 1.428571em;">)</span>
<span style="line-height: 1.428571em;">i</span> <span style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">Implementation</span><span style="line-height: 1.428571em;">()</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">print</span> <span style="color: #ba2121; line-height: 1.428571em;">'Implementation.value:'</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">i</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">value</span>
</pre>
</div>
</div>
<div style="border: 0px; line-height: 1.428571em; margin-bottom: 0.714285em; padding: 0px;">
The <tt style="background-color: #ecf0f3; font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">Base</span></tt> class in the example cannot be instantiated because it has only an abstract version of the property getter method.</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px 2em; padding: 0px;">
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.8000011444092px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;" xml:space="preserve">$ python abc_abstractproperty.py
ERROR: Can't instantiate abstract class Base with abstract methods value
Implementation.value: concrete property
</pre>
</div>
</div>
<div style="border: 0px; line-height: 1.428571em; margin-bottom: 0.714285em; padding: 0px;">
You can also define abstract read/write properties.</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px 2em; padding: 0px;">
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.8000011444092px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;" xml:space="preserve"><span style="line-height: 1.428571em;">import</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">abc</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">class</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">Base</span><span style="line-height: 1.428571em;">(</span><span style="color: green; line-height: 1.428571em;">object</span><span style="line-height: 1.428571em;">):</span>
<span style="line-height: 1.428571em;">__metaclass__</span> <span style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">abc</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">ABCMeta</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">def</span> <span style="color: blue; line-height: 1.428571em;">value_getter</span><span style="line-height: 1.428571em;">(</span><span style="color: green; line-height: 1.428571em;">self</span><span style="line-height: 1.428571em;">):</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">return</span> <span style="color: #ba2121; line-height: 1.428571em;">'Should never see this'</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">def</span> <span style="color: blue; line-height: 1.428571em;">value_setter</span><span style="line-height: 1.428571em;">(</span><span style="color: green; line-height: 1.428571em;">self</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">newvalue</span><span style="line-height: 1.428571em;">):</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">return</span>
<span style="line-height: 1.428571em;">value</span> <span style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">abc</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">abstractproperty</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">value_getter</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">value_setter</span><span style="line-height: 1.428571em;">)</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">class</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">PartialImplementation</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">Base</span><span style="line-height: 1.428571em;">):</span>
<span style="color: #aa22ff; line-height: 1.428571em;">@abc.abstractproperty</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">def</span> <span style="color: blue; line-height: 1.428571em;">value</span><span style="line-height: 1.428571em;">(</span><span style="color: green; line-height: 1.428571em;">self</span><span style="line-height: 1.428571em;">):</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">return</span> <span style="color: #ba2121; line-height: 1.428571em;">'Read-only'</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">class</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">Implementation</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">Base</span><span style="line-height: 1.428571em;">):</span>
<span style="line-height: 1.428571em;">_value</span> <span style="color: #666666; line-height: 1.428571em;">=</span> <span style="color: #ba2121; line-height: 1.428571em;">'Default value'</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">def</span> <span style="color: blue; line-height: 1.428571em;">value_getter</span><span style="line-height: 1.428571em;">(</span><span style="color: green; line-height: 1.428571em;">self</span><span style="line-height: 1.428571em;">):</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">return</span> <span style="color: green; line-height: 1.428571em;">self</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">_value</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">def</span> <span style="color: blue; line-height: 1.428571em;">value_setter</span><span style="line-height: 1.428571em;">(</span><span style="color: green; line-height: 1.428571em;">self</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">newvalue</span><span style="line-height: 1.428571em;">):</span>
<span style="color: green; line-height: 1.428571em;">self</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">_value</span> <span style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">newvalue</span>
<span style="line-height: 1.428571em;">value</span> <span style="color: #666666; line-height: 1.428571em;">=</span> <span style="color: green; line-height: 1.428571em;">property</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">value_getter</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">value_setter</span><span style="line-height: 1.428571em;">)</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">try</span><span style="line-height: 1.428571em;">:</span>
<span style="line-height: 1.428571em;">b</span> <span style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">Base</span><span style="line-height: 1.428571em;">()</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">print</span> <span style="color: #ba2121; line-height: 1.428571em;">'Base.value:'</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">b</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">value</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">except</span> <span style="color: #d2413a; font-weight: bold; line-height: 1.428571em;">Exception</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">err</span><span style="line-height: 1.428571em;">:</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">print</span> <span style="color: #ba2121; line-height: 1.428571em;">'ERROR:'</span><span style="line-height: 1.428571em;">,</span> <span style="color: green; line-height: 1.428571em;">str</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">err</span><span style="line-height: 1.428571em;">)</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">try</span><span style="line-height: 1.428571em;">:</span>
<span style="line-height: 1.428571em;">p</span> <span style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">PartialImplementation</span><span style="line-height: 1.428571em;">()</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">print</span> <span style="color: #ba2121; line-height: 1.428571em;">'PartialImplementation.value:'</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">p</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">value</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">except</span> <span style="color: #d2413a; font-weight: bold; line-height: 1.428571em;">Exception</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">err</span><span style="line-height: 1.428571em;">:</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">print</span> <span style="color: #ba2121; line-height: 1.428571em;">'ERROR:'</span><span style="line-height: 1.428571em;">,</span> <span style="color: green; line-height: 1.428571em;">str</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">err</span><span style="line-height: 1.428571em;">)</span>
<span style="line-height: 1.428571em;">i</span> <span style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">Implementation</span><span style="line-height: 1.428571em;">()</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">print</span> <span style="color: #ba2121; line-height: 1.428571em;">'Implementation.value:'</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">i</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">value</span>
<span style="line-height: 1.428571em;">i</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">value</span> <span style="color: #666666; line-height: 1.428571em;">=</span> <span style="color: #ba2121; line-height: 1.428571em;">'New value'</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">print</span> <span style="color: #ba2121; line-height: 1.428571em;">'Changed value:'</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">i</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">value</span>
</pre>
</div>
</div>
<div style="border: 0px; line-height: 1.428571em; margin-bottom: 0.714285em; padding: 0px;">
Notice that the concrete property must be defined the same way as the abstract property. Trying to override a read/write property in <tt style="background-color: #ecf0f3; font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">PartialImplementation</span></tt> with one that is read-only does not work.</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px 2em; padding: 0px;">
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.8000011444092px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;" xml:space="preserve">$ python abc_abstractproperty_rw.py
ERROR: Can't instantiate abstract class Base with abstract methods value
ERROR: Can't instantiate abstract class PartialImplementation with abstract methods value
Implementation.value: Default value
Changed value: New value
</pre>
</div>
</div>
<div style="border: 0px; line-height: 1.428571em; margin-bottom: 0.714285em; padding: 0px;">
To use the decorator syntax does with read/write abstract properties, the methods to get and set the value should be named the same.</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px 2em; padding: 0px;">
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.8000011444092px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;" xml:space="preserve"><span style="line-height: 1.428571em;">import</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">abc</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">class</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">Base</span><span style="line-height: 1.428571em;">(</span><span style="color: green; line-height: 1.428571em;">object</span><span style="line-height: 1.428571em;">):</span>
<span style="line-height: 1.428571em;">__metaclass__</span> <span style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">abc</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">ABCMeta</span>
<span style="color: #aa22ff; line-height: 1.428571em;">@abc.abstractproperty</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">def</span> <span style="color: blue; line-height: 1.428571em;">value</span><span style="line-height: 1.428571em;">(</span><span style="color: green; line-height: 1.428571em;">self</span><span style="line-height: 1.428571em;">):</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">return</span> <span style="color: #ba2121; line-height: 1.428571em;">'Should never see this'</span>
<span style="color: #aa22ff; line-height: 1.428571em;">@value.setter</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">def</span> <span style="color: blue; line-height: 1.428571em;">value</span><span style="line-height: 1.428571em;">(</span><span style="color: green; line-height: 1.428571em;">self</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">newvalue</span><span style="line-height: 1.428571em;">):</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">return</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">class</span> <span style="color: blue; font-weight: bold; line-height: 1.428571em;">Implementation</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">Base</span><span style="line-height: 1.428571em;">):</span>
<span style="line-height: 1.428571em;">_value</span> <span style="color: #666666; line-height: 1.428571em;">=</span> <span style="color: #ba2121; line-height: 1.428571em;">'Default value'</span>
<span style="color: #aa22ff; line-height: 1.428571em;">@property</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">def</span> <span style="color: blue; line-height: 1.428571em;">value</span><span style="line-height: 1.428571em;">(</span><span style="color: green; line-height: 1.428571em;">self</span><span style="line-height: 1.428571em;">):</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">return</span> <span style="color: green; line-height: 1.428571em;">self</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">_value</span>
<span style="color: #aa22ff; line-height: 1.428571em;">@value.setter</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">def</span> <span style="color: blue; line-height: 1.428571em;">value</span><span style="line-height: 1.428571em;">(</span><span style="color: green; line-height: 1.428571em;">self</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">newvalue</span><span style="line-height: 1.428571em;">):</span>
<span style="color: green; line-height: 1.428571em;">self</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">_value</span> <span style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">newvalue</span>
<span style="line-height: 1.428571em;">i</span> <span style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">Implementation</span><span style="line-height: 1.428571em;">()</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">print</span> <span style="color: #ba2121; line-height: 1.428571em;">'Implementation.value:'</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">i</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">value</span>
<span style="line-height: 1.428571em;">i</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">value</span> <span style="color: #666666; line-height: 1.428571em;">=</span> <span style="color: #ba2121; line-height: 1.428571em;">'New value'</span>
<span style="color: green; font-weight: bold; line-height: 1.428571em;">print</span> <span style="color: #ba2121; line-height: 1.428571em;">'Changed value:'</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">i</span><span style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">value</span>
</pre>
</div>
</div>
<div style="border: 0px; line-height: 1.428571em; margin-bottom: 0.714285em; padding: 0px;">
Notice that both methods in the <tt style="background-color: #ecf0f3; font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">Base</span></tt> and <tt style="background-color: #ecf0f3; font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">Implementation</span></tt> classes are named <tt style="background-color: #ecf0f3; font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">value()</span></tt>, although they have different signatures.</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px 2em; padding: 0px;">
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.8000011444092px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;" xml:space="preserve">$ python abc_abstractproperty_rw_deco.py
Implementation.value: Default value
Changed value: New value
</pre>
</div>
</div>
</div>
<div style="background-color: white; border: 0px; font-family: Helvetica, Arial, 'Droid Sans', sans-serif; font-size: 14px; line-height: 19.9999942779541px; margin: 0px; padding: 0px;">
<span style="line-height: 1.428571em;"></span><h2 style="border-bottom-color: rgb(221, 221, 221); border-bottom-style: solid; border-width: 0px 0px 1px; color: #666666; font-family: Arial, Helvetica, sans-serif; font-size: 18px; line-height: 1.111em; margin: 1.111em 0px 0.5556em; padding: 0px;">
Collection Types<a href="http://pymotw.com/2/abc/#collection-types" style="border: 0px; color: #c60f0f; font-size: 0.8em; line-height: 1.428571em; margin: 0px; padding: 0px 4px; text-decoration: none; visibility: hidden; word-break: break-all;" title="Permalink to this headline"></a></h2>
<div style="border: 0px; line-height: 1.428571em; margin-bottom: 0.714285em; padding: 0px;">
The <a href="http://pymotw.com/2/collections/index.html#module-collections" style="border: 0px; color: #cc6714; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none; word-break: break-all;" title="collections: Container data types."><tt style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">collections</span></tt></a> module defines several abstract base classes related to container (and containable) types.</div>
<div style="border: 0px; line-height: 1.428571em; margin-bottom: 0.714285em; padding: 0px;">
General container classes:</div>
<ul style="border: 0px; line-height: 1.428571em; list-style-position: outside; margin: -0.428571em 0px 0.714285em 2em; padding: 0px;">
<li style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">Container</li>
<li style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">Sized</li>
</ul>
<div style="border: 0px; line-height: 1.428571em; margin-bottom: 0.714285em; padding: 0px;">
Iterator and Sequence classes:</div>
<ul style="border: 0px; line-height: 1.428571em; list-style-position: outside; margin: -0.428571em 0px 0.714285em 2em; padding: 0px;">
<li style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">Iterable</li>
<li style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">Iterator</li>
<li style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">Sequence</li>
<li style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">MutableSequence</li>
</ul>
<div style="border: 0px; line-height: 1.428571em; margin-bottom: 0.714285em; padding: 0px;">
Unique values:</div>
<ul style="border: 0px; line-height: 1.428571em; list-style-position: outside; margin: -0.428571em 0px 0.714285em 2em; padding: 0px;">
<li style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">Hashable</li>
<li style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">Set</li>
<li style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">MutableSet</li>
</ul>
<div style="border: 0px; line-height: 1.428571em; margin-bottom: 0.714285em; padding: 0px;">
Mappings:</div>
<ul style="border: 0px; line-height: 1.428571em; list-style-position: outside; margin: -0.428571em 0px 0.714285em 2em; padding: 0px;">
<li style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">Mapping</li>
<li style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">MutableMapping</li>
<li style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">MappingView</li>
<li style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">KeysView</li>
<li style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">ItemsView</li>
<li style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">ValuesView</li>
</ul>
<div style="border: 0px; line-height: 1.428571em; margin-bottom: 0.714285em; padding: 0px;">
Miscelaneous:</div>
<ul style="border: 0px; line-height: 1.428571em; list-style-position: outside; margin: -0.428571em 0px 0.714285em 2em; padding: 0px;">
<li style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">Callable</li>
</ul>
<div style="border: 0px; line-height: 1.428571em; margin-bottom: 0.714285em; padding: 0px;">
In addition to serving as detailed real-world examples of abstract base classes, Python’s built-in types are automatically registered to these classes when you import <a href="http://pymotw.com/2/collections/index.html#module-collections" style="border: 0px; color: #cc6714; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none; word-break: break-all;" title="collections: Container data types."><tt style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">collections</span></tt></a>. This means you can safely use <tt style="background-color: #ecf0f3; font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">isinstance()</span></tt> to check parameters in your code to ensure that they support the API you need. The base classes can also be used to define your own collection types, since many of them provide concrete implementations of the internals and only need a few methods overridden. Refer to the standard library docs for collections for more details.</div>
<div style="background-color: #ffffcc; border: 1px solid rgb(255, 255, 102); line-height: 1.428571em; margin: 10px 0px; padding: 7px;">
<div style="border: 0px; display: inline; font-weight: bold; line-height: 1.428571em; margin-bottom: 5px; margin-right: 10px; padding: 0px;">
See also<span style="line-height: 1.428571em;">:</span></div>
<dl style="border: 0px; line-height: 1.428571em; margin: 0px 0px 0px 1em; padding: 0px;">
<dt style="border: 0px; font-weight: bold; line-height: 1.428571em; margin: 0px; padding: 0px;"><a href="http://docs.python.org/library/abc.html" style="border: 0px; color: #cc6714; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none; word-break: break-all;">abc</a></dt>
<dd style="border: 0px; line-height: 1.428571em; margin: 3px 0px 10px 30px; padding: 0px;">The standard library documentation for this module.</dd>
<dt style="border: 0px; font-weight: bold; line-height: 1.428571em; margin: 0px; padding: 0px;"><span style="line-height: 1.428571em;"></span><a href="http://www.python.org/dev/peps/pep-3119" style="border: 0px; color: #cc6714; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none; word-break: break-all;">PEP 3119</a></dt>
<dd style="border: 0px; line-height: 1.428571em; margin: 3px 0px 10px 30px; padding: 0px;">Introducing Abstract Base Classes</dd>
<dt style="border: 0px; font-weight: bold; line-height: 1.428571em; margin: 0px; padding: 0px;"><a href="http://pymotw.com/2/collections/index.html#module-collections" style="border: 0px; color: #cc6714; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none; word-break: break-all;" title="collections: Container data types."><tt style="background-color: transparent; font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">collections</span></tt></a></dt>
<dd style="border: 0px; line-height: 1.428571em; margin: 3px 0px 10px 30px; padding: 0px;">The collections module includes abstract base classes for several collection types.</dd>
<dt style="border: 0px; font-weight: bold; line-height: 1.428571em; margin: 0px; padding: 0px;"><a href="http://docs.python.org/library/collections.html" style="border: 0px; color: #cc6714; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none; word-break: break-all;">collections</a></dt>
<dd style="border: 0px; line-height: 1.428571em; margin: 3px 0px 10px 30px; padding: 0px;">The standard library documentation for collections.</dd>
<dt style="border: 0px; font-weight: bold; line-height: 1.428571em; margin: 0px; padding: 0px;"><span style="line-height: 1.428571em;"></span><a href="http://www.python.org/dev/peps/pep-3141" style="border: 0px; color: #cc6714; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none; word-break: break-all;">PEP 3141</a></dt>
<dd style="border: 0px; line-height: 1.428571em; margin: 3px 0px 10px 30px; padding: 0px;">A Type Hierarchy for Numbers</dd>
<dt style="border: 0px; font-weight: bold; line-height: 1.428571em; margin: 0px; padding: 0px;"><a href="http://en.wikipedia.org/wiki/Strategy_pattern" style="border: 0px; color: #cc6714; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none; word-break: break-all;">Wikipedia: Strategy Pattern</a></dt>
<dd style="border: 0px; line-height: 1.428571em; margin: 3px 0px 10px 30px; padding: 0px;">Description and examples of the strategy pattern.</dd>
<dt style="border: 0px; font-weight: bold; line-height: 1.428571em; margin: 0px; padding: 0px;"><a href="http://us.pycon.org/2009/conference/schedule/event/47/" style="border: 0px; color: #cc6714; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none; word-break: break-all;">Plugins and monkeypatching</a></dt>
<dd style="border: 0px; line-height: 1.428571em; margin: 3px 0px 10px 30px; padding: 0px;">PyCon 2009 presentation by Dr. André Roberge</dd></dl>
</div>
</div>
Jeffyehttp://www.blogger.com/profile/07633630042179070371noreply@blogger.com1tag:blogger.com,1999:blog-5448019410873028021.post-33286765618487776152015-08-22T21:06:00.001-07:002015-08-22T21:06:27.707-07:00__new__() in python<h1 class="post_title" itemprop="headline" style="border-bottom-color: rgb(207, 189, 131); border-bottom-style: dashed; border-width: 0px 0px 1px; color: #962000; font-family: 'Droid Serif', arial, serif; font-size: 24px; font-style: normal; font-weight: normal; line-height: 40px; margin: 0px; outline: 0px; padding: 0px; text-decoration: none;">
__new__() in python</h1>
<h6 class="posted_by" style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #962000; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: italic; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 30px; margin: 0px 0px 20px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-decoration: none; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
By : <a href="http://agiliq.com/blog/author/akshar/" style="border: 0px; color: black; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-weight: bold; margin: 0px; outline: 0px; padding: 0px; text-decoration: underline;">Akshar Raaj</a> from http://agiliq.com/blog/2012/06/__new__-python/</h6>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Lately I started looking into Django code and wish to write about internals of Django. I started with Django models and will be writing about it soon. For understanding how Django models work, I had to understand what metaclasses are and how metaclasses work. Metaclasses use method "__new__" and so I looked at what "__new__" does.</div>
<h3 style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #962000; font-family: 'Droid Serif', arial, serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; margin: 0px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-decoration: none; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
As __new__ is a static method, we will see a lttle bit about static methods and then __new__ in detail.</h3>
<ol style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 10px 0px 10px 30px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
<li style="border: 0px; list-style-type: decimal; margin: 0px; outline: 0px; padding: 0px;"><div style="border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; line-height: 20px; margin: 0px 0px 15px; outline: 0px; padding: 0px; text-align: left;">
Understanding static methods.</div>
</li>
<li style="border: 0px; list-style-type: decimal; margin: 0px; outline: 0px; padding: 0px;"><div style="border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; line-height: 20px; margin: 0px 0px 15px; outline: 0px; padding: 0px; text-align: left;">
Understanding method "__new__" of any class. We will see how to override method __new__ in a class.</div>
</li>
</ol>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Also, I will be trying all the code we write here on Ipython and I suggest you to try everything on Ipython as well.</div>
<h3 style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #962000; font-family: 'Droid Serif', arial, serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; margin: 0px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-decoration: none; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Static methods</h3>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
A little bit about instance methods first. Let's write a class.</div>
<div class="highlight" style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 10px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
<pre class="prettyprint" style="background-color: #fafaf3; border: 3px solid rgb(206, 199, 147); margin: 0px; outline: 0px; overflow: scroll; padding: 5px; position: relative; z-index: 9999999;"><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">1</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">class</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">A</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">object</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">...</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">def</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">met</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">self</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">a</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">b</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">...</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">print</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">a</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">b</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">...</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span></pre>
</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
In this case, met() is an instance method. So, it is expected that we pass an instance of A as the first argument to met.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Let's create an object and call met() on the created object and pass two arguments to met().</div>
<div class="highlight" style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 10px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
<pre class="prettyprint" style="background-color: #fafaf3; border: 3px solid rgb(206, 199, 147); margin: 0px; outline: 0px; overflow: scroll; padding: 5px; position: relative; z-index: 9999999;"><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">4</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">obj</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">=</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">A</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">()</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">5</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">obj</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">.</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">met</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">1</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">2</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">1</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">2</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">#</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">output</span></span></pre>
</div>
<h4 style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #962000; font-family: 'Droid Serif', arial, serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-decoration: none; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
What happened here?</h4>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
When we called met(), we passed two arguments although met() expects three argument as per its definition. When we wrote obj.met(1, 2), interpreter took care of sending instance <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">obj</em> as the first argument to met() and 1 and 2 were passed as second and third arguments respectively.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Let's try calling met() without an instance or in other words let's call the method using class.</div>
<div class="highlight" style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 10px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
<pre class="prettyprint" style="background-color: #fafaf3; border: 3px solid rgb(206, 199, 147); margin: 0px; outline: 0px; overflow: scroll; padding: 5px; position: relative; z-index: 9999999;"><span class="k" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">6</span></span><span class="cp" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> A</span><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">met</span><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">1</span><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">2</span><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">---------------------------------------------------------------------------</span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">TypeError</span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">Traceback</span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">most recent call </span><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">last</span><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">/</span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">home</span><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">/</span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">akshar</span><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">/</span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">branding_git</span><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">/</span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">netconference</span><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">/</span><span class="nt" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;"><</span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">ipython</span></span><span class="na" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">-</span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">input</span><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">-</span><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">6</span><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">-</span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">e8b323dba928</span></span><span class="nt" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">></span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">in</span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="nt" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;"><</span><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">module</span><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">></span></span><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">()</span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">----></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">1</span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> A</span><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">met</span><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">1</span><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">2</span><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">TypeError</span><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> unbound method met</span><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">()</span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> must be called </span><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">with</span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> A instance </span><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">as</span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> first argument </span><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">got </span><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">int</span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> instance instead</span><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></pre>
</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
We defined met() as an instance method of class A, so it expected an instance of A as the first argument. As its very clear from the error, met() expected an instance of A as the first argument but got an 'int' instead.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
If we pass an instance of A as the first argument, it will work as expected.</div>
<div class="highlight" style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 10px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
<pre class="prettyprint" style="background-color: #fafaf3; border: 3px solid rgb(206, 199, 147); margin: 0px; outline: 0px; overflow: scroll; padding: 5px; position: relative; z-index: 9999999;"><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">7</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">A</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">.</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">met</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">obj</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">3</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">4</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">3</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">4</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">#</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">output</span></span></pre>
</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Notice that we called the method on class A and not on an instance of A. But we took care of sending an instance of A as the first argument to met() and it worked as expected.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Let's see static method now.</div>
<div class="highlight" style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 10px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
<pre class="prettyprint" style="background-color: #fafaf3; border: 3px solid rgb(206, 199, 147); margin: 0px; outline: 0px; overflow: scroll; padding: 5px; position: relative; z-index: 9999999;"><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">8</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">class</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">B</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">object</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">...</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">@</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">staticmethod</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">...</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">def</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">met</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">a</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">b</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">...</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">print</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">a</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">b</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">...</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span></pre>
</div>
<h4 style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #962000; font-family: 'Droid Serif', arial, serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-decoration: none; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
What does <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">@staticmethod</em> above the method definition do?</h4>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Its a <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">decorator</em> which changes a method to static method. It means the method is no longer an instance method, which means that the method does not expect its first argument to be an instance. So, for our method definition, the method does not expect its first argument to be an instance of B. Even if we call the method on an instance of B, current instance will not be passed as the first argument to this method, since its a <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">static method</em>. For <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">instance method</em> that we saw earlier the current instance was passed as the first argument.</div>
<div class="highlight" style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 10px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
<pre class="prettyprint" style="background-color: #fafaf3; border: 3px solid rgb(206, 199, 147); margin: 0px; outline: 0px; overflow: scroll; padding: 5px; position: relative; z-index: 9999999;"><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">9</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">B</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">.</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">met</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">5</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">6</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">5</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">6</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">#</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">output</span></span></pre>
</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Here we were able to call the method on the class and were not required to pass an instance of B as the first argument.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Let's call this method on an instance.</div>
<div class="highlight" style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 10px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
<pre class="prettyprint" style="background-color: #fafaf3; border: 3px solid rgb(206, 199, 147); margin: 0px; outline: 0px; overflow: scroll; padding: 5px; position: relative; z-index: 9999999;"><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">10</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">b</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">=</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">B</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">()</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">11</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">b</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">.</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">met</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">5</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">6</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">5</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">6</span></span></pre>
</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Here we called the method on an instance and passed two arguments. Since its a static method, the current instance i.e <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">b</em> was not passed as the first argument to met(). Had it not been a static method, current instance would have been passed as first argument, 5 as second argument and 6 as third argument.</div>
<h3 style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #962000; font-family: 'Droid Serif', arial, serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; margin: 0px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-decoration: none; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Understanding method <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">__new__</em> of a class. We will also see how to override method __new__ in a class.</h3>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
First let's see a little bit about <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">__init__</em></div>
<div class="highlight" style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 10px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
<pre class="prettyprint" style="background-color: #fafaf3; border: 3px solid rgb(206, 199, 147); margin: 0px; outline: 0px; overflow: scroll; padding: 5px; position: relative; z-index: 9999999;"><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">13</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">class</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">A</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">object</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">def</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">__init__</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">self</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">a</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">b</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">print</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="s" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">"init gets called"</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">print</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="s" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">"self is"</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">self</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">self</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">.</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">a</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">self</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">.</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">b</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">=</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">a</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">b</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span></pre>
</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
In __init__, we print something as we enter the method which is to validate that __init__ has been called. Then we print the first argument which is self and then we perform some assignments. __init__ is an instance method and expects the first argument to be an instance.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Let's call the class passing it two arguments. Keep in mind the part <strong style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">call the class</strong>, we are going to again use it in next few lines. If you have any confusion about the part <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">calling the class</em>, it will be clear in next few lines.</div>
<div class="highlight" style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 10px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
<pre class="prettyprint" style="background-color: #fafaf3; border: 3px solid rgb(206, 199, 147); margin: 0px; outline: 0px; overflow: scroll; padding: 5px; position: relative; z-index: 9999999;"><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">16</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">a</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">=</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">A</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">1</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">2</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">init</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">gets</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">called</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">#</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">output</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">self</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">is</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;"><</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">__main__</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">.</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">A</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">object</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">at</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="mh" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">0x3357210</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">></span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">#</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">output</span></span></pre>
</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Notice the second line of output which is "self is <__main__.A object at 0x3357210>". As apparent from second line of output, when __init__ is entered, object/instance has already been created by that time. Its only the assignment which is done in __init__, althought you could do some other stuff as well. But __init__ doesn't create the instance. __init__ receives the created instance as the first argument.</div>
<h4 style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #962000; font-family: 'Droid Serif', arial, serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-decoration: none; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
What creates the object?</h4>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Method __new__() creates the object.</div>
<h4 style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #962000; font-family: 'Droid Serif', arial, serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-decoration: none; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
What is __new__?</h4>
<ol style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 10px 0px 10px 30px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
<li style="border: 0px; list-style-type: decimal; margin: 0px; outline: 0px; padding: 0px;">__new__ is a static method which creates an instance. We will see the method signature soon. One reason i could think of having __new__ as a static method is because the instance has not been created yet when __new__ is called. So, they could not have had it as an instance method.</li>
<li style="border: 0px; list-style-type: decimal; margin: 0px; outline: 0px; padding: 0px;">__new__ gets called when you <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">call the class</em>. <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">Call the class</em> means issuing the statement "a=A(1,2)". Here A(1,2) is like calling the class. A is a class and we put two parenthesis in front of it and put some arguments between the parenthesis. So, its like "calling the class" similar to calling a method.</li>
<li style="border: 0px; list-style-type: decimal; margin: 0px; outline: 0px; padding: 0px;">__new__ must return the created object.</li>
<li style="border: 0px; list-style-type: decimal; margin: 0px; outline: 0px; padding: 0px;">Only when __new__ returns the created instance then __init__ gets called. If __new__ does not return an instance then __init__ would not be called. Remember __new__ is always called before __init__.</li>
<li style="border: 0px; list-style-type: decimal; margin: 0px; outline: 0px; padding: 0px;">__new__ gets passed all the arguments that we pass while calling the class. Also, it gets passed one extra argument that we will see soon.</li>
</ol>
<h4 style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #962000; font-family: 'Droid Serif', arial, serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-decoration: none; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
How was the instance created in the last example when we didn't define __new__?</h4>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
class A extends from object(Here we mean the class named object) i.e subclasses from object. object defines a method __new__, so A gets this method from object since its extending object. This inherited __new__ created the instance of A.</div>
<h4 style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #962000; font-family: 'Droid Serif', arial, serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-decoration: none; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Method signature of __new__</h4>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
__new__ receives the class whose instance need to be created as the first argument. This statement could be a little confusing, just continue reading and see the next example and again read it after seeing the example, it will be clear. The other arguments received by __new__ are same as what were passed while calling the class.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
So, __new__ receives all the arguments that we pass while calling the class. Also, it receives one extra argument. This extra argument is the class whose instance need to be created and it will be received as first argument by __new__.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
So, signature of __new__ could be written as:</div>
<div class="highlight" style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 10px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
<pre class="prettyprint" style="background-color: #fafaf3; border: 3px solid rgb(206, 199, 147); margin: 0px; outline: 0px; overflow: scroll; padding: 5px; position: relative; z-index: 9999999;"><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">__new__</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">cls</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">*</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">args</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">**</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">kwargs</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span></pre>
</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Let's see an example.</div>
<div class="highlight" style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 10px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
<pre class="prettyprint" style="background-color: #fafaf3; border: 3px solid rgb(206, 199, 147); margin: 0px; outline: 0px; overflow: scroll; padding: 5px; position: relative; z-index: 9999999;"><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">22</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">class</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">A</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">object</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">def</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">__new__</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">cls</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">*</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">args</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">**</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">kwargs</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">print</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">cls</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">print</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="s" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">"args is"</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">args</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">print</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="s" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">"kwargs is"</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">kwargs</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span></pre>
</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Here we override __new__ that we inherit from the superclass. We are printing all the arguments that this method receives so that we can check what gets passed to __new__. Let's try to create an instance of A by calling the class.</div>
<div class="highlight" style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 10px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
<pre class="prettyprint" style="background-color: #fafaf3; border: 3px solid rgb(206, 199, 147); margin: 0px; outline: 0px; overflow: scroll; padding: 5px; position: relative; z-index: 9999999;"><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">23</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">a</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">=</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">A</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">()</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;"><</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">class</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">'</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">__main__</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">.</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">A</span></span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">'</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">></span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">#</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">output</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">args</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">is</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">()</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">#</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">output</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">kwargs</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">is</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">{}</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">#</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">output</span></span></pre>
</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
As we mentioned earlier, __new__ gets called when we call the class. As is apparent from the output __new__ was called and it printed three lines of output.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
First line of output prints the first argument received by __new__. As we can see, it is <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">class A</em> itself. <strong style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">We tried to create an instance of A and __new__ of A received class A itself as the first argument.</strong> This is what we meant when we said "__new__ receives the class whose instance need to be created as the first argument". Now go back to the section "Method signature of __new__" and read it again.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
While calling the class we did not pass any arguments. So our output shows that args and kwargs did not receive anything.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
You can verify that all the arguments passed while calling the class gets sent to __new__. Just call the class passing it some arguments.</div>
<div class="highlight" style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 10px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
<pre class="prettyprint" style="background-color: #fafaf3; border: 3px solid rgb(206, 199, 147); margin: 0px; outline: 0px; overflow: scroll; padding: 5px; position: relative; z-index: 9999999;"><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">25</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">a</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">=</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">A</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">1</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">2</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">named</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">=</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">5</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;"><</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">class</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">'</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">__main__</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">.</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">A</span></span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">'</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">></span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">#</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">output</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">args</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">is</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">1</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">2</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">#</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">output</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">kwargs</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">is</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">{</span></span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">'</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">named</span></span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">'</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">5</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">}</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">#</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">output</span></span></pre>
</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
So, whatever arguments we passed while calling the class were passed to __new__ and were received by args and kwargs in __new__.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Let's check whether an object really gets created with how we have currently overridden __new__.</div>
<div class="highlight" style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 10px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
<pre class="prettyprint" style="background-color: #fafaf3; border: 3px solid rgb(206, 199, 147); margin: 0px; outline: 0px; overflow: scroll; padding: 5px; position: relative; z-index: 9999999;"><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">26</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">a</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">=</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">A</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">1</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">2</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;"><</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">class</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">'</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">__main__</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">.</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">A</span></span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">'</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">></span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">#</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">output</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">args</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">is</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">1</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">2</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">#</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">output</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">kwargs</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">is</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">{}</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">#</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">output</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">27</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">print</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">a</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">None</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">#</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">output</span></span></pre>
</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
We tried to create an instance and then tried printing the instance. But an instance of <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">A</em> was not created as apparent from the last print statement which printed None.</div>
<h4 style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #962000; font-family: 'Droid Serif', arial, serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-decoration: none; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Why did this happen?</h4>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
As we know if we don't return any value from a method, it implicitly returns None. Under the section "What is __new__", we mentioned that __new__ must return the created instance. Here we did not return the created instance from __new__, so None was implicitly returned and was assigned to <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">a</em>.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Let's combine __new__ and __init__.</div>
<div class="highlight" style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 10px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
<pre class="prettyprint" style="background-color: #fafaf3; border: 3px solid rgb(206, 199, 147); margin: 0px; outline: 0px; overflow: scroll; padding: 5px; position: relative; z-index: 9999999;"><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">29</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">class</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">A</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">object</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">def</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">__new__</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">cls</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">*</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">args</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">**</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">kwargs</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">print</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">cls</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">print</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">args</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">print</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">kwargs</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">def</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">__init__</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">self</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">a</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">b</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">print</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="s" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">"init gets called"</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">print</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="s" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">"self is"</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">self</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">self</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">.</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">a</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">self</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">.</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">b</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">=</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">a</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">b</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span></pre>
</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Let's try to create an instance of A.</div>
<div class="highlight" style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 10px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
<pre class="prettyprint" style="background-color: #fafaf3; border: 3px solid rgb(206, 199, 147); margin: 0px; outline: 0px; overflow: scroll; padding: 5px; position: relative; z-index: 9999999;"><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">31</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">a</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">=</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">A</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">1</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">2</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;"><</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">class</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">'</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">__main__</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">.</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">A</span></span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">'</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">></span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">#</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">output</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">1</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">2</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">#</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">output</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">{}</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">#</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">output</span></span></pre>
</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
As we mentioned earlier when a class gets called, first __new__ is called. Only when __new__ returns an instance then __init__ is called.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
In our previous example __new__ did not return an instance. So __init__ was not called. Had __init__ been called we would have seen the print statements that we have inside __init__.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Also since __new__ did not return an instance, <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">a</em> will still be None. Verify that.</div>
<div class="highlight" style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 10px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
<pre class="prettyprint" style="background-color: #fafaf3; border: 3px solid rgb(206, 199, 147); margin: 0px; outline: 0px; overflow: scroll; padding: 5px; position: relative; z-index: 9999999;"><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">32</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">print</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">a</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">None</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">#</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">output</span></span></pre>
</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Let's redefine the class to make it proper. We should return an instance from __new__, so that __init__ gets called and we get the desired behaviour.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
In case we don't override __new__, __new__ of parent class creates the instance and then __init__ gets called. In case we are overriding __new__, we should call the __new__ of parent class to get the created instance. However if you know how object creation works at the low level and you can implement it in your overridden __new__, you don't need to call parent __new__ to get the created instance. I don't know such details of how object creation work and will use the parent __new__ to get the created instance.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Once we get the created instance we can perform any extra operations we wish before returning the instance from __new__ method.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
For demonstration purpose, let us take a weird example where we need to add an attribute named 'created_at' to the created instance. For our case lets consider it needs to be done inside the __new__ method, althought we could have done it inside __init__.</div>
<div class="highlight" style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 10px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
<pre class="prettyprint" style="background-color: #fafaf3; border: 3px solid rgb(206, 199, 147); margin: 0px; outline: 0px; overflow: scroll; padding: 5px; position: relative; z-index: 9999999;"><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">33</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">import</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">datetime</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">35</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">class</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">A</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">object</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">def</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">__new__</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">cls</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">*</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">args</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">**</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">kwargs</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">new_instance</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">=</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">object</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">.</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">__new__</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">cls</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">*</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">args</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">**</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">kwargs</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">setattr</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">new_instance</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">'</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">created_at</span></span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">'</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">datetime</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">.</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">datetime</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">.</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">now</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">())</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="k" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">return</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">new_instance</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">def</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">__init__</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">self</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">a</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">b</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">print</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="s" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="str" style="border: 0px; color: #00946f; margin: 0px; outline: 0px; padding: 0px;">"inside init"</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">self</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">.</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">a</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">self</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">.</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">b</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">=</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">a</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">b</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span></pre>
</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
In the first line of __new__, we called the __new__ of parent class to get the created instance. __new__ of parent class should be passed the same arguments that we received in the overridden __new__. __new__ of parent class i.e __new__ of class<em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">object</em> knows how to create an instance and it returns the created instance.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
In the next line, we used inbuilt method setattr() to set an attribute 'created_at' on the newly created instance. The value we set for this attribute is the current time. This line is equivalent to writing new_instance.created_at=datetime.datetime.now().</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
In the final line we returned the newly created instance. Since we are returning an instance from __new__, __init__ will be called passing it whatever arguments were used in the class call. Let's verify this.</div>
<div class="highlight" style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 10px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
<pre class="prettyprint" style="background-color: #fafaf3; border: 3px solid rgb(206, 199, 147); margin: 0px; outline: 0px; overflow: scroll; padding: 5px; position: relative; z-index: 9999999;"><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">36</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">obj1</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">=</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">A</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">1</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">2</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">inside</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">init</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">#</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">output</span></span></pre>
</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
This statement suggests that __init__ was called. Let's print the created instance.</div>
<div class="highlight" style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 10px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
<pre class="prettyprint" style="background-color: #fafaf3; border: 3px solid rgb(206, 199, 147); margin: 0px; outline: 0px; overflow: scroll; padding: 5px; position: relative; z-index: 9999999;"><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">37</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">obj1</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">Out</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">37</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;"><</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">__main__</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">.</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">A</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">at</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="mh" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">0x3357390</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">></span></span></pre>
</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Notice that earlier when we were not returning anything from __new__ and were trying to print it, we were getting output as None. But this time the output shows that obj1 refers to an instance of A.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
We can verify that obj1 has an attribute 'created_at' and __init__ was properly executed by printing the three attributes of obj1.</div>
<div class="highlight" style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 10px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
<pre class="prettyprint" style="background-color: #fafaf3; border: 3px solid rgb(206, 199, 147); margin: 0px; outline: 0px; overflow: scroll; padding: 5px; position: relative; z-index: 9999999;"><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">37</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">print</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">obj1</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">.</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">created_at</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">2012</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">-</span></span><span class="mo" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">06</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">-</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">09</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">22</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">44</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="mf" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">30.376914</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">#</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">output</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">38</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">print</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">obj1</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">.</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">a</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">obj1</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">.</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">b</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">1</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">2</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">#</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">output</span></span></pre>
</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Let's see our final example.</div>
<div class="highlight" style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 10px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
<pre class="prettyprint" style="background-color: #fafaf3; border: 3px solid rgb(206, 199, 147); margin: 0px; outline: 0px; overflow: scroll; padding: 5px; position: relative; z-index: 9999999;"><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">60</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">class</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">B</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">object</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">pass</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">61</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">class</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">A</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">object</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">def</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">__new__</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">cls</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">*</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">args</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">**</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">kwargs</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">new_instance</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">=</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">object</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">.</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">__new__</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">B</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">*</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">args</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">**</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">kwargs</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="k" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">return</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">new_instance</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span></pre>
</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Pay attention to first line of A's new. Instead of passing <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">cls</em> as the first argument to<em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">object.__new__</em>, we pass <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">class B</em> as first argument. Let's see what happens in such case.</div>
<div class="highlight" style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 10px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
<pre class="prettyprint" style="background-color: #fafaf3; border: 3px solid rgb(206, 199, 147); margin: 0px; outline: 0px; overflow: scroll; padding: 5px; position: relative; z-index: 9999999;"><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">62</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">a</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">=</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">A</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">()</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">63</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">print</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">a</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;"><</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">__main__</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">.</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">B</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">object</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">at</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="mh" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">0x7f912c036750</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">></span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">#</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">Output</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">.</span></span><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">Tried</span></span><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">creating</span></span><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">an</span></span><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">instance</span></span><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">of</span></span><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">A</span></span><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">but</span></span><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">got</span></span><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">an</span></span><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">instance</span></span><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">of</span></span><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">B</span></span></pre>
</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
We tried to create an instance of A. But when we printed it, we realise that an instance of B has been created.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
This happened because we passed <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">class B</em> as the first argument to <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">object.__new__</em>. This shows that whatever class we pass to superclass' __new__, an instance of that class will be created.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Remember __new__ receives the class whose instance need to be created as first argument. So for any __new__, the first argument (which is <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">cls</em> for our case) will always refer to the class inside which __new__ is defined. So, for our case, <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">cls</em> will be <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">class A</em>.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
Here we wanted to create an instance of A. So, <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">class A</em> must be passed as first argument to <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">object.__new__</em>. Inside __new__ of <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">class A</em>, <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">cls</em> refers to <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">class A</em>. So, we need to pass <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">cls</em> as first argument to <em style="border: 0px; margin: 0px; outline: 0px; padding: 0px;">object.__new__</em>.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
That's why if we want proper behaviour we need to pass the same arguments to the superclass' __new__ as it was received by the overridden __new__.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
We can make that single line change in A's __new__ and our code will behave as expected.</div>
<div class="highlight" style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 10px; orphans: auto; outline: 0px; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
<pre class="prettyprint" style="background-color: #fafaf3; border: 3px solid rgb(206, 199, 147); margin: 0px; outline: 0px; overflow: scroll; padding: 5px; position: relative; z-index: 9999999;"><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">64</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">class</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">A</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">object</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">def</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">__new__</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">cls</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">*</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">args</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">**</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">kwargs</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">new_instance</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">=</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">object</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">.</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">__new__</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">(</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">cls</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">*</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">args</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">,</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">**</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">kwargs</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">)</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="k" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">return</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">new_instance</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">....</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">65</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">a</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">=</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">A</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">()</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="typ" style="border: 0px; color: #660066; margin: 0px; outline: 0px; padding: 0px;">In</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">[</span></span><span class="mi" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">66</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">]</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">:</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">print</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">a</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">
</span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;"><</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">__main__</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">.</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">A</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="kwd" style="border: 0px; color: #000088; margin: 0px; outline: 0px; padding: 0px;">object</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;">at</span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="mh" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="lit" style="border: 0px; color: #006666; margin: 0px; outline: 0px; padding: 0px;">0x7f912c0368d0</span></span><span class="o" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="pun" style="border: 0px; color: #666600; margin: 0px; outline: 0px; padding: 0px;">></span></span><span class="pln" style="border: 0px; color: black; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="err" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">#</span></span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">Output</span></span><span class="p" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">.</span></span><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">We</span></span><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">got</span></span><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">an</span></span><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">instance</span></span><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">of</span></span><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;"> </span><span class="n" style="border: 0px; margin: 0px; outline: 0px; padding: 0px;"><span class="com" style="border: 0px; color: #880000; margin: 0px; outline: 0px; padding: 0px;">A</span></span></pre>
</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin: 0px 0px 15px; orphans: auto; outline: 0px; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
That was all about method __new__. Hopefully next post would be about metaclasses and there we can see some more useful uses of __new__.</div>
<a class="noline" href="http://agiliq.com/blog/2012/06/__new__-python/" style="background-color: white; border: 0px; color: #962000; font-family: 'Droid Serif', arial, serif; font-size: 12px; font-weight: bold; line-height: 20px; margin: 0px; outline: 0px; padding: 0px; text-decoration: none;"></a><span style="background-color: white; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; line-height: 20px;"></span><br />
<div style="background-color: white; border: 0px; color: #393939; font-family: 'Droid Serif', arial, serif; font-size: 12px; line-height: 20px; margin-bottom: 15px; outline: 0px; padding: 0px;">
Hope you liked the post.</div>
Jeffyehttp://www.blogger.com/profile/07633630042179070371noreply@blogger.com0tag:blogger.com,1999:blog-5448019410873028021.post-25895363359273595762015-08-22T19:24:00.001-07:002015-08-22T19:25:00.979-07:00Speeding up non-vectorizable code with Cython08 Aug 2015<div class="cell border-box-sizing text_cell rendered" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; background-color: white; border: 1px solid transparent; box-sizing: border-box; color: #333333; display: flex; flex-direction: row; font-family: Roboto, HelveticaNeue-Light, 'Helvetica Neue Light', 'Helvetica Neue', sans-serif; font-size: 14px; line-height: 20px; margin: 0px; outline: none; padding: 0px; width: 847.5px;">
<div class="inner_cell" style="-webkit-box-align: stretch; -webkit-box-flex: 1; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; flex: 1 1 0%;">
<div class="text_cell_render border-box-sizing rendered_html" style="border-style: none; box-sizing: border-box; outline: none; padding: 0.5em 0em; resize: none; width: inherit;">
<div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
from: http://isaacslavitt.com/2015/08/08/speeding-up-with-cython/<br />
<br />
<div style="color: black;">
Most people know that when working with numeric arrays in Python, it is almost always faster to use vectorized operations instead of loops. But what happens when there's no obvious way to vectorize a slow function? There are many approaches for speeding up code, and plenty of <a href="http://quant-econ.net/py/need_for_speed.html" style="background-color: transparent; box-sizing: border-box; color: #333333; line-height: inherit; outline: 0px;">other</a> <a href="http://eng.climate.com/2015/04/09/numba-vs-cython-how-to-choose/" style="background-color: transparent; box-sizing: border-box; color: #333333; line-height: inherit; outline: 0px;">great</a> <a href="https://jakevdp.github.io/blog/2013/06/15/numba-vs-cython-take-2/" style="background-color: transparent; box-sizing: border-box; color: #333333; line-height: inherit; outline: 0px;">posts</a> on the subject. This is just a simple example with <a href="http://www.cython.org/" style="background-color: transparent; box-sizing: border-box; color: #333333; line-height: inherit; outline: 0px;">Cython</a>.</div>
</div>
<h3 id="the-set-up" style="box-sizing: border-box; color: inherit; font-family: 'Crimson Text', serif; font-size: 18.0040016174316px; line-height: 1; margin: 20px 0px 0px; text-rendering: optimizeLegibility;">
The set up</h3>
<div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
Let's take an <a href="http://qr.ae/RAEjsK" style="background-color: transparent; box-sizing: border-box; color: #333333; line-height: inherit; outline: 0px;">arbitrary example</a> of some function with logic that makes it annoying to vectorize:</div>
<blockquote style="border-left-color: rgb(238, 238, 238); border-left-style: solid; border-left-width: 5px; box-sizing: border-box; font-size: 17.5px; margin: 1em 2em; padding: 10px 20px;">
<div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-top: 12px !important; text-align: justify;">
Excel uses column names like A, B,C ... AA, AB, AC up to infinite. Write a function to convert the column name to its column index [...]</div>
</blockquote>
<div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
This function is a little contrived, but the same process applies to almost any function that is either difficult or impossible to vectorize (or is not actually faster when vectorized).</div>
<div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
Let's create an array of fake data:</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered" style="-webkit-box-align: stretch; -webkit-box-orient: vertical; align-items: stretch; background-color: white; border: 1px solid transparent; box-sizing: border-box; color: #333333; display: flex; flex-direction: column; font-family: Roboto, HelveticaNeue-Light, 'Helvetica Neue Light', 'Helvetica Neue', sans-serif; font-size: 14px; line-height: 20px; margin: 0px; outline: none; padding: 5px 5px 5px 0px; width: 847.5px;">
<div class="input" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: row; page-break-inside: avoid;">
<div class="prompt input_prompt" style="border-top-color: transparent; border-top-style: solid; border-top-width: 1px; box-sizing: border-box; color: navy; font-family: monospace; line-height: 1.21429em; margin: 0px; min-width: 11ex; padding: 0.4em; text-align: right;">
In [1]:</div>
<div class="inner_cell" style="-webkit-box-align: stretch; -webkit-box-flex: 1; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; flex: 1 1 0%;">
<div class="input_area" style="background: rgb(247, 247, 247); border-radius: 4px; border: 1px solid rgb(207, 207, 207); box-sizing: border-box; line-height: 1.21429em; margin-right: 4px; padding: 0.2em;">
<div class="highlight-ipynb" style="background: rgb(248, 248, 248); box-sizing: border-box;">
<pre class="ipynb" style="background: rgb(247, 247, 247); border-radius: 4px; border: none; box-shadow: none; box-sizing: border-box; color: black; font-family: 'Source Code Pro', monospace !important; font-size: 13px !important; line-height: 16.25px; overflow: auto; padding: 0px; white-space: pre-wrap; word-break: break-all; word-wrap: break-word;"><span class="kn" style="box-sizing: border-box; color: green; font-weight: bold;">import</span> <span class="nn" style="box-sizing: border-box; color: blue; font-weight: bold;">string</span>
<span class="kn" style="box-sizing: border-box; color: green; font-weight: bold;">import</span> <span class="nn" style="box-sizing: border-box; color: blue; font-weight: bold;">random</span>
<span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">def</span> <span class="nf" style="box-sizing: border-box; color: blue;">get_random_col_name</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">low</span><span class="o" style="box-sizing: border-box; color: #666666;">=</span><span class="mi" style="box-sizing: border-box; color: #666666;">1</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">high</span><span class="o" style="box-sizing: border-box; color: #666666;">=</span><span class="mi" style="box-sizing: border-box; color: #666666;">6</span><span class="p" style="box-sizing: border-box;">):</span>
<span class="n" style="box-sizing: border-box;">n</span> <span class="o" style="box-sizing: border-box; color: #666666;">=</span> <span class="n" style="box-sizing: border-box;">random</span><span class="o" style="box-sizing: border-box; color: #666666;">.</span><span class="n" style="box-sizing: border-box;">randint</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">low</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">high</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">return</span> <span class="s" style="box-sizing: border-box; color: #ba2121;">''</span><span class="o" style="box-sizing: border-box; color: #666666;">.</span><span class="n" style="box-sizing: border-box;">join</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">random</span><span class="o" style="box-sizing: border-box; color: #666666;">.</span><span class="n" style="box-sizing: border-box;">choice</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">string</span><span class="o" style="box-sizing: border-box; color: #666666;">.</span><span class="n" style="box-sizing: border-box;">ascii_uppercase</span><span class="p" style="box-sizing: border-box;">)</span> <span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">for</span> <span class="n" style="box-sizing: border-box;">_</span> <span class="ow" style="box-sizing: border-box; color: #aa22ff; font-weight: bold;">in</span> <span class="nb" style="box-sizing: border-box; color: green;">xrange</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">n</span><span class="p" style="box-sizing: border-box;">))</span>
<span class="n" style="box-sizing: border-box;">cols</span> <span class="o" style="box-sizing: border-box; color: #666666;">=</span> <span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">get_random_col_name</span><span class="p" style="box-sizing: border-box;">()</span> <span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">for</span> <span class="n" style="box-sizing: border-box;">_</span> <span class="ow" style="box-sizing: border-box; color: #aa22ff; font-weight: bold;">in</span> <span class="nb" style="box-sizing: border-box; color: green;">xrange</span><span class="p" style="box-sizing: border-box;">(</span><span class="mi" style="box-sizing: border-box; color: #666666;">1000000</span><span class="p" style="box-sizing: border-box;">)]</span>
<span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">print</span><span class="p" style="box-sizing: border-box;">(</span><span class="s" style="box-sizing: border-box; color: #ba2121;">'first ten cols (of {}):</span><span class="se" style="box-sizing: border-box; color: #bb6622; font-weight: bold;">\n</span><span class="s" style="box-sizing: border-box; color: #ba2121;">{}'</span><span class="o" style="box-sizing: border-box; color: #666666;">.</span><span class="n" style="box-sizing: border-box;">format</span><span class="p" style="box-sizing: border-box;">(</span><span class="nb" style="box-sizing: border-box; color: green;">len</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">cols</span><span class="p" style="box-sizing: border-box;">),</span> <span class="s" style="box-sizing: border-box; color: #ba2121;">'</span><span class="se" style="box-sizing: border-box; color: #bb6622; font-weight: bold;">\n</span><span class="s" style="box-sizing: border-box; color: #ba2121;">'</span><span class="o" style="box-sizing: border-box; color: #666666;">.</span><span class="n" style="box-sizing: border-box;">join</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">cols</span><span class="p" style="box-sizing: border-box;">[:</span><span class="mi" style="box-sizing: border-box; color: #666666;">10</span><span class="p" style="box-sizing: border-box;">])))</span>
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper" style="-webkit-box-align: stretch; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; position: relative;">
<div class="output" style="-webkit-box-align: stretch; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column;">
<div class="output_area" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: row; padding: 0px; page-break-inside: avoid;">
<div class="prompt" style="box-sizing: border-box; font-family: monospace; line-height: 1.21429em; margin: 0px; min-width: 11ex; padding: 0px 0.4em; text-align: right;">
</div>
<div class="output_subarea output_stream output_stdout output_text" style="-webkit-box-flex: 1; box-sizing: border-box; color: black; flex: 1 1 0%; line-height: 1.21429em; padding: 0.4em 0.4em 0px;">
<pre style="background-color: transparent; border-radius: 0px; border: 0px; box-sizing: border-box; font-family: 'Source Code Pro', monospace !important; font-size: 13px !important; line-height: 16.25px; overflow: auto; padding: 0px; vertical-align: baseline; white-space: pre-wrap; word-break: break-all; word-wrap: break-word;">first ten cols (of 1000000):
HI
CURGQ
EOBCS
Y
GLRT
GTYJ
Y
JJG
K
JAZJ
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; background-color: white; border: 1px solid transparent; box-sizing: border-box; color: #333333; display: flex; flex-direction: row; font-family: Roboto, HelveticaNeue-Light, 'Helvetica Neue Light', 'Helvetica Neue', sans-serif; font-size: 14px; line-height: 20px; margin: 0px; outline: none; padding: 0px; width: 847.5px;">
<div class="inner_cell" style="-webkit-box-align: stretch; -webkit-box-flex: 1; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; flex: 1 1 0%;">
<div class="text_cell_render border-box-sizing rendered_html" style="border-style: none; box-sizing: border-box; color: black; outline: none; padding: 0.5em 0em; resize: none; width: inherit;">
<div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
Here's a plain Python implementation of the index calculation:</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered" style="-webkit-box-align: stretch; -webkit-box-orient: vertical; align-items: stretch; background-color: white; border: 1px solid transparent; box-sizing: border-box; color: #333333; display: flex; flex-direction: column; font-family: Roboto, HelveticaNeue-Light, 'Helvetica Neue Light', 'Helvetica Neue', sans-serif; font-size: 14px; line-height: 20px; margin: 0px; outline: none; padding: 5px 5px 5px 0px; width: 847.5px;">
<div class="input" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: row; page-break-inside: avoid;">
<div class="prompt input_prompt" style="border-top-color: transparent; border-top-style: solid; border-top-width: 1px; box-sizing: border-box; color: navy; font-family: monospace; line-height: 1.21429em; margin: 0px; min-width: 11ex; padding: 0.4em; text-align: right;">
In [2]:</div>
<div class="inner_cell" style="-webkit-box-align: stretch; -webkit-box-flex: 1; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; flex: 1 1 0%;">
<div class="input_area" style="background: rgb(247, 247, 247); border-radius: 4px; border: 1px solid rgb(207, 207, 207); box-sizing: border-box; line-height: 1.21429em; margin-right: 4px; padding: 0.2em;">
<div class="highlight-ipynb" style="background: rgb(248, 248, 248); box-sizing: border-box;">
<pre class="ipynb" style="background: rgb(247, 247, 247); border-radius: 4px; border: none; box-shadow: none; box-sizing: border-box; color: black; font-family: 'Source Code Pro', monospace !important; font-size: 13px !important; line-height: 16.25px; overflow: auto; padding: 0px; white-space: pre-wrap; word-break: break-all; word-wrap: break-word;"><span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">def</span> <span class="nf" style="box-sizing: border-box; color: blue;">column_to_index</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">col</span><span class="p" style="box-sizing: border-box;">):</span>
<span class="n" style="box-sizing: border-box;">ord_a</span> <span class="o" style="box-sizing: border-box; color: #666666;">=</span> <span class="mi" style="box-sizing: border-box; color: #666666;">65</span> <span class="c" style="box-sizing: border-box; color: #408080; font-style: italic;"># equivalent to ord('A')</span>
<span class="n" style="box-sizing: border-box;">col_index</span> <span class="o" style="box-sizing: border-box; color: #666666;">=</span> <span class="mi" style="box-sizing: border-box; color: #666666;">0</span>
<span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">for</span> <span class="n" style="box-sizing: border-box;">place</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">letter</span> <span class="ow" style="box-sizing: border-box; color: #aa22ff; font-weight: bold;">in</span> <span class="nb" style="box-sizing: border-box; color: green;">enumerate</span><span class="p" style="box-sizing: border-box;">(</span><span class="nb" style="box-sizing: border-box; color: green;">reversed</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">col</span><span class="p" style="box-sizing: border-box;">)):</span>
<span class="n" style="box-sizing: border-box;">col_index</span> <span class="o" style="box-sizing: border-box; color: #666666;">+=</span> <span class="p" style="box-sizing: border-box;">(</span><span class="mi" style="box-sizing: border-box; color: #666666;">26</span> <span class="o" style="box-sizing: border-box; color: #666666;">**</span> <span class="n" style="box-sizing: border-box;">place</span><span class="p" style="box-sizing: border-box;">)</span> <span class="o" style="box-sizing: border-box; color: #666666;">*</span> <span class="p" style="box-sizing: border-box;">(</span><span class="nb" style="box-sizing: border-box; color: green;">ord</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">letter</span><span class="p" style="box-sizing: border-box;">)</span> <span class="o" style="box-sizing: border-box; color: #666666;">-</span> <span class="n" style="box-sizing: border-box;">ord_a</span> <span class="o" style="box-sizing: border-box; color: #666666;">+</span> <span class="mi" style="box-sizing: border-box; color: #666666;">1</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">return</span> <span class="n" style="box-sizing: border-box;">col_index</span> <span class="o" style="box-sizing: border-box; color: #666666;">-</span> <span class="mi" style="box-sizing: border-box; color: #666666;">1</span>
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; background-color: white; border: 1px solid transparent; box-sizing: border-box; color: #333333; display: flex; flex-direction: row; font-family: Roboto, HelveticaNeue-Light, 'Helvetica Neue Light', 'Helvetica Neue', sans-serif; font-size: 14px; line-height: 20px; margin: 0px; outline: none; padding: 0px; width: 847.5px;">
<div class="inner_cell" style="-webkit-box-align: stretch; -webkit-box-flex: 1; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; flex: 1 1 0%;">
<div class="text_cell_render border-box-sizing rendered_html" style="border-style: none; box-sizing: border-box; color: black; outline: none; padding: 0.5em 0em; resize: none; width: inherit;">
<div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
Quick tests for sanity check:</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered" style="-webkit-box-align: stretch; -webkit-box-orient: vertical; align-items: stretch; background-color: white; border: 1px solid transparent; box-sizing: border-box; color: #333333; display: flex; flex-direction: column; font-family: Roboto, HelveticaNeue-Light, 'Helvetica Neue Light', 'Helvetica Neue', sans-serif; font-size: 14px; line-height: 20px; margin: 0px; outline: none; padding: 5px 5px 5px 0px; width: 847.5px;">
<div class="input" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: row; page-break-inside: avoid;">
<div class="prompt input_prompt" style="border-top-color: transparent; border-top-style: solid; border-top-width: 1px; box-sizing: border-box; color: navy; font-family: monospace; line-height: 1.21429em; margin: 0px; min-width: 11ex; padding: 0.4em; text-align: right;">
In [3]:</div>
<div class="inner_cell" style="-webkit-box-align: stretch; -webkit-box-flex: 1; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; flex: 1 1 0%;">
<div class="input_area" style="background: rgb(247, 247, 247); border-radius: 4px; border: 1px solid rgb(207, 207, 207); box-sizing: border-box; line-height: 1.21429em; margin-right: 4px; padding: 0.2em;">
<div class="highlight-ipynb" style="background: rgb(248, 248, 248); box-sizing: border-box;">
<pre class="ipynb" style="background: rgb(247, 247, 247); border-radius: 4px; border: none; box-shadow: none; box-sizing: border-box; color: black; font-family: 'Source Code Pro', monospace !important; font-size: 13px !important; line-height: 16.25px; overflow: auto; padding: 0px; white-space: pre-wrap; word-break: break-all; word-wrap: break-word;"><span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">assert</span> <span class="n" style="box-sizing: border-box;">column_to_index</span><span class="p" style="box-sizing: border-box;">(</span><span class="s" style="box-sizing: border-box; color: #ba2121;">'A'</span><span class="p" style="box-sizing: border-box;">)</span> <span class="o" style="box-sizing: border-box; color: #666666;">==</span> <span class="mi" style="box-sizing: border-box; color: #666666;">0</span>
<span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">assert</span> <span class="n" style="box-sizing: border-box;">column_to_index</span><span class="p" style="box-sizing: border-box;">(</span><span class="s" style="box-sizing: border-box; color: #ba2121;">'Z'</span><span class="p" style="box-sizing: border-box;">)</span> <span class="o" style="box-sizing: border-box; color: #666666;">==</span> <span class="mi" style="box-sizing: border-box; color: #666666;">25</span>
<span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">assert</span> <span class="n" style="box-sizing: border-box;">column_to_index</span><span class="p" style="box-sizing: border-box;">(</span><span class="s" style="box-sizing: border-box; color: #ba2121;">'AA'</span><span class="p" style="box-sizing: border-box;">)</span> <span class="o" style="box-sizing: border-box; color: #666666;">==</span> <span class="mi" style="box-sizing: border-box; color: #666666;">26</span>
<span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">assert</span> <span class="n" style="box-sizing: border-box;">column_to_index</span><span class="p" style="box-sizing: border-box;">(</span><span class="s" style="box-sizing: border-box; color: #ba2121;">'AB'</span><span class="p" style="box-sizing: border-box;">)</span> <span class="o" style="box-sizing: border-box; color: #666666;">==</span> <span class="mi" style="box-sizing: border-box; color: #666666;">27</span>
<span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">assert</span> <span class="n" style="box-sizing: border-box;">column_to_index</span><span class="p" style="box-sizing: border-box;">(</span><span class="s" style="box-sizing: border-box; color: #ba2121;">'ZAZ'</span><span class="p" style="box-sizing: border-box;">)</span> <span class="o" style="box-sizing: border-box; color: #666666;">==</span> <span class="p" style="box-sizing: border-box;">(</span><span class="mi" style="box-sizing: border-box; color: #666666;">26</span> <span class="o" style="box-sizing: border-box; color: #666666;">**</span> <span class="mi" style="box-sizing: border-box; color: #666666;">2</span><span class="p" style="box-sizing: border-box;">)</span> <span class="o" style="box-sizing: border-box; color: #666666;">*</span> <span class="mi" style="box-sizing: border-box; color: #666666;">26</span> <span class="o" style="box-sizing: border-box; color: #666666;">+</span> <span class="p" style="box-sizing: border-box;">(</span><span class="mi" style="box-sizing: border-box; color: #666666;">26</span> <span class="o" style="box-sizing: border-box; color: #666666;">**</span> <span class="mi" style="box-sizing: border-box; color: #666666;">1</span><span class="p" style="box-sizing: border-box;">)</span> <span class="o" style="box-sizing: border-box; color: #666666;">*</span> <span class="mi" style="box-sizing: border-box; color: #666666;">1</span> <span class="o" style="box-sizing: border-box; color: #666666;">+</span> <span class="p" style="box-sizing: border-box;">(</span><span class="mi" style="box-sizing: border-box; color: #666666;">26</span> <span class="o" style="box-sizing: border-box; color: #666666;">**</span> <span class="mi" style="box-sizing: border-box; color: #666666;">0</span><span class="p" style="box-sizing: border-box;">)</span> <span class="o" style="box-sizing: border-box; color: #666666;">*</span> <span class="mi" style="box-sizing: border-box; color: #666666;">26</span> <span class="o" style="box-sizing: border-box; color: #666666;">-</span> <span class="mi" style="box-sizing: border-box; color: #666666;">1</span>
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; background-color: white; border: 1px solid transparent; box-sizing: border-box; color: #333333; display: flex; flex-direction: row; font-family: Roboto, HelveticaNeue-Light, 'Helvetica Neue Light', 'Helvetica Neue', sans-serif; font-size: 14px; line-height: 20px; margin: 0px; outline: none; padding: 0px; width: 847.5px;">
<div class="inner_cell" style="-webkit-box-align: stretch; -webkit-box-flex: 1; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; flex: 1 1 0%;">
<div class="text_cell_render border-box-sizing rendered_html" style="border-style: none; box-sizing: border-box; color: black; outline: none; padding: 0.5em 0em; resize: none; width: inherit;">
<div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
Let's see how fast this implementation is when run on all 1,000,000 random column names:</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered" style="-webkit-box-align: stretch; -webkit-box-orient: vertical; align-items: stretch; background-color: white; border: 1px solid transparent; box-sizing: border-box; color: #333333; display: flex; flex-direction: column; font-family: Roboto, HelveticaNeue-Light, 'Helvetica Neue Light', 'Helvetica Neue', sans-serif; font-size: 14px; line-height: 20px; margin: 0px; outline: none; padding: 5px 5px 5px 0px; width: 847.5px;">
<div class="input" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: row; page-break-inside: avoid;">
<div class="prompt input_prompt" style="border-top-color: transparent; border-top-style: solid; border-top-width: 1px; box-sizing: border-box; color: navy; font-family: monospace; line-height: 1.21429em; margin: 0px; min-width: 11ex; padding: 0.4em; text-align: right;">
In [4]:</div>
<div class="inner_cell" style="-webkit-box-align: stretch; -webkit-box-flex: 1; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; flex: 1 1 0%;">
<div class="input_area" style="background: rgb(247, 247, 247); border-radius: 4px; border: 1px solid rgb(207, 207, 207); box-sizing: border-box; line-height: 1.21429em; margin-right: 4px; padding: 0.2em;">
<div class="highlight-ipynb" style="background: rgb(248, 248, 248); box-sizing: border-box;">
<pre class="ipynb" style="background: rgb(247, 247, 247); border-radius: 4px; border: none; box-shadow: none; box-sizing: border-box; color: black; font-family: 'Source Code Pro', monospace !important; font-size: 13px !important; line-height: 16.25px; overflow: auto; padding: 0px; white-space: pre-wrap; word-break: break-all; word-wrap: break-word;"><span class="o" style="box-sizing: border-box; color: #666666;">%</span><span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">time</span> <span class="n" style="box-sizing: border-box;">idxs</span> <span class="o" style="box-sizing: border-box; color: #666666;">=</span> <span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">column_to_index</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">col</span><span class="p" style="box-sizing: border-box;">)</span> <span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">for</span> <span class="n" style="box-sizing: border-box;">col</span> <span class="ow" style="box-sizing: border-box; color: #aa22ff; font-weight: bold;">in</span> <span class="n" style="box-sizing: border-box;">cols</span><span class="p" style="box-sizing: border-box;">]</span>
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper" style="-webkit-box-align: stretch; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; position: relative;">
<div class="output" style="-webkit-box-align: stretch; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column;">
<div class="output_area" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: row; padding: 0px; page-break-inside: avoid;">
<div class="prompt" style="box-sizing: border-box; font-family: monospace; line-height: 1.21429em; margin: 0px; min-width: 11ex; padding: 0px 0.4em; text-align: right;">
</div>
<div class="output_subarea output_stream output_stdout output_text" style="-webkit-box-flex: 1; box-sizing: border-box; color: black; flex: 1 1 0%; line-height: 1.21429em; padding: 0.4em 0.4em 0px;">
<pre style="background-color: transparent; border-radius: 0px; border: 0px; box-sizing: border-box; font-family: 'Source Code Pro', monospace !important; font-size: 13px !important; line-height: 16.25px; overflow: auto; padding: 0px; vertical-align: baseline; white-space: pre-wrap; word-break: break-all; word-wrap: break-word;">CPU times: user 3.47 s, sys: 52.1 ms, total: 3.52 s
Wall time: 3.51 s
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; background-color: white; border: 1px solid transparent; box-sizing: border-box; color: #333333; display: flex; flex-direction: row; font-family: Roboto, HelveticaNeue-Light, 'Helvetica Neue Light', 'Helvetica Neue', sans-serif; font-size: 14px; line-height: 20px; margin: 0px; outline: none; padding: 0px; width: 847.5px;">
<div class="inner_cell" style="-webkit-box-align: stretch; -webkit-box-flex: 1; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; flex: 1 1 0%;">
<div class="text_cell_render border-box-sizing rendered_html" style="border-style: none; box-sizing: border-box; color: black; outline: none; padding: 0.5em 0em; resize: none; width: inherit;">
<h3 id="Speeding-things-up-with-Cython" style="box-sizing: border-box; color: inherit; font-family: 'Crimson Text', serif; font-size: 18.0040016174316px; line-height: 1; margin: 20px 0px 0px; text-rendering: optimizeLegibility;">
Speeding things up with Cython<a class="anchor-link" href="http://isaacslavitt.com/2015/08/08/speeding-up-with-cython/#Speeding-things-up-with-Cython" style="background-color: transparent; box-sizing: border-box; color: #333333; outline: 0px; padding: 0px 20px; text-decoration: none; visibility: hidden;"></a></h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; background-color: white; border: 1px solid transparent; box-sizing: border-box; color: #333333; display: flex; flex-direction: row; font-family: Roboto, HelveticaNeue-Light, 'Helvetica Neue Light', 'Helvetica Neue', sans-serif; font-size: 14px; line-height: 20px; margin: 0px; outline: none; padding: 0px; width: 847.5px;">
<div class="inner_cell" style="-webkit-box-align: stretch; -webkit-box-flex: 1; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; flex: 1 1 0%;">
<div class="text_cell_render border-box-sizing rendered_html" style="border-style: none; box-sizing: border-box; color: black; outline: none; padding: 0.5em 0em; resize: none; width: inherit;">
<div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
Cython is a superset of Python that allows for optimization by declaring types and compiling Python code. The documentation is very good, and there is a great intro <a href="http://docs.cython.org/src/userguide/language_basics.html" style="background-color: transparent; box-sizing: border-box; color: #333333; line-height: inherit; outline: 0px;">here</a>.</div>
<div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
A few general principles:</div>
<ul style="box-sizing: border-box; margin: 1em 2em 0px; padding: 0px;">
<li style="box-sizing: border-box;"><div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
<strong style="box-sizing: border-box;">Don't optimize until you know what the slow part is and that it will actually be problematic.</strong> Self explanatory, but <a href="http://c2.com/cgi/wiki?PrematureOptimization" style="background-color: transparent; box-sizing: border-box; color: #333333; line-height: inherit; outline: 0px;">it's a thing</a>. You may want to do some <a href="https://github.com/rkern/line_profiler" style="background-color: transparent; box-sizing: border-box; color: #333333; line-height: inherit; outline: 0px;">line profiling</a> first to see if the slow part is what you actually thought it was, and whether there are easy wins in pure Python.</div>
</li>
<li style="box-sizing: border-box;"><div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
<strong style="box-sizing: border-box;">Don't optimize if doing the optimization will take longer than the time it will save.</strong> Particularly true for one-off code, less true when writing something that will be run often. Usually a judgment call.</div>
</li>
<li style="box-sizing: border-box;"><div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
<strong style="box-sizing: border-box;">Start with working, vanilla Python and make small changes from there.</strong> Cython is a superset, so you can actually start with the original and it will compile. Think twice before doing a from-scratch rewrite of the function. You probably want tests or, at the very least, sanity checks to make sure the functionality is the same as your reference implementation. There is a big risk of introducing subtle bugs in a function that is supposed to be identical. Check the speed as you go, which brings us to the last point...</div>
</li>
<li style="box-sizing: border-box;"><div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
<strong style="box-sizing: border-box;">The cardinal rule of optimizing: stop when it's fast enough.</strong> Speeding things up can be entertaining but "optimization golf" doesn't really help anybody in the long term, especially when you're using so many clever tricks that the code becomes hard to read.</div>
</li>
</ul>
<div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
And a few key tricks for getting the fastest (correct) result possible:</div>
<ul style="box-sizing: border-box; margin: 1em 2em 0px; padding: 0px;">
<li style="box-sizing: border-box;"><div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
<strong style="box-sizing: border-box;">Declare appropriate types wherever practical.</strong> This is usually the first set of changes, and often the source of major efficiency wins.</div>
</li>
<li style="box-sizing: border-box;"><div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
<strong style="box-sizing: border-box;">Use simple control structures and avoid dynamic type iteration.</strong> Think about how you would write a given loop in C, because if you write it that way it will probably be faster than using idiomatic Python. For example, we are trained to avoid this kind of list iteration in Python:</div>
<pre style="border-radius: 4px; border: 0px; box-sizing: border-box; font-family: 'Source Code Pro', monospace !important; font-size: 13px; line-height: 16.25px; margin: 1em 2em; overflow: auto; padding: 0px; white-space: pre-wrap; word-break: break-all; word-wrap: break-word;"><code style="border-radius: 0px; border: 0px; box-sizing: border-box; display: block; font-family: 'Source Code Pro', monospace !important; font-size: 0.8em; padding: 0px;"> N = len(arr)
for i in range(N):
value = arr[i]
# do stuff with value
</code></pre>
<div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
in favor of the more Pythonic:</div>
<pre style="border-radius: 4px; border: 0px; box-sizing: border-box; font-family: 'Source Code Pro', monospace !important; font-size: 13px; line-height: 16.25px; margin: 1em 2em; overflow: auto; padding: 0px; white-space: pre-wrap; word-break: break-all; word-wrap: break-word;"><code style="border-radius: 0px; border: 0px; box-sizing: border-box; display: block; font-family: 'Source Code Pro', monospace !important; font-size: 0.8em; padding: 0px;"> for value in arr:
# do stuff with value
</code></pre>
<div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
but the <code style="border-radius: 4px; border: 0px; box-sizing: border-box; font-family: 'Source Code Pro', monospace !important; font-size: 0.8em; line-height: 16.25px; padding: 0px; white-space: pre-wrap;">for item in iterable</code> idiom involves a fair amount of overhead in order to abstract over all the possible types that could be in the iterable. In the example above, you may have already declared a type for <code style="border-radius: 4px; border: 0px; box-sizing: border-box; font-family: 'Source Code Pro', monospace !important; font-size: 0.8em; line-height: 16.25px; padding: 0px; white-space: pre-wrap;">value</code> since you know what kind of data your are iterating over.</div>
</li>
<li style="box-sizing: border-box;"><div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
<strong style="box-sizing: border-box;">If you're working with arrays, consider disabling much of the safety checking.</strong> Normally, Python looks out for you by raising exceptions if you go out of array bounds or try to do other invalid things with memory. Of course, these checks add overhead. If you have already taken things like array bounds into account, you can instruct the compiler to omit those types of checks with decorators on your function such as <code style="border-radius: 4px; border: 0px; box-sizing: border-box; font-family: 'Source Code Pro', monospace !important; font-size: 0.8em; line-height: 16.25px; padding: 0px; white-space: pre-wrap;">@cython.boundscheck(False)</code> or <code style="border-radius: 4px; border: 0px; box-sizing: border-box; font-family: 'Source Code Pro', monospace !important; font-size: 0.8em; line-height: 16.25px; padding: 0px; white-space: pre-wrap;">@cython.wraparound(False)</code>. A list of compiler directives can be found <a href="http://docs.cython.org/src/reference/compilation.html#compiler-directives" style="background-color: transparent; box-sizing: border-box; color: #333333; line-height: inherit; outline: 0px;">here</a>.</div>
</li>
</ul>
<div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
The <a href="http://docs.cython.org/src/reference/compilation.html" style="background-color: transparent; box-sizing: border-box; color: #333333; line-height: inherit; outline: 0px;">common workflow</a> would actually be to rewrite the slowest parts of your program in a <code style="border-radius: 4px; border: 0px; box-sizing: border-box; font-family: 'Source Code Pro', monospace !important; font-size: 0.8em; line-height: 16.25px; padding: 0px; white-space: pre-wrap;">.pyx</code> file, transpile the Python code to C code (<code style="border-radius: 4px; border: 0px; box-sizing: border-box; font-family: 'Source Code Pro', monospace !important; font-size: 0.8em; line-height: 16.25px; padding: 0px; white-space: pre-wrap;">.c</code>), and then compile that to a shared object file (<code style="border-radius: 4px; border: 0px; box-sizing: border-box; font-family: 'Source Code Pro', monospace !important; font-size: 0.8em; line-height: 16.25px; padding: 0px; white-space: pre-wrap;">.so</code>). From there, you can import your optimized functions in any <code style="border-radius: 4px; border: 0px; box-sizing: border-box; font-family: 'Source Code Pro', monospace !important; font-size: 0.8em; line-height: 16.25px; padding: 0px; white-space: pre-wrap;">.py</code> file and use them as normal.</div>
<div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
Conveniently, IPython notebooks have a plugin for writing Cython code directly in the notebook.</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered" style="-webkit-box-align: stretch; -webkit-box-orient: vertical; align-items: stretch; background-color: white; border: 1px solid transparent; box-sizing: border-box; color: #333333; display: flex; flex-direction: column; font-family: Roboto, HelveticaNeue-Light, 'Helvetica Neue Light', 'Helvetica Neue', sans-serif; font-size: 14px; line-height: 20px; margin: 0px; outline: none; padding: 5px 5px 5px 0px; width: 847.5px;">
<div class="input" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: row; page-break-inside: avoid;">
<div class="prompt input_prompt" style="border-top-color: transparent; border-top-style: solid; border-top-width: 1px; box-sizing: border-box; color: navy; font-family: monospace; line-height: 1.21429em; margin: 0px; min-width: 11ex; padding: 0.4em; text-align: right;">
In [5]:</div>
<div class="inner_cell" style="-webkit-box-align: stretch; -webkit-box-flex: 1; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; flex: 1 1 0%;">
<div class="input_area" style="background: rgb(247, 247, 247); border-radius: 4px; border: 1px solid rgb(207, 207, 207); box-sizing: border-box; line-height: 1.21429em; margin-right: 4px; padding: 0.2em;">
<div class="highlight-ipynb" style="background: rgb(248, 248, 248); box-sizing: border-box;">
<pre class="ipynb" style="background: rgb(247, 247, 247); border-radius: 4px; border: none; box-shadow: none; box-sizing: border-box; color: black; font-family: 'Source Code Pro', monospace !important; font-size: 13px !important; line-height: 16.25px; overflow: auto; padding: 0px; white-space: pre-wrap; word-break: break-all; word-wrap: break-word;"><span class="o" style="box-sizing: border-box; color: #666666;">%</span><span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">load_ext</span> <span class="n" style="box-sizing: border-box;">Cython</span>
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; background-color: white; border: 1px solid transparent; box-sizing: border-box; color: #333333; display: flex; flex-direction: row; font-family: Roboto, HelveticaNeue-Light, 'Helvetica Neue Light', 'Helvetica Neue', sans-serif; font-size: 14px; line-height: 20px; margin: 0px; outline: none; padding: 0px; width: 847.5px;">
<div class="inner_cell" style="-webkit-box-align: stretch; -webkit-box-flex: 1; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; flex: 1 1 0%;">
<div class="text_cell_render border-box-sizing rendered_html" style="border-style: none; box-sizing: border-box; color: black; outline: none; padding: 0.5em 0em; resize: none; width: inherit;">
<div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
Here is the re-write:</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered" style="-webkit-box-align: stretch; -webkit-box-orient: vertical; align-items: stretch; background-color: white; border: 1px solid transparent; box-sizing: border-box; color: #333333; display: flex; flex-direction: column; font-family: Roboto, HelveticaNeue-Light, 'Helvetica Neue Light', 'Helvetica Neue', sans-serif; font-size: 14px; line-height: 20px; margin: 0px; outline: none; padding: 5px 5px 5px 0px; width: 847.5px;">
<div class="input" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: row; page-break-inside: avoid;">
<div class="prompt input_prompt" style="border-top-color: transparent; border-top-style: solid; border-top-width: 1px; box-sizing: border-box; color: navy; font-family: monospace; line-height: 1.21429em; margin: 0px; min-width: 11ex; padding: 0.4em; text-align: right;">
In [6]:</div>
<div class="inner_cell" style="-webkit-box-align: stretch; -webkit-box-flex: 1; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; flex: 1 1 0%;">
<div class="input_area" style="background: rgb(247, 247, 247); border-radius: 4px; border: 1px solid rgb(207, 207, 207); box-sizing: border-box; line-height: 1.21429em; margin-right: 4px; padding: 0.2em;">
<div class="highlight-ipynb" style="background: rgb(248, 248, 248); box-sizing: border-box;">
<pre class="ipynb" style="background: rgb(247, 247, 247); border-radius: 4px; border: none; box-shadow: none; box-sizing: border-box; color: black; font-family: 'Source Code Pro', monospace !important; font-size: 13px !important; line-height: 16.25px; overflow: auto; padding: 0px; white-space: pre-wrap; word-break: break-all; word-wrap: break-word;"><span class="o" style="box-sizing: border-box; color: #666666;">%%</span><span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">cython</span>
<span class="n" style="box-sizing: border-box;">cimport</span> <span class="n" style="box-sizing: border-box;">cython</span>
<span class="nd" style="box-sizing: border-box; color: #aa22ff;">@cython.boundscheck</span><span class="p" style="box-sizing: border-box;">(</span><span class="bp" style="box-sizing: border-box; color: green;">False</span><span class="p" style="box-sizing: border-box;">)</span> <span class="c" style="box-sizing: border-box; color: #408080; font-style: italic;"># these don't contribute much in this example</span>
<span class="nd" style="box-sizing: border-box; color: #aa22ff;">@cython.wraparound</span><span class="p" style="box-sizing: border-box;">(</span><span class="bp" style="box-sizing: border-box; color: green;">False</span><span class="p" style="box-sizing: border-box;">)</span> <span class="c" style="box-sizing: border-box; color: #408080; font-style: italic;"># but are often useful for numeric arrays</span>
<span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">def</span> <span class="nf" style="box-sizing: border-box; color: blue;">column_to_index_cy</span><span class="p" style="box-sizing: border-box;">(</span><span class="nb" style="box-sizing: border-box; color: green;">str</span> <span class="n" style="box-sizing: border-box;">col</span><span class="p" style="box-sizing: border-box;">):</span>
<span class="n" style="box-sizing: border-box;">cdef</span> <span class="nb" style="box-sizing: border-box; color: green;">int</span> <span class="n" style="box-sizing: border-box;">i</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">n</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">letter</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">ord_a</span> <span class="c" style="box-sizing: border-box; color: #408080; font-style: italic;"># declare types - easy win</span>
<span class="n" style="box-sizing: border-box;">cdef</span> <span class="n" style="box-sizing: border-box;">unsigned</span> <span class="nb" style="box-sizing: border-box; color: green;">long</span> <span class="nb" style="box-sizing: border-box; color: green;">long</span> <span class="n" style="box-sizing: border-box;">col_index</span> <span class="o" style="box-sizing: border-box; color: #666666;">=</span> <span class="mi" style="box-sizing: border-box; color: #666666;">0</span> <span class="c" style="box-sizing: border-box; color: #408080; font-style: italic;"># max value alert! (see below)</span>
<span class="n" style="box-sizing: border-box;">ord_a</span> <span class="o" style="box-sizing: border-box; color: #666666;">=</span> <span class="mi" style="box-sizing: border-box; color: #666666;">65</span>
<span class="n" style="box-sizing: border-box;">n</span> <span class="o" style="box-sizing: border-box; color: #666666;">=</span> <span class="nb" style="box-sizing: border-box; color: green;">len</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">col</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="n" style="box-sizing: border-box;">i</span> <span class="o" style="box-sizing: border-box; color: #666666;">=</span> <span class="mi" style="box-sizing: border-box; color: #666666;">0</span>
<span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">while</span> <span class="n" style="box-sizing: border-box;">i</span> <span class="o" style="box-sizing: border-box; color: #666666;"><</span> <span class="n" style="box-sizing: border-box;">n</span><span class="p" style="box-sizing: border-box;">:</span> <span class="c" style="box-sizing: border-box; color: #408080; font-style: italic;"># very C-like</span>
<span class="n" style="box-sizing: border-box;">letter</span> <span class="o" style="box-sizing: border-box; color: #666666;">=</span> <span class="nb" style="box-sizing: border-box; color: green;">ord</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">col</span><span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">n</span> <span class="o" style="box-sizing: border-box; color: #666666;">-</span> <span class="n" style="box-sizing: border-box;">i</span> <span class="o" style="box-sizing: border-box; color: #666666;">-</span> <span class="mi" style="box-sizing: border-box; color: #666666;">1</span><span class="p" style="box-sizing: border-box;">])</span> <span class="c" style="box-sizing: border-box; color: #408080; font-style: italic;"># access str in reverse order</span>
<span class="n" style="box-sizing: border-box;">col_index</span> <span class="o" style="box-sizing: border-box; color: #666666;">+=</span> <span class="p" style="box-sizing: border-box;">(</span><span class="mi" style="box-sizing: border-box; color: #666666;">26</span> <span class="o" style="box-sizing: border-box; color: #666666;">**</span> <span class="n" style="box-sizing: border-box;">i</span><span class="p" style="box-sizing: border-box;">)</span> <span class="o" style="box-sizing: border-box; color: #666666;">*</span> <span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">letter</span> <span class="o" style="box-sizing: border-box; color: #666666;">-</span> <span class="n" style="box-sizing: border-box;">ord_a</span> <span class="o" style="box-sizing: border-box; color: #666666;">+</span> <span class="mi" style="box-sizing: border-box; color: #666666;">1</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="n" style="box-sizing: border-box;">i</span> <span class="o" style="box-sizing: border-box; color: #666666;">+=</span> <span class="mi" style="box-sizing: border-box; color: #666666;">1</span>
<span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">return</span> <span class="n" style="box-sizing: border-box;">col_index</span> <span class="o" style="box-sizing: border-box; color: #666666;">-</span> <span class="mi" style="box-sizing: border-box; color: #666666;">1</span>
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; background-color: white; border: 1px solid transparent; box-sizing: border-box; color: #333333; display: flex; flex-direction: row; font-family: Roboto, HelveticaNeue-Light, 'Helvetica Neue Light', 'Helvetica Neue', sans-serif; font-size: 14px; line-height: 20px; margin: 0px; outline: none; padding: 0px; width: 847.5px;">
<div class="inner_cell" style="-webkit-box-align: stretch; -webkit-box-flex: 1; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; flex: 1 1 0%;">
<div class="text_cell_render border-box-sizing rendered_html" style="border-style: none; box-sizing: border-box; color: black; outline: none; padding: 0.5em 0em; resize: none; width: inherit;">
<div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
Let's convince ourselves that these are equivalent at least for the first <span class="MathJax_Preview" style="box-sizing: border-box; color: inherit;"></span><span class="MathJax" id="MathJax-Element-1-Frame" style="border: 0px; box-sizing: border-box; direction: ltr; display: inline; float: none; line-height: normal; margin: 0px; max-height: none; max-width: none; min-height: 0px; min-width: 0px; padding: 0px; text-align: left; white-space: nowrap; word-spacing: normal; word-wrap: normal;"><nobr style="border: 0px; box-sizing: border-box; margin: 0px; max-height: none; max-width: none; min-height: 0px; min-width: 0px; padding: 0px; transition: none; vertical-align: 0px;"><span class="math" id="MathJax-Span-1" role="math" style="border: 0px; box-sizing: border-box; display: inline-block; margin: 0px; padding: 0px; position: static; transition: none; vertical-align: 0px; width: 1.778em;"><span style="border: 0px; box-sizing: border-box; display: inline-block; font-size: 21.9599990844727px; height: 0px; margin: 0px; padding: 0px; position: relative; transition: none; vertical-align: 0px; width: 1.459em;"><span style="border: 0px; box-sizing: border-box; clip: rect(1.141em 1000.002em 2.279em -999.998em); left: 0.002em; margin: 0px; padding: 0px; position: absolute; top: -2.138em; transition: none; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-2" style="border: 0px; box-sizing: border-box; display: inline; margin: 0px; padding: 0px; position: static; transition: none; vertical-align: 0px;"><span class="msubsup" id="MathJax-Span-3" style="border: 0px; box-sizing: border-box; display: inline; margin: 0px; padding: 0px; position: static; transition: none; vertical-align: 0px;"><span style="border: 0px; box-sizing: border-box; display: inline-block; height: 0px; margin: 0px; padding: 0px; position: relative; transition: none; vertical-align: 0px; width: 1.414em;"><span style="border: 0px; box-sizing: border-box; clip: rect(3.19em 1000.002em 4.146em -999.998em); left: 0.002em; margin: 0px; padding: 0px; position: absolute; top: -4.005em; transition: none; vertical-align: 0px;"><span class="mn" id="MathJax-Span-4" style="border: 0px; box-sizing: border-box; display: inline; font-family: MathJax_Main; margin: 0px; padding: 0px; position: static; transition: none; vertical-align: 0px;">26</span><span style="border: 0px; box-sizing: border-box; display: inline-block; height: 4.01em; margin: 0px; padding: 0px; position: static; transition: none; vertical-align: 0px; width: 0px;"></span></span><span style="border: 0px; box-sizing: border-box; left: 1.004em; margin: 0px; padding: 0px; position: absolute; top: -4.415em; transition: none; vertical-align: 0px;"><span class="mn" id="MathJax-Span-5" style="border: 0px; box-sizing: border-box; display: inline; font-family: MathJax_Main; font-size: 15.5257186889648px; margin: 0px; padding: 0px; position: static; transition: none; vertical-align: 0px;">4</span><span style="border: 0px; box-sizing: border-box; display: inline-block; height: 4.01em; margin: 0px; padding: 0px; position: static; transition: none; vertical-align: 0px; width: 0px;"></span></span></span></span></span><span style="border: 0px; box-sizing: border-box; display: inline-block; height: 2.143em; margin: 0px; padding: 0px; position: static; transition: none; vertical-align: 0px; width: 0px;"></span></span></span><span style="border-color: initial; border-left-style: solid; border-width: 0px 0px 0px 0.003em; box-sizing: border-box; display: inline-block; height: 1.225em; margin: 0px; overflow: hidden; padding: 0px; position: static; transition: none; vertical-align: -0.053em; width: 0px;"></span></span></nobr></span> columns:</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered" style="-webkit-box-align: stretch; -webkit-box-orient: vertical; align-items: stretch; background-color: white; border: 1px solid transparent; box-sizing: border-box; color: #333333; display: flex; flex-direction: column; font-family: Roboto, HelveticaNeue-Light, 'Helvetica Neue Light', 'Helvetica Neue', sans-serif; font-size: 14px; line-height: 20px; margin: 0px; outline: none; padding: 5px 5px 5px 0px; width: 847.5px;">
<div class="input" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: row; page-break-inside: avoid;">
<div class="prompt input_prompt" style="border-top-color: transparent; border-top-style: solid; border-top-width: 1px; box-sizing: border-box; color: navy; font-family: monospace; line-height: 1.21429em; margin: 0px; min-width: 11ex; padding: 0.4em; text-align: right;">
In [7]:</div>
<div class="inner_cell" style="-webkit-box-align: stretch; -webkit-box-flex: 1; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; flex: 1 1 0%;">
<div class="input_area" style="background: rgb(247, 247, 247); border-radius: 4px; border: 1px solid rgb(207, 207, 207); box-sizing: border-box; line-height: 1.21429em; margin-right: 4px; padding: 0.2em;">
<div class="highlight-ipynb" style="background: rgb(248, 248, 248); box-sizing: border-box;">
<pre class="ipynb" style="background: rgb(247, 247, 247); border-radius: 4px; border: none; box-shadow: none; box-sizing: border-box; color: black; font-family: 'Source Code Pro', monospace !important; font-size: 13px !important; line-height: 16.25px; overflow: auto; padding: 0px; white-space: pre-wrap; word-break: break-all; word-wrap: break-word;"><span class="kn" style="box-sizing: border-box; color: green; font-weight: bold;">from</span> <span class="nn" style="box-sizing: border-box; color: blue; font-weight: bold;">itertools</span> <span class="kn" style="box-sizing: border-box; color: green; font-weight: bold;">import</span> <span class="n" style="box-sizing: border-box;">permutations</span>
<span class="kn" style="box-sizing: border-box; color: green; font-weight: bold;">import</span> <span class="nn" style="box-sizing: border-box; color: blue; font-weight: bold;">string</span>
<span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">for</span> <span class="n" style="box-sizing: border-box;">n</span> <span class="ow" style="box-sizing: border-box; color: #aa22ff; font-weight: bold;">in</span> <span class="nb" style="box-sizing: border-box; color: green;">xrange</span><span class="p" style="box-sizing: border-box;">(</span><span class="mi" style="box-sizing: border-box; color: #666666;">1</span><span class="p" style="box-sizing: border-box;">,</span> <span class="mi" style="box-sizing: border-box; color: #666666;">5</span><span class="p" style="box-sizing: border-box;">):</span>
<span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">for</span> <span class="n" style="box-sizing: border-box;">perm</span> <span class="ow" style="box-sizing: border-box; color: #aa22ff; font-weight: bold;">in</span> <span class="n" style="box-sizing: border-box;">permutations</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">string</span><span class="o" style="box-sizing: border-box; color: #666666;">.</span><span class="n" style="box-sizing: border-box;">ascii_uppercase</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">n</span><span class="p" style="box-sizing: border-box;">):</span>
<span class="n" style="box-sizing: border-box;">col</span> <span class="o" style="box-sizing: border-box; color: #666666;">=</span> <span class="s" style="box-sizing: border-box; color: #ba2121;">''</span><span class="o" style="box-sizing: border-box; color: #666666;">.</span><span class="n" style="box-sizing: border-box;">join</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">perm</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">assert</span> <span class="n" style="box-sizing: border-box;">column_to_index_cy</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">col</span><span class="p" style="box-sizing: border-box;">)</span> <span class="o" style="box-sizing: border-box; color: #666666;">==</span> <span class="n" style="box-sizing: border-box;">column_to_index</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">col</span><span class="p" style="box-sizing: border-box;">)</span>
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; background-color: white; border: 1px solid transparent; box-sizing: border-box; color: #333333; display: flex; flex-direction: row; font-family: Roboto, HelveticaNeue-Light, 'Helvetica Neue Light', 'Helvetica Neue', sans-serif; font-size: 14px; line-height: 20px; margin: 0px; outline: none; padding: 0px; width: 847.5px;">
<div class="inner_cell" style="-webkit-box-align: stretch; -webkit-box-flex: 1; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; flex: 1 1 0%;">
<div class="text_cell_render border-box-sizing rendered_html" style="border-style: none; box-sizing: border-box; color: black; outline: none; padding: 0.5em 0em; resize: none; width: inherit;">
<div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
Now we can see how much faster the Cython version was:</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered" style="-webkit-box-align: stretch; -webkit-box-orient: vertical; align-items: stretch; background-color: white; border: 1px solid transparent; box-sizing: border-box; color: #333333; display: flex; flex-direction: column; font-family: Roboto, HelveticaNeue-Light, 'Helvetica Neue Light', 'Helvetica Neue', sans-serif; font-size: 14px; line-height: 20px; margin: 0px; outline: none; padding: 5px 5px 5px 0px; width: 847.5px;">
<div class="input" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: row; page-break-inside: avoid;">
<div class="prompt input_prompt" style="border-top-color: transparent; border-top-style: solid; border-top-width: 1px; box-sizing: border-box; color: navy; font-family: monospace; line-height: 1.21429em; margin: 0px; min-width: 11ex; padding: 0.4em; text-align: right;">
In [8]:</div>
<div class="inner_cell" style="-webkit-box-align: stretch; -webkit-box-flex: 1; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; flex: 1 1 0%;">
<div class="input_area" style="background: rgb(247, 247, 247); border-radius: 4px; border: 1px solid rgb(207, 207, 207); box-sizing: border-box; line-height: 1.21429em; margin-right: 4px; padding: 0.2em;">
<div class="highlight-ipynb" style="background: rgb(248, 248, 248); box-sizing: border-box;">
<pre class="ipynb" style="background: rgb(247, 247, 247); border-radius: 4px; border: none; box-shadow: none; box-sizing: border-box; color: black; font-family: 'Source Code Pro', monospace !important; font-size: 13px !important; line-height: 16.25px; overflow: auto; padding: 0px; white-space: pre-wrap; word-break: break-all; word-wrap: break-word;"><span class="o" style="box-sizing: border-box; color: #666666;">%</span><span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">time</span> <span class="n" style="box-sizing: border-box;">idxs</span> <span class="o" style="box-sizing: border-box; color: #666666;">=</span> <span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">column_to_index_cy</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">col</span><span class="p" style="box-sizing: border-box;">)</span> <span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">for</span> <span class="n" style="box-sizing: border-box;">col</span> <span class="ow" style="box-sizing: border-box; color: #aa22ff; font-weight: bold;">in</span> <span class="n" style="box-sizing: border-box;">cols</span><span class="p" style="box-sizing: border-box;">]</span>
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper" style="-webkit-box-align: stretch; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; position: relative;">
<div class="output" style="-webkit-box-align: stretch; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column;">
<div class="output_area" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: row; padding: 0px; page-break-inside: avoid;">
<div class="prompt" style="box-sizing: border-box; font-family: monospace; line-height: 1.21429em; margin: 0px; min-width: 11ex; padding: 0px 0.4em; text-align: right;">
</div>
<div class="output_subarea output_stream output_stdout output_text" style="-webkit-box-flex: 1; box-sizing: border-box; color: black; flex: 1 1 0%; line-height: 1.21429em; padding: 0.4em 0.4em 0px;">
<pre style="background-color: transparent; border-radius: 0px; border: 0px; box-sizing: border-box; font-family: 'Source Code Pro', monospace !important; font-size: 13px !important; line-height: 16.25px; overflow: auto; padding: 0px; vertical-align: baseline; white-space: pre-wrap; word-break: break-all; word-wrap: break-word;">CPU times: user 315 ms, sys: 56.3 ms, total: 371 ms
Wall time: 352 ms
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; background-color: white; border: 1px solid transparent; box-sizing: border-box; color: #333333; display: flex; flex-direction: row; font-family: Roboto, HelveticaNeue-Light, 'Helvetica Neue Light', 'Helvetica Neue', sans-serif; font-size: 14px; line-height: 20px; margin: 0px; outline: none; padding: 0px; width: 847.5px;">
<div class="inner_cell" style="-webkit-box-align: stretch; -webkit-box-flex: 1; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; flex: 1 1 0%;">
<div class="text_cell_render border-box-sizing rendered_html" style="border-style: none; box-sizing: border-box; color: black; outline: none; padding: 0.5em 0em; resize: none; width: inherit;">
<div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
This is an enormous speedup—the Cython version takes around 10% as long as the pure Python version.</div>
<h3 id="lower-level-means-worrying-about-more-details" style="box-sizing: border-box; color: inherit; font-family: 'Crimson Text', serif; font-size: 18.0040016174316px; line-height: 1; margin: 20px 0px 0px; text-rendering: optimizeLegibility;">
Lower level means worrying about more details</h3>
<div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
Here, for example, we have to remember that the C types we chose have strict limits and will silently overflow in many cases. While Python will often handle large numbers gracefully, our <code style="border-radius: 4px; border: 0px; box-sizing: border-box; font-family: 'Source Code Pro', monospace !important; font-size: 0.8em; line-height: 16.25px; padding: 0px; white-space: pre-wrap;">unsigned long long</code> will explode at a certain point:</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered" style="-webkit-box-align: stretch; -webkit-box-orient: vertical; align-items: stretch; background-color: white; border: 1px solid transparent; box-sizing: border-box; color: #333333; display: flex; flex-direction: column; font-family: Roboto, HelveticaNeue-Light, 'Helvetica Neue Light', 'Helvetica Neue', sans-serif; font-size: 14px; line-height: 20px; margin: 0px; outline: none; padding: 5px 5px 5px 0px; width: 847.5px;">
<div class="input" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: row; page-break-inside: avoid;">
<div class="prompt input_prompt" style="border-top-color: transparent; border-top-style: solid; border-top-width: 1px; box-sizing: border-box; color: navy; font-family: monospace; line-height: 1.21429em; margin: 0px; min-width: 11ex; padding: 0.4em; text-align: right;">
In [9]:</div>
<div class="inner_cell" style="-webkit-box-align: stretch; -webkit-box-flex: 1; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; flex: 1 1 0%;">
<div class="input_area" style="background: rgb(247, 247, 247); border-radius: 4px; border: 1px solid rgb(207, 207, 207); box-sizing: border-box; line-height: 1.21429em; margin-right: 4px; padding: 0.2em;">
<div class="highlight-ipynb" style="background: rgb(248, 248, 248); box-sizing: border-box;">
<pre class="ipynb" style="background: rgb(247, 247, 247); border-radius: 4px; border: none; box-shadow: none; box-sizing: border-box; color: black; font-family: 'Source Code Pro', monospace !important; font-size: 13px !important; line-height: 16.25px; overflow: auto; padding: 0px; white-space: pre-wrap; word-break: break-all; word-wrap: break-word;"><span class="o" style="box-sizing: border-box; color: #666666;">%</span><span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">matplotlib</span> <span class="n" style="box-sizing: border-box;">inline</span>
<span class="kn" style="box-sizing: border-box; color: green; font-weight: bold;">from</span> <span class="nn" style="box-sizing: border-box; color: blue; font-weight: bold;">matplotlib</span> <span class="kn" style="box-sizing: border-box; color: green; font-weight: bold;">import</span> <span class="n" style="box-sizing: border-box;">pyplot</span> <span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">as</span> <span class="n" style="box-sizing: border-box;">plt</span>
<span class="kn" style="box-sizing: border-box; color: green; font-weight: bold;">import</span> <span class="nn" style="box-sizing: border-box; color: blue; font-weight: bold;">seaborn</span> <span class="kn" style="box-sizing: border-box; color: green; font-weight: bold;">as</span> <span class="nn" style="box-sizing: border-box; color: blue; font-weight: bold;">sns</span>
<span class="kn" style="box-sizing: border-box; color: green; font-weight: bold;">from</span> <span class="nn" style="box-sizing: border-box; color: blue; font-weight: bold;">math</span> <span class="kn" style="box-sizing: border-box; color: green; font-weight: bold;">import</span> <span class="n" style="box-sizing: border-box;">log</span>
<span class="n" style="box-sizing: border-box;">ullong_max</span> <span class="o" style="box-sizing: border-box; color: #666666;">=</span> <span class="mi" style="box-sizing: border-box; color: #666666;">2</span><span class="o" style="box-sizing: border-box; color: #666666;">**</span><span class="mi" style="box-sizing: border-box; color: #666666;">64</span><span class="o" style="box-sizing: border-box; color: #666666;">-</span><span class="mi" style="box-sizing: border-box; color: #666666;">1</span>
<span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">print</span><span class="p" style="box-sizing: border-box;">(</span><span class="s" style="box-sizing: border-box; color: #ba2121;">'max value for unsigned long long: {}'</span><span class="o" style="box-sizing: border-box; color: #666666;">.</span><span class="n" style="box-sizing: border-box;">format</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">ullong_max</span><span class="p" style="box-sizing: border-box;">))</span>
<span class="n" style="box-sizing: border-box;">blowup</span> <span class="o" style="box-sizing: border-box; color: #666666;">=</span> <span class="n" style="box-sizing: border-box;">log</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">ullong_max</span><span class="p" style="box-sizing: border-box;">,</span> <span class="mi" style="box-sizing: border-box; color: #666666;">26</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">print</span><span class="p" style="box-sizing: border-box;">(</span><span class="s" style="box-sizing: border-box; color: #ba2121;">'max letters: {}'</span><span class="o" style="box-sizing: border-box; color: #666666;">.</span><span class="n" style="box-sizing: border-box;">format</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">blowup</span><span class="p" style="box-sizing: border-box;">))</span>
<span class="n" style="box-sizing: border-box;">xs</span> <span class="o" style="box-sizing: border-box; color: #666666;">=</span> <span class="nb" style="box-sizing: border-box; color: green;">xrange</span><span class="p" style="box-sizing: border-box;">(</span><span class="mi" style="box-sizing: border-box; color: #666666;">20</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="n" style="box-sizing: border-box;">ys</span> <span class="o" style="box-sizing: border-box; color: #666666;">=</span> <span class="p" style="box-sizing: border-box;">[</span><span class="mi" style="box-sizing: border-box; color: #666666;">26</span><span class="o" style="box-sizing: border-box; color: #666666;">**</span><span class="n" style="box-sizing: border-box;">x</span> <span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">for</span> <span class="n" style="box-sizing: border-box;">x</span> <span class="ow" style="box-sizing: border-box; color: #aa22ff; font-weight: bold;">in</span> <span class="n" style="box-sizing: border-box;">xs</span><span class="p" style="box-sizing: border-box;">]</span>
<span class="n" style="box-sizing: border-box;">fig</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">ax</span> <span class="o" style="box-sizing: border-box; color: #666666;">=</span> <span class="n" style="box-sizing: border-box;">plt</span><span class="o" style="box-sizing: border-box; color: #666666;">.</span><span class="n" style="box-sizing: border-box;">subplots</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">figsize</span><span class="o" style="box-sizing: border-box; color: #666666;">=</span><span class="p" style="box-sizing: border-box;">(</span><span class="mi" style="box-sizing: border-box; color: #666666;">10</span><span class="p" style="box-sizing: border-box;">,</span> <span class="mi" style="box-sizing: border-box; color: #666666;">6</span><span class="p" style="box-sizing: border-box;">))</span>
<span class="n" style="box-sizing: border-box;">plt</span><span class="o" style="box-sizing: border-box; color: #666666;">.</span><span class="n" style="box-sizing: border-box;">axhline</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">ullong_max</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">c</span><span class="o" style="box-sizing: border-box; color: #666666;">=</span><span class="s" style="box-sizing: border-box; color: #ba2121;">'r'</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">ls</span><span class="o" style="box-sizing: border-box; color: #666666;">=</span><span class="s" style="box-sizing: border-box; color: #ba2121;">'--'</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">label</span><span class="o" style="box-sizing: border-box; color: #666666;">=</span><span class="s" style="box-sizing: border-box; color: #ba2121;">'largest index we can store'</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="n" style="box-sizing: border-box;">plt</span><span class="o" style="box-sizing: border-box; color: #666666;">.</span><span class="n" style="box-sizing: border-box;">plot</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">xs</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">ys</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">label</span><span class="o" style="box-sizing: border-box; color: #666666;">=</span><span class="s" style="box-sizing: border-box; color: #ba2121;">'growth of column index'</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="n" style="box-sizing: border-box;">plt</span><span class="o" style="box-sizing: border-box; color: #666666;">.</span><span class="n" style="box-sizing: border-box;">semilogy</span><span class="p" style="box-sizing: border-box;">()</span>
<span class="n" style="box-sizing: border-box;">ax</span><span class="o" style="box-sizing: border-box; color: #666666;">.</span><span class="n" style="box-sizing: border-box;">annotate</span><span class="p" style="box-sizing: border-box;">(</span><span class="s" style="box-sizing: border-box; color: #ba2121;">'sadness ensues here'</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">xy</span><span class="o" style="box-sizing: border-box; color: #666666;">=</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">blowup</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">ullong_max</span><span class="p" style="box-sizing: border-box;">),</span>
<span class="n" style="box-sizing: border-box;">xycoords</span><span class="o" style="box-sizing: border-box; color: #666666;">=</span><span class="s" style="box-sizing: border-box; color: #ba2121;">'data'</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">xytext</span><span class="o" style="box-sizing: border-box; color: #666666;">=</span><span class="p" style="box-sizing: border-box;">(</span><span class="mf" style="box-sizing: border-box; color: #666666;">0.85</span><span class="p" style="box-sizing: border-box;">,</span> <span class="mf" style="box-sizing: border-box; color: #666666;">0.4</span><span class="p" style="box-sizing: border-box;">),</span> <span class="n" style="box-sizing: border-box;">textcoords</span><span class="o" style="box-sizing: border-box; color: #666666;">=</span><span class="s" style="box-sizing: border-box; color: #ba2121;">'axes fraction'</span><span class="p" style="box-sizing: border-box;">,</span>
<span class="n" style="box-sizing: border-box;">arrowprops</span><span class="o" style="box-sizing: border-box; color: #666666;">=</span><span class="nb" style="box-sizing: border-box; color: green;">dict</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">facecolor</span><span class="o" style="box-sizing: border-box; color: #666666;">=</span><span class="s" style="box-sizing: border-box; color: #ba2121;">'black'</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">shrink</span><span class="o" style="box-sizing: border-box; color: #666666;">=</span><span class="mf" style="box-sizing: border-box; color: #666666;">0.05</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">width</span><span class="o" style="box-sizing: border-box; color: #666666;">=</span><span class="mi" style="box-sizing: border-box; color: #666666;">2</span><span class="p" style="box-sizing: border-box;">),</span>
<span class="n" style="box-sizing: border-box;">horizontalalignment</span><span class="o" style="box-sizing: border-box; color: #666666;">=</span><span class="s" style="box-sizing: border-box; color: #ba2121;">'center'</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">verticalalignment</span><span class="o" style="box-sizing: border-box; color: #666666;">=</span><span class="s" style="box-sizing: border-box; color: #ba2121;">'top'</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="n" style="box-sizing: border-box;">plt</span><span class="o" style="box-sizing: border-box; color: #666666;">.</span><span class="n" style="box-sizing: border-box;">xlabel</span><span class="p" style="box-sizing: border-box;">(</span><span class="s" style="box-sizing: border-box; color: #ba2121;">'number of letters in column name'</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="n" style="box-sizing: border-box;">plt</span><span class="o" style="box-sizing: border-box; color: #666666;">.</span><span class="n" style="box-sizing: border-box;">ylabel</span><span class="p" style="box-sizing: border-box;">(</span><span class="s" style="box-sizing: border-box; color: #ba2121;">'column index'</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="n" style="box-sizing: border-box;">plt</span><span class="o" style="box-sizing: border-box; color: #666666;">.</span><span class="n" style="box-sizing: border-box;">legend</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box;">loc</span><span class="o" style="box-sizing: border-box; color: #666666;">=</span><span class="s" style="box-sizing: border-box; color: #ba2121;">'lower right'</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box;">fontsize</span><span class="o" style="box-sizing: border-box; color: #666666;">=</span><span class="mi" style="box-sizing: border-box; color: #666666;">16</span><span class="p" style="box-sizing: border-box;">)</span>
<span class="n" style="box-sizing: border-box;">plt</span><span class="o" style="box-sizing: border-box; color: #666666;">.</span><span class="n" style="box-sizing: border-box;">show</span><span class="p" style="box-sizing: border-box;">()</span>
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper" style="-webkit-box-align: stretch; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; position: relative;">
<div class="output" style="-webkit-box-align: stretch; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column;">
<div class="output_area" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: row; padding: 0px; page-break-inside: avoid;">
<div class="prompt" style="box-sizing: border-box; font-family: monospace; line-height: 1.21429em; margin: 0px; min-width: 11ex; padding: 0px 0.4em; text-align: right;">
</div>
<div class="output_subarea output_stream output_stdout output_text" style="-webkit-box-flex: 1; box-sizing: border-box; color: black; flex: 1 1 0%; line-height: 1.21429em; padding: 0.4em 0.4em 0px;">
<pre style="background-color: transparent; border-radius: 0px; border: 0px; box-sizing: border-box; font-family: 'Source Code Pro', monospace !important; font-size: 13px !important; line-height: 16.25px; overflow: auto; padding: 0px; vertical-align: baseline; white-space: pre-wrap; word-break: break-all; word-wrap: break-word;">max value for unsigned long long: 18446744073709551615
max letters: 13.6157474274
</pre>
</div>
</div>
<div class="output_area" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: row; padding: 0px; page-break-inside: avoid;">
<div class="prompt" style="box-sizing: border-box; font-family: monospace; line-height: 1.21429em; margin: 0px; min-width: 11ex; padding: 0px 0.4em; text-align: right;">
</div>
<div class="output_png output_subarea " style="-webkit-box-flex: 1; box-sizing: border-box; flex: 1 1 0%; padding: 0.4em 0.4em 0px;">
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAmwAAAF8CAYAAAByq+cjAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xl4FFWi/vFvOntI2Jt9NZATRBEFARGRnaCiCO4sUQFH1NFZ1LnjzDiLep1xfs6dGe8o4zYC4i4uIIRdEJBVEEFyAmHfE7ZsZOuu3x8JXlSUDiSpTuf9PE+edJXVXa9w6LyprlMV5jgOIiIiIhK8PG4HEBEREZEfp8ImIiIiEuRU2ERERESCnAqbiIiISJBTYRMREREJcipsIiIiIkEuwu0AlaW01OccO1bgdgypARo0iENjRQKl8SKB0lipmRzH4avtR/HWj6F5ozrVtl+vNyGsItuHTGGLiAh3O4LUEBorUhEaLxIojZWa59DRAqbOtWzZdYyeFzblJ9d3djvSDwqZwiYiIiISiFKfnzmrdjNz+U5KfX66JDbi5n6Jbsf6USpsIiIiUmts3XucqWmWfdn51IuPYvSgJLoZL2FhFfqEstqpsImIiEjIKygs4d1PM1myYT9hQP9LWzLq6kTiYmpGFaoZKUVERETOgeM4rEk/zBsLtpKTX0xLbx1SU5Lp0LKe29EqJGgKmzFmIHArEAc8A5QCDwFRwP+z1m52MZ6IiIjUMNnHT/L6/Aw2Zh4hMsLDqKsvYGiPNkSE17yrmgVNYQNirbX3GGO6AkOA5sBeoAWw081gIiIiUnP4/H7mr9nLh8u2U1zi58J2DRg71NC0QZzb0c5Z0BQ2a+0sY0wd4EHgV8DLQCrQDRgHvOBiPBEREakBdhzIYUpaOrsP5REfG0nq0GR6dW4a9JMKzqZaCpsxpifwZ2ttf2OMB3ge6AIUAROstZnGmMaUfRT6uLU2yxhzGCgAjqE7MoiIiMiPOFlUygefbWfhur04DvS5uDm3DOhAfGyk29EqRZUXNmPMo8AYIK981Qggylrbu7zIPVu+7lnACzxtjPkQmAy8BIRRdi6biIiIyPes35rF6/MyOJZbRNMGsYxLSaZT2wZux6pU1XGEbRswEphWvtwHSAOw1q4yxnQvf5x6hueeaZ2IiIgIx3KLeGN+Busysgj3hDG8dzuu692WyBC860SVFzZr7QxjTLvTViUAOact+4wxHmut/3z35fUmnO9LSC2hsSIVofEigdJYqR4+v8OcFTuYOnsLJ4tKubB9Q+6/6RLaNKvrdrQq48akgxzKStsplVLWALKycivjZSTEeb0JGisSMI0XCZTGSvXYcziPKWnpbN+fQ1x0BKkphqsuaYEnLKxG/flXtNy7UdiWA8OBd40xvYCNLmQQERGRGqSoxMfHy3Ywd/Ue/I5Dj05NuH1gR+rFR7sdrVpUZ2Fzyr9/AAw2xiwvX76rGjOIiIhIDbNp+xGmzrVknyikcb0Yxg41XHxBI7djVatqKWzW2p1A7/LHDjCpOvYrIiIiNdeJ/GLeWriVVV8fwhMWRkrPNtxwZXuio0JvUsHZBM2Fc0VEREQA/I7Dso0HeHfxNvILS2nfPIHUlGTaNK29kzpU2ERERCRo7M/OZ2paOhl7TxAdFc4dgzoy4LJWeDw1+04F50uFTURERFxXUurjk8938cnnu/D5HS7t2JjRg5NoWDfG7WhBQYVNREREXJW+6xhT5loOHS2gQUI0owcncVmS1+1YQUWFTURERFyRd7KEdxZtY9lXBwgDBnZrxci+FxAbrXryXfoTERERkWrlOA4rNx/irUVbyS0ooXWTeFJTkrmgRejeqeB8qbCJiIhItTl8rIBpcy2bdx4jKsLDzf0TGdy9NRHhHrejBTUVNhEREalypT4/c1fv5uPlOykp9XPRBQ0ZO8TgrR/rdrQaQYVNREREqtS2fSeYkpbOvqx86sZFcvc1nejRqQlhYbX7Uh0VocImIiIiVaKgsJT3l2Ty6fp9OEDfS1pwc/9E6sREuh2txlFhExERkUrlOA7rbBbTF2RwIq+Y5o3iSE1JJql1fbej1VgqbCIiIlJpjpwo5PV5li8zjxAR7mHEVe0Z1rMtkRGaVHA+VNhERETkvPn8fhau3csHn+2gqMRHcpv6jEtJplnDOLejhYSgKWzGmIHArUAc8Iy1dqMxpikwy1p7ubvpRERE5IfsOpjLa3PS2XUolzoxEYwe3IkrL26mSQWVKGgKGxBrrb3HGNMVGAJsBB4FdrqaSkRERM6osLiUDz/bwfy1e3AcuKJzM24d2IG6cVFuRws5QVPYrLWzjDF1gAeBR40xk4BpwC/dTSYiIiLftWFbNtPnWY7kFNGkfixjUwyd2zV0O1bIqpbCZozpCfzZWtvfGOMBnge6AEXABGttpjGmMfAM8Li1NtsYM6h8mx7GmFHW2verI6uIiIj8sON5RbwxP4O1NotwTxjXXtGW4b3bERUZ7na0kFblhc0Y8ygwBsgrXzUCiLLW9i4vcs+Wr3sWaAw8bYz50Fo7qvz5U1XWRERE3OV3HJas38d7SzI5WeSjQ8t6jEsxtPLGux2tVqiOI2zbgJGUfbwJ0AdIA7DWrjLGdC9/nHqmJ1trx1VDRhEREfkBe7PymJKWTua+HGKjIxg71HB11xZ4NKmg2lR5YbPWzjDGtDttVQKQc9qyzxjjsdb6qzqLiIiIBK64xMfMFTtJW7Ubn9+he3IT7hjUkfrx0W5Hq3XcmHSQQ1lpO6XSyprXm3D2jUTQWJGK0XiRQIXSWNmQcZjn39vIgSP5eBvEMmlkFy6/sJnbsWotNwrbcmA48K4xphdll++oFFlZuZX1UhLCvN4EjRUJmMaLBCpUxkpOQTFvL9zK55sPERYGQ3u05oY+7YmJigiJ/79gUdFyX52FzSn//gEw2BizvHz5rmrMICIiImfgOA7LvjrAO4u2kV9YSttmCdyZkkzbZqFz1LAmC3Mc5+xb1QyOmr8EIlR+C5bqofEigarJY+XAkXymzbWk7z5OdGQ4I/tewMBurfB4NKmgqni9CRX6ww2aC+eKiIhI9Sop9TNn5S5mfb6TUp9D1w6NGTMkiYZ1Y9yOJt+hwiYiIlILZew5zpS0dA4cKaB+fBSjBydxWZJX9/8MUipsIiIitUh+YQnvLt7G0i8PEAYMuKwlI/smEhejShDM9LcjIiJSCziOw6oth3hrwVZyCkpo5a1DakoyiS3ruR1NAqDCJiIiEuKyjp9k2lzLph1HiYzwcFO/RIZc3pqIcI/b0SRAKmwiIiIhqtTnZ/6aPXy0bAfFpX46t2/I2KGGJvVj3Y4mFaTCJiIiEoIy959gyhzL3qw8EuIiuXNYMj0vbKpJBTWUCpuIiEgIOVlUyvtLMln8xT4c4Kouzbm5fwfiYyPdjibnQYVNREQkRKyzWUyfbzmeV0yzhnGkphhMmwZux5JKoMImIiJSwx3NKWT6/AzWb80mIjyMG/q055pebYmM0KSCUKHCJiIiUkP5/Q4Lv9jLjKXbKSr2YVrXZ1yKoXmjOm5Hk0qmwiYiIlID7T6Uy5S0dHYcyKVOTAR3DEumT5fmmlQQolTYREREapCiYh8fLdvBvDV78DsOvTo35bYBHalbJ8rtaFKFVNhERERqiI2ZR5g213IkpxBv/RjGDjVc1L6R27GkGgRNYTPGDARuBeKAZwDv6cvW2o0uxhMREXHNibwi3ly4ldVbDhPuCeOaXm0ZfmU7oiPD3Y4m1SRoChsQa629xxjTFRgCbPnOsgqbiIjUKn7HYemX+3lvcSYFRaUktqhLakoyrZrEux1NqlnQFDZr7SxjTB3gQeBRa2326cvuphMREale+7LzmZKWzra9J4iNDmfMkCT6dW2Jx6NJBbVRtRQ2Y0xP4M/W2v7GGA/wPNAFKAImWGszjTGNKfso9PHysvat5erIKSIi4raSUh8zV+xizspd+PwO3YyXOwYl0SAh2u1o4qIqL2zGmEeBMUBe+aoRQJS1tnd5kXu2fN2zlJ239rQx5kPg+tOXrbXvV3VWERERN23ZeZSpcy2Hjp2kYd1oxgw2dO3Y2O1YEgSq4wjbNmAkMK18uQ+QBmCtXWWM6V7+OPU7z1NBExGRWiG3oJh3Fm1j+aaDhIXB4O6tubFve2KigubMJXFZlY8Ea+0MY0y701YlADmnLfuMMR5rrb+qs4iIiAQTx3FYsekgby/aRt7JEto0jefOYcm0a1bX7WgSZNyo7jmUlbZTKq2seb0JZ99IBI0VqRiNFwlURcbK/qw8np+xkS+3ZhMdFc746zszvM8FhIfr/p/yfW4UtuXAcOBdY0wvKvFyHVlZuZX1UhLCvN4EjRUJmMaLBCrQsVLq8zNn1W5mLt9Jqc9Pl8RGjBmSRON6sRw9ml8NSSUYVPQXweosbE759w+AwcaY5eXLd1VjBhEREdds3XucqWmWfdn51KsTxR2Dk+huvLr/p5xVtRQ2a+1OoHf5YweYVB37FRERCQYFhSW892kmn27YD0C/S1ty09UXEBcT6XIyqSk0/URERKSKOI7DmvTDvLlgKyfyi2nZuA6pKcl0aFXP7WhSw6iwiYiIVIHsEyd5fV4GGzOPEBHuYWTfC0jp2YYITSqQc6DCJiIiUol8fj/z1+zlw2XbKS7x06ltA8alGJo2iHM7mtRgKmwiIiKVZMeBHKakpbP7UB7xsZGMG2q4onMzTSqQ86bCJiIicp5OFpXy0odfMXPZdhwHrry4Gbf070BCXJTb0SREqLCJiIich/Vbs5g+P4OjOUU0bRDLuJRkOrVt4HYsCTEqbCIiIufgWG4Rb8zPYF1GFuGeMG4dlMSArs2JjAh3O5qEIBU2ERGRCvD7HRav38f7SzIpLPbRsVU9xqUk07VTM90VQ6qMCpuIiEiA9hzOY0paOtv35xAXHUFqiuGqS1rg0aQCqWIqbCIiImdRVOLj4+U7mLd6Dz6/Q49OTbh9YEfqxUe7HU1qCRU2ERGRH7FpxxGmplmyTxTSuF4MY4YYuiQ2cjuW1DIqbCIiImeQk1/MWwu3svLrQ3jCwkjp2YYbrmxPdJQmFUj1U2ETERE5jeM4fLbxAO8u3kZ+YSntmyeQmpJMm6YJbkeTWkyFTUREpNyBI/lMSbNk7DlOdFQ4dwzqyIDLWuHxaFKBuCtoCpsxZiBwKxAHPAPEA/eU/+eHrLUn3MomIiKhraTUxyef72L2yl2U+hwu7diY0YOTaFg3xu1oIkAQFTYg1lp7jzGmKzAE6ExZYetJWZF70c1wIiISmtJ3HWPKXMuhowU0SIhm9OAkLkvyuh1L5FuCprBZa2cZY+oADwK/Ap611hYbYw4AA9xNJyIioSbvZAnvLNrGsq8OEAYM7NaKkX0vIDY6aH40inyjWkalMaYn8GdrbX9jjAd4HugCFAETrLWZxpjGlH0U+ri1NssYU2CMiQJaAAerI6eIiIQ+x3FYufkQby3aSm5BCa2bxJOakswFLeq6HU3kB1V5YTPGPAqMAfLKV40Aoqy1vcuL3LPl654FvMDTxpgPKfsI9N/lGX9S1TlFRCT0HT5WwLS5ls07jxEV4eHm/okM7t6aiHCP29FEflR1HGHbBowEppUv9wHSAKy1q4wx3csfp57huXdVQz4REQlxpT4/c1fv5uPlOykp9XPRBQ0ZO8TgrR/rdjSRgFR5YbPWzjDGtDttVQKQc9qyzxjjsdb6qzqLiIjUPpn7TjAlLZ29WfnUjYvk7ms60aNTE8J0/0+pQdw4szKHstJ2SqWVNa9XFzWUwGisSEVovNRM+SdLmDr7a+Z8vhPHgaG92nLntRcSHxdVZfvUWJGq4kZhWw4MB941xvQCNlbWC2dl5VbWS0kI83oTNFYkYBovNY/jOKyzWUxfkMGJvGKaN4ojNSWZpNb1OZlfxMn8oirZr8aKVERFy311Fjan/PsHwGBjzPLyZZ2nJiIileLIiUKmz89gw7ZsIsLDGHFVe4b1bEtkhCYVSM1WLYXNWrsT6F3+2AEmVcd+RUSkdvD5/Sxcu5cPPttBUYmP5Db1GZeSTLOGcW5HE6kUujqgiIjUaLsO5vLanHR2HcqlTkwEowd34sqLm2lSgYQUFTYREamRCotL+fCzHcxfuwfHgSs6N+PWgR2oW4WTCkTcosImIiI1zoZt2UyfZzmSU0ST+rGMTTF0btfQ7VgiVUaFTUREaozjeUW8MT+DtTaLcE8Y117RluG92xEVGe52NJEqFTqFrV07Gvqd760+um7TGTdv2O2iM67X9tpe22v7b23vCfvmvSUo8tTS7f2E8cFLc3hvSSYni3x0aFmPcSmGVt74GpFf22v779m968zrf0DoFDYREQlJu+o25/lut2HnZRAbHcHYoYaru7bAo0kFUouEOc73j0rVUI4uWCiB0MUtpSI0XtxTXOJj5oqdpK3ajc/v0D25CXcM6kj9+Gi3o52RxopUhNebUKHfOHSETUREgs7mnUeZlmY5fPwkjepGM2aI4ZIOjd2OJeIaFTYREQkaOQXFvL1wK59vPkRYGAy5vDUjrmpPTJR+XEntpn8BIiLiOsdxWPbVAd5ZtI38wlLaNkvgzpRk2jY78/0WfT4fHo9HF8eVWkM3VxMREVcdPFrAX99cz39mp1Pqc7h9YEd+N677D5Y1gDVrVjF69M0cOXKkGpOKuEdH2ERExBUlpX7mrNzFrM93Uupz6NqhMWOGJNGwbsxZn5uRYVmwYB433ngNv/3tHxgyZFjVBxZxkQqbiIhUu4w9x5mSls6BIwXUj49i9OAkLkvyBvwRZ3Z2FgDp6VuYNGkCo0en8vjjfyIiQj/WJDRpZIuISLXJLyzh3cXbWPrlAcKAAZe1ZGTfROJiKvbj6OjRo988zs3NZfLk/+Wrrzbyl788S1KSqeTUIu4LqsJmjBkA3G6tnWiMuQ4YCUQCz1prN7ibTkREzpXjOKzacoi3Fmwlp6CEVt46pKYkk9iy3jm93vHjR7+3bvnypdx220h+9rOHGTfurvONLBJUgqawGWMSga7AqZMXsoAWQBSwx61cIiJyfrKOn2TaXMumHUeJjPBwU79Ehlzemojwc5/3dvoRttPt3buHxx57hJUrV/DMM/9DfHz8Oe9DJJgETWGz1mYCfzPGTCtfNRG4BbgCuBaY6lY2ERGpuFKfn/lr9vDRsh0Ul/rp3K4BY4camjSIO+/X/qHCBlBcXMx7771NRkY6f/rT0/Tu3ee89yfitmopbMaYnsCfrbX9jTEe4HmgC1AETCgva2fKlg9kAxdWR04REakc2/fn8NqcdPZm5ZEQF0nqsGR6Xdi00q6bdqaPRL8rPDyCL79cr8ImIaHKC5sx5lFgDJBXvmoEEGWt7V1e5J4tX/ddk4FXAAd4pKpziojI+TtZVMqMJdtZ9MVeHKBPl+bc0r8D8bGRlbaPvLxcjh07fsb/FhMTQ+/efRg16hZGjboFj0eXG5XQUB1H2LZRNnng1EedfYA0AGvtKmNM99M3ttaOLf++ElhZDflERKQSrLNZvLEgg2O5RTRrGEdqisG0aVDp+9mzZw/Hjx8743+77bYxPPPM3yp9nyJuq/LCZq2dYYxpd9qqBCDntGWfMcZjrfVXdRYREal8R3MKmT4/g/Vbs4kID+OGPu25pldbIiOq5uhWevrXOI4DQNu27Rg8eCh79uxm7tw5fPHFWnw+H+Hh4VWybxG3uDHpIIey0nZKpZU1r/eHb2MicjqNFakIjZcz8/kdPlm+ndfnbOFkkY+LEhtx36hLaN20av+8jh49RPfu3bnhhhv42c9+Rnx8PAsXLmTx4oVs3LiB2bNncPfdd1dphh+isSJVxY3CthwYDrxrjOkFbKysF87Kyq2sl5IQ5vUmaKxIwDRezmz3oVympKWz40AudWIiuGtYMn26NCcsrOrfi6+//hbuumsSHo+HkycdTp7MpUuXHlxxxZUsWbKYqVNfZ/jwm6s0w5lorEhFVLTcV2dhc8q/fwAMNsYsL1/W1Q1FRGqIomIfHy3bwbw1e/A7Dr06N+W2AR2pWyeq2jJ4vd4zrr/xxptYsmQxn3++nKVLl9C379XVlkmkqoWdOg8gBDj6zUYCod+CpSI0Xv7PxswjvD7Pkn2iEG/9GMYONVzUvpHbsb7h8/kYOrQ/GzduYMSIUbz44n+qdf8aK1IRXm9Cha5xEzQXzhURkeB0Iq+INxduZfWWw4R7wrimV1uGX9mO6MjgOrE/PDyclJRr2LhxA59+uojMzG0kJnZwO5ZIpdAFakRE5Iz8jsOnG/bxm5dWsXrLYRJb1OX3d17OTf0Sg66snXLvvffTpk07jh8/xosvPu92HJFKoyNsIiLyPfuy85mals7WvSeIjQ5nzJAk+nVticdTOXcqqCrx8QkMGjSYV199iQUL5pGbm0NCQl23Y4mcNx1hExGRb5SU+pixdDt/eHU1W/eeoJvx8uSEXgy4rFXQl7VT7r33pzRo0JA9e3bzwgv/63YckUqhwiYiIgBs2XWMx19ZzawVO6kXH8WDo7pw/40X0yAh2u1oFdKuXTv69x8AQFrabHw+n8uJRM6fCpuISC2Xd7KEVz75mr++uZ7Dx08yuHtrnhjfk64dG7sd7ZyNG3cXMTExbNq0kenTp7odR+S8qbCJiNRSjuOwYtMBHntxJcu/OkibpvH8dlx3bh/Ukdjomn2Kc+/eV3HFFVcC8NFHM1xOI3L+VNhERGqhQ8cKePbtDbw8awvFpT5uHdCB36V2p33z0DlB/8YbbwJg1arPWbx4octpRM6PCpuISC1S6vMza8VOHn9lNV/vPEaXxEY8OaEnQ3u0IdwTWj8Sbrnldrp2vZTi4mJ9LCo1Xs0+5i0iIgHbtvcEU9LS2ZedT706UdxxXRLdjZewsJox+7OiPB4Pw4Zdx4YN61myZBEZGZakJON2LJFzElq/TomIyPcUFJYwda7lv19fx77sfPpd2pKnJvbk8uQmIVvWTpk4cRLt2l3AiRMneOmlF9yOI3LOdIRNRCREOY7DmvTDvLlgKyfyi2nZuA6pKcl0aFXP7WjVJj4+nkGDhvDyy5NZsGAeJ04cp169+m7HEqkwHWETEQlB2SdO8o/3NjL5o83kF5Yysu8F/P6uy2tVWTtl0qQHaNSoEfv27dWFdKXGUmETEQkhPr+ftFW7+e3Lq9iYeYRObRvwxIQeXNe7HRHhtfMtv3XrNvTrNxCAuXNnU1pa6nIikYoLqn+9xpgBxpiXyh9faIz5tzHmP8aYzm5nExEJdjsO5PDElLW8s3gbURHhTLiuEw/f1pWmDeLcjua6O+8cT0xMLJs3b+L116e4HUekwoKmsBljEoGuQEz5qgnAXqAQ2OlSLBGRoHeyqJQ3FmTw5NS17D6Ux5UXN+OpiT3pfVHzkJ9UEKiePa/gyiv7AGUX0nUcx+VEIhUTNIXNWptprf3baasSgeeA94Bx7qQSEQlu67dm8btXVrFg7V6a1I/lkdsvZfy1F5IQF+V2tKAzcuQtAKxevZKFC+e7nEakYqpllqgxpifwZ2ttf2OMB3ge6AIUAROstZlneNphoAA4RhAVSxGRYHAst4g35mewLiOLcE8Yw3u347rebYmMCHc7WtAaNepmXn55MuvXr+ONN6YyaNAQtyOJBKzKC5sx5lFgDJBXvmoEEGWt7V1e5J4tX/ddk4GXgDDgoarOKSJSE/j9DovX7+P9JZkUFvvo2Koe41KSadm4jtvRgp7H4+Gaa4azfv06li79lPT0LSQnd3I7lkhAquMI2zZgJDCtfLkPkAZgrV1ljOl++sbW2rHl39cBqdWQT0SkRthzOI8paels359DXHQEqSmGqy5pgUfnqQVs4sR7efPNaWzfnslLL73As8/+0+1IIgE5a2EzxtxrrZ182nIc8Fdr7f2B7MBaO8MY0+60VQlAzmnLPmOMx1rrDzDzD/J6E873JaSW0FiRinB7vBQWl/LWPMuHSzLx+R36dm3JhBsuokHdmLM/Wb4jgeuvH87f//53Fi9eQHh4CQ0bNqy0V3d7rEjoCuQI243GmOHA3YABXgbmnsc+cygrbadUSlkDyMrKrYyXkRDn9SZorEjA3B4vm3YcYdpcS9bxQhrXi2HMEEOXxEaUFpWQlVXiWq6aLDX1Hl5/fTp79+7liSf+m8ce+32lvK7bY0VqloqW+7OezG+tHQrMBizwJjDaWvvTc0pXZjlwDYAxphew8TxeS0QkJOXkF/Pix5v529tfcuREESk92/DE+J50SWzkdrQar2XLVvTvf+pCummUlKj4SvA7a2EzxgwAfkpZWbPAb4wxLc9hX6cuevMBUGiMWU7ZhIOfn8NriYiEJMdxWPrlfn7z0kpWfn2I9s0TePzO7tzSvwPRUZoBWlnuumsCsbFxbNmymSlTXnU7jshZBfKR6CvA3dbaxcaYMOA+YA3QItCdWGt3Ar3LHzvApIpHFREJbQeO5DMlzZKx5zjRUeHcMagjAy5rhcejSQWVrXv3HvTpcxXz589l5swPGT/+Hl1kWIJaIIWti7U2F74pW/8yxsyu2lgiIrVHSamfTz7fyeyVuyj1OVzasTGjByfRUJMKqtSoUbeyYME81qxZxbx5cxg69Bq3I4n8oEAKWyNjzAygPdAXmE7ZBAQRETlPdvcxpqRZDh4toEFCNHcMSqKb8bodq1a48cZRvPTSC6xbt4Y335yuwiZBLZA7CPwb+H9ALnCQssKmO+eKiJyHvJMlvDp7C395Yz2HjhYw8LJWPDmhp8paNQoLC+Paa4cDsHTpYjZt+srlRCI/LJDC1thaOxfAWuu31r4M1KvaWCIioclxHD7ffJDfvLSSZRsP0Mobz2PjujF6SBKx0dVyt0A5zYQJ95KY2JG8vDxeeeXfbscR+UGBvDsUGGNanVowxvQBCqsukohIaDp8rIBp8zLYvOMoUREebu6XyODLWxMRrtsluyUmJobBg4eQmbmVhQvnc+TIERo10qVTJPgE8i7xC+AToIMx5kvKLu+he3uKiASo1Fc2qeB3r6xm846jXNS+IU9M6MmwXm1V1oLAffc9iNfbhIMHD/D887pVlQSnQC6cuwboDvQCxgKJ1tqVVR1MRCQUZO47wZ9eW8P7S7aZPUX1AAAgAElEQVQTGxXOPddfyM9vuQRv/Vi3o0m5Zs2aM2DAIADmz0+juLjY5UQi3/eDH4kaY/5z2qIDnLpAjWOMwVqrmaIiIj+goLCU95dm8ukX+3CAvpc056Z+HYiPjXQ7mpzB3XdPZObMj0hP38Jrr73CPffocqESXH7sCNsn5V8xQEPgI2BG+bKIiJyB4zisTT/Mb15eyeIv9tGsURz/Nfoy7hzWSWUtiF16aTf69OkLwMcff4DjOGd5hkj1+sEjbNba9wCMMb8Cep66QbsxZhZldzoQEZHTHDlRyPT5GWzYlk1EeBgj+rRnWK+2REboPLWa4JZbbmPBgrmsW7eG2bNncu2117sdSeQbgbyLxAOnXxioJRBXNXFERGoev99h3po9/PblVWzYlk1ym/r88e4eXN+nvcpaDTJ8+Ai6dbscn8/H22+/6XYckW8J5LIeTwIbjDErKDuP7Qp0L1AREQB2HczltbR0dh3MpU5MBHcMTqbPxc11X8oaKCwsjOuuu541a1bx2WdL2LhxA126dHU7lggAYYF8Tm+MaUFZUXOAZdbaw1Ud7Bw4WVm5bmeQGsDrTUBjRQL1Q+OlsLiUDz/bwfy1e3AcuKJzU24d2JG6cVEupJTKUlRUxIABV7J1awa33z6Gf/zj+YCfq/cWqQivN6FCv9Wd9QibMaYBMJKyiQdhwMXGGMda+6dzi/ij+xoA3G6tnVi+3BSYZa29vLL3JSJyrr7cls3r8yxHcopoUj+WsUMNnds3dDuWVILo6GgGD05h69YMFi2az+HDh2nSpInbsUQCOoftXaDfd7at9GP9xphEoCvls1CNMWHAI8DOyt6XiMi5OJ5XxPMfbuIf723keF4x117Rlj+N76GyFmLuu+9BmjZtyqFDh3jhhefcjiMCBHYOW1Nr7aCqDmKtzQT+ZoyZVr7qXuB14JdVvW8RkR/jdxyWbNjPe59mcrKolMSWdUkdmkyrJvFuR5Mq0KRJEwYMGMybb77O/Plp/Nd//Zbo6Gi3Y0ktF8gRtvXGmEvOZyfGmJ7GmMXljz3GmMnGmBXGmMXlR9bOZBDwE6CHMWbU+exfRORc7TqQw59f/4Jpcy3gMHZIEr8e001lLcSNH38PderEk5FhefXVF92OIxLQEbaLgS+MMYf5v5u+O9baCwLZgTHmUWAMkFe+agQQZa3tbYzpCTxbvu5brLWjyp8/1Vr7fiD7EhGpLMUlPmau2Enaqt34/A7djZfbByXRIEFHWmqDLl26ctVVV5OW9gmzZn3Evfc+oJm/4qpACtsIvn/OWkUuAb2NskkLpz7q7AOkAVhrVxljup++sbV27HeWx1VgXyIi523zzqNMS7McPn4Sb4NYbh/Yka4dGrsdS6rZrbfewfz5aaxbt5aZMz/k+utvdDuS1GI/+JGoMWZ4+cN+QN/Tvq4u/wqItXYGUHraqgQg57RlnzFGV5YUEdflFBTz0szNPPvWBrJOnGTI5a351yMDVNZqqWuuuY7u3Xvg9/t555233I4jtdyPHWHrDswE+nPmI2pTz3GfOZSVtlM8p257db683oSzbySCxop8m+M4LFyzm1dnbia3oITEVvV44KaudGhdH4BYjZda67bbbmHVqs9ZvnwpO3emc/nlP36VKb23SFUJ6MK558sY0w5401p7hTFmJDDcWnuXMaYX8Dtr7bWVsBtdOFcCootbyukOHi1galo66buPEx0Zzo1XtWdg91aEe8oO/Gu81G7FxcUMHNgHa9O59dY7eO65yT+4rcaKVESlXzi3Ep1qhh8Ag40xy8uX76rGDCIiAJSU+pmzchezPt9Jqc/hksRGjBliaFQvxu1oEkSioqIYPDgFa9NZvHghhw4dpGnTZm7HklqoWo6wVRMdYZOA6LdgydhznClp6Rw4UkC9+ChGD0qim/GecRagxotkZ2czYMCVHDx4gEmTfsof//jUGbfTWJGKqOgRNp3sLyK1Rn5hCa/NSefP07/g4JEC+l/Wkqcm9KJ7chNdskF+UOPGjRk4cDAA8+alUVhYeJZniFS+QO4lmgI8yf/dSxQqcB02ERG3OY7D6i2HeXPhVnLyi2nprUNqSjIdWtZzO5rUEOPH/4SPPppBZuZWXn55Mg888DO3I0ktE8g5bM8BPwc2U7Hrr4mIuC7r+EmmzbNs2n6UyAgPo66+gKE92hARrg8YJHAXXXQxffv2Z/bsmXzyyUzuv/8hHZWVahVIYcuy1s6q8iQiIpWo1Odn/to9fPTZDopL/XRu14CxQw1NGsS5HU1qqNtvH828eXP44ou1fPDB+4wceZPbkaQWCaSwfWaM+Rtldyf45oN7a+3SKkslInIetu/PYUpaOnsO55EQF0nqsGR6XdhUR0TkvAwZMozu3XuwcuUK3n//bRU2qVaBFLaelH0Ueul31vev/DgiIufuZFEpM5ZuZ9G6vThAny7NuaV/B+JjI92OJiEgLCyM668fwcqVK1i27DPWrl1N9+493I4ltcRZC5u1tl815BAROS9fZGQxfX4Gx3KLaNYwjtQUg2nTwO1YEmJSU8czdeprpKd/zX/+84oKm1SbQGaJ9gV+Bpz+zudYawdUWSoRkQAdzSlk+vwM1m/NJiI8jOuvbMe1V7QjMkKTCqTyRUZGMnRoCunpX7N48QIOHNhP8+Yt3I4ltUAgH4m+BvwB2F2lSUREKsDvd1j0xV5mLN1OYbGPpNb1SU0xNG9Ux+1oEuLuu+9B3n33Lfbv38+//vVPnnzyz25HklogkMK211p7rjd6FxGpdLsP5TIlzbLjQA51YiK4c1gyfbo0x6NJBVINGjRoyMCBQ5g27TXmz0/jscceJy5Os4+lagVS2P5pjHkdWAT4ytc5KnEiUt2Kin18tHwH81bvwe849LqwKbcN7EjdOlFuR5NaZuLESXz00Qx27NjOSy9N5qGHfuF2JAlxgZzkcR/QArgK6Ff+pRmiIlKtvtp+hN+9soq0VbtpWDeaX9xyCfdc31llTVyRnNyJvn37ATB79kz8fr+7gSTkBXKErbm1tlOVJxEROYMT+cW8uSCD1VsOE+4JY1ivNlx/ZXuiI8Pdjia13O23j2Xu3DmsX7+O999/l/vum+B2JAlhgRxh+8wYM9wYE0i5ExGpFH7HYcmGffzmxZWs3nKYxBZ1+f2dl3Nzvw4qaxIUBg0awuWX9wRgxox3XE4joS6QEnY9MAHAGHNqnWOtrfR3TGPMAOB2a+1EY8xA4FYgDnjGWruxsvcnIsFpf3Y+U9LS2br3BLHR4YwZkkS/ri3xeDSpQIJHWFgYN9wwkhUrlrF8+TKWLVuGMZe4HUtCVCAXzm1WHUGMMYlAVyCmfFWstfYeY0xXYAigwiYS4kpKfcxasYvZK3fh8zt0M17uGJREg4Rot6OJnNHYsXcyZcqrfP31JiZPnsz//M8LbkeSEBXIhXN/T9mtqb7FWvunygxirc0E/maMmVa+PMsYUwd4EHi0MvclIsFny65jTJ1rOXS0gIZ1oxkz2NC1Y2O3Y4n8qIiICFJShvH115uYO3cue/bspnXrNm7HkhAUyDlsYad9RQM3AE0rshNjTE9jzOLyxx5jzGRjzApjzOLyI2tnek5j4DngcWttdkX2JyI1R97JEl755Gv++uZ6Dh8rYHD31jwxvqfKmtQY9977AC1btiI7O5sXXnjO7TgSos5a2Ky1f7DW/rH86zdAb+DiQHdgjHkUeImysgcwAoiy1vYG/gt49jtPOXU071nKiuHTxphRge5PRGoGx3FYsekAj724kuVfHaRN03h+O647tw/qSGy05jhJzVG/fgMGDRoCwIIF88jLy3M5kYSic3lXTABaV2D7bcBIYFr5ch8gDcBau8oY0/30ja2148q/p55DNhGpAQ4dK2BqmmXLrmNERXq4dUAHBnVvRbhH9/+UmunUhXR37tzBSy+9wM9//ojbkSTEnPXd0Riz47SvnUAm8EqgO7DWzgBKT1uVAOSctuwzxuhdWqQWKPX5mbViJ4+/spotu47RJbERT47vydAebVTWpEZLSjIMGjQIgDlzZulCulLpAjnC1p+yjynDAD9w3Fqb8+NP+VE5lJW2UzzW2koZ2V5vwtk3EkFjxQ1bdhzlf9/bwO6DuTRIiGbiiIvpc0kLwmrA/T81XiQQ48eP5+OPP2bDhvWkpX1Iaqo+KJLK84OFzRiTyhlmh5b/N87jXqLLgeHAu8aYXlTi5TqysnIr66UkhHm9CRor1aigsIT3lmzn0/X7AOjXtQU39UskLiaS7OzgP9dH40UClZKSQo8evVi2bClTpkzjmmtGuh1JglhFfxH8sSNsp46sfVdY+fqKFrZTr/UBMNgYs7x8+a4Kvo6I1ACO47DWZvHG/AxO5BfTonEdUlMMHVvVdzuaSJUZMWIUy5YtZcWKZaxY8Rm9e1/ldiQJEWGOc8aDaN9ijIkCDGUFb5O1tqSqg50DR78FSyB0xKTqZZ84yevzMtiYeYSIcA/Dr2zHsJ5tiAiveeepabxIoLzeBA4cOMaQIVezadNXjBx5E5Mnv+p2LAlSXm9Chc4HCeTCud2B94CjlB1da2qMGWmtXXluEUUkVPn8fhas3csHn22nuMRPp7YNGDfU0LRhnNvRRKpFREQEQ4dew6ZNX7F48SJ27txJu3bt3I4lISCQX3f/Cdxqrb3MWnspZZfo+GfVxhKRmmbnwRyenLKOtxdtIyoinPHXduLh27qqrEmtc999P6V16zYcO3aUyZN1IV2pHIEUtjrW2lWnFsqPrMX8yPYiUosUFpfy5oKtPDFlLbsO5XLlRc14amJPrry4eY2YASpS2RIS6jJw4OkX0tVH6nL+Ailsx4wxI04tGGNuBI5UXSQRqSk2bM3mty+vYv7aPTSpH8sjt3Vl/HUXkhAX5XY0EVfde+/91K/fgN27dzF58r/cjiMhIJDCdg/wmDHmiDHmKPAYcG/VxhKRYHYst4h/zfiKf76/kRN5xVzXux1/Gt+DTu0auh1NJChccEEiV1/dH4C0tE/w+XwuJ5Ka7qyTDqy1GcaY4UA+EA40sdZurfJkIhJ0/H6Hxev38f6STAqLfXRoVY/UoYaW3ni3o4kEnTFjUvn88+V06JBEbm4O9es3cDuS1GBnvayHMeZB4C5r7aXGmHaU3Qf0f6y1/66GfBWhy3pIQHSZhnOz53AeU9LS2b4/h9joCG7un0jfS1rgCfHz1DReJFBnGivZ2dk0btzYpUQSzCr9sh7AT4AeANbancaYy4DVQLAVNhGpAkUlPj5evoN5q/fg8zv06NSE2wd2pF58tNvRRIKeyppUlkAKWwRQfNpyMWX3FBWRELdpxxGmzbVkHS+kUd0Yxg41dEls5HYsEZFaJ5DC9iGwyBjzNmUXzh0JfFylqUTEVTn5xby1cCsrvz6EJyyMlB5tuKFPe6Kjwt2OJiJSKwUy6eBXxpibgb5ACfAPa+2HVZ5MRKqd4zgs23iAdxZvI7+wlHbNErhzWDJtmlbsJsUiIlK5AjnChrX2XeDdKs4iIi46cCSfqWkWu+c40VHh3D6oIwMva4XHE9qTCkSCXXr618yY8S6PPfZ7t6NUq6ee+gODBg2lZ88r3I4SFAIqbCISukpK/Xzy+U5mr9xFqc/h0o6NGT04iYZ1dUMTEXGP7pTybSpsIrWY3X2MKWmWg0cLaJAQzR2DkuhmvG7HEglJu3fv4umn/0h4eASO4/D73z9J48ZennnmKQ4fPsyRI9n06dOXiRMnsXv3Tp5++k9ERcVQr149YmLKfoG67bYb6dKlK7t376JBg4Y89dQz+P1+/vrX/2bfvr34/X4mTpzEpZd249///hcbNqyjtNRHv34DGD06lRkz3iUt7RM8Hg/JyRfys589/K2M69ev46WXXsDj8dCyZSseeeQx5s2bw+efL6eoqIj9+/cyenQqw4Zdd8bXOv2o2MqVK1i0aD6PPfZ7Fi1awDvvvIHH46FLl67ce+8DbNy4gf/9378TGRlJdHQMTz75F+Livn3v4Y8+msEbb0wlLy+Phx/+Lzp16sx7773FggXzCAuDgQOHcNNNt/HUU38gJ+cEOTk5PPPM35k+fQobN27A7/dz66130L//oGr7e64qQVXYjDEDgNuttRONMb0pu8sCwEPW2hMuRhMJKXknS3hn8TaWbTxAGDDwslaMvPoCYqOD6i1BJKSsXbuaCy+8mEmTfsrGjRvIy8vD7/dz0UUXc911IygqKmLUqGuZOHES//rXPxg//l66d+/Bxx9/wKZNGwE4cGA/zz33b7zeJkyaNJ4tW74mIyOd+vUb8OtfP86JE8d54IF7mDbtHRYsmMtzz71Io0aNmD17JgBz5szkl7/8NcnJnfjww/fw+XyEh5dNJnIch2eeeYoXXniV+vXr8/LLk5kzZxYRERHk5+fzt789x969e/jVr37OsGHXnfG1wsLCvjkydup7Tk4Or776Iq+8Mo3o6GieeOJx1qxZxZo1Kxk0aAg333w7y5YtITc353uFLTm5E+PG3c2cObOYPXsWsbFxLFq0gBdeeAW/388vfvEAPXpcQVhYGN269eCWW27n88+Xc+DAfp5//mWKioq49967uPzyXsTH1+wLfAfNu7MxJhHoCpy6uNNEygpbT+BW4EWXoomEDMdxWPn1Id5auJXcghJaeeNJHWZIbFHP7WgiIe+6625g+vQp/PKXDxIfX4ef/OR+EhIS2LLla774Yh1xcXUoKSkByo7Gdep0IQCXXHLpN4WtXr36eL1NAGjSpCnFxUVs357Jxo3r+frrTQD4/X5OnDjO448/wQsv/JOjR4/Qq1dvAH7969/z1luvs3//Pi66qAunXzz/+PFjHDlyhN/97lcAFBUVcfnlPWnVqjUdOyYB4PU2obi4+KyvdSoHwL59ezh+/BgPP/wgAAUFBezfv4+xY+9m6tRXeeihSXi9Xi688KLv/ZkZ0wmAhg0bUVRUyPbtmRw8eIAHHyy7Q2ZeXi579+4BoE2btgBs374Na9P56U9/AoDP5+PgwQN06NCxwn9nwSRoCpu1NhP4mzFmWvmqcGttsTHmADDAxWgiIeHwsQKmzctg846jREV4uLlfIoMvb01EeCC3FBaR8/XZZ0u45JJLueuuicyfn8brr08hKckQH5/AI488xt69e5g58wMA2rW7gI0bN3DFFX3YvPmrb17jTKd1tW3bliZNmjB27F3k5+fx1lvTiYurw+LFC/jjH/8bx3EYO/YWBg4cysyZH/Lww78mKiqKX/zip2ze/BWXXHIpUFYGmzRpwl/+8jfi4uqwdOmnJCQkcPDggTOeT/bd19q0aSNRUVFkZ2cBkJGRDkDz5i1p0qQpf//784SHhzNr1kckJ1/IvHmzGTbsOu6//yGmTfsPH3/8AXfdNfGMf3anymCbNm1p3z6RZ5/9JwBvvfU6iYkd+PTThd9kbNu2PZdd1o1HH/0NpaWlTJv2H1q0aHkuf2VBpVoKmzGmJ/Bna21/Y4wHeB7oAhQBE8rL2ncVGGOigBbAwerIKRKKSn1+5q7ezcfLd1JS6uei9g0ZO9TgrR/rdjSRWiU5uRNPPfUHIiMj8fv9PPjgL4iIiOSPf/wt1m6hWbPmGNOJ7OxsHnzwFzz11B94663peL1N8HhO/WL17eIUFhbGDTeM4i9/eZIHHriHgoJ8Ro68mcjISOrWrcc999xJdHQ0PXr0olmzZiQmJnL//ROIi6uD19vkW0e1PB4PDz30Sx5++CEcx0+dOvH85jd/PENhK3v83dfq3PliYmJiefrpPzFv3hxaty474lW/fn1uu200DzwwEZ/PT/PmLRg8eCjFxUX85S9PEhMTS3i4h0cf/c33/sy++/Fqhw4d6dbtciZNGk9xcTGdO1/0zRHHU9v06dOX9evXcf/9Ezl5soC+fft/76PWmuis9xI9X8aYR4ExQJ61trcxZiRwnbX27vIi92tr7YjTtp9mrR1bfgusn1JWKn9irS04y650L1EJSG26N2TmvhNMSUtnb1Y+deMiuW1QR3p2aqrZVxVQm8aLnB+NFamIqriX6PnaRtndEU591NmHshvIY61dZYzpfvrG1tqx5d+/AO6qhnwiIaegsJT3l2by6Rf7cIC+lzTnpn4diI+NdDuaiIicgyovbNbaGcaYdqetSgByTlv2GWM81lrdn1TkPDmOwzqbxRsLMjieV0zzRnGkpiST1Lq+29FEROQ8uDHpIIey0nZKpZU1r1e3z5HAhOJYOXysgBc/+IpVmw8SEe7hjqHJ3DSgA5ERuv/n+QrF8SJVQ2NFqoobhW05MBx41xjTC9hYWS+scwckEKF2nonf77Bg3V4+WLqdohIfyW3qM3aooXmjOhw/drZTP+VsQm28SNXRWJGKqGi5r87Cdmp2wwfAYGPM8vJlnacmco52HczltbR0dh3MpU5MBKMHd+LKi5tpUoGISIiplsJmrd0J9C5/7ACTqmO/IqGqsLiUDz/bwfy1e3AcuKJzM24d2IG6cVFuRxMRkSoQNBfOFZHAfLktm9fnWY7kFNGkfixjUwyd2zV0O5aIiFQhFTaRGuJ4XhFvLNjK2vTDhHvCuPaKtgzv3Y6oSE0qEBEJdSpsIkHO7zgs2bCf9z7N5GRRKYkt65Kakkwrb82+kbGIiAROhU0kiO3NymNqmmXbvhPERoczdqjh6q4t8GhSgYhIraLCJhKEikt8zFyxk7RVu/H5HbonN+GOQR2pHx/tdjQREXGBCptIkPl651GmzrUcPnaSRnWjGT3E0LVDY7djiYiIi1TYRIJETkExby/cxuebDxIWBkMub82Iq9oTE6V/piIitZ1+Eoi4zHEcln91kHcWbyPvZAltmyVwZ0oybZvpFjciIlJGhU3ERQePFjA1LZ303ceJjgzntoEdGditJeEej9vRREQkiKiwibigpNTPnJW7mPX5Tkp9Dl07NGb04CQa1YtxO5qIiAQhFTaRapax5zhT0tI5cKSAevFRjB6URDfj1f0/RUTkB6mwiVST/MIS3l2cydIv9xMG9L+sJaP6JhIXo3+GIiLy4/STQqSKOY7D6i2HeXPhVnLyi2nprUNqSjIdWtZzO5qIiNQQKmwiVSjr+EmmzbNs2n6UyAgPo66+gKE92hARrkkFIiISuKAtbMaYa4FRQCTwrLV2g8uRRAJW6vMzf+0ePvpsB8Wlfjq3a8DYoYYmDeLcjiYiIjVQ0BY2IBtoAUQBe1zOIhKw7ftzmJKWzp7DeSTERZI6LJleFzbVpAIRETlnwVzYJgK3AFcA1wJT3Y0j8uNOFpUyY+l2Fq3biwP06dKcW/p3ID420u1oIiJSw7lS2IwxPYE/W2v7G2M8wPNAF6AImGCtzSzPlk/ZkbYL3cgpEqgvMrKYPj+DY7lFNGsYR2qKwbRp4HYsEREJEdVe2IwxjwJjgLzyVSOAKGtt7/Ii92z5usnAK4ADPFLdOUUCcTSnkOnzM1i/NZuI8DCuv7Id117RjsgITSoQEZHK48YRtm3ASGBa+XIfIA3AWrvKGNO9/PFKYKUL+UTOyu93WPTFXmYs3U5hsY+k1vVJTTE0b1TH7WgiIhKCqr2wWWtnGGPanbYqAcg5bdlnjPFYa/3Vm0wkMLsP5TIlzbLjQA51YiK4c1gyfbo0x6NJBSIiUkWCYdJBDmWl7ZRzLmteb8LZNxLh3MZKYVEpb86zfLg0E7/f4epLWzHhhouonxBdBQklmOi9RQKlsSJVJRgK23JgOPCuMaYXsPFcXygrK7fSQkno8noTKjxWvtp+hGlzLdknCmlcL4ZxQw0XXdCIksJisgqLqyipBINzGS9SO2msSEVUtNy7Wdic8u8fAIONMcvLl+9yKY/I95zIL+bNBRms3nIYT1gYw3q14for2xMdGe52NBERqUVcKWzW2p1A7/LHDjDJjRwiP8TvOHz25X7eXZxJQVEpF7SoS2pKMq2bxLsdTUREaqFg+EhUJKjsz85nSlo6W/eeICYqnNGDk+h/aUs8Hk0qEBERd6iwiZQrKfUxa8UuZq/chc/v0C3Jyx2Dk2igSQUiIuIyFTYR4P+3d9/xNV5/AMc/2YkMCSKJvU/MmBV7pvjVqhatXbTVUqpaLR06qD1bUntTNUtrU4raYpZjz9okJGTc5P7+uDdXQkJi3Uu+79fLy/Oc+4zvM9z7dZ5znnP4zE1mrNJcvnEHH08X2rxahDKFfa0dlhBCCAFIwiYyuMi7ccxbf4wtBy5hZwd1y+fi9WoFcHORfxpCCCFsh/wqiQzJaDSy9dAlfl13nMi7ceTJ7kH7BoHkD/CydmhCCCHEAyRhExnOf9ciGT1vL/+evomzkz0tahUipEIuHOxl/E8hhBC2SRI2kWEY4hNYuf0sf/xzmlhDAqUKZqVNSBGyebtZOzQhhBDioSRhExnC8fMRTF91hAtXo/D2dKFj7UJUCMyOnYz/KYQQ4gUgCZt4qd2JjmPBxpNsCLsAQM3SOXj/zdLcjYy2cmRCCCFE2knCJl5KRqORXfoqc9YcJSIqlhzZ3GlfX1E4lzcebk6SsAkhhHihSMImXjrXIu4ya/VR9p+4jqODPa9XL0CDinlwdJBOBUIIIV5MkrCJl0Z8QgJrd51n8aaTxMYlUDSvD+3qKfyyZLJ2aEIIIcQTkYRNvBROX7rF9BWaM5dv4+HmRNtXFZVL+EunAiGEEC8FSdjECy061sDiv0+xdvc5jEaoUsKfFrUL4ZnJ2dqhCSGEEE+NzSZsSqliQA/AGRimtT5k5ZCEjdl77Bqz1mhu3Iohu48b7espiubLYu2whBBCiKfOZhM2oDNwHsgBnLZuKMKW3Lwdw5y1R9mtr+Jgb0fDyvloVDkvTo4O1g5NCCGEeCZsOWErCLQHygHtgFDrhiOsLSHByIa9F1i48QR3Y+IplCsz7espcvp6WDs0IYQQ4pmySsKmlKoIDNJa11JK2QPjgFJADNBZa30CuALcAW4C8j6GDO7clZMSFdkAACAASURBVEimrzzCyf9u4ebiSLv6iupBObCXTgVCCCEygOeesCmlegNtgEhzUVPAWWtd2ZzIDTeX/QJMBOwwtWUTGVBMXDxLt5xi9Y5zxCcYeaVodt6uU5jMHi7WDk0IIYR4bqxRw3YcaAbMNM9XBVYCaK23K6XKm6d3Y3okKjKog6euM3OV5mp4NFm9XGlbT1GqYFZrhyWEEEI8d889YdNaL1JK5UtS5AncSjIfr5Sy11onpHfbvr6eTxqesAHht2OY9PtBNoadx97ejtdrFqLVqwpXl6d3u8q9ItJD7heRVnKviGfFFjod3MKUtCV6rGQN4OrV208nImEVRqORzfsv8ttfx4mKNpDP35MODQLJ4+fJ7Vt3eVpX19fXU+4VkWZyv4i0kntFpEd6k3tbSNi2AI2A+UqpYGC/leMRVnDxehQzVmr0uXBcnB14u25h6pTNhb29dCoQQgghrJmwGc1/LwZClFJbzPPvWCkeYQVxhgSWbzvDn1tPY4g3UqZwNlqHFCGLl6u1QxNCCCFshlUSNq31aaCyedoIfGCNOIR16bM3mbFKc/H6Hbw9nGkdoiinfK0dlhBCCGFzbOGRqMhgIu/GMf+v42zafxE7oE7ZXDSrUQC3p9ipQAghhHiZyC+keG6MRiPb/r3Mr+uOcftOHLl8PWjfQFEwR2ZrhyaEEELYNEnYxHNx5eYdZq4+yqFTN3B2tKd5zYKEVMiNo4MMYiGEEEI8iiRs4pkyxCewasdZlm45TZwhgRL5s9C2nsLX283aoQkhhBAvDEnYxDNz4kIE01ce4fzVKLwyOfHO/wKpWNQPOxn/UwghhEgXSdjEU3cn2sDCv0+wYc8FjED1oADerFkIDzcna4cmhBBCvJAkYRNPjdFoZLe+ypy1RwmPjCUgayba1w+kSG5va4cmhBBCvNAkYRNPxY1b0cxafZS9x6/h6GBH06r5aRCcFydH6VQghBBCPClJ2MQTSUgwsnb3eRb/fZKYuHgC83jTtp4iIKu7tUMTQgghXhqSsInHdubSbaatPMKZS7dxd3WkVUggVUsGSKcCIYQQ4imThE2kW3SsgSWbTrFm1zmMRqhU3I+WdQrjlcnZ2qEJIYQQLyVJ2ES67Dt+jVmrNddvxZDd24229RTF82exdlhCCCHES00SNpEm4ZExzFl7jF1HruBgb8drlfLSqHI+nJ0crB2aEEII8dKz6YRNKeUH/KG1rmDtWDKqBKORjXv/Y8GGE9yNMVAwpxft6weSy9fD2qEJIYQQGYbNJmxKKTvgM+C0lUPJsM5fjWTGSs3xCxG4uTjQtp6iRukc2EunAiGEEOK5stmEDegCzAJ6WTuQjCY2Lp5l/5xm5fazxCcYKR+YnVZ1C+Pt4WLt0IQQQogMySoJm1KqIjBIa11LKWUPjANKATFAZ631CaCuuewVpdQbWuuF1og1o/n39A1mrNJcuXmXrF4utH5VUbpQNmuHJYQQQmRozz1hU0r1BtoAkeaipoCz1rqyOZEbDjTVWr9hXn6GJGvP3q07scxbd5ythy5hZwevVshN02r5cXW25UpYIYQQImOwxrhBx4FmQGJDqKrASgCt9XagfNKFtdbtnmt0GYzRaGTz/ot8NXE7Ww9dIq+fJ1+3L89bdQpLsiaEeGLVqlVg7txZ1g4jzZYuXczEiaGpfn7x4n9Uq1aBjRvXP9F+pk2bRPPmjZ9oGyJ1sbGxjBo1jE2bNlg7lKfmuSdsWutFgCFJkSdwK8l8vPkxqXjGLt24w9C5YUxZfpg4QwJv1S7EV+3Lkc/fy9qhCSFeIi9SP6UZM6YQFRWZ6ufZsvkyfvxUypQpn+oywvquX7/GwoXzSEhIsHYoT40tVKHcwpS0JbLXWj/WGfb19Xz0QoI4QwIL/zrGb2uPEmdIoEIxP7o0K0V2n0zWDu25kXtFpIfcL0/Gw8P1hTmHDg72uLo6PTTeHDkqpfpZWo/T3d0Fe3u7F+a8vGhiYkzjWXt6vjj33qPYQsK2BWgEzFdKBQP7H3dDV6/efmpBvayOngtn+sojXLx+h8wezrSuW4Ryyhc7Q3yGOX++vp4Z5ljFk5P75clFRkZbzuG//x5kypQJHDx4gJiYaAICctCyZWuaNGkGwPLlyxg7dhStW7dn1qzpuLq6MnfuQsCO0NAxrF27mri4WGrXDsHb24e1a1cxf/5Sy77mz/+VhQvnceXKZXLmzEWHDu9Sp06I5fOtWzczadJ4zpw5hZtbJipXrkrXrh/j5eXFm2824vLlS8yePZvZs2ezadPOB47l4sX/aNGiCf37D6ZGjdoMGPAtd+/epVSp0ixYMJfr169TvHhJevX6grx581nWmzNnJgsXziMiIpzq1Wvh5+dPQoIx2b31sNjnzJlBaOhPDBs2hooVTQnjzJnTmDQplNDQyRQrViJZnL/9NodffhnLqlUbcHJyAqBPn15s376VlSs34OxsGkqwd++euLi48MMPgzAYDEybNonly5cRHh5OgQIF+eCDjyhX7uGvQt24cT0zZkzlzJlT+PhkpXHjprRt+06SWObyxx9LOH/+PI6OjhQvXoKPPupJgQKFAOjW7T2UKoqzszN//rmUO3eiqFAhmF69viBbttQ7vc2ZM4Pff1/E1atX8fX1pUGDhrRv34lLly7SokUTAHr06EGZMuUYM+YXjEYjy5YtYcGCX7lw4Ty+vn40a9acFi3etmyzWrUKvPfeh6xatYLLly/Sp08/ateuy5EjhwkNHcOhQwdwdXWjbt1X+eCDj3BxcX3ouXmY9CaS1nz0aDT/vRiIVkptwdThoKf1Qnp5RUXHMW3FEQbN3sOl63eoVTYnAzoHUz4wuwzWLoR4Li5dukT37l1wd3enf//BDBo0gty58zBs2EBOnjxuWS4yMpK1a1fx3XcD6NGjFy4urgwc+D0rVvxBp07v06/fAM6fP8e8eXO41xwapkyZwNixowgJqc/gwSOpUKEi3333JX/9tRaA8+fP8eWXvQkKKs2wYWPo1u1jtmzZxIgRgwEYOHAYWbJkpVatuowfPzXNx7Vr1w5WrVrOV199xTff/MD58+cYMOBby+dz5sxk/Pifee21xvz44zDi4+P59ddZ6Yr9rbfaEBhYlFGjhhIXF8fZs6eZOnUirVq1eyBZAwgOrkxcXCwHDuwDICEhgb17wzAYDBw+fAiAuLg49u7dQ+XKVQEYMmQA8+bNpmXLVgwaNJy8efPy6afdOXgw9XqUDRvW8dVXn1OoUGEGDhxG8+YtmTJlArNmTbMc+y+//Ezjxq8zcuTP9Oz5GadPn2LAgO+SbefPP5dy5Mi/9O3bj169viAsbBc//TQ81f2uWrWcSZPG8/bbbRg58mcaNmzKlCkTWLp0Mdmy+TJgwFAA3n+/K716fQHA+PFjGT58ENWr12LQoBHUqlWHsWNHPdBmcfr0ybRs2YqvvvqOMmXKcurUSbp1exd7e3t++GEQH3zwEevWreHrr/ukGt+zYJUaNq31aaCyedoIfGCNODICo9HIjsNXmLvuGLeiYsnp6077+oEUypnZ2qEJIdIhS7kHf5QBbuw+aJXlH8epUycoWTKIb77pj4ODaVi7okWL89prddi7N8xS45KQkMA777xLhQrBAJw9e4Z161bTt28/GjRoCEC5cuVp3ryJZdu3b99m1qzptGnTgU6d3gegQoWK3Llzh19++Zlatepy5Mi/xMXF0bp1e7JmNdXcuLll4vLlSwAULqxwdnYmS5YsKSZBqbl79w5Dh45CqXxcvXqbq1evMHr0cG7duoWHhwdz5kynceNmdOz4HgCvvBJMhw6tiIqKSnPs9vb2fPHFN3Tu3JZff53F1q1byJMnr2X5++XJkw9//xzs2bOLsmXLc+LEMWJiYsiXLz/79oURFFSGAwf2ER19l+Dgypw5c5oVK/7g88+/omHDJpY4r127zsSJoYwenXJHjOnTJ1OuXAX69PnGHHcwN27c4NChAwBcvXqFd97pzJtvvgVAUFAZbt2K4OefRxEdHY2rq6mGytHRgSFDRllqA48fP8ayZUtSPef79+8lICCApk3ftGzXyckRX9/sODk5UbhwEQBy585D3rz5iIgIZ9682bRq1Y7OnbtYzjHA3LkzadmyFV5emS3H0KhRU8u+Ro0aRrZsvgwdOhpHR1PalCtXHrp1e9dyLp8HW3gkKp6Rq+F3mblac/DkDZwc7XmjRgHqvZIHRwfp0yGEeP4qVapCpUpViImJ4eTJE5w/fzZJbU9ssmXz5Mlnmd67dw8A1avXtJS5uLhSqVIV9uzZDcChQweIi4slOLgKBsO9fm0VK1bizz+XcunSRYoVK4GTkzPvvtueunVfpVKlqlStWh17+yf7TvT3DyBLlqyWeV/f7ABER9/lxo3rREREEBxcOdk6NWrUYvnyZWmO3d8/gIIFC9GmTQcmTgzF0dGRCROmWxKIlFSsWIndu3fSuXMXwsJ2U7RoMQoWLMT+/XsB2LlzO0WKBOLjk8XS6/X+GIKDKzNhwlgMBsMD+4qJieb48WN07578/fZdunSzTPfoYfrs5s2bnD17mjNnTrNlyybA1JMzMWErWLCIJVkznUNfoqPvpnpsQUFlWbp0MZ07t6NmzdpUrlyVt95qk+ryhw4dxGAwULt23WTltWuHMGvWNA4dOkClSqaaxjx58iZbJixst+XeSzw3xYuXwN3dnV27dkjCJh5ffEICq3ee4/dNp4g1JFA8nw9t66kM1alAiJdNemu6nvXyjyM+Pp6ffx7F0qWLMBgM5MyZy/JjZzQaky3r4+NjmY6ICMfR0RF3d4/7lslCYuuaW7ciAPjgg44P7NfOzo5r165RokRJRo8ex8yZ01iw4Dfmzp1FlixZ+OCD7tSv/9pjH5eLS/JRYOzsTAlgQoKR27dNL0HInNk7hdhJc+z+/gEAhITUZ9q0Sfj5+ZMvX/6HxhUcXIk///yd6OhowsJ2ExRUhgIFCrJ69QrT05cd2yyPQyMiTDG8/nqDFGOIiAi31Erei/uW+Vh8Hlgn0Zkzpxk8uD8HDuzD1dWVQoWKkCmTu/nTe9fc1TX5ObS3t3/gnkjq1VfrEx9vYNGi+UyYMI7x48dSsGBhvvjiawIDiz6wfOJ18PHJmqw8SxbTdUis7UzpeCIiwvn990X8/vuiZOV2dnbcuHE91RifNknYXjKnLt5i2oojnLsSiWcmJ9o3CCS4mJ+0UxNCWN2MGVNYtmwxX3/9PZUqVcHFxZWYmGj++OP3h66XLZsvBoOBqKjIZElbePhNEtuBJZYPHDgMX1+/ZOsbjUZLrUnJkkEMGTKSmJgYdu3awZw5Mxg48HvKl6/40Abujytz5szmWG8kK09M0tITO8DIkUPIlSs3V65cZtasaXTo0DnVfZctWwE7Ozv27t3D/v17ef315hQoUJCoqCh27drB8eNH6dmzNwAeHh7Y2dnxyy9TcHC4lxokJk33J5ymuN3Nx3YzWfnVq1c4f/4cQUFl+Pzznnh7+zBjxjzy5y8AwKJF89mxY2uqcadVgwYNadCgIeHh4WzevJGpUyfSv/83zJo1/4FlvbxMr6u6efN6suucmHAlXqeUeHp6Uq1aTcvj10RGoxFv7wfPy7Miz8ZeEndjDMxec5T+03dx7kokVUsFMODdYCoV95dkTQhhEw4ePEBgYDFq1qxj6V23bds/ADykMoWSJYOwt7dn06aNlrK4uDi2b7/3o1+sWAkcHR25ceMGSgVa/pw+fZLp0ycDRpYsWUDz5o0xGAy4uLhQpUo1OnfuQkJCAteuXQV4rMejD/uOzZ07L9my+bJhQ/IX7W7duoXEZPPRsZv8+edSdu/eSd++/Wjb9h1mzJjC6dOnUt13pkyZKFmyNIsXzycyMpKSJYPIls2XnDlzMWXKeDw9PSlRoiQApUqVwWg0EhUVmSyGPXt2MX/+XEubw+Tbd6dAgUKWR5yJfvttLt9//zUREeFcuHCexo1ftyRrANu3J17zh1z0R+jfvx9fffU5AN7e3jRs2ITXXmtsaY94/3UsWtR0jtevX5usfN26NTg6OlK0aOptFkuWLM3p06eSnZfs2f2YMGEcJ0+eeOxjSC+pYXsJ7Dl6ldlrjnLzdgz+WTLRvr5C5Um9iloIIayhWLHizJo1jYULf6NAgYIcPvwvc+fOxNXV7aHtlXLlyk1ISH1GjRpGdPRd/Pz8mT9/HjduXLc8KvTx8eHNN9/i559Hcfv2LYoWLc6xY5qJE0OpVq0mmTK5U7ZsecaMGcnXX39Bs2ZvEhsbx/Tpk8mZM5elkbqHhydHjhwmLGw3ZcqUS9NxPSzxsLOzo1On9xkyZABZsmShfPmKrF+/hqNHj1javT069kxcv36Nn38exf/+14iSJYMIDCzGqlXLGTToB0JDJ6eaNFasWInQ0DEEBhbFzc0NMDXQX758GSEh9S3LFS5chBo1avP999/QseN75M2bj7Cw3cyYMYVWrdqluv133unM119/wZAhA6hVqw7Hjh1j4cJ5dO36MT4+WfDz82fevDl4e/tgb2/PihV/WDokREdHk1ixld7crWzZ8gwc+D3jx4+lQoWKXL58id9/X0iNGrUBU40hmNrp5ciRk8KFFW+++RZz5szAwcGBoKDS7N0bxty5M3nrrTaW5VPSoUNnPvigI19//QX/+18jYmNjmT59ElevXqVIkcD0Bf4EJGF7gd24Fc3sNUcJO3YNRwc7GlfJx2uV8uHkKBWnQgjb06ZNe65du8bUqROJiYmmVKkyjBjxE5Mm/WL5EYeUa6w+/bQPrq6uTJgQSkJCPHXr1sPTs06yGqYPP+yOj48PS5cuZvLk8WTN6kuLFq0svTPz5MnH4MHDmTx5Al9++Tn29naULVuBb78dYKlBatfuHYYOHUjv3h8zZ85CSweC1NjZ2aUYb9IyU69LI7NmTWfhwt8oX/4VOnR4l6VLF6c59hEjBuPgYM+HH3YHwMnJiU8+6c0nn3zEggXzaN78rRTjCw6uTGjoGIKCylrKSpcuy4oVfxAcXCXZsv369WfSpF+YOXMq4eE38fcPoEuXj3j77dQb89esWYfvvx/ItGmTWbnyT/z8/OnWrSfNmjUHYMCAoYwaNYRvvvkCDw9P6tatx6RJM2nevDEHDx7Az8/ffA5TPrep+d//GhEVFcWSJQuYN28Onp6e1K79Kl26dAVMj5lbt27PwoXzOHBgP9Onz+XDD7vj7e3N778vYs6cGQQE5OCjj3paerCmRqlARo8OZcKEcXz99ec4O7tQqpSpt/OzeIyeGrsnqZK0McaM8nLLhAQj6/ecZ9HfJ4mOjadIbm/a11cEZHV/9MpCXoQq0kXuF+uLiAhn+/ZtVK1anUyZ7nWe6tKlI9myZaN//yFWjO4euVdEevj6eqarvZLUsL1gzl6+zfSVmlMXb+Hu6kiHBoFULRWAvbRTE0K8pJydXRgxYjAbNqyjSZNmODg48Ndfazl8+BAjR461dnhCPBdSw/aCiImLZ+nmU6zacY4Eo5HgYn60rFOYzO7O1g7thSP/CxbpIfeLbTh8+BATJozjyJHDGAxxFCpUmHbtOlGpUpVHr/ycyL0i0iO9NWySsL0ADp68zoxVmmsR0WTL7Eq7eooSBbI+ekWRIvlSFekh94tIK7lXRHrII9GXSERULL+uO8b2fy9jb2dHg+A8NK6SHxenB7tXCyGEEOLlJQmbDUowGtm07z/m/3WCOzEGCuTwon39QHJnT73bsRBCCCFeXpKw2Zj/rkUxfeURjp2PwNXZgdYhRahVJif29tKpQAghhMioJGGzEXGGeP745wzLt50hPsFIuSK+tAopgo+ny6NXFkIIIcRLzWYTNqVUHaAlkAkYorXeb+WQnpnDZ24yY5Xm8o07+Hi60ObVIpQp7GvtsIQQQghhI2w2YQPctNbvKaVKA68CL13CFnk3jnnrj7HlwCXs7KBu+Vy8Xq0Abi62fFmEEEII8bzZbGagtf5DKeUOdAd6Wzuep8loNLL10CV+XXecyLtx5MnuQfsGgeQP8LJ2aEIIIYSwQVYZdFIpVVEp9Zd52l4p9YtS6h+l1F9KqYLm8mzAT8A3Wutr1ojzWbh88w7D5+1l0h+HiTXE06JWIb7uUF6SNSGEeMamTp3IokXzLfPdur1H7949n8m+5syZScOGdQkJqca6dWueyT4GDPiWdu1aPpNtP6nJk8cTElL9ibfTsmVTpkyZ8BQievE99xo2pVRvoA0QaS5qCjhrrSsrpSoCw81lw4FswECl1BKt9cLnHevTZIhPYOX2syz75zRxhgRKFcxKm5AiZPN2s3ZoQgiRIUyZMoGuXXtY5lMbdPxJRUZGEho6hpCQ+jRt+gZ58+Z7+jsxe9gA6dbUuPHrVKny5Amb6RrZ5jE+b9Z4JHocaAbMNM9XBVYCaK23K6XKm6fbWyG2Z+L4+QimrzzChWtReLk70+m1wlQIzC43oRBCPGdJB/d5ViP9REaaRjuoVq0GpUqVfib7SGSroxX5+mbH1ze7tcN4qTz3hE1rvUgplS9JkSdwK8l8vFLKXmud8Hwje/ruRMexYONJNoRdAKBm6Ry8WbMgmVydrByZEEI8HzExMYSGjmHt2tXExcVSu3YI3t4+rF27ivnzlwJQrVoF3nvvQ1atWsHlyxfp06cftWvXZe/ePUycGMrx40dxcXGlVq06dOnyEW5ubowePZzNmzdatgHQoUMrbt++xcKFf1jK2rVrSXBwZebOnQXAuHGjWbRoPvPn/w5AQkICEyeGsmzZEu7evUO5chX47LO+ZM2aLdVjunjxP8aNG8PevXuIiYmhXLnydO36Mb6+xVi+fBkDB34PwDff9MHff0yyGJM6fvwYoaE/cfDgPlxcXKlUqQpdu36Ml5dXss8PHz4EQKVKVejW7WN8fLKkuL1q1Srw4Yc9ePvtNpayPn16ERkZyU8/jefixf9o0aIJP/44lAULfuPQof1kyZKVrl0/Jk+evAwd+iNHjx4hT5689O79JYGBxQB4881GNGvWnP/+u8D69WuJjzdQvXotevbsTaZMmVKMZfLk8fz662zWrPnbElvfvv3Ytm0LW7f+g7OzE6++2oCuXT/GwcE0es/169cYOXIou3Ztx80tE507d3lguzdv3uDnn0exdesW4uLiKFeuPD16fEpAQA7i4+Pp1KktUVFRzJr1Gy4uLhgMBjp1aoODgwMTJkzH0dFmm+4/ki1EfgtT0pbosZM1X1/PRy/0HBiNRrbs/48Jiw9w83YMuf086dY8iGL5ZfxPW2Er94p4Mcj98vh69fqWDRs20KtXL3LkyMHkyZNZvXoFvr6+yc7rjBlT+PLLL8mcOTPlypXj4ME9dO/ehQYNGvDRR125cOECI0eO5Ny508yYMYN69eqwYMGvREeHkzt3bsLDwzlx4hh2dnYYDJEEBARw+fJlTp06yXfffUvTpo1o2bIlbdu25Y033sDX1xNnZ0d27NiGvT0MHTqES5cu8eOPPzJu3EjGjBmT4vFcunSJ99/vQEBAAD/88D0JCQmMHTuWbt3eZfHixTRqVJ+cObPTrVs3PvnkE6pXr57i/XPhwgW6du2MUoqhQ4cSExPD4MGDGTiwH5MmTeLw4cN06fIOZcuWZejQIURERDB69Gh69OjCokWLcHNzw9XVCUdHh2Tb9/R0TTbv7OyIs7Mjvr6exMS4AzB4cH/ee+89PvroQ8aOHcuAAf3w8/OjXbt25MrVlR9++IEBA/qxcuVKABwc7Jk1axrVq1dn9OhRnDhxgsGDB5MrVwCffvppiufJ3d0FO7vk/3Z++mkETZo0ITR0HDt37mTs2LEUK6Z4++23zclWD+7cuUP//v0xGo0MHz6cy5cvkymTM76+nkRHR9Ohw4fExsbSr983uLq6Mn78eLp3f5+lS5fi5eXNkCGDaNGiBYsWzaF79+789NNPnD9/joULFxIQ4JOOO9f22ELCtgVoBMxXSgXzBK/vsIVBd69F3GXW6qPsP3EdRwd7Xq9egAYV8+DoYG8T8QkZoFmkjy3cL7+tP87OI1esGkOFwOy0qF0oXeucPXuGP//8k759+xES0hCAgQOL07x5E+LjjcnOa/nyFalZsz4ACQkwbNgIihcvQZ8+3wEQGFgaT8+s9Or1EUuXrqR8+VdwcXFh7dqNNGzYhL//3oSfnz/R0XdZv34zr75an5Ur1+Hu7k6ePEUstTheXlnImjUnV6/eJjbWgKenJ99/PwQXFxeUCiIs7ACrV69I9ZqPGzeBmJgYhg4dg5dXZgBGjChGixZNmDp1Kh07fkj27LkByJLFz7Kv+/3yy0QcHBwZNGiUpZbq44+NjBs3mhMnzjNy5Gh8fLLw448jLLVCOXLkp337t5g+fTZvvNGS6Og4DIb4ZNuPjIxONh8bayAuzrTMjRtRANSqFULjxi0AePvt9vTq9RF16tQjJKQRAK1atWfw4P6cPn0Rd3cP4uMT8PXNzhdffAtA4cIl2bRpC+vX/0X79u+neJ6iomIwGpP/LpcoUYr33ze1ISxYsDirV69lzZp11K3bkM2bN3L06FHGj59KsWIlAPj+e186dWrLnTuxXL16myVLFnLq1ClmzvyNPHnyAjBsWAneeKMR48dPpkOHzmTPnoe33mrDxImTyJu3MBMmTKBjx/fw9va3+r/j+6X3P4JW6SVqlvjgfTEQrZTagqmjwbPpsvOMxScksGrHWb6atJ39J65TNK8PP3R6hUaV8+HoYM3TLIQQ1rF37x4AqlevaSlLfPR37yfAJPEHGODOnTscP36UWrXqJlvmlVeC8fT0Yu/e3Tg7O1O6dFn27NkFQFjYboKCylCsWAn2798LwI4d2yhfvqIlWUtJwYJFcHG5N6KMv7+/pQ1aSvbtC6Ns2fKWZA0gc2ZvypV7hR07dqS63v0OHNhP6dJlkz1SrFq1OnPmLMTLKzP79oVRtWqNZI/w8uXLT8GChdi7NyzNrryq5AAAEh5JREFU+0lJsWLFLdM+PqZap8THn4Dl2G7fNvUNtLOzo2jR4iTl65udu3fvpnO/Je7bhi/R0TEA7N+/Dy8vr2TLFCkSiL9/Dst8WNgucufOQ86cuTAYDBgMBpydXShVKohdu+6d+44d38Pf358+fXqhVCCtW78cTeKtUsOmtT4NVDZPG4EPrBHH03Lq4i2mrzzC2cuReLg50fZVReUS/tKpQAjxVLSoXSjdtVu2ICIiHEdHR9zdPZKVp9QGKzFxAFOjfaPRmOpyUVGmmqKKFSsze/Z0wJSwvfFGC8LDw1mzZgUAu3bt4IMPPnpojK6uyYf/s7e3f2hD/sjI2xQpolKM6+zZUw/dV1K3b6e8naSfZ8ny4PF7e2exHP/jSqndmaur60PXuf9zO7uHn6e0bMN0rk0toG7fvp0sCU6UNeu9pkQRERGcOXOamjWDH1gud+48lmlnZ2dq1KjNrFnTqFAh+KX5LbaFR6IvrLsxBhZvOsm63ecxGqFKCX9a1C6EZyZna4cmhBBWly2bLwaDgaioyGRJW3j4zYeu5+HhiZ2dHTdv3njgs+vXr1l+2CtWrMSYMcM5dOggp06dICioDBER4UycOI6dO7cTERFOcHDlp3pMnp5e3Lhx/YHyGzeuJ0s6H8XDw4ObN5Ofh7i4OHbt2kGJEqXw8srM9esp7yd//gKpbjchIXkSdedO+mrBrCVz5swPnA+A8PBwy7SHhweFChXmiy++SbaM0WjE2fleZ77z58/x229zKFSoMLNnTyckpH6yhO5FJc/qHlPYsat8PXk7a3edx9fbjc/eKk2nhsUkWRNCCLOSJYOwt7dn06aNlrK4uDi2b98KpF7rkSlTJgoVKsL69WuTlW/fvpWoqChKlQoCTI9Rc+TIyYwZk/H29iZPnrwULVocZ2dnpkwZT+HCRZL19rS3f/KfvFKlSrNnz24iIu4lEuHh4ezatZOyZcumeTslSpRi7949yR4r7ty5nd69PyY8/CalSgWxefNGDAaD5fPTp09x6tQJSpYMSnGb7u7uXLt2r63j3bt3OXZMp+fwrKZs2fJERUVaHnEDnD17mosXL1jmS5Uqw8WL/+Hv749SgSgVSJEiigULfuWffzYDpuRt8OD+5MyZi9DQKQQE5GDw4P7P/XieBUnY0unm7RjGLjrATwsPEBEZS8PK+fih0ysUzZdyN2shhMiocuXKTUhIfUaNGsaSJQvYunUzn3/+CTduXMfe/uGPqTp1ep9//z1Iv3592LbtH37/fRHff/8VJUqUIji4imW5ihUr8c8/mylVqgwAjo6OFC9ekoMHDyRbDkw1NPv27eXgwQOWsvS+xqxly1Y4OjrSs2dXNm5cz4YN6+jZ80NcXJxp3z7tbaVatmxFQkI8n33Wgy1bNrF69UqGDRtIjRq1yZ07D+3adeLGjev06tWdf/7ZzOrVK/jssx4EBOSgQYOGKW6zYsXKLF++jJUr/2Tbtn/4/POe2NvbPfG72p7lu94St/3KK8EEBZXhu+++YtWq5WzcuJ6+fT9L1oavYcPGeHllpmfPrqxfv5adO7fTr19f1qxZSaFCRQD4/feF7N27h169vsDV1ZVPPvmcffvCWLx4wTM7hudFHommUUKCkb/CLrBw4wmiY+MplDMz7esrcvp6PHplIYTIoD79tA+urq5MmBBKQkI8devWw9OzDqdPP7y9V5Uq1fjxx2FMnTqRvn0/xcsrMyEhDXj//a7J2iQFB1dhyZKFlC5dxlJWunRZwsJ2mzs33NOx43tMnBjKvn1hLFu2OtWRDh7W5il7dj/Gjp1IaOgY+vf/FgcHB8qVK88PPwzGz88vzT0RAwJy8NNPExg3bjT9+vXBw8OTWrXq8v77XQFQKpDRo0MZP34sX3/9OW5ublSqVJUPP+yOm5ubJc6ksXbv/gnDhsUybNhA3N09aNasOUWKBHL06JGHxpLS8SYtS/lzeFgtadpGKEi+zODBIxg9ejijRg3DycmJt95qzebNf1s+z5TJnbFjJzJ27GiGDRtIXFwsBQoUYtCg4QQHV+bq1Sv88svP1K//GkFBpvuhbNny1K1bj/Hjf6Zq1eov9Mt87Wz1LcmPwfisuuyeuxLJ9JVHOPnfLdxcHGlesyDVS+fA/iVpyJjR2MJrGsSLQ+6XxxcREc727duoWrV6sobuXbp0JFu2bPTvP8SK0T19cq+I9PD19UxXEiE1bA8RExfP0i2nWL3jHPEJRioEZuftuoXx9nB59MpCCJHBOTu7MGLEYDZsWEeTJs1wcHDgr7/WcvjwIUaOHGvt8IR4oUgNWyoOnrrOzFWaq+HRZPVypW29IpQqmPpQJeLFIf8LFukh98uTOXz4EBMmjOPIkcMYDHEUKlSYdu06PfC48mUg94pIj/TWsEnCdp9bUbH8uu4Y2/69jL2dHSEVctG0agFcnFN/8aJ4sciXqkgPuV9EWsm9ItJDHok+JqPRyOb9F/ntr+NERRvI5+9J+/qB5PWXMQSFEEIIYV2SsAEXr0cxY6VGnwvHxdmBt+sWpk7ZXI/sdi6EEEII8Txk6IQtzpDA8m1n+HPraQzxRsoUzkbrkCJk8Xr4EB1CCCGEEM9Thk3Y9NmbzFiluXj9Dt4ezrQOUZRTvtYOSwghhBDiARkuYYu8G8f8v46zaf9F7IA6ZXPRrEYB3Fwy3KkQQgghxAsiw2QpRqOR7f9e5td1x7h1J45cvh60b6AomCOztUMTQgghhHgom03YlFKVgffMsz201hGPu60r4XeZuUpz6NQNnB3taV6zICEVcuPoIEOpCiGEEML22WzCBryLKWGrCLQEJqR3A4b4BFbvPMfSzaeINSRQIn8W2tRTZPd2e9qxCiGEEEI8M7acsDlorWOVUheB2uld+cR/EUxfoTl/NRKvTE50+F8gFYv6pWEwWiGEEEII22KVhE0pVREYpLWupZSyB8YBpYAYoLPW+gRwRynlDOQALqV123djDCzceIK/9lzACFQPCuDNmoXwcHN6BkcihBBCCPHsPfeETSnVG2gDRJqLmgLOWuvK5kRuuLlsAjDeHOP7j9qu0Whkt77C7DVHCY+MJSBrJtrVU6g8Ps/mQIQQQgghnhNr1LAdB5oBM83zVYGVAFrr7Uqp8ubpPcA7ad3ogKk72H7oEo4OdjStmp8GwXlxcpROBUIIIYR48T33jEZrvQgwJCnyBG4lmY83PyZNl+2HLqFye/Ndx1doXDW/JGtCCCGEeGnYQqeDW5iStkT2WuuE9G5k2fAm0ptApJmvr+ejFxLCTO4XkVZyr4hnxRaqobYA/wNQSgUD+60bjhBCCCGEbbFmDZvR/PdiIEQptcU8n+Z2a0IIIYQQGYGd0Wh89FJCCCGEEMJqbOGRqBBCCCGEeAhJ2IQQQgghbJwkbEIIIYQQNs4WXuvx2B4yrJUQKVJK7QEizLMntdadrBmPsD33DZ1XCJgGJAAHga5aa2n4K4AH7pUywDLgmPnjUK31b9aLTtgSpZQTMAXIC7gA/YHDpOP75YVO2Eh9WCshHqCUcgXQWteydizCNqUwdN4IoK/W+m+lVCjQBFhirfiE7UjhXikHjNBaj7BeVMKGtQauaq3bKqV8gH1AGOn4fnnRH4lWIcmwVkB564YjbFwQkEkptUoptc6c5AuRVOLQeYkv4i6rtf7bPL0CqGuVqIQtuv9eKQe8ppTaqJSapJTysF5owgbNB74xT9sDcaTz++VFT9i8eArDWokMIwoYqrWuB3QBZsv9IpJKYei8pCOoRAKZn29EwlalcK9sBz7VWtcATgL9rBKYsEla6yitdaRSyhNT8vYVyXOwR36/vOg/Vk9lWCuRYRwFZgNorY8B14EAq0YkbF3S7xNPINxagQibt1hrHWaeXgKUsWYwwvYopXID64EZWuu5pPP75UVP2GRYK5Ee72Bq54hSKgemGtqLVo1I2LowpVQN83QD4O+HLSwytJVKqQrm6TrALmsGI2yLUsoPWA301lpPMxen6/vlRe90IMNaifSYDExVSiX+o3hHamRFKhJ7avUCJiqlnIF/gQXWC0nYqMR7pQswVikVh+k/gu9ZLyRhg/pieuT5jVIqsS1bD2BMWr9fZGgqIYQQQggb96I/EhVCCCGEeOlJwiaEEEIIYeMkYRNCCCGEsHGSsAkhhBBC2DhJ2IQQQgghbJwkbEIIIYQQNk4SNiFEuiilNiilyj7jfXgppXYppfYopQonKe+glJr6iHXfU0q9ZZ5upJTq+ZRjK6eUmvg0t3nf9uXdgEKIB7zoL84VQjx/RpKPsfkslAZitNZVUtj3o1QG/jJPl0vjOmmmtd4NvPs0tymEEI8iCZsQLymlVE1Mb9eOAooCB4BWQE7gL611fvNy3wJGrfV3SqlLwFKgGqa3tY8DugO5gA5a68RRInoopYqbpz/WWm9SSnkAY4HigAMwWGv9q1KqA9AeyAos1Vp/lSRGP0wjUOTGNJB2X2APMAXwU0ot0Vo3TXJYdknWrQCMADIB14D3gUJAI6CWUiqzuQyl1GlgYRriWwYcAj4D4oFTQButdcx957Wf1rqWUmoDpkG/qwG+wEda65X3XYe8wFTz53eAzlrrA0qpd4BPMCWUu4FuWuuoJOtZrkuSY6gB1AJeA3KYr8soIA9QG9P4uA0wjZG7GNM1LwNcBpprrW/eF9tFTANRVzWf/xZa69NKqebm2NzMfzqbr/EG8/Wpay7/CNPb2osBI7XWo1K7DxBCPBF5JCrEy60S0BVTwpYHqJfCMkbu1UJlB5ZprYua55tqrasD3wIfm8vsgJta63KYhoObqZRyAr4Cdmmty2NKLL5USuU3r5MTKJ00WTP7CVirtQ4C3sSUqNkBnczbakoKzPubBLxtjmMEMFFrvRZTwvm11noM8AsQqrWensb4vgR+AELMyx0BAlOKIcm5c9JaVwZ6Av1TWGYcMF9rXdJ8Hr9SSpXAlJxW11qXwpRU90th2/fPJ5ZVwHQtq2EaH3e5+RzCvWtcChhu3m840DqF2Pwwnf+ymMYx7KaUssOU6L6mtS4NDMaUwFpiMMc8E9P1e90cR+JwOw87z0KIxyQ1bEK83A5qrf8DUEodBnzSsM4K899ngE3m6bNJ1jViSpbQWu9XSl3HlNTUBdyUUh3Ny2XCVMtiBPakMm5rLUzJGVrrU0qp7UBF4HYqsSUmLEWAAsAypVTiZ55JlrO772/SEd8y4B+l1BJgodZ6XyqxJEqsUTsEZEnh8+pASwCt9QpghVKqG6baxsQarwmYauGSsuPBpC3xeLZorSOBSPPxrzOXnwG8zdNXksR+MJXYksZ/EFMCaVRKvQ40VqaN18BU+5Yo8f44C2zTWkcDZ5VSiftN6TwXw1RbKYR4TFLDJsTLLTrJdGLbswSSJzLOSVfQWif9cY5PZbtJy+0w/aDbA6211mW01mWAKsAq8zJ3U9mO/X2x2GF6jPaodmcOwMkk+yqHKTFKZEzyd+J0muLTWn8MvAHcAGYppVKqmUoq8Ryn1rYvjuSPcouZ55Mua8+D/4G+f3tOSaZjky6YSjKc0rV/gNY66bbslFLuwC4gL7ABGEPy34qkyye9VxKldJ5Xp7RvIUTaScImRMYTDvgopbIppVyA+ulc3w7z4zWlVHlMNVvHgPXAh+byACAMU9u0h3VQWI+5hk0pVQDTj/vWh6yTWH4EyKKUqmqe7wjMNk8buJfcJJ1+ZHxKKQellAauaa0HATMwdYB4En8Dib1WQ4DxmBKhxkqpxFrLd83xJXUVU80USqlXMLVLS0sHiiftEFIEU0I+0Bzn/zAlyGmV0nnO9YQxCZHhScImxMsrae2Shdb6FjAU2AmsAbbdt87927h/2ogpWQrD1D6rlblW7jtMj8IOYHpE11trfTK1OMy6A7WVUvsxNZLvpLW+/JB1EttQxQLNgeFKqX1AO0xJG8BaoK9SqhmmZKm1UqprWuLTWsdjaku2Vim1E1PbrBEpxZDK8aRU3g14w3y++gHvaq0PYEqINpofVXthavuVdBu/AlmVUofM29jDvcekKV2X++N72LVMqSxxnX3AXuAwsBHYj6n9Y0rrphRHSudZHocK8YTsjMan2uNdCCGEEEI8ZVLDJoQQQghh4yRhE0IIIYSwcZKwCSGEEELYOEnYhBBCCCFsnCRsQgghhBA2ThI2IYQQQggbJwmbEEIIIYSNk4RNCCGEEMLG/R+atSsC54Tv3AAAAABJRU5ErkJggg==" style="border: 0px; box-sizing: border-box; display: block; height: auto; margin-left: auto; margin-right: auto; max-width: 90%; vertical-align: middle;" /></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; background-color: white; border: 1px solid transparent; box-sizing: border-box; color: #333333; display: flex; flex-direction: row; font-family: Roboto, HelveticaNeue-Light, 'Helvetica Neue Light', 'Helvetica Neue', sans-serif; font-size: 14px; line-height: 20px; margin: 0px; outline: none; padding: 0px; width: 847.5px;">
<div class="inner_cell" style="-webkit-box-align: stretch; -webkit-box-flex: 1; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; flex: 1 1 0%;">
<div class="text_cell_render border-box-sizing rendered_html" style="border-style: none; box-sizing: border-box; color: black; outline: none; padding: 0.5em 0em; resize: none; width: inherit;">
<div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
We can see this issue in action:</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered" style="-webkit-box-align: stretch; -webkit-box-orient: vertical; align-items: stretch; background-color: white; border: 1px solid transparent; box-sizing: border-box; color: #333333; display: flex; flex-direction: column; font-family: Roboto, HelveticaNeue-Light, 'Helvetica Neue Light', 'Helvetica Neue', sans-serif; font-size: 14px; line-height: 20px; margin: 0px; outline: none; padding: 5px 5px 5px 0px; width: 847.5px;">
<div class="input" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: row; page-break-inside: avoid;">
<div class="prompt input_prompt" style="border-top-color: transparent; border-top-style: solid; border-top-width: 1px; box-sizing: border-box; color: navy; font-family: monospace; line-height: 1.21429em; margin: 0px; min-width: 11ex; padding: 0.4em; text-align: right;">
In [10]:</div>
<div class="inner_cell" style="-webkit-box-align: stretch; -webkit-box-flex: 1; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; flex: 1 1 0%;">
<div class="input_area" style="background: rgb(247, 247, 247); border-radius: 4px; border: 1px solid rgb(207, 207, 207); box-sizing: border-box; line-height: 1.21429em; margin-right: 4px; padding: 0.2em;">
<div class="highlight-ipynb" style="background: rgb(248, 248, 248); box-sizing: border-box;">
<pre class="ipynb" style="background: rgb(247, 247, 247); border-radius: 4px; border: none; box-shadow: none; box-sizing: border-box; color: black; font-family: 'Source Code Pro', monospace !important; font-size: 13px !important; line-height: 16.25px; overflow: auto; padding: 0px; white-space: pre-wrap; word-break: break-all; word-wrap: break-word;"><span class="p" style="box-sizing: border-box;">[</span><span class="n" style="box-sizing: border-box;">column_to_index</span><span class="p" style="box-sizing: border-box;">(</span><span class="s" style="box-sizing: border-box; color: #ba2121;">'A'</span> <span class="o" style="box-sizing: border-box; color: #666666;">*</span> <span class="n" style="box-sizing: border-box;">i</span><span class="p" style="box-sizing: border-box;">)</span> <span class="o" style="box-sizing: border-box; color: #666666;">-</span> <span class="n" style="box-sizing: border-box;">column_to_index_cy</span><span class="p" style="box-sizing: border-box;">(</span><span class="s" style="box-sizing: border-box; color: #ba2121;">'A'</span> <span class="o" style="box-sizing: border-box; color: #666666;">*</span> <span class="n" style="box-sizing: border-box;">i</span><span class="p" style="box-sizing: border-box;">)</span> <span class="k" style="box-sizing: border-box; color: green; font-weight: bold;">for</span> <span class="n" style="box-sizing: border-box;">i</span> <span class="ow" style="box-sizing: border-box; color: #aa22ff; font-weight: bold;">in</span> <span class="nb" style="box-sizing: border-box; color: green;">xrange</span><span class="p" style="box-sizing: border-box;">(</span><span class="mi" style="box-sizing: border-box; color: #666666;">1</span><span class="p" style="box-sizing: border-box;">,</span> <span class="mi" style="box-sizing: border-box; color: #666666;">20</span><span class="p" style="box-sizing: border-box;">)]</span>
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper" style="-webkit-box-align: stretch; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; position: relative;">
<div class="output" style="-webkit-box-align: stretch; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column;">
<div class="output_area" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: row; padding: 0px; page-break-inside: avoid;">
<div class="prompt output_prompt" style="box-sizing: border-box; color: darkred; font-family: monospace; line-height: 1.21429em; margin: 0px; min-width: 11ex; padding: 0.4em; text-align: right;">
Out[10]:</div>
<div class="output_text output_subarea output_pyout" style="-webkit-box-flex: 1; box-sizing: border-box; color: black; flex: 1 1 0%; line-height: 1.21429em; padding: 0.4em 0.4em 0px;">
<pre style="background-color: transparent; border-radius: 0px; border: 0px; box-sizing: border-box; font-family: 'Source Code Pro', monospace !important; font-size: 13px !important; line-height: 16.25px; overflow: auto; padding: 0px; vertical-align: baseline; white-space: pre-wrap; word-break: break-all; word-wrap: break-word;">[0L,
0L,
0L,
0L,
0L,
0L,
0L,
0L,
0L,
0L,
0L,
0L,
0L,
0L,
55340232221128654848L,
1733993942928697851904L,
45342096933178077872128L,
1179171221423735667949568L,
30658673117946011881308160L]
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; background-color: white; border: 1px solid transparent; box-sizing: border-box; color: #333333; display: flex; flex-direction: row; font-family: Roboto, HelveticaNeue-Light, 'Helvetica Neue Light', 'Helvetica Neue', sans-serif; font-size: 14px; line-height: 20px; margin: 0px; outline: none; padding: 0px; width: 847.5px;">
<div class="inner_cell" style="-webkit-box-align: stretch; -webkit-box-flex: 1; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; flex: 1 1 0%;">
<div class="text_cell_render border-box-sizing rendered_html" style="border-style: none; box-sizing: border-box; color: black; outline: none; padding: 0.5em 0em; resize: none; width: inherit;">
<div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
There are ways to mitigate type problems, some more painful than others. Can you afford this to be a little slower? Leave the variable as a Python object and it will be taken care of dynamically. Maybe you want to go wild and use a library like <a href="https://gmplib.org/" style="background-color: transparent; box-sizing: border-box; color: #333333; line-height: inherit; outline: 0px;">GMP</a> for arbitary precision. In the real world, maybe your business logic maxes out at 10 characters anyway, making this a total non-issue and waste of time to deal with. It's all about keeping the end goal in mind.</div>
<div class="cell border-box-sizing text_cell rendered" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; border: 1px solid transparent; box-sizing: border-box; color: #333333; display: flex; flex-direction: row; margin: 0px; outline: none; padding: 0px; width: 847.5px;">
<div class="inner_cell" style="-webkit-box-align: stretch; -webkit-box-flex: 1; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; flex: 1 1 0%;">
<div class="text_cell_render border-box-sizing rendered_html" style="border-style: none; box-sizing: border-box; color: black; outline: none; padding: 0.5em 0em; resize: none; width: inherit;">
<h3 id="Use-when-appropriate,-not-when-not" style="box-sizing: border-box; color: inherit; font-family: 'Crimson Text', serif; font-size: 18.0040016174316px; line-height: 1; margin: 20px 0px 0px; text-rendering: optimizeLegibility;">
Use when appropriate, not when not<a class="anchor-link" href="http://isaacslavitt.com/2015/08/08/speeding-up-with-cython/#Use-when-appropriate,-not-when-not" style="background-color: transparent; box-sizing: border-box; color: #333333; outline: 0px; padding: 0px 20px; text-decoration: none; visibility: hidden;"></a></h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered" style="-webkit-box-align: stretch; -webkit-box-orient: horizontal; align-items: stretch; border: 1px solid transparent; box-sizing: border-box; color: #333333; display: flex; flex-direction: row; margin: 0px; outline: none; padding: 0px; width: 847.5px;">
<div class="inner_cell" style="-webkit-box-align: stretch; -webkit-box-flex: 1; -webkit-box-orient: vertical; align-items: stretch; box-sizing: border-box; display: flex; flex-direction: column; flex: 1 1 0%;">
<div class="text_cell_render border-box-sizing rendered_html" style="border-style: none; box-sizing: border-box; color: black; outline: none; padding: 0.5em 0em; resize: none; width: inherit;">
<div style="box-sizing: border-box; font-size: 18px; line-height: 1.4em; margin-bottom: 12px !important; margin-top: 12px !important; text-align: justify;">
Cython is not the only approach—plenty of good articles out there explain the tradeoffs between Cython and Numba (another strategic compiling package). It's just another tool in the toolbox, but it often turns out to be extremely handy and not too much extra work.</div>
</div>
</div>
</div>
</div>
</div>
</div>
Jeffyehttp://www.blogger.com/profile/07633630042179070371noreply@blogger.com0tag:blogger.com,1999:blog-5448019410873028021.post-12798038375007963722015-08-06T19:31:00.001-07:002015-08-06T19:31:27.766-07:00Top 10 Python idioms I wish I'd learned earlier<h3 class="post-title entry-title" itemprop="name" style="background-color: white; color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 22px; font-stretch: normal; margin: 0px; position: relative;">
Top 10 Python idioms I wish I'd learned earlier</h3>
<div class="post-header" style="background-color: white; color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 1.6; margin: 0px 0px 1em;">
<div class="post-header-line-1">
</div>
</div>
<div class="post-body entry-content" id="post-body-4282361933906847930" itemprop="description articleBody" style="background-color: white; color: #444444; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 18.2000007629395px; position: relative; width: 686px;">
<br /><br /><div class="cell border-box-sizing text_cell rendered">
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
I've been programming all my life, but never been <em>a</em> programmer. Most of my work was done in Visual Basic because it's what I was most comfortable with, plus a smattering of other languages (R, C, JavaScript, etc... Pascal, Applescript, Hypertext and BASIC, which I learned in 1979, if you go far back enough). A couple of years ago, I decided to use Python exclusively so that I could improve my coding. I ended up re-inventing many wheels -- which I didn't mind too much, because I enjoy solving puzzles. But sometimes it's good to have a more efficient, Pythonesque approach, and time after time I found myself having "aha!" moments, realizing I'd been doing things the hard, excessively verbose way for no reason. Here is a list of ten Python idioms that would have made my life much easier if I'd thought to search for them early on.<br /><br />Missing from this list are some idioms such as list comprehensions and lambda functions, which are very Pythonesque and very efficient and very cool, but also very difficult to miss because they're mentioned on StackOverflow every other answer! Also ternary x if y else z constructions, decorators and generators, because I don't use them very often.<br /><br />There's also an <a href="http://nbviewer.ipython.org/github/Prooffreader/Misc_ipynb/blob/master/top_10_python_idioms.ipynb" style="color: #4d469c; text-decoration: none;" target="new">IPython notebook nbviewer version</a> of this document if you prefer.</div>
</div>
</div>
<hr />
<br /><br /><div class="cell border-box-sizing text_cell rendered">
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="1-python-3-style-printing-in-python-2" style="border-bottom-color: transparent; border-bottom-style: solid; border-bottom-width: 1px; font-size: 14px; font-stretch: normal; line-height: normal; margin: 0px -15px; padding: 0.6em 15px 0.5em; position: relative;">
1. Python 3-style printing in Python 2</h2>
<br />One of the things that kept me from concentrating on Python was this whole version 2 - version 3 debacle. Finally I went with Python 2 because all the libraries I wanted were not 3-compatible, and I figured if I needed to, I would laboriously adjust my code later.<br /><br />But really, the biggest differences in everyday programming are printing and division, and now I just import from future. Now that almost all the libraries I use heavily are v3-compliant, I'll make the switch soon.<br /> </div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: rgb(248, 248, 248); border-radius: 4px; border: 0.1em solid gray; overflow: auto; padding: 0em 0.5em; width: auto;">
<pre><span class="n" style="color: black;">mynumber</span> <span class="o" style="color: black;">=</span> <span class="mi" style="color: black;">5</span>
<span class="k" style="color: #338822; font-weight: bold;">print</span> <span class="s" style="color: #cc2222;">"Python 2:"</span>
<span class="k" style="color: #338822; font-weight: bold;">print</span> <span class="s" style="color: #cc2222;">"The number is </span><span class="si" style="color: #c06688; font-weight: bold;">%d</span><span class="s" style="color: #cc2222;">"</span> <span class="o" style="color: black;">%</span> <span class="p">(</span><span class="n" style="color: black;">mynumber</span><span class="p">)</span>
<span class="k" style="color: #338822; font-weight: bold;">print</span> <span class="n" style="color: black;">mynumber</span> <span class="o" style="color: black;">/</span> <span class="mi" style="color: black;">2</span><span class="p">,</span>
<span class="k" style="color: #338822; font-weight: bold;">print</span> <span class="n" style="color: black;">mynumber</span> <span class="o" style="color: black;">//</span> <span class="mi" style="color: black;">2</span>
<span class="kn" style="color: #338822; font-weight: bold;">from</span> <span class="nn" style="color: #4d00ff; font-weight: bold;">__future__</span> <span class="kn" style="color: #338822; font-weight: bold;">import</span> <span class="n" style="color: black;">print_function</span>
<span class="kn" style="color: #338822; font-weight: bold;">from</span> <span class="nn" style="color: #4d00ff; font-weight: bold;">__future__</span> <span class="kn" style="color: #338822; font-weight: bold;">import</span> <span class="n" style="color: black;">division</span>
<span class="k" style="color: #338822; font-weight: bold;">print</span><span class="p">(</span><span class="s" style="color: #cc2222;">'</span><span class="se" style="color: #cc2222; font-weight: bold;">\n</span><span class="s" style="color: #cc2222;">Python 3:'</span><span class="p">)</span>
<span class="k" style="color: #338822; font-weight: bold;">print</span><span class="p">(</span><span class="s" style="color: #cc2222;">"The number is {}"</span><span class="o" style="color: black;">.</span><span class="n" style="color: black;">format</span><span class="p">(</span><span class="n" style="color: black;">mynumber</span><span class="p">))</span>
<span class="k" style="color: #338822; font-weight: bold;">print</span><span class="p">(</span><span class="n" style="color: black;">mynumber</span> <span class="o" style="color: black;">/</span> <span class="mi" style="color: black;">2</span><span class="p">,</span> <span class="n" style="color: black;">end</span><span class="o" style="color: black;">=</span><span class="s" style="color: #cc2222;">' '</span><span class="p">)</span>
<span class="k" style="color: #338822; font-weight: bold;">print</span><span class="p">(</span><span class="n" style="color: black;">mynumber</span> <span class="o" style="color: black;">//</span> <span class="mi" style="color: black;">2</span><span class="p">)</span>
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt">
</div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>Python 2:
The number is 5
2 2
Python 3:
The number is 5
2.5 2
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
Oh, and here's an easter egg for C programmers:</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: rgb(248, 248, 248); border-radius: 4px; border: 0.1em solid gray; overflow: auto; padding: 0em 0.5em; width: auto;">
<pre><span class="kn" style="color: #338822; font-weight: bold;">from</span> <span class="nn" style="color: #4d00ff; font-weight: bold;">__future__</span> <span class="kn" style="color: #338822; font-weight: bold;">import</span> <span class="n" style="color: black;">braces</span>
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt">
</div>
<div class="output_subarea output_text output_pyerr">
<pre><span class="ansicyan"> File </span><span class="ansigreen">"<ipython-input-3-2aebb3fc8ecf>"</span><span class="ansicyan">, line </span><span class="ansigreen">1</span>
<span class="ansiyellow"> from __future__ import braces</span>
<span class="ansired">SyntaxError</span><span class="ansired">:</span> not a chance
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="2-enumerate-list-" style="border-bottom-color: transparent; border-bottom-style: solid; border-bottom-width: 1px; font-size: 14px; font-stretch: normal; line-height: normal; margin: 0px -15px; padding: 0.6em 15px 0.5em; position: relative;">
2. enumerate(list)</h2>
<br />It might seem obvious that you should be able to iterate over a list and its index at the same time, but I used counter variables or slices for an embarrassingly long time.<br /> </div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: rgb(248, 248, 248); border-radius: 4px; border: 0.1em solid gray; overflow: auto; padding: 0em 0.5em; width: auto;">
<pre><span class="n" style="color: black;">mylist</span> <span class="o" style="color: black;">=</span> <span class="p">[</span><span class="s" style="color: #cc2222;">"It's"</span><span class="p">,</span> <span class="s" style="color: #cc2222;">'only'</span><span class="p">,</span> <span class="s" style="color: #cc2222;">'a'</span><span class="p">,</span> <span class="s" style="color: #cc2222;">'model'</span><span class="p">]</span>
<span class="k" style="color: #338822; font-weight: bold;">for</span> <span class="n" style="color: black;">index</span><span class="p">,</span> <span class="n" style="color: black;">item</span> <span class="ow" style="color: #ba22ff; font-weight: bold;">in</span> <span class="nb" style="color: #338822;">enumerate</span><span class="p">(</span><span class="n" style="color: black;">mylist</span><span class="p">):</span>
<span class="k" style="color: #338822; font-weight: bold;">print</span><span class="p">(</span><span class="n" style="color: black;">index</span><span class="p">,</span> <span class="n" style="color: black;">item</span><span class="p">)</span>
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt">
</div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>0 It's
1 only
2 a
3 model
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="3-chained-comparison-operators" style="border-bottom-color: transparent; border-bottom-style: solid; border-bottom-width: 1px; font-size: 14px; font-stretch: normal; line-height: normal; margin: 0px -15px; padding: 0.6em 15px 0.5em; position: relative;">
3. Chained comparison operators</h2>
<br />Because I was so used to statically typed languages (where this idiom would be ambiguous), it never occurred to me to put two operators in the same expression. In many languages, 4 > 3 > 2 would return as False, because (4 > 3) would be evaluated as a boolean, and then True > 2 would be evaluated as False.<br /> </div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: rgb(248, 248, 248); border-radius: 4px; border: 0.1em solid gray; overflow: auto; padding: 0em 0.5em; width: auto;">
<pre><span class="n" style="color: black;">mynumber</span> <span class="o" style="color: black;">=</span> <span class="mi" style="color: black;">3</span>
<span class="k" style="color: #338822; font-weight: bold;">if</span> <span class="mi" style="color: black;">4</span> <span class="o" style="color: black;">></span> <span class="n" style="color: black;">mynumber</span> <span class="o" style="color: black;">></span> <span class="mi" style="color: black;">2</span><span class="p">:</span>
<span class="k" style="color: #338822; font-weight: bold;">print</span><span class="p">(</span><span class="s" style="color: #cc2222;">"Chained comparison operators work! </span><span class="se" style="color: #cc2222; font-weight: bold;">\n</span><span class="s" style="color: #cc2222;">"</span> <span class="o" style="color: black;">*</span> <span class="mi" style="color: black;">3</span><span class="p">)</span>
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt">
</div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>Chained comparison operators work!
Chained comparison operators work!
Chained comparison operators work!
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="4-collections-counter" style="border-bottom-color: transparent; border-bottom-style: solid; border-bottom-width: 1px; font-size: 14px; font-stretch: normal; line-height: normal; margin: 0px -15px; padding: 0.6em 15px 0.5em; position: relative;">
4. collections.Counter</h2>
<br />The collections library is, like, the best thing ever. Stackoverflow turned me on to ordered dicts early on, but I kept using a snippet to create dicts to count occurrences of results in my code. One of these days, I'll figure out a use for collections.deque.<br /> </div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: rgb(248, 248, 248); border-radius: 4px; border: 0.1em solid gray; overflow: auto; padding: 0em 0.5em; width: auto;">
<pre><span class="kn" style="color: #338822; font-weight: bold;">from</span> <span class="nn" style="color: #4d00ff; font-weight: bold;">collections</span> <span class="kn" style="color: #338822; font-weight: bold;">import</span> <span class="n" style="color: black;">Counter</span>
<span class="kn" style="color: #338822; font-weight: bold;">from</span> <span class="nn" style="color: #4d00ff; font-weight: bold;">random</span> <span class="kn" style="color: #338822; font-weight: bold;">import</span> <span class="n" style="color: black;">randrange</span>
<span class="kn" style="color: #338822; font-weight: bold;">import</span> <span class="nn" style="color: #4d00ff; font-weight: bold;">pprint</span>
<span class="n" style="color: black;">mycounter</span> <span class="o" style="color: black;">=</span> <span class="n" style="color: black;">Counter</span><span class="p">()</span>
<span class="k" style="color: #338822; font-weight: bold;">for</span> <span class="n" style="color: black;">i</span> <span class="ow" style="color: #ba22ff; font-weight: bold;">in</span> <span class="nb" style="color: #338822;">range</span><span class="p">(</span><span class="mi" style="color: black;">100</span><span class="p">):</span>
<span class="n" style="color: black;">random_number</span> <span class="o" style="color: black;">=</span> <span class="n" style="color: black;">randrange</span><span class="p">(</span><span class="mi" style="color: black;">10</span><span class="p">)</span>
<span class="n" style="color: black;">mycounter</span><span class="p">[</span><span class="n" style="color: black;">random_number</span><span class="p">]</span> <span class="o" style="color: black;">+=</span> <span class="mi" style="color: black;">1</span>
<span class="k" style="color: #338822; font-weight: bold;">for</span> <span class="n" style="color: black;">i</span> <span class="ow" style="color: #ba22ff; font-weight: bold;">in</span> <span class="nb" style="color: #338822;">range</span><span class="p">(</span><span class="mi" style="color: black;">10</span><span class="p">):</span>
<span class="k" style="color: #338822; font-weight: bold;">print</span><span class="p">(</span><span class="n" style="color: black;">i</span><span class="p">,</span> <span class="n" style="color: black;">mycounter</span><span class="p">[</span><span class="n" style="color: black;">i</span><span class="p">])</span>
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt">
</div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>0 10
1 10
2 13
3 6
4 6
5 11
6 10
7 14
8 12
9 8
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="5-dict-comprehensions" style="border-bottom-color: transparent; border-bottom-style: solid; border-bottom-width: 1px; font-size: 14px; font-stretch: normal; line-height: normal; margin: 0px -15px; padding: 0.6em 15px 0.5em; position: relative;">
5. Dict comprehensions</h2>
<br />A rite of passage for a Python programmer is understanding list comprehensions, but eventually I realized dict comprehensions are just as useful -- especially for reversing dicts.<br /> </div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: rgb(248, 248, 248); border-radius: 4px; border: 0.1em solid gray; overflow: auto; padding: 0em 0.5em; width: auto;">
<pre><span class="n" style="color: black;">my_phrase</span> <span class="o" style="color: black;">=</span> <span class="p">[</span><span class="s" style="color: #cc2222;">"No"</span><span class="p">,</span> <span class="s" style="color: #cc2222;">"one"</span><span class="p">,</span> <span class="s" style="color: #cc2222;">"expects"</span><span class="p">,</span> <span class="s" style="color: #cc2222;">"the"</span><span class="p">,</span> <span class="s" style="color: #cc2222;">"Spanish"</span><span class="p">,</span> <span class="s" style="color: #cc2222;">"Inquisition"</span><span class="p">]</span>
<span class="n" style="color: black;">my_dict</span> <span class="o" style="color: black;">=</span> <span class="p">{</span><span class="n" style="color: black;">key</span><span class="p">:</span> <span class="n" style="color: black;">value</span> <span class="k" style="color: #338822; font-weight: bold;">for</span> <span class="n" style="color: black;">value</span><span class="p">,</span> <span class="n" style="color: black;">key</span> <span class="ow" style="color: #ba22ff; font-weight: bold;">in</span> <span class="nb" style="color: #338822;">enumerate</span><span class="p">(</span><span class="n" style="color: black;">my_phrase</span><span class="p">)}</span>
<span class="k" style="color: #338822; font-weight: bold;">print</span><span class="p">(</span><span class="n" style="color: black;">my_dict</span><span class="p">)</span>
<span class="n" style="color: black;">reversed_dict</span> <span class="o" style="color: black;">=</span> <span class="p">{</span><span class="n" style="color: black;">value</span><span class="p">:</span> <span class="n" style="color: black;">key</span> <span class="k" style="color: #338822; font-weight: bold;">for</span> <span class="n" style="color: black;">key</span><span class="p">,</span> <span class="n" style="color: black;">value</span> <span class="ow" style="color: #ba22ff; font-weight: bold;">in</span> <span class="n" style="color: black;">my_dict</span><span class="o" style="color: black;">.</span><span class="n" style="color: black;">items</span><span class="p">()}</span>
<span class="k" style="color: #338822; font-weight: bold;">print</span><span class="p">(</span><span class="n" style="color: black;">reversed_dict</span><span class="p">)</span>
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt">
</div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>{'Inquisition': 5, 'No': 0, 'expects': 2, 'one': 1, 'Spanish': 4, 'the': 3}
{0: 'No', 1: 'one', 2: 'expects', 3: 'the', 4: 'Spanish', 5: 'Inquisition'}
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="6-executing-shell-commands-with-subprocess" style="border-bottom-color: transparent; border-bottom-style: solid; border-bottom-width: 1px; font-size: 14px; font-stretch: normal; line-height: normal; margin: 0px -15px; padding: 0.6em 15px 0.5em; position: relative;">
6. Executing shell commands with subprocess</h2>
<br />I used to use the os library exclusively to manipulate files; now I can even programmatically call complex command-line tools like ffmpeg for video editing<br /><br />(And yes, I use Windows, so do all of my clients. But I have the good grace to be embarrassed about it!)<br /><br />Note that the particular subprocess I picked would be much better done with the os library; I just wanted a command everyone would be familiar with. And in general, shell=True is a VERY bad idea, I put it here so that the command output would appear in the IPython notebook cell. Don't try this at home, kids!<br /> </div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: rgb(248, 248, 248); border-radius: 4px; border: 0.1em solid gray; overflow: auto; padding: 0em 0.5em; width: auto;">
<pre><span class="kn" style="color: #338822; font-weight: bold;">import</span> <span class="nn" style="color: #4d00ff; font-weight: bold;">subprocess</span>
<span class="n" style="color: black;">output</span> <span class="o" style="color: black;">=</span> <span class="n" style="color: black;">subprocess</span><span class="o" style="color: black;">.</span><span class="n" style="color: black;">check_output</span><span class="p">(</span><span class="s" style="color: #cc2222;">'dir'</span><span class="p">,</span> <span class="n" style="color: black;">shell</span><span class="o" style="color: black;">=</span><span class="bp">True</span><span class="p">)</span>
<span class="k" style="color: #338822; font-weight: bold;">print</span><span class="p">(</span><span class="n" style="color: black;">output</span><span class="p">)</span>
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt">
</div>
<div class="output_subarea output_stream output_stdout output_text">
<pre> Volume in drive C is OS
Volume Serial Number is [REDACTED]
Directory of C:\Users\David\Documents\[REDACTED]
2014-11-26 06:04 AM <DIR> .
2014-11-26 06:04 AM <DIR> ..
2014-11-23 11:47 AM <DIR> .git
2014-11-26 06:06 AM <DIR> .ipynb_checkpoints
2014-11-23 08:59 AM <DIR> CCCma
2014-09-03 06:58 AM 19,450 colorbrewdict.py
2014-09-03 06:58 AM 92,175 imagecompare.ipynb
2014-11-23 08:41 AM <DIR> Japan_Earthquakes
2014-09-03 06:58 AM 1,100 LICENSE
2014-09-03 06:58 AM 5,263 monty_monte.ipynb
2014-09-03 06:58 AM 31,082 pocket_tumblr_reddit_api.ipynb
2014-11-26 06:04 AM 3,211 README.md
2014-11-26 06:14 AM 19,898 top_10_python_idioms.ipynb
2014-09-03 06:58 AM 5,813 tree_convert_mega_to_gexf.ipynb
2014-09-03 06:58 AM 5,453 tree_convert_mega_to_json.ipynb
2014-09-03 06:58 AM 1,211 tree_convert_newick_to_json.py
2014-09-03 06:58 AM 55,970 weather_ML.ipynb
11 File(s) 240,626 bytes
6 Dir(s) 180,880,490,496 bytes free
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="7-dict-get-and-iteritems-methods" style="border-bottom-color: transparent; border-bottom-style: solid; border-bottom-width: 1px; font-size: 14px; font-stretch: normal; line-height: normal; margin: 0px -15px; padding: 0.6em 15px 0.5em; position: relative;">
7. dict .get() and .iteritems() methods</h2>
<br />Having a default value when a key does not exist has all kinds of uses, and just like enumerate() for lists, you can iterate over key, value tuples in dicts<br /> </div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: rgb(248, 248, 248); border-radius: 4px; border: 0.1em solid gray; overflow: auto; padding: 0em 0.5em; width: auto;">
<pre><span class="n" style="color: black;">my_dict</span> <span class="o" style="color: black;">=</span> <span class="p">{</span><span class="s" style="color: #cc2222;">'name'</span><span class="p">:</span> <span class="s" style="color: #cc2222;">'Lancelot'</span><span class="p">,</span> <span class="s" style="color: #cc2222;">'quest'</span><span class="p">:</span> <span class="s" style="color: #cc2222;">'Holy Grail'</span><span class="p">,</span> <span class="s" style="color: #cc2222;">'favourite_color'</span><span class="p">:</span> <span class="s" style="color: #cc2222;">'blue'</span><span class="p">}</span>
<span class="k" style="color: #338822; font-weight: bold;">print</span><span class="p">(</span><span class="n" style="color: black;">my_dict</span><span class="o" style="color: black;">.</span><span class="n" style="color: black;">get</span><span class="p">(</span><span class="s" style="color: #cc2222;">'airspeed velocity of an unladen swallow'</span><span class="p">,</span> <span class="s" style="color: #cc2222;">'African or European?</span><span class="se" style="color: #cc2222; font-weight: bold;">\n</span><span class="s" style="color: #cc2222;">'</span><span class="p">))</span>
<span class="k" style="color: #338822; font-weight: bold;">for</span> <span class="n" style="color: black;">key</span><span class="p">,</span> <span class="n" style="color: black;">value</span> <span class="ow" style="color: #ba22ff; font-weight: bold;">in</span> <span class="n" style="color: black;">my_dict</span><span class="o" style="color: black;">.</span><span class="n" style="color: black;">iteritems</span><span class="p">():</span>
<span class="k" style="color: #338822; font-weight: bold;">print</span><span class="p">(</span><span class="n" style="color: black;">key</span><span class="p">,</span> <span class="n" style="color: black;">value</span><span class="p">,</span> <span class="n" style="color: black;">sep</span><span class="o" style="color: black;">=</span><span class="s" style="color: #cc2222;">": "</span><span class="p">)</span>
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt">
</div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>African or European?
quest: Holy Grail
name: Lancelot
favourite_color: blue
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="8-tuple-unpacking-for-switching-variables" style="border-bottom-color: transparent; border-bottom-style: solid; border-bottom-width: 1px; font-size: 14px; font-stretch: normal; line-height: normal; margin: 0px -15px; padding: 0.6em 15px 0.5em; position: relative;">
8. Tuple unpacking for switching variables</h2>
<br />Do you know how many times I had to use a third, temporary dummy variable in VB? c = a; a = b; b = c?<br /> </div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: rgb(248, 248, 248); border-radius: 4px; border: 0.1em solid gray; overflow: auto; padding: 0em 0.5em; width: auto;">
<pre><span class="n" style="color: black;">a</span> <span class="o" style="color: black;">=</span> <span class="s" style="color: #cc2222;">'Spam'</span>
<span class="n" style="color: black;">b</span> <span class="o" style="color: black;">=</span> <span class="s" style="color: #cc2222;">'Eggs'</span>
<span class="k" style="color: #338822; font-weight: bold;">print</span><span class="p">(</span><span class="n" style="color: black;">a</span><span class="p">,</span> <span class="n" style="color: black;">b</span><span class="p">)</span>
<span class="n" style="color: black;">a</span><span class="p">,</span> <span class="n" style="color: black;">b</span> <span class="o" style="color: black;">=</span> <span class="n" style="color: black;">b</span><span class="p">,</span> <span class="n" style="color: black;">a</span>
<span class="k" style="color: #338822; font-weight: bold;">print</span><span class="p">(</span><span class="n" style="color: black;">a</span><span class="p">,</span> <span class="n" style="color: black;">b</span><span class="p">)</span>
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt">
</div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>Spam Eggs
Eggs Spam
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="9-introspection-tools" style="border-bottom-color: transparent; border-bottom-style: solid; border-bottom-width: 1px; font-size: 14px; font-stretch: normal; line-height: normal; margin: 0px -15px; padding: 0.6em 15px 0.5em; position: relative;">
9. Introspection tools</h2>
<br />I was aware of dir(), but I had assumed help() would do the same thing as IPython's ? magic command. It does way more. (This post has been updated after some great advice from reddit's /r/python which, indeed, I wish I'd known about before!)<br /> </div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: rgb(248, 248, 248); border-radius: 4px; border: 0.1em solid gray; overflow: auto; padding: 0em 0.5em; width: auto;">
<pre><span class="n" style="color: black;">my_dict</span> <span class="o" style="color: black;">=</span> <span class="p">{</span><span class="s" style="color: #cc2222;">'That'</span><span class="p">:</span> <span class="s" style="color: #cc2222;">'an ex-parrot!'</span><span class="p">}</span>
<span class="n" style="color: black;">help</span><span class="p">(</span><span class="n" style="color: black;">my_dict</span><span class="p">)</span>
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt">
</div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>Help on dict object:
class dict(object)
| dict() -> new empty dictionary
| dict(mapping) -> new dictionary initialized from a mapping object's
| (key, value) pairs
| dict(iterable) -> new dictionary initialized as if via:
| d = {}
| for k, v in iterable:
| d[k] = v
| dict(**kwargs) -> new dictionary initialized with the name=value pairs
| in the keyword argument list. For example: dict(one=1, two=2)
|
| Methods defined here:
|
| __cmp__(...)
| x.__cmp__(y) <==> cmp(x,y)
|
| __contains__(...)
| D.__contains__(k) -> True if D has a key k, else False
|
| __delitem__(...)
| x.__delitem__(y) <==> del x[y]
|
| __eq__(...)
| x.__eq__(y) <==> x==y
|
[TRUNCATED FOR SPACE]
|
| update(...)
| D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
| If E present and has a .keys() method, does: for k in E: D[k] = E[k]
| If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v
| In either case, this is followed by: for k in F: D[k] = F[k]
|
| values(...)
| D.values() -> list of D's values
|
| viewitems(...)
| D.viewitems() -> a set-like object providing a view on D's items
|
| viewkeys(...)
| D.viewkeys() -> a set-like object providing a view on D's keys
|
| viewvalues(...)
| D.viewvalues() -> an object providing a view on D's values
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __hash__ = None
|
| __new__ = <built-in __new__="" method="" object="" of="" type="">
| T.__new__(S, ...) -> a new object with type S, a subtype of T
</built-in></pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="10-pep-8-compliant-string-chaining" style="border-bottom-color: transparent; border-bottom-style: solid; border-bottom-width: 1px; font-size: 14px; font-stretch: normal; line-height: normal; margin: 0px -15px; padding: 0.6em 15px 0.5em; position: relative;">
10. PEP-8 compliant string chaining</h2>
<br /><a href="https://www.python.org/dev/peps/pep-0008" style="color: #4d469c; text-decoration: none;">PEP8</a> is the style guide for Python code. Among other things, it directs that lines not be over 80 characters long and that indenting by consistent over line breaks.<br /><br />This can be accomplished with a combination of backslashes \; parentheses () with commas , ; and addition operators +; but every one of these solutions is awkward for multiline strings. There is a multiline string signifier, the triple quote, but it does not allow consistent indenting over line breaks.<br /><br />There is a solution: parentheses without commas. I don't know why this works, but I'm glad it does.<br /> </div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: rgb(248, 248, 248); border-radius: 4px; border: 0.1em solid gray; overflow: auto; padding: 0em 0.5em; width: auto;">
<pre><span class="n" style="color: black;">my_long_text</span> <span class="o" style="color: black;">=</span> <span class="p">(</span><span class="s" style="color: #cc2222;">"We are no longer the knights who say Ni! "</span>
<span class="s" style="color: #cc2222;">"We are now the knights who say ekki-ekki-"</span>
<span class="s" style="color: #cc2222;">"ekki-p'tang-zoom-boing-z'nourrwringmm!"</span><span class="p">)</span>
<span class="k" style="color: #338822; font-weight: bold;">print</span><span class="p">(</span><span class="n" style="color: black;">my_long_text</span><span class="p">)</span>
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt">
</div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>We are no longer the knights who say Ni! We are now the knights who say ekki-ekki-ekki-p'tang-zoom-boing-z'nourrwringmm!
</pre>
<div>
<br /></div>
</div>
</div>
</div>
</div>
</div>
</div>
Jeffyehttp://www.blogger.com/profile/07633630042179070371noreply@blogger.com0tag:blogger.com,1999:blog-5448019410873028021.post-66289065117831962652015-06-04T07:13:00.001-07:002015-06-04T07:13:41.594-07:00Overcoming frustration: Correctly using unicode in python2<h1 data-mce-style="font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: #20435c; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #cccccc; margin: 20px -20px 10px; padding: 3px 0px 3px 10px; background-color: #f2f2f2; margin-top: 0px; font-size: 200%;" style="background-color: #f2f2f2; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-width: 0px 0px 1px; color: #20435c; font-family: 'Trebuchet MS', sans-serif; font-size: 28px; font-weight: normal; line-height: 0.9523em; margin: 0px -20px 10px; padding: 3px 0px 3px 10px;">
Overcoming frustration: Correctly using unicode in python2<a data-mce-href="https://pythonhosted.org/kitchen/unicode-frustrations.html#overcoming-frustration-correctly-using-unicode-in-python2" data-mce-style="color: #c60f0f; text-decoration: none; visibility: hidden; font-size: 0.8em; padding: 0px 4px;" href="https://pythonhosted.org/kitchen/unicode-frustrations.html#overcoming-frustration-correctly-using-unicode-in-python2" style="border: 0px; color: #c60f0f; font-size: 0.8em; line-height: 1.428571em; margin: 0px; padding: 0px 4px; text-decoration: none; visibility: hidden;" title="Permalink to this headline"></a></h1>
<div data-mce-style="text-align: justify; line-height: 130%;" style="background-color: white; border: 0px; font-family: Helvetica, Arial, 'Droid Sans', sans-serif; font-size: 14px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
In <span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">python</span>-2.x, there’s two types that deal with text.</div>
<ol data-mce-style="list-style: decimal;" style="background-color: white; border: 0px; font-family: Helvetica, Arial, 'Droid Sans', sans-serif; font-size: 14px; line-height: 19.9999942779541px; margin: -0.428571em 0px 0.714285em 2em; padding: 0px;">
<li data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin: 0px; padding: 0px; text-align: justify;"><a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> is for strings of bytes. These are very similar in nature to how strings are handled in C.</li>
<li data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin: 0px; padding: 0px; text-align: justify;"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> is for strings of unicode <a data-mce-href="https://pythonhosted.org/kitchen/glossary.html#term-code-points" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/glossary.html#term-code-points" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;"><em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">code points</em></a>.</li>
</ol>
<div data-mce-style="margin-top: 10px; margin-bottom: 10px; padding: 7px; border: 1px solid #cccccc; background-color: #eeeeee;" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); font-family: Helvetica, Arial, 'Droid Sans', sans-serif; font-size: 14px; line-height: 19.9999942779541px; margin: 10px 0px; padding: 7px;">
<div data-mce-style="margin: 0px 10px 5px 0px; font-weight: bold; display: inline; text-align: justify; line-height: 130%; margin-bottom: 5px;" style="border: 0px; display: inline; font-weight: bold; line-height: 18.2000007629395px; margin-bottom: 5px; margin-right: 10px; padding: 0px; text-align: justify;">
Note<span data-mce-style="font-weight: bold; text-align: justify; line-height: 130%;" style="line-height: 18.2000007629395px;">:</span></div>
<div data-mce-style="text-align: justify; line-height: 130%; margin-bottom: 5px; display: inline;" style="border: 0px; display: inline; line-height: 18.2000007629395px; margin-bottom: 5px; padding: 0px; text-align: justify;">
<strong style="line-height: 1.428571em;">Just what the dickens is “Unicode”?</strong></div>
<div data-mce-style="text-align: justify; line-height: 130%; margin-bottom: 5px;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 5px; padding: 0px; text-align: justify;">
One mistake that people encountering this issue for the first time make is confusing the <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; font-weight: bold; background: #d6d6d6;" style="background: rgb(214, 214, 214); font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> type and the encodings of unicode stored in the <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; font-weight: bold; background: #d6d6d6;" style="background: rgb(214, 214, 214); font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> type. In <span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">python</span>, the <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; font-weight: bold; background: #d6d6d6;" style="background: rgb(214, 214, 214); font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> type stores an abstract sequence of <a data-mce-href="https://pythonhosted.org/kitchen/glossary.html#term-code-points" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/glossary.html#term-code-points" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;"><em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">code points</em></a>. Each <a data-mce-href="https://pythonhosted.org/kitchen/glossary.html#term-code-point" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/glossary.html#term-code-point" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;"><em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">code point</em></a> represents a <a data-mce-href="https://pythonhosted.org/kitchen/glossary.html#term-grapheme" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/glossary.html#term-grapheme" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;"><em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">grapheme</em></a>. By contrast, byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; font-weight: bold; background: #d6d6d6;" style="background: rgb(214, 214, 214); font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> stores a sequence of bytes which can then be mapped to a sequence of <a data-mce-href="https://pythonhosted.org/kitchen/glossary.html#term-code-points" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/glossary.html#term-code-points" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;"><em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">code points</em></a>. Each unicode <span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">encodi</span><span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">ng</span> (<a data-mce-href="https://pythonhosted.org/kitchen/glossary.html#term-utf-8" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/glossary.html#term-utf-8" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;"><em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">UTF-8</em></a>, UTF-7, UTF-16, UTF-32, etc) maps different sequences of bytes to the unicode <a data-mce-href="https://pythonhosted.org/kitchen/glossary.html#term-code-points" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/glossary.html#term-code-points" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;"><em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">code points</em></a>.</div>
<div data-mce-style="text-align: justify; line-height: 130%; margin-bottom: 5px;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 5px; padding: 0px; text-align: justify;">
What does that mean to you as a programmer? When you’re dealing with text manipulations (finding the number of characters in a string or cutting a string on word boundaries) you should be dealing with <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; font-weight: bold; background: #d6d6d6;" style="background: rgb(214, 214, 214); font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> strings as they abstract characters in a manner that’s appropriate for thinking of them as a sequence of letters that you will see on a page. When dealing with I/O, reading to and from the disk, printing to a terminal, sending something over a network link, etc, you should be dealing with byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; font-weight: bold; background: #d6d6d6;" style="background: rgb(214, 214, 214); font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> as those devices are going to need to deal with concrete implementations of what bytes represent your abstract characters.</div>
</div>
<div data-mce-style="text-align: justify; line-height: 130%;" style="background-color: white; border: 0px; font-family: Helvetica, Arial, 'Droid Sans', sans-serif; font-size: 14px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
In the python2 world many APIs use these two classes interchangably but there are several important APIs where only one or the other will do the right thing. When you give the wrong type of string to an API that wants the other type, you may end up with an exception being raised (<tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">UnicodeDecodeError</span></tt> or <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">UnicodeEncodeError</span></tt>). However, these exceptions aren’t always raised because <span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">python</span> implicitly converts between types... <em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">sometimes</em>.</div>
<div style="background-color: white; border: 0px; font-family: Helvetica, Arial, 'Droid Sans', sans-serif; font-size: 14px; line-height: 19.9999942779541px; margin: 0px; padding: 0px;">
<h2 data-mce-style="font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: #20435c; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #cccccc; margin: 20px -20px 10px; padding: 3px 0px 3px 10px; background-color: #f2f2f2; font-size: 160%;" style="background-color: #f2f2f2; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-width: 0px 0px 1px; color: #20435c; font-family: 'Trebuchet MS', sans-serif; font-size: 22.3999996185303px; font-weight: normal; line-height: 1.111em; margin: 20px -20px 10px; padding: 3px 0px 3px 10px;">
Frustration #1: Inconsistent Errors<a data-mce-href="https://pythonhosted.org/kitchen/unicode-frustrations.html#frustration-1-inconsistent-errors" data-mce-style="color: #c60f0f; text-decoration: none; visibility: hidden; font-size: 0.8em; padding: 0px 4px;" href="https://pythonhosted.org/kitchen/unicode-frustrations.html#frustration-1-inconsistent-errors" style="border: 0px; color: #c60f0f; font-size: 0.8em; line-height: 1.428571em; margin: 0px; padding: 0px 4px; text-decoration: none; visibility: hidden;" title="Permalink to this headline"></a></h2>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
Although converting when possible seems like the right thing to do, it’s actually the first source of frustration. A programmer can test out their program with a string like: <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: #ecf0f3;" style="background-color: #ecf0f3; font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">The</span> <span style="line-height: 1.428571em;">quick</span> <span style="line-height: 1.428571em;">brown</span> <span style="line-height: 1.428571em;">fox</span> <span style="line-height: 1.428571em;">jumped</span> <span style="line-height: 1.428571em;">over</span> <span style="line-height: 1.428571em;">the</span> <span style="line-height: 1.428571em;">lazy</span> <span style="line-height: 1.428571em;">dog</span></tt> and not encounter any issues. But when they release their software into the wild, someone enters the string: <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: #ecf0f3;" style="background-color: #ecf0f3; font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">I</span> <span style="line-height: 1.428571em;">sat</span> <span style="line-height: 1.428571em;">down</span> <span style="line-height: 1.428571em;">for</span> <span style="line-height: 1.428571em;">coffee</span> <span style="line-height: 1.428571em;">at</span> <span style="line-height: 1.428571em;">the</span> <span style="line-height: 1.428571em;">café</span></tt> and suddenly an exception is thrown. The reason? The mechanism that converts between the two types is only able to deal with <a data-mce-href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;"><em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">ASCII</em></a> characters. Once you throw non-<a data-mce-href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;"><em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">ASCII</em></a> characters into your strings, you have to start dealing with the conversion manually.</div>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
So, if I manually convert everything to either byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> or <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> strings, will I be okay? The answer is.... <em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">sometimes</em>.</div>
</div>
<div style="background-color: white; border: 0px; font-family: Helvetica, Arial, 'Droid Sans', sans-serif; font-size: 14px; line-height: 19.9999942779541px; margin: 0px; padding: 0px;">
<h2 data-mce-style="font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: #20435c; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #cccccc; margin: 20px -20px 10px; padding: 3px 0px 3px 10px; background-color: #f2f2f2; font-size: 160%;" style="background-color: #f2f2f2; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-width: 0px 0px 1px; color: #20435c; font-family: 'Trebuchet MS', sans-serif; font-size: 22.3999996185303px; font-weight: normal; line-height: 1.111em; margin: 20px -20px 10px; padding: 3px 0px 3px 10px;">
Frustration #2: Inconsistent APIs<a data-mce-href="https://pythonhosted.org/kitchen/unicode-frustrations.html#frustration-2-inconsistent-apis" data-mce-style="color: #c60f0f; text-decoration: none; visibility: hidden; font-size: 0.8em; padding: 0px 4px;" href="https://pythonhosted.org/kitchen/unicode-frustrations.html#frustration-2-inconsistent-apis" style="border: 0px; color: #c60f0f; font-size: 0.8em; line-height: 1.428571em; margin: 0px; padding: 0px 4px; text-decoration: none; visibility: hidden;" title="Permalink to this headline"></a></h2>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
The problem you run into when converting everything to byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> or <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> strings is that you’ll be using someone else’s API quite often (this includes the APIs in the <span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;"></span><a data-mce-href="http://docs.python.org/library" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;"><span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">python</span> standard library</a>) and find that the API will only accept byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> or only accept <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> strings. Or worse, that the code will accept either when you’re dealing with strings that consist solely of <a data-mce-href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;"><em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">ASCII</em></a> but throw an error when you give it a string that’s got non-<a data-mce-href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;"><em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">ASCII</em></a> characters. When you encounter these APIs you first need to identify which type will work better and then you have to convert your values to the correct type for that code. Thus the programmer that wants to proactively fix all unicode errors in their code needs to do two things:</div>
<ol data-mce-style="list-style: decimal;" style="border: 0px; line-height: 1.428571em; margin: -0.428571em 0px 0.714285em 2em; padding: 0px;">
<li data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin: 0px; padding: 0px; text-align: justify;">You must keep track of what type your sequences of text are. Does <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: #ecf0f3;" style="background-color: #ecf0f3; font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">my_sentence</span></tt> contain <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> or <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a>? If you don’t know that then you’re going to be in for a world of hurt.</li>
<li data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin: 0px; padding: 0px; text-align: justify;">Anytime you call a function you need to evaluate whether that function will do the right thing with <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> or <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> values. Sending the wrong value here will lead to a <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">UnicodeError</span></tt> being thrown when the string contains non-<a data-mce-href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;"><em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">ASCII</em></a> characters.</li>
</ol>
<div data-mce-style="margin-top: 10px; margin-bottom: 10px; padding: 7px; border: 1px solid #cccccc; background-color: #eeeeee;" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); line-height: 1.428571em; margin: 10px 0px; padding: 7px;">
<div data-mce-style="margin: 0px 10px 5px 0px; font-weight: bold; display: inline; text-align: justify; line-height: 130%; margin-bottom: 5px;" style="border: 0px; display: inline; font-weight: bold; line-height: 18.2000007629395px; margin-bottom: 5px; margin-right: 10px; padding: 0px; text-align: justify;">
Note<span data-mce-style="font-weight: bold; text-align: justify; line-height: 130%;" style="line-height: 18.2000007629395px;">:</span></div>
<div data-mce-style="text-align: justify; line-height: 130%; margin-bottom: 5px; display: inline;" style="border: 0px; display: inline; line-height: 18.2000007629395px; margin-bottom: 5px; padding: 0px; text-align: justify;">
There is one mitigating factor here. The <span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">python</span> community has been standardizing on using <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; font-weight: bold; background: #d6d6d6;" style="background: rgb(214, 214, 214); font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> in all its APIs. Although there are some APIs that you need to send byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; font-weight: bold; background: #d6d6d6;" style="background: rgb(214, 214, 214); font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> to in order to be safe, (including things as ubiquitous as <a data-mce-href="http://docs.python.org/library/functions.html#print" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#print" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; font-weight: bold; background: #d6d6d6;" style="background: rgb(214, 214, 214); font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">print()</span></tt></a> as we’ll see in the next section), it’s getting easier and easier to use <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; font-weight: bold; background: #d6d6d6;" style="background: rgb(214, 214, 214); font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> strings with most APIs.</div>
</div>
</div>
<div style="background-color: white; border: 0px; font-family: Helvetica, Arial, 'Droid Sans', sans-serif; font-size: 14px; line-height: 19.9999942779541px; margin: 0px; padding: 0px;">
<h2 data-mce-style="font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: #20435c; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #cccccc; margin: 20px -20px 10px; padding: 3px 0px 3px 10px; background-color: #f2f2f2; font-size: 160%;" style="background-color: #f2f2f2; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-width: 0px 0px 1px; color: #20435c; font-family: 'Trebuchet MS', sans-serif; font-size: 22.3999996185303px; font-weight: normal; line-height: 1.111em; margin: 20px -20px 10px; padding: 3px 0px 3px 10px;">
Frustration #3: Inconsistent treatment of output<a data-mce-href="https://pythonhosted.org/kitchen/unicode-frustrations.html#frustration-3-inconsistent-treatment-of-output" data-mce-style="color: #c60f0f; text-decoration: none; visibility: hidden; font-size: 0.8em; padding: 0px 4px;" href="https://pythonhosted.org/kitchen/unicode-frustrations.html#frustration-3-inconsistent-treatment-of-output" style="border: 0px; color: #c60f0f; font-size: 0.8em; line-height: 1.428571em; margin: 0px; padding: 0px 4px; text-decoration: none; visibility: hidden;" title="Permalink to this headline"></a></h2>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
Alright, since the <span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">python</span> community is moving to using <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> strings everywhere, we might as well convert everything to <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> strings and use that by default, right? Sounds good most of the time but there’s at least one huge caveat to be aware of. Anytime you output text to the terminal or to a file, the text has to be converted into a byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a>. <span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">Python</span> will try to implicitly convert from <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> to byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a>... but it will throw an exception if the bytes are non-<a data-mce-href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;"><em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">ASCII</em></a>:</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<div data-mce-style="background: #eeffcc;" style="background: rgb(238, 255, 204); border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre data-mce-style="overflow-x: auto; overflow-y: hidden; padding: 5px; color: #333333; line-height: 120%; border-top-width: 1px; border-bottom-width: 1px; border-style: solid none; border-top-color: #aacc99; border-bottom-color: #aacc99; background-color: #eeffcc;" style="border-bottom-color: rgb(170, 204, 153); border-style: solid none; border-top-color: rgb(170, 204, 153); border-width: 1px 0px; color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.7999992370605px; overflow-x: auto; overflow-y: hidden; padding: 5px;" xml:space="preserve"><span data-mce-style="color: #c65d09; font-weight: bold;" style="color: #c65d09; font-weight: bold; line-height: 1.428571em;">>>> </span><span style="line-height: 1.428571em;">string</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span data-mce-style="color: #007020;" style="color: #007020; line-height: 1.428571em;">unicode</span><span style="line-height: 1.428571em;">(</span><span data-mce-style="color: #007020;" style="color: #007020; line-height: 1.428571em;">raw_input</span><span style="line-height: 1.428571em;">(),</span> <span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'utf8'</span><span style="line-height: 1.428571em;">)</span>
<span data-mce-style="color: #333333;" style="line-height: 1.428571em;">café</span>
<span data-mce-style="color: #c65d09; font-weight: bold;" style="color: #c65d09; font-weight: bold; line-height: 1.428571em;">>>> </span><span style="line-height: 1.428571em;">log</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span data-mce-style="color: #007020;" style="color: #007020; line-height: 1.428571em;">open</span><span style="line-height: 1.428571em;">(</span><span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'/var/tmp/debug.log'</span><span style="line-height: 1.428571em;">,</span> <span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'w'</span><span style="line-height: 1.428571em;">)</span>
<span data-mce-style="color: #c65d09; font-weight: bold;" style="color: #c65d09; font-weight: bold; line-height: 1.428571em;">>>> </span><span style="line-height: 1.428571em;">log</span><span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">write</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">string</span><span style="line-height: 1.428571em;">)</span>
<span data-mce-style="color: #0044dd;" style="color: #0044dd; line-height: 1.428571em;">Traceback (most recent call last):</span>
File <span data-mce-style="color: #007020;" style="color: #007020; line-height: 1.428571em;">"<stdin>"</span>, line <span data-mce-style="color: #208050;" style="color: #208050; line-height: 1.428571em;">1</span>, in <span style="line-height: 1.428571em;"><module></span>
<span data-mce-style="color: #ff0000;" style="color: red; line-height: 1.428571em;">UnicodeEncodeError</span>: <span style="line-height: 1.428571em;">'ascii' codec can't encode character u'\xe9' in position 3: ordinal not in range(128)</span>
<span data-mce-style="color: #0044dd;" style="color: #0044dd; line-height: 1.428571em;">Traceback (most recent call last):</span>
File <span data-mce-style="color: #007020;" style="color: #007020; line-height: 1.428571em;">"<stdin>"</span>, line <span data-mce-style="color: #208050;" style="color: #208050; line-height: 1.428571em;">1</span>, in <span style="line-height: 1.428571em;"><module></span>
<span data-mce-style="color: #ff0000;" style="color: red; line-height: 1.428571em;">UnicodeEncodeError</span>: <span style="line-height: 1.428571em;">'ascii' codec can't encode character u'\xe9' in position 3: ordinal not in range(128)</span>
</pre>
</div>
</div>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
Okay, this is simple enough to solve: Just convert to a byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> and we’re all set:</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<div data-mce-style="background: #eeffcc;" style="background: rgb(238, 255, 204); border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre data-mce-style="overflow-x: auto; overflow-y: hidden; padding: 5px; color: #333333; line-height: 120%; border-top-width: 1px; border-bottom-width: 1px; border-style: solid none; border-top-color: #aacc99; border-bottom-color: #aacc99; background-color: #eeffcc;" style="border-bottom-color: rgb(170, 204, 153); border-style: solid none; border-top-color: rgb(170, 204, 153); border-width: 1px 0px; color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.7999992370605px; overflow-x: auto; overflow-y: hidden; padding: 5px;" xml:space="preserve"><span data-mce-style="color: #c65d09; font-weight: bold;" style="color: #c65d09; font-weight: bold; line-height: 1.428571em;">>>> </span><span style="line-height: 1.428571em;">string</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span data-mce-style="color: #007020;" style="color: #007020; line-height: 1.428571em;">unicode</span><span style="line-height: 1.428571em;">(</span><span data-mce-style="color: #007020;" style="color: #007020; line-height: 1.428571em;">raw_input</span><span style="line-height: 1.428571em;">(),</span> <span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'utf8'</span><span style="line-height: 1.428571em;">)</span>
<span data-mce-style="color: #333333;" style="line-height: 1.428571em;">café</span>
<span data-mce-style="color: #c65d09; font-weight: bold;" style="color: #c65d09; font-weight: bold; line-height: 1.428571em;">>>> </span><span style="line-height: 1.428571em;">string_for_output</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">string</span><span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">encode</span><span style="line-height: 1.428571em;">(</span><span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'utf8'</span><span style="line-height: 1.428571em;">,</span> <span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'replace'</span><span style="line-height: 1.428571em;">)</span>
<span data-mce-style="color: #c65d09; font-weight: bold;" style="color: #c65d09; font-weight: bold; line-height: 1.428571em;">>>> </span><span style="line-height: 1.428571em;">log</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span data-mce-style="color: #007020;" style="color: #007020; line-height: 1.428571em;">open</span><span style="line-height: 1.428571em;">(</span><span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'/var/tmp/debug.log'</span><span style="line-height: 1.428571em;">,</span> <span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'w'</span><span style="line-height: 1.428571em;">)</span>
<span data-mce-style="color: #c65d09; font-weight: bold;" style="color: #c65d09; font-weight: bold; line-height: 1.428571em;">>>> </span><span style="line-height: 1.428571em;">log</span><span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">write</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">string_for_output</span><span style="line-height: 1.428571em;">)</span>
<span data-mce-style="color: #333333;" style="line-height: 1.428571em;">>>></span>
</pre>
</div>
</div>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
So that was simple, right? Well... there’s one gotcha that makes things a bit harder to debug sometimes. When you attempt to write non-<a data-mce-href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;"><em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">ASCII</em></a> <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> strings to a file-like object you get a traceback everytime. But what happens when you use <a data-mce-href="http://docs.python.org/library/functions.html#print" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#print" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">print()</span></tt></a>? The terminal is a file-like object so it should raise an exception right? The answer to that is.... <em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">sometimes</em>:</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<div data-mce-style="background: #eeffcc;" style="background: rgb(238, 255, 204); border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre data-mce-style="overflow-x: auto; overflow-y: hidden; padding: 5px; color: #333333; line-height: 120%; border-top-width: 1px; border-bottom-width: 1px; border-style: solid none; border-top-color: #aacc99; border-bottom-color: #aacc99; background-color: #eeffcc;" style="border-bottom-color: rgb(170, 204, 153); border-style: solid none; border-top-color: rgb(170, 204, 153); border-width: 1px 0px; color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.7999992370605px; overflow-x: auto; overflow-y: hidden; padding: 5px;" xml:space="preserve"><span data-mce-style="color: #333333;" style="line-height: 1.428571em;">$ <span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">python</span></span>
<span data-mce-style="color: #c65d09; font-weight: bold;" style="color: #c65d09; font-weight: bold; line-height: 1.428571em;">>>> </span><span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">print</span> <span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">u'café'</span>
<span data-mce-style="color: #333333;" style="line-height: 1.428571em;">café</span>
</pre>
</div>
</div>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
No exception. Okay, we’re fine then?</div>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
We are until someone does one of the following:</div>
<ul style="border: 0px; line-height: 1.428571em; list-style-position: outside; margin: -0.428571em 0px 0.714285em 2em; padding: 0px;">
<li data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin: 0px; padding: 0px; text-align: justify;"><div data-mce-style="margin-top: 0px; text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px;">
Runs the script in a different locale:</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<div data-mce-style="background: #eeffcc;" style="background: rgb(238, 255, 204); border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre data-mce-style="overflow-x: auto; overflow-y: hidden; padding: 5px; color: #333333; line-height: 120%; border-top-width: 1px; border-bottom-width: 1px; border-style: solid none; border-top-color: #aacc99; border-bottom-color: #aacc99; background-color: #eeffcc;" style="border-bottom-color: rgb(170, 204, 153); border-style: solid none; border-top-color: rgb(170, 204, 153); border-width: 1px 0px; color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.7999992370605px; overflow-x: auto; overflow-y: hidden; padding: 5px;" xml:space="preserve"><span data-mce-style="color: #333333;" style="line-height: 1.428571em;">$ LC_ALL=C <span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">python</span></span>
<span data-mce-style="color: #c65d09; font-weight: bold;" style="color: #c65d09; font-weight: bold; line-height: 1.428571em;">>>> </span><span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># Note: if you're using a good terminal program when running in the C locale</span>
<span data-mce-style="color: #c65d09; font-weight: bold;" style="color: #c65d09; font-weight: bold; line-height: 1.428571em;">>>> </span><span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># The terminal program will prevent you from entering non-ASCII characters</span>
<span data-mce-style="color: #c65d09; font-weight: bold;" style="color: #c65d09; font-weight: bold; line-height: 1.428571em;">>>> </span><span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># <span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">python</span> will still recognize them if you use the codepoint instead:</span>
<span data-mce-style="color: #c65d09; font-weight: bold;" style="color: #c65d09; font-weight: bold; line-height: 1.428571em;">>>> </span><span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">print</span> <span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">u'caf</span><span data-mce-style="color: #4070a0; font-weight: bold;" style="color: #4070a0; font-weight: bold; line-height: 1.428571em;">\xe9</span><span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'</span>
<span data-mce-style="color: #0044dd;" style="color: #0044dd; line-height: 1.428571em;">Traceback (most recent call last):</span>
File <span data-mce-style="color: #007020;" style="color: #007020; line-height: 1.428571em;">"<stdin>"</span>, line <span data-mce-style="color: #208050;" style="color: #208050; line-height: 1.428571em;">1</span>, in <span style="line-height: 1.428571em;"><module></span>
<span data-mce-style="color: #ff0000;" style="color: red; line-height: 1.428571em;">UnicodeEncodeError</span>: <span style="line-height: 1.428571em;">'ascii' codec can't encode character u'\xe9' in position 3: ordinal not in range(128)</span>
<span data-mce-style="color: #0044dd;" style="color: #0044dd; line-height: 1.428571em;">Traceback (most recent call last):</span>
File <span data-mce-style="color: #007020;" style="color: #007020; line-height: 1.428571em;">"<stdin>"</span>, line <span data-mce-style="color: #208050;" style="color: #208050; line-height: 1.428571em;">1</span>, in <span style="line-height: 1.428571em;"><module></span>
<span data-mce-style="color: #ff0000;" style="color: red; line-height: 1.428571em;">UnicodeEncodeError</span>: <span style="line-height: 1.428571em;">'ascii' codec can't encode character u'\xe9' in position 3: ordinal not in range(128)</span>
</pre>
</div>
</div>
</li>
<li data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin: 0px; padding: 0px; text-align: justify;"><div data-mce-style="margin-top: 0px; text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px;">
Redirects output to a file:</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<div data-mce-style="background: #eeffcc;" style="background: rgb(238, 255, 204); border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre data-mce-style="overflow-x: auto; overflow-y: hidden; padding: 5px; color: #333333; line-height: 120%; border-top-width: 1px; border-bottom-width: 1px; border-style: solid none; border-top-color: #aacc99; border-bottom-color: #aacc99; background-color: #eeffcc;" style="border-bottom-color: rgb(170, 204, 153); border-style: solid none; border-top-color: rgb(170, 204, 153); border-width: 1px 0px; color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.7999992370605px; overflow-x: auto; overflow-y: hidden; padding: 5px;" xml:space="preserve"><span data-mce-style="color: #333333;" style="line-height: 1.428571em;">$ cat test.py</span>
<span data-mce-style="color: #333333;" style="line-height: 1.428571em;">#!/usr/bin/<span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">python</span> -tt</span>
<span data-mce-style="color: #333333;" style="line-height: 1.428571em;"># -*- coding: utf-8 -*-</span>
<span data-mce-style="color: #333333;" style="line-height: 1.428571em;">print u'café'</span>
<span data-mce-style="color: #333333;" style="line-height: 1.428571em;">$ ./test.py >t</span>
<span data-mce-style="color: #0044dd;" style="color: #0044dd; line-height: 1.428571em;">Traceback (most recent call last):</span>
File <span data-mce-style="color: #007020;" style="color: #007020; line-height: 1.428571em;">"./test.py"</span>, line <span data-mce-style="color: #208050;" style="color: #208050; line-height: 1.428571em;">4</span>, in <span style="line-height: 1.428571em;"><module></span>
<span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">print</span> <span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">u'café'</span>
<span data-mce-style="color: #ff0000;" style="color: red; line-height: 1.428571em;">UnicodeEncodeError</span>: <span style="line-height: 1.428571em;">'ascii' codec can't encode character u'\xe9' in position 3: ordinal not in range(128)</span>
<span data-mce-style="color: #0044dd;" style="color: #0044dd; line-height: 1.428571em;">Traceback (most recent call last):</span>
File <span data-mce-style="color: #007020;" style="color: #007020; line-height: 1.428571em;">"./test.py"</span>, line <span data-mce-style="color: #208050;" style="color: #208050; line-height: 1.428571em;">4</span>, in <span style="line-height: 1.428571em;"><module></span>
<span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">print</span> <span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">u'café'</span>
<span data-mce-style="color: #ff0000;" style="color: red; line-height: 1.428571em;">UnicodeEncodeError</span>: <span style="line-height: 1.428571em;">'ascii' codec can't encode character u'\xe9' in position 3: ordinal not in range(128)</span>
</pre>
</div>
</div>
</li>
</ul>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
Okay, the locale thing is a pain but understandable: the C locale doesn’t understand any characters outside of <a data-mce-href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;"><em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">ASCII</em></a> so naturally attempting to display those won’t work. Now why does redirecting to a file cause problems? It’s because <a data-mce-href="http://docs.python.org/library/functions.html#print" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#print" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">print()</span></tt></a> in python2 is treated specially. Whereas the other file-like objects in <span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">python</span> always convert to <a data-mce-href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;"><em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">ASCII</em></a> unless you set them up differently, using <a data-mce-href="http://docs.python.org/library/functions.html#print" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#print" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">print()</span></tt></a> to output to the terminal will use the user’s locale to convert before sending the output to the terminal. When <a data-mce-href="http://docs.python.org/library/functions.html#print" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#print" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">print()</span></tt></a> is not outputting to the terminal (being redirected to a file, for instance), <a data-mce-href="http://docs.python.org/library/functions.html#print" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#print" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">print()</span></tt></a> decides that it doesn’t know what locale to use for that file and so it tries to convert to <a data-mce-href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;"><em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">ASCII</em></a> instead.</div>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
So what does this mean for you, as a programmer? Unless you have the luxury of controlling how your users use your code, you should always, always, always convert to a byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> before outputting strings to the terminal or to a file. <span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">Python</span> even provides you with a facility to do just this. If you know that every <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> string you send to a particular file-like object (for instance, <a data-mce-href="http://docs.python.org/library/sys.html#sys.stdout" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/sys.html#sys.stdout" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">stdout</span></tt></a>) should be converted to a particular <span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">encoding</span> you can use a <a data-mce-href="http://docs.python.org/library/codecs.html#codecs.StreamWriter" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/codecs.html#codecs.StreamWriter" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">codecs.StreamWriter</span></tt></a> object to convert from a <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> string into a byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a>. In particular, <a data-mce-href="http://docs.python.org/library/codecs.html#codecs.getwriter" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/codecs.html#codecs.getwriter" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">codecs.getwriter()</span></tt></a> will return a <a data-mce-href="http://docs.python.org/library/codecs.html#codecs.StreamWriter" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/codecs.html#codecs.StreamWriter" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">StreamWriter</span></tt></a> class that will help you to wrap a file-like object for output. Using our <a data-mce-href="http://docs.python.org/library/functions.html#print" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#print" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">print()</span></tt></a> example:</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<div data-mce-style="background: #eeffcc;" style="background: rgb(238, 255, 204); border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre data-mce-style="overflow-x: auto; overflow-y: hidden; padding: 5px; color: #333333; line-height: 120%; border-top-width: 1px; border-bottom-width: 1px; border-style: solid none; border-top-color: #aacc99; border-bottom-color: #aacc99; background-color: #eeffcc;" style="border-bottom-color: rgb(170, 204, 153); border-style: solid none; border-top-color: rgb(170, 204, 153); border-width: 1px 0px; color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.7999992370605px; overflow-x: auto; overflow-y: hidden; padding: 5px;" xml:space="preserve">$ cat test.py
#!/usr/bin/<span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">python</span> -tt
# -*- coding: utf-8 -*-
import codecs
import sys
UTF8Writer = codecs.getwriter('utf8')
sys.stdout = UTF8Writer(sys.stdout)
print u'café'
$ ./test.py >t
$ cat t
café
</pre>
</div>
</div>
</div>
<div style="background-color: white; border: 0px; font-family: Helvetica, Arial, 'Droid Sans', sans-serif; font-size: 14px; line-height: 19.9999942779541px; margin: 0px; padding: 0px;">
<h2 data-mce-style="font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: #20435c; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #cccccc; margin: 20px -20px 10px; padding: 3px 0px 3px 10px; background-color: #f2f2f2; font-size: 160%;" style="background-color: #f2f2f2; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-width: 0px 0px 1px; color: #20435c; font-family: 'Trebuchet MS', sans-serif; font-size: 22.3999996185303px; font-weight: normal; line-height: 1.111em; margin: 20px -20px 10px; padding: 3px 0px 3px 10px;">
Frustrations #4 and #5 – The other shoes<a data-mce-href="https://pythonhosted.org/kitchen/unicode-frustrations.html#frustrations-4-and-5-the-other-shoes" data-mce-style="color: #c60f0f; text-decoration: none; visibility: hidden; font-size: 0.8em; padding: 0px 4px;" href="https://pythonhosted.org/kitchen/unicode-frustrations.html#frustrations-4-and-5-the-other-shoes" style="border: 0px; color: #c60f0f; font-size: 0.8em; line-height: 1.428571em; margin: 0px; padding: 0px 4px; text-decoration: none; visibility: hidden;" title="Permalink to this headline"></a></h2>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
In English, there’s a saying “waiting for the other shoe to drop”. It means that when one event (usually bad) happens, you come to expect another event (usually worse) to come after. In this case we have two other shoes.</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<h3 data-mce-style="font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: #20435c; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #cccccc; margin: 20px -20px 10px; padding: 3px 0px 3px 10px; background-color: #f2f2f2; font-size: 140%;" style="background-color: #f2f2f2; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-width: 0px 0px 1px; color: #20435c; font-family: 'Trebuchet MS', sans-serif; font-size: 19.6000003814697px; font-weight: normal; line-height: 1.428571em; margin: 20px -20px 10px; padding: 3px 0px 3px 10px;">
Frustration #4: Now it doesn’t take byte strings?!<a data-mce-href="https://pythonhosted.org/kitchen/unicode-frustrations.html#frustration-4-now-it-doesn-t-take-byte-strings" data-mce-style="color: #c60f0f; text-decoration: none; visibility: hidden; font-size: 0.8em; padding: 0px 4px;" href="https://pythonhosted.org/kitchen/unicode-frustrations.html#frustration-4-now-it-doesn-t-take-byte-strings" style="border: 0px; color: #c60f0f; font-size: 0.8em; line-height: 1.428571em; margin: 0px; padding: 0px 4px; text-decoration: none; visibility: hidden;" title="Permalink to this headline"></a></h3>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
If you wrap <a data-mce-href="http://docs.python.org/library/sys.html#sys.stdout" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/sys.html#sys.stdout" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">sys.stdout</span></tt></a> using <a data-mce-href="http://docs.python.org/library/codecs.html#codecs.getwriter" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/codecs.html#codecs.getwriter" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">codecs.getwriter()</span></tt></a> and think you are now safe to print any variable without checking its type I am afraid I must inform you that you’re not paying enough attention to <a data-mce-href="https://pythonhosted.org/kitchen/glossary.html#term-murphy-s-law" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/glossary.html#term-murphy-s-law" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;"><em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">Murphy’s Law</em></a>. The <a data-mce-href="http://docs.python.org/library/codecs.html#codecs.StreamWriter" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/codecs.html#codecs.StreamWriter" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">StreamWriter</span></tt></a> that <a data-mce-href="http://docs.python.org/library/codecs.html#codecs.getwriter" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/codecs.html#codecs.getwriter" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">codecs.getwriter()</span></tt></a> provides will take <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> strings and transform them into byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> before they get to <a data-mce-href="http://docs.python.org/library/sys.html#sys.stdout" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/sys.html#sys.stdout" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">sys.stdout</span></tt></a>. The problem is if you give it something that’s already a byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> it tries to transform that as well. To do that it tries to turn the byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> you give it into <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> and then transform that back into a byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a>... and since it uses the <a data-mce-href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;"><em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">ASCII</em></a> codec to perform those conversions, chances are that it’ll blow up when making them:</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<div data-mce-style="background: #eeffcc;" style="background: rgb(238, 255, 204); border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre data-mce-style="overflow-x: auto; overflow-y: hidden; padding: 5px; color: #333333; line-height: 120%; border-top-width: 1px; border-bottom-width: 1px; border-style: solid none; border-top-color: #aacc99; border-bottom-color: #aacc99; background-color: #eeffcc;" style="border-bottom-color: rgb(170, 204, 153); border-style: solid none; border-top-color: rgb(170, 204, 153); border-width: 1px 0px; color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.7999992370605px; overflow-x: auto; overflow-y: hidden; padding: 5px;" xml:space="preserve"><span data-mce-style="color: #c65d09; font-weight: bold;" style="color: #c65d09; font-weight: bold; line-height: 1.428571em;">>>> </span><span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">import</span> <span data-mce-style="color: #0e84b5; font-weight: bold;" style="color: #0e84b5; font-weight: bold; line-height: 1.428571em;">codecs</span>
<span data-mce-style="color: #c65d09; font-weight: bold;" style="color: #c65d09; font-weight: bold; line-height: 1.428571em;">>>> </span><span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">import</span> <span data-mce-style="color: #0e84b5; font-weight: bold;" style="color: #0e84b5; font-weight: bold; line-height: 1.428571em;">sys</span>
<span data-mce-style="color: #c65d09; font-weight: bold;" style="color: #c65d09; font-weight: bold; line-height: 1.428571em;">>>> </span><span style="line-height: 1.428571em;">UTF8Writer</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">codecs</span><span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">getwriter</span><span style="line-height: 1.428571em;">(</span><span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'utf8'</span><span style="line-height: 1.428571em;">)</span>
<span data-mce-style="color: #c65d09; font-weight: bold;" style="color: #c65d09; font-weight: bold; line-height: 1.428571em;">>>> </span><span style="line-height: 1.428571em;">sys</span><span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">stdout</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">UTF8Writer</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">sys</span><span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">stdout</span><span style="line-height: 1.428571em;">)</span>
<span data-mce-style="color: #c65d09; font-weight: bold;" style="color: #c65d09; font-weight: bold; line-height: 1.428571em;">>>> </span><span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">print</span> <span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'café'</span>
<span data-mce-style="color: #0044dd;" style="color: #0044dd; line-height: 1.428571em;">Traceback (most recent call last):</span>
File <span data-mce-style="color: #007020;" style="color: #007020; line-height: 1.428571em;">"<stdin>"</span>, line <span data-mce-style="color: #208050;" style="color: #208050; line-height: 1.428571em;">1</span>, in <span style="line-height: 1.428571em;"><module></span>
File <span data-mce-style="color: #007020;" style="color: #007020; line-height: 1.428571em;">"/usr/lib64/python2.6/codecs.py"</span>, line <span data-mce-style="color: #208050;" style="color: #208050; line-height: 1.428571em;">351</span>, in <span style="line-height: 1.428571em;">write</span>
<span style="line-height: 1.428571em;">data</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">consumed</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span data-mce-style="color: #007020;" style="color: #007020; line-height: 1.428571em;">self</span><span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">encode</span><span style="line-height: 1.428571em;">(</span><span data-mce-style="color: #007020;" style="color: #007020; line-height: 1.428571em;">object</span><span style="line-height: 1.428571em;">,</span> <span data-mce-style="color: #007020;" style="color: #007020; line-height: 1.428571em;">self</span><span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">errors</span><span style="line-height: 1.428571em;">)</span>
<span data-mce-style="color: #ff0000;" style="color: red; line-height: 1.428571em;">UnicodeDecodeError</span>: <span style="line-height: 1.428571em;">'ascii' codec can't decode byte 0xc3 in position 3: ordinal not in range(128)</span>
<span data-mce-style="color: #0044dd;" style="color: #0044dd; line-height: 1.428571em;">Traceback (most recent call last):</span>
File <span data-mce-style="color: #007020;" style="color: #007020; line-height: 1.428571em;">"<stdin>"</span>, line <span data-mce-style="color: #208050;" style="color: #208050; line-height: 1.428571em;">1</span>, in <span style="line-height: 1.428571em;"><module></span>
File <span data-mce-style="color: #007020;" style="color: #007020; line-height: 1.428571em;">"/usr/lib64/python2.6/codecs.py"</span>, line <span data-mce-style="color: #208050;" style="color: #208050; line-height: 1.428571em;">351</span>, in <span style="line-height: 1.428571em;">write</span>
<span style="line-height: 1.428571em;">data</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">consumed</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span data-mce-style="color: #007020;" style="color: #007020; line-height: 1.428571em;">self</span><span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">encode</span><span style="line-height: 1.428571em;">(</span><span data-mce-style="color: #007020;" style="color: #007020; line-height: 1.428571em;">object</span><span style="line-height: 1.428571em;">,</span> <span data-mce-style="color: #007020;" style="color: #007020; line-height: 1.428571em;">self</span><span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">errors</span><span style="line-height: 1.428571em;">)</span>
<span data-mce-style="color: #ff0000;" style="color: red; line-height: 1.428571em;">UnicodeDecodeError</span>: <span style="line-height: 1.428571em;">'ascii' codec can't decode byte 0xc3 in position 3: ordinal not in range(128)</span>
</pre>
</div>
</div>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
To work around this, kitchen provides an alternate version of <a data-mce-href="http://docs.python.org/library/codecs.html#codecs.getwriter" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/codecs.html#codecs.getwriter" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">codecs.getwriter()</span></tt></a> that can deal with both byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> and <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> strings. Use <a data-mce-href="https://pythonhosted.org/kitchen/api-text-converters.html#kitchen.text.converters.getwriter" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/api-text-converters.html#kitchen.text.converters.getwriter" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="kitchen.text.converters.getwriter"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">kitchen.text.converters.getwriter()</span></tt></a> in place of the <a data-mce-href="http://docs.python.org/library/codecs.html#module-codecs" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/codecs.html#module-codecs" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">codecs</span></tt></a> version like this:</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<div data-mce-style="background: #eeffcc;" style="background: rgb(238, 255, 204); border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre data-mce-style="overflow-x: auto; overflow-y: hidden; padding: 5px; color: #333333; line-height: 120%; border-top-width: 1px; border-bottom-width: 1px; border-style: solid none; border-top-color: #aacc99; border-bottom-color: #aacc99; background-color: #eeffcc;" style="border-bottom-color: rgb(170, 204, 153); border-style: solid none; border-top-color: rgb(170, 204, 153); border-width: 1px 0px; color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.7999992370605px; overflow-x: auto; overflow-y: hidden; padding: 5px;" xml:space="preserve"><span data-mce-style="color: #c65d09; font-weight: bold;" style="color: #c65d09; font-weight: bold; line-height: 1.428571em;">>>> </span><span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">import</span> <span data-mce-style="color: #0e84b5; font-weight: bold;" style="color: #0e84b5; font-weight: bold; line-height: 1.428571em;">sys</span>
<span data-mce-style="color: #c65d09; font-weight: bold;" style="color: #c65d09; font-weight: bold; line-height: 1.428571em;">>>> </span><span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">from</span> <span data-mce-style="color: #0e84b5; font-weight: bold;" style="color: #0e84b5; font-weight: bold; line-height: 1.428571em;">kitchen.text.converters</span> <span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">import</span> <span style="line-height: 1.428571em;">getwriter</span>
<span data-mce-style="color: #c65d09; font-weight: bold;" style="color: #c65d09; font-weight: bold; line-height: 1.428571em;">>>> </span><span style="line-height: 1.428571em;">UTF8Writer</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">getwriter</span><span style="line-height: 1.428571em;">(</span><span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'utf8'</span><span style="line-height: 1.428571em;">)</span>
<span data-mce-style="color: #c65d09; font-weight: bold;" style="color: #c65d09; font-weight: bold; line-height: 1.428571em;">>>> </span><span style="line-height: 1.428571em;">sys</span><span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">stdout</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">UTF8Writer</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">sys</span><span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">stdout</span><span style="line-height: 1.428571em;">)</span>
<span data-mce-style="color: #c65d09; font-weight: bold;" style="color: #c65d09; font-weight: bold; line-height: 1.428571em;">>>> </span><span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">print</span> <span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">u'café'</span>
<span data-mce-style="color: #333333;" style="line-height: 1.428571em;">café</span>
<span data-mce-style="color: #c65d09; font-weight: bold;" style="color: #c65d09; font-weight: bold; line-height: 1.428571em;">>>> </span><span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">print</span> <span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'café'</span>
<span data-mce-style="color: #333333;" style="line-height: 1.428571em;">café</span>
</pre>
</div>
</div>
</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<h3 data-mce-style="font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: #20435c; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #cccccc; margin: 20px -20px 10px; padding: 3px 0px 3px 10px; background-color: #f2f2f2; font-size: 140%;" style="background-color: #f2f2f2; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-width: 0px 0px 1px; color: #20435c; font-family: 'Trebuchet MS', sans-serif; font-size: 19.6000003814697px; font-weight: normal; line-height: 1.428571em; margin: 20px -20px 10px; padding: 3px 0px 3px 10px;">
Frustration #5: Exceptions<a data-mce-href="https://pythonhosted.org/kitchen/unicode-frustrations.html#frustration-5-exceptions" data-mce-style="color: #c60f0f; text-decoration: none; visibility: hidden; font-size: 0.8em; padding: 0px 4px;" href="https://pythonhosted.org/kitchen/unicode-frustrations.html#frustration-5-exceptions" style="border: 0px; color: #c60f0f; font-size: 0.8em; line-height: 1.428571em; margin: 0px; padding: 0px 4px; text-decoration: none; visibility: hidden;" title="Permalink to this headline"></a></h3>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
Okay, so we’ve gotten ourselves this far. We convert everything to <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> strings. We’re aware that we need to convert back into byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> before we write to the terminal. We’ve worked around the inability of the standard <a data-mce-href="http://docs.python.org/library/codecs.html#codecs.getwriter" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/codecs.html#codecs.getwriter" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">getwriter()</span></tt></a> to deal with both byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> and <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> strings. Are we all set? Well, there’s at least one more gotcha: raising exceptions with a <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> message. Take a look:</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<div data-mce-style="background: #eeffcc;" style="background: rgb(238, 255, 204); border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre data-mce-style="overflow-x: auto; overflow-y: hidden; padding: 5px; color: #333333; line-height: 120%; border-top-width: 1px; border-bottom-width: 1px; border-style: solid none; border-top-color: #aacc99; border-bottom-color: #aacc99; background-color: #eeffcc;" style="border-bottom-color: rgb(170, 204, 153); border-style: solid none; border-top-color: rgb(170, 204, 153); border-width: 1px 0px; color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.7999992370605px; overflow-x: auto; overflow-y: hidden; padding: 5px;" xml:space="preserve">>>> class MyException(Exception):
>>> pass
>>>
>>> raise MyException(u'Cannot do this')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
__main__.MyException: Cannot do this
>>> raise MyException(u'Cannot do this while at a café')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
__main__.MyException:
>>>
</pre>
</div>
</div>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
No, I didn’t truncate that last line; raising exceptions really cannot handle non-<a data-mce-href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;"><em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">ASCII</em></a> characters in a <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> string and will output an exception without the message if the message contains them. What happens if we try to use the handy dandy <a data-mce-href="https://pythonhosted.org/kitchen/api-text-converters.html#kitchen.text.converters.getwriter" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/api-text-converters.html#kitchen.text.converters.getwriter" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="kitchen.text.converters.getwriter"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">getwriter()</span></tt></a> trick to work around this?</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<div data-mce-style="background: #eeffcc;" style="background: rgb(238, 255, 204); border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre data-mce-style="overflow-x: auto; overflow-y: hidden; padding: 5px; color: #333333; line-height: 120%; border-top-width: 1px; border-bottom-width: 1px; border-style: solid none; border-top-color: #aacc99; border-bottom-color: #aacc99; background-color: #eeffcc;" style="border-bottom-color: rgb(170, 204, 153); border-style: solid none; border-top-color: rgb(170, 204, 153); border-width: 1px 0px; color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.7999992370605px; overflow-x: auto; overflow-y: hidden; padding: 5px;" xml:space="preserve">>>> import sys
>>> from kitchen.text.converters import getwriter
>>> sys.stderr = getwriter('utf8')(sys.stderr)
>>> raise MyException(u'Cannot do this')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
__main__.MyException: Cannot do this
>>> raise MyException(u'Cannot do this while at a café')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
__main__.MyException>>>
</pre>
</div>
</div>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
Not only did this also fail, it even swallowed the trailing newline that’s normally there.... So how to make this work? Transform from <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> strings to byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> manually before outputting:</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<div data-mce-style="background: #eeffcc;" style="background: rgb(238, 255, 204); border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre data-mce-style="overflow-x: auto; overflow-y: hidden; padding: 5px; color: #333333; line-height: 120%; border-top-width: 1px; border-bottom-width: 1px; border-style: solid none; border-top-color: #aacc99; border-bottom-color: #aacc99; background-color: #eeffcc;" style="border-bottom-color: rgb(170, 204, 153); border-style: solid none; border-top-color: rgb(170, 204, 153); border-width: 1px 0px; color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.7999992370605px; overflow-x: auto; overflow-y: hidden; padding: 5px;" xml:space="preserve"><span data-mce-style="color: #c65d09; font-weight: bold;" style="color: #c65d09; font-weight: bold; line-height: 1.428571em;">>>> </span><span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">from</span> <span data-mce-style="color: #0e84b5; font-weight: bold;" style="color: #0e84b5; font-weight: bold; line-height: 1.428571em;">kitchen.text.converters</span> <span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">import</span> <span style="line-height: 1.428571em;">to_bytes</span>
<span data-mce-style="color: #c65d09; font-weight: bold;" style="color: #c65d09; font-weight: bold; line-height: 1.428571em;">>>> </span><span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">raise</span> <span style="line-height: 1.428571em;">MyException</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">to_bytes</span><span style="line-height: 1.428571em;">(</span><span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">u'Cannot do this while at a café'</span><span style="line-height: 1.428571em;">))</span>
<span data-mce-style="color: #0044dd;" style="color: #0044dd; line-height: 1.428571em;">Traceback (most recent call last):</span>
File <span data-mce-style="color: #007020;" style="color: #007020; line-height: 1.428571em;">"<stdin>"</span>, line <span data-mce-style="color: #208050;" style="color: #208050; line-height: 1.428571em;">1</span>, in <span style="line-height: 1.428571em;"><module></span>
<span data-mce-style="color: #ff0000;" style="color: red; line-height: 1.428571em;">__main__.MyException</span>: <span style="line-height: 1.428571em;">Cannot do this while at a café</span>
<span data-mce-style="color: #333333;" style="line-height: 1.428571em;">>>></span>
<span data-mce-style="color: #0044dd;" style="color: #0044dd; line-height: 1.428571em;">Traceback (most recent call last):</span>
File <span data-mce-style="color: #007020;" style="color: #007020; line-height: 1.428571em;">"<stdin>"</span>, line <span data-mce-style="color: #208050;" style="color: #208050; line-height: 1.428571em;">1</span>, in <span style="line-height: 1.428571em;"><module></span>
<span data-mce-style="color: #ff0000;" style="color: red; line-height: 1.428571em;">__main__.MyException</span>: <span style="line-height: 1.428571em;">Cannot do this while at a café</span>
</pre>
</div>
</div>
<div data-mce-style="margin-top: 10px; margin-bottom: 10px; padding: 7px; border: 1px solid #ff6666; background-color: #ffe4e4;" style="background-color: #ffe4e4; border: 1px solid rgb(255, 102, 102); line-height: 1.428571em; margin: 10px 0px; padding: 7px;">
<div data-mce-style="margin: 0px 10px 5px 0px; font-weight: bold; display: inline; text-align: justify; line-height: 130%; margin-bottom: 5px;" style="border: 0px; display: inline; font-weight: bold; line-height: 18.2000007629395px; margin-bottom: 5px; margin-right: 10px; padding: 0px; text-align: justify;">
Warning<span data-mce-style="font-weight: bold; text-align: justify; line-height: 130%;" style="line-height: 18.2000007629395px;">:</span></div>
<div data-mce-style="text-align: justify; line-height: 130%; margin-bottom: 5px; display: inline;" style="border: 0px; display: inline; line-height: 18.2000007629395px; margin-bottom: 5px; padding: 0px; text-align: justify;">
If you use <a data-mce-href="http://docs.python.org/library/codecs.html#codecs.getwriter" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/codecs.html#codecs.getwriter" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; font-weight: bold; background: #efc2c2;" style="background: rgb(239, 194, 194); font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">codecs.getwriter()</span></tt></a> on <a data-mce-href="http://docs.python.org/library/sys.html#sys.stderr" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/sys.html#sys.stderr" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; font-weight: bold; background: #efc2c2;" style="background: rgb(239, 194, 194); font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">sys.stderr</span></tt></a>, you’ll find that raising an exception with a byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; font-weight: bold; background: #efc2c2;" style="background: rgb(239, 194, 194); font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> is broken by the default <a data-mce-href="http://docs.python.org/library/codecs.html#codecs.StreamWriter" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/codecs.html#codecs.StreamWriter" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; font-weight: bold; background: #efc2c2;" style="background: rgb(239, 194, 194); font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">StreamWriter</span></tt></a> as well. Don’t do that or you’ll have no way to output non-<a data-mce-href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;"><em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">ASCII</em></a> characters. If you want to use a <a data-mce-href="http://docs.python.org/library/codecs.html#codecs.StreamWriter" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/codecs.html#codecs.StreamWriter" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; font-weight: bold; background: #efc2c2;" style="background: rgb(239, 194, 194); font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">StreamWriter</span></tt></a> to encode other things on stderr while still having working exceptions, use <a data-mce-href="https://pythonhosted.org/kitchen/api-text-converters.html#kitchen.text.converters.getwriter" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/api-text-converters.html#kitchen.text.converters.getwriter" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="kitchen.text.converters.getwriter"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; font-weight: bold; background: #efc2c2;" style="background: rgb(239, 194, 194); font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">kitchen.text.converters.getwriter()</span></tt></a>.</div>
</div>
</div>
</div>
<div style="background-color: white; border: 0px; font-family: Helvetica, Arial, 'Droid Sans', sans-serif; font-size: 14px; line-height: 19.9999942779541px; margin: 0px; padding: 0px;">
<h2 data-mce-style="font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: #20435c; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #cccccc; margin: 20px -20px 10px; padding: 3px 0px 3px 10px; background-color: #f2f2f2; font-size: 160%;" style="background-color: #f2f2f2; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-width: 0px 0px 1px; color: #20435c; font-family: 'Trebuchet MS', sans-serif; font-size: 22.3999996185303px; font-weight: normal; line-height: 1.111em; margin: 20px -20px 10px; padding: 3px 0px 3px 10px;">
Frustration #6: Inconsistent APIs Part deux<a data-mce-href="https://pythonhosted.org/kitchen/unicode-frustrations.html#frustration-6-inconsistent-apis-part-deux" data-mce-style="color: #c60f0f; text-decoration: none; visibility: hidden; font-size: 0.8em; padding: 0px 4px;" href="https://pythonhosted.org/kitchen/unicode-frustrations.html#frustration-6-inconsistent-apis-part-deux" style="border: 0px; color: #c60f0f; font-size: 0.8em; line-height: 1.428571em; margin: 0px; padding: 0px 4px; text-decoration: none; visibility: hidden;" title="Permalink to this headline"></a></h2>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
Sometimes you do everything right in your code but other people’s code fails you. With unicode issues this happens more often than we want. A glaring example of this is when you get values back from a function that aren’t consistently <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> string or byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a>.</div>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
An example from the <span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;"></span><a data-mce-href="http://docs.python.org/library" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;"><span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">python</span> standard library</a> is <a data-mce-href="http://docs.python.org/library/gettext.html#module-gettext" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/gettext.html#module-gettext" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">gettext</span></tt></a>. The <a data-mce-href="http://docs.python.org/library/gettext.html#module-gettext" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/gettext.html#module-gettext" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">gettext</span></tt></a> functions are used to help translate messages that you display to users in the users’ native languages. Since most languages contain letters outside of the <a data-mce-href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;"><em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">ASCII</em></a> range, the values that are returned contain unicode characters. <a data-mce-href="http://docs.python.org/library/gettext.html#module-gettext" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/gettext.html#module-gettext" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">gettext</span></tt></a> provides you with <a data-mce-href="http://docs.python.org/library/gettext.html#gettext.GNUTranslations.ugettext" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/gettext.html#gettext.GNUTranslations.ugettext" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">ugettext()</span></tt></a> and <a data-mce-href="http://docs.python.org/library/gettext.html#gettext.GNUTranslations.ungettext" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/gettext.html#gettext.GNUTranslations.ungettext" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">ungettext()</span></tt></a> to return these translations as <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> strings and <a data-mce-href="http://docs.python.org/library/gettext.html#gettext.GNUTranslations.gettext" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/gettext.html#gettext.GNUTranslations.gettext" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">gettext()</span></tt></a>, <a data-mce-href="http://docs.python.org/library/gettext.html#gettext.GNUTranslations.ngettext" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/gettext.html#gettext.GNUTranslations.ngettext" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">ngettext()</span></tt></a>, <a data-mce-href="http://docs.python.org/library/gettext.html#gettext.GNUTranslations.lgettext" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/gettext.html#gettext.GNUTranslations.lgettext" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">lgettext()</span></tt></a>, and <a data-mce-href="http://docs.python.org/library/gettext.html#gettext.GNUTranslations.lngettext" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/gettext.html#gettext.GNUTranslations.lngettext" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">lngettext()</span></tt></a> to return them as encoded byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a>. Unfortunately, even though they’re documented to return only one type of string or the other, the implementation has corner cases where the wrong type can be returned.</div>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
This means that even if you separate your <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> string and byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> correctly before you pass your strings to a <a data-mce-href="http://docs.python.org/library/gettext.html#module-gettext" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/gettext.html#module-gettext" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">gettext</span></tt></a> function, afterwards, you might have to check that you have the right sort of string type again.</div>
<div data-mce-style="margin-top: 10px; margin-bottom: 10px; padding: 7px; border: 1px solid #cccccc; background-color: #eeeeee;" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); line-height: 1.428571em; margin: 10px 0px; padding: 7px;">
<div data-mce-style="margin: 0px 10px 5px 0px; font-weight: bold; display: inline; text-align: justify; line-height: 130%; margin-bottom: 5px;" style="border: 0px; display: inline; font-weight: bold; line-height: 18.2000007629395px; margin-bottom: 5px; margin-right: 10px; padding: 0px; text-align: justify;">
Note<span data-mce-style="font-weight: bold; text-align: justify; line-height: 130%;" style="line-height: 18.2000007629395px;">:</span></div>
<div data-mce-style="text-align: justify; line-height: 130%; margin-bottom: 5px; display: inline;" style="border: 0px; display: inline; line-height: 18.2000007629395px; margin-bottom: 5px; padding: 0px; text-align: justify;">
<a data-mce-href="https://pythonhosted.org/kitchen/api-i18n.html#module-kitchen.i18n" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/api-i18n.html#module-kitchen.i18n" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="kitchen.i18n"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; font-weight: bold; background: #d6d6d6;" style="background: rgb(214, 214, 214); font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">kitchen.i18n</span></tt></a> provides alternate gettext translation objects that return only byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; font-weight: bold; background: #d6d6d6;" style="background: rgb(214, 214, 214); font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> or only <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; font-weight: bold; background: #d6d6d6;" style="background: rgb(214, 214, 214); font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> string.</div>
</div>
</div>
<div style="background-color: white; border: 0px; font-family: Helvetica, Arial, 'Droid Sans', sans-serif; font-size: 14px; line-height: 19.9999942779541px; margin: 0px; padding: 0px;">
<h2 data-mce-style="font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: #20435c; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #cccccc; margin: 20px -20px 10px; padding: 3px 0px 3px 10px; background-color: #f2f2f2; font-size: 160%;" style="background-color: #f2f2f2; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-width: 0px 0px 1px; color: #20435c; font-family: 'Trebuchet MS', sans-serif; font-size: 22.3999996185303px; font-weight: normal; line-height: 1.111em; margin: 20px -20px 10px; padding: 3px 0px 3px 10px;">
A few solutions<a data-mce-href="https://pythonhosted.org/kitchen/unicode-frustrations.html#a-few-solutions" data-mce-style="color: #c60f0f; text-decoration: none; visibility: hidden; font-size: 0.8em; padding: 0px 4px;" href="https://pythonhosted.org/kitchen/unicode-frustrations.html#a-few-solutions" style="border: 0px; color: #c60f0f; font-size: 0.8em; line-height: 1.428571em; margin: 0px; padding: 0px 4px; text-decoration: none; visibility: hidden;" title="Permalink to this headline"></a></h2>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
Now that we’ve identified the issues, can we define a comprehensive strategy for dealing with them?</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<h3 data-mce-style="font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: #20435c; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #cccccc; margin: 20px -20px 10px; padding: 3px 0px 3px 10px; background-color: #f2f2f2; font-size: 140%;" style="background-color: #f2f2f2; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-width: 0px 0px 1px; color: #20435c; font-family: 'Trebuchet MS', sans-serif; font-size: 19.6000003814697px; font-weight: normal; line-height: 1.428571em; margin: 20px -20px 10px; padding: 3px 0px 3px 10px;">
Convert text at the border<a data-mce-href="https://pythonhosted.org/kitchen/unicode-frustrations.html#convert-text-at-the-border" data-mce-style="color: #c60f0f; text-decoration: none; visibility: hidden; font-size: 0.8em; padding: 0px 4px;" href="https://pythonhosted.org/kitchen/unicode-frustrations.html#convert-text-at-the-border" style="border: 0px; color: #c60f0f; font-size: 0.8em; line-height: 1.428571em; margin: 0px; padding: 0px 4px; text-decoration: none; visibility: hidden;" title="Permalink to this headline"></a></h3>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
If you get some piece of text from a library, read from a file, etc, turn it into a <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> string immediately. Since <span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">python</span> is moving in the direction of <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> strings everywhere it’s going to be easier to work with <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> strings within your code.</div>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
If your code is heavily involved with using things that are bytes, you can do the opposite and convert all text into byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> at the border and only convert to <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> when you need it for passing to another library or performing string operations on it.</div>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
In either case, the important thing is to pick a default type for strings and stick with it throughout your code. When you mix the types it becomes much easier to operate on a string with a function that can only use the other type by mistake.</div>
<div data-mce-style="margin-top: 10px; margin-bottom: 10px; padding: 7px; border: 1px solid #cccccc; background-color: #eeeeee;" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); line-height: 1.428571em; margin: 10px 0px; padding: 7px;">
<div data-mce-style="margin: 0px 10px 5px 0px; font-weight: bold; display: inline; text-align: justify; line-height: 130%; margin-bottom: 5px;" style="border: 0px; display: inline; font-weight: bold; line-height: 18.2000007629395px; margin-bottom: 5px; margin-right: 10px; padding: 0px; text-align: justify;">
Note<span data-mce-style="font-weight: bold; text-align: justify; line-height: 130%;" style="line-height: 18.2000007629395px;">:</span></div>
<div data-mce-style="text-align: justify; line-height: 130%; margin-bottom: 5px; display: inline;" style="border: 0px; display: inline; line-height: 18.2000007629395px; margin-bottom: 5px; padding: 0px; text-align: justify;">
In python3, the abstract unicode type becomes much more prominent. The type named <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background: #d6d6d6;" style="background: rgb(214, 214, 214); font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt> is the equivalent of python2’s <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; font-weight: bold; background: #d6d6d6;" style="background: rgb(214, 214, 214); font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> and python3’s <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background: #d6d6d6;" style="background: rgb(214, 214, 214); font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">bytes</span></tt> type replaces python2’s <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; font-weight: bold; background: #d6d6d6;" style="background: rgb(214, 214, 214); font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a>. Most APIs deal in the unicode type of string with just some pieces that are low level dealing with bytes. The implicit conversions between bytes and unicode is removed and whenever you want to make the conversion you need to do so explicitly.</div>
</div>
</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<h3 data-mce-style="font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: #20435c; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #cccccc; margin: 20px -20px 10px; padding: 3px 0px 3px 10px; background-color: #f2f2f2; font-size: 140%;" style="background-color: #f2f2f2; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-width: 0px 0px 1px; color: #20435c; font-family: 'Trebuchet MS', sans-serif; font-size: 19.6000003814697px; font-weight: normal; line-height: 1.428571em; margin: 20px -20px 10px; padding: 3px 0px 3px 10px;">
When the data needs to be treated as bytes (or unicode) use a naming convention<a data-mce-href="https://pythonhosted.org/kitchen/unicode-frustrations.html#when-the-data-needs-to-be-treated-as-bytes-or-unicode-use-a-naming-convention" data-mce-style="color: #c60f0f; text-decoration: none; visibility: hidden; font-size: 0.8em; padding: 0px 4px;" href="https://pythonhosted.org/kitchen/unicode-frustrations.html#when-the-data-needs-to-be-treated-as-bytes-or-unicode-use-a-naming-convention" style="border: 0px; color: #c60f0f; font-size: 0.8em; line-height: 1.428571em; margin: 0px; padding: 0px 4px; text-decoration: none; visibility: hidden;" title="Permalink to this headline"></a></h3>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
Sometimes you’re converting nearly all of your data to <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> strings but you have one or two values where you have to keep byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> around. This is often the case when you need to use the value verbatim with some external resource. For instance, filenames or key values in a database. When you do this, use a naming convention for the data you’re working with so you (and others reading your code later) don’t get confused about what’s being stored in the value.</div>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
If you need both a textual string to present to the user and a byte value for an exact match, consider keeping both versions around. You can either use two variables for this or a <a data-mce-href="http://docs.python.org/library/stdtypes.html#dict" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/stdtypes.html#dict" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">dict</span></tt></a> whose key is the byte value.</div>
<div data-mce-style="margin-top: 10px; margin-bottom: 10px; padding: 7px; border: 1px solid #cccccc; background-color: #eeeeee;" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); line-height: 1.428571em; margin: 10px 0px; padding: 7px;">
<div data-mce-style="margin: 0px 10px 5px 0px; font-weight: bold; display: inline; text-align: justify; line-height: 130%; margin-bottom: 5px;" style="border: 0px; display: inline; font-weight: bold; line-height: 18.2000007629395px; margin-bottom: 5px; margin-right: 10px; padding: 0px; text-align: justify;">
Note<span data-mce-style="font-weight: bold; text-align: justify; line-height: 130%;" style="line-height: 18.2000007629395px;">:</span></div>
<div data-mce-style="text-align: justify; line-height: 130%; margin-bottom: 5px; display: inline;" style="border: 0px; display: inline; line-height: 18.2000007629395px; margin-bottom: 5px; padding: 0px; text-align: justify;">
You can use the naming convention used in kitchen as a guide for implementing your own naming convention. It prefixes byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; font-weight: bold; background: #d6d6d6;" style="background: rgb(214, 214, 214); font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> variables of unknown <span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">encoding</span> with <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background: #d6d6d6;" style="background: rgb(214, 214, 214); font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">b_</span></tt> and byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; font-weight: bold; background: #d6d6d6;" style="background: rgb(214, 214, 214); font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> of known <span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">encoding</span> with the <span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">encoding</span> name like: <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background: #d6d6d6;" style="background: rgb(214, 214, 214); font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">utf8_</span></tt>. If the default was to handle <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; font-weight: bold; background: #d6d6d6;" style="background: rgb(214, 214, 214); font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> and only keep a few <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; font-weight: bold; background: #d6d6d6;" style="background: rgb(214, 214, 214); font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> values, those variables would be prefixed with <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background: #d6d6d6;" style="background: rgb(214, 214, 214); font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">u_</span></tt>.</div>
</div>
</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<h3 data-mce-style="font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: #20435c; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #cccccc; margin: 20px -20px 10px; padding: 3px 0px 3px 10px; background-color: #f2f2f2; font-size: 140%;" style="background-color: #f2f2f2; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-width: 0px 0px 1px; color: #20435c; font-family: 'Trebuchet MS', sans-serif; font-size: 19.6000003814697px; font-weight: normal; line-height: 1.428571em; margin: 20px -20px 10px; padding: 3px 0px 3px 10px;">
When outputting data, convert back into bytes<a data-mce-href="https://pythonhosted.org/kitchen/unicode-frustrations.html#when-outputting-data-convert-back-into-bytes" data-mce-style="color: #c60f0f; text-decoration: none; visibility: hidden; font-size: 0.8em; padding: 0px 4px;" href="https://pythonhosted.org/kitchen/unicode-frustrations.html#when-outputting-data-convert-back-into-bytes" style="border: 0px; color: #c60f0f; font-size: 0.8em; line-height: 1.428571em; margin: 0px; padding: 0px 4px; text-decoration: none; visibility: hidden;" title="Permalink to this headline"></a></h3>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
When you go to send your data back outside of your program (to the filesystem, over the network, displaying to the user, etc) turn the data back into a byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a>. How you do this will depend on the expected output format of the data. For displaying to the user, you can use the user’s default <span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">encoding</span> using <a data-mce-href="http://docs.python.org/library/locale.html#locale.getpreferredencoding" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/locale.html#locale.getpreferredencoding" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">locale.getpreferredencoding()</span></tt></a>. For entering into a file, you’re best bet is to pick a single <span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">encoding</span> and stick with it.</div>
<div data-mce-style="margin-top: 10px; margin-bottom: 10px; padding: 7px; border: 1px solid #ff6666; background-color: #ffe4e4;" style="background-color: #ffe4e4; border: 1px solid rgb(255, 102, 102); line-height: 1.428571em; margin: 10px 0px; padding: 7px;">
<div data-mce-style="margin: 0px 10px 5px 0px; font-weight: bold; display: inline; text-align: justify; line-height: 130%; margin-bottom: 5px;" style="border: 0px; display: inline; font-weight: bold; line-height: 18.2000007629395px; margin-bottom: 5px; margin-right: 10px; padding: 0px; text-align: justify;">
Warning<span data-mce-style="font-weight: bold; text-align: justify; line-height: 130%;" style="line-height: 18.2000007629395px;">:</span></div>
<div data-mce-style="text-align: justify; line-height: 130%; margin-bottom: 5px; display: inline;" style="border: 0px; display: inline; line-height: 18.2000007629395px; margin-bottom: 5px; padding: 0px; text-align: justify;">
When using the <span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">en</span><span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">coding</span> that the user has set (for instance, using <a data-mce-href="http://docs.python.org/library/locale.html#locale.getpreferredencoding" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/locale.html#locale.getpreferredencoding" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; font-weight: bold; background: #efc2c2;" style="background: rgb(239, 194, 194); font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">locale.getpreferredencoding()</span></tt></a>, remember that they may have their <span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">encoding</span> set to something that can’t display every single unicode character. That means when you convert from <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; font-weight: bold; background: #efc2c2;" style="background: rgb(239, 194, 194); font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> to a byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; font-weight: bold; background: #efc2c2;" style="background: rgb(239, 194, 194); font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> you need to decide what should happen if the byte value is not valid in the user’s <span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">encoding</span>. For purposes of displaying messages to the user, it’s usually okay to use the <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background: #efc2c2;" style="background: rgb(239, 194, 194); font-size: 0.95em; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">replace</span></tt> <span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">encoding</span> error handler to replace the invalid characters with a question mark or other symbol meaning the character couldn’t be displayed.</div>
</div>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
You can use <a data-mce-href="https://pythonhosted.org/kitchen/api-text-converters.html#kitchen.text.converters.getwriter" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/api-text-converters.html#kitchen.text.converters.getwriter" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="kitchen.text.converters.getwriter"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">kitchen.text.converters.getwriter()</span></tt></a> to do this automatically for <a data-mce-href="http://docs.python.org/library/sys.html#sys.stdout" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/sys.html#sys.stdout" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">sys.stdout</span></tt></a>. When creating exception messages be sure to convert to bytes manually.</div>
</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<h3 data-mce-style="font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: #20435c; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #cccccc; margin: 20px -20px 10px; padding: 3px 0px 3px 10px; background-color: #f2f2f2; font-size: 140%;" style="background-color: #f2f2f2; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-width: 0px 0px 1px; color: #20435c; font-family: 'Trebuchet MS', sans-serif; font-size: 19.6000003814697px; font-weight: normal; line-height: 1.428571em; margin: 20px -20px 10px; padding: 3px 0px 3px 10px;">
When writing unittests, include non-ASCII values and both unicode and str type<a data-mce-href="https://pythonhosted.org/kitchen/unicode-frustrations.html#when-writing-unittests-include-non-ascii-values-and-both-unicode-and-str-type" data-mce-style="color: #c60f0f; text-decoration: none; visibility: hidden; font-size: 0.8em; padding: 0px 4px;" href="https://pythonhosted.org/kitchen/unicode-frustrations.html#when-writing-unittests-include-non-ascii-values-and-both-unicode-and-str-type" style="border: 0px; color: #c60f0f; font-size: 0.8em; line-height: 1.428571em; margin: 0px; padding: 0px 4px; text-decoration: none; visibility: hidden;" title="Permalink to this headline"></a></h3>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
Unless you know that a specific portion of your code will only deal with <a data-mce-href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;"><em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">ASCII</em></a>, be sure to include non-<a data-mce-href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;"><em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">ASCII</em></a> values in your unittests. Including a few characters from several different scripts is highly advised as well because some code may have special cased accented roman characters but not know how to handle characters used in Asian alphabets.</div>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
Similarly, unless you know that that portion of your code will only be given <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> strings or only byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> be sure to try variables of both types in your unittests. When doing this, make sure that the variables are also non-<a data-mce-href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;"><em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">ASCII</em></a> as <span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">python</span>’s implicit conversion will mask problems with pure <a data-mce-href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" data-mce-style="color: #355f7c; text-decoration: none;" href="https://pythonhosted.org/kitchen/glossary.html#term-ascii" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;"><em style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">ASCII</em></a> data. In many cases, it makes sense to check what happens if byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> and <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> strings that won’t decode in the present locale are given.</div>
</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<h3 data-mce-style="font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: #20435c; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #cccccc; margin: 20px -20px 10px; padding: 3px 0px 3px 10px; background-color: #f2f2f2; font-size: 140%;" style="background-color: #f2f2f2; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-width: 0px 0px 1px; color: #20435c; font-family: 'Trebuchet MS', sans-serif; font-size: 19.6000003814697px; font-weight: normal; line-height: 1.428571em; margin: 20px -20px 10px; padding: 3px 0px 3px 10px;">
Be vigilant about spotting poor APIs<a data-mce-href="https://pythonhosted.org/kitchen/unicode-frustrations.html#be-vigilant-about-spotting-poor-apis" data-mce-style="color: #c60f0f; text-decoration: none; visibility: hidden; font-size: 0.8em; padding: 0px 4px;" href="https://pythonhosted.org/kitchen/unicode-frustrations.html#be-vigilant-about-spotting-poor-apis" style="border: 0px; color: #c60f0f; font-size: 0.8em; line-height: 1.428571em; margin: 0px; padding: 0px 4px; text-decoration: none; visibility: hidden;" title="Permalink to this headline"></a></h3>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
Make sure that the libraries you use return only <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> strings or byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a>. Unittests can help you spot issues here by running many variations of data through your functions and checking that you’re still getting the types of string that you expect.</div>
</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<h3 data-mce-style="font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: #20435c; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #cccccc; margin: 20px -20px 10px; padding: 3px 0px 3px 10px; background-color: #f2f2f2; font-size: 140%;" style="background-color: #f2f2f2; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-width: 0px 0px 1px; color: #20435c; font-family: 'Trebuchet MS', sans-serif; font-size: 19.6000003814697px; font-weight: normal; line-height: 1.428571em; margin: 20px -20px 10px; padding: 3px 0px 3px 10px;">
Example: Putting this all together with kitchen<a data-mce-href="https://pythonhosted.org/kitchen/unicode-frustrations.html#example-putting-this-all-together-with-kitchen" data-mce-style="color: #c60f0f; text-decoration: none; visibility: hidden; font-size: 0.8em; padding: 0px 4px;" href="https://pythonhosted.org/kitchen/unicode-frustrations.html#example-putting-this-all-together-with-kitchen" style="border: 0px; color: #c60f0f; font-size: 0.8em; line-height: 1.428571em; margin: 0px; padding: 0px 4px; text-decoration: none; visibility: hidden;" title="Permalink to this headline"></a></h3>
<div data-mce-style="text-align: justify; line-height: 130%;" style="border: 0px; line-height: 18.2000007629395px; margin-bottom: 0.714285em; padding: 0px; text-align: justify;">
The kitchen library provides a wide array of functions to help you deal with byte <a data-mce-href="http://docs.python.org/library/functions.html#str" data-mce-style="color: #355f7c; text-decoration: none;" href="http://docs.python.org/library/functions.html#str" style="border: 0px; color: #355f7c; line-height: 1.428571em; margin: 0px; padding: 0px; text-decoration: none;" title="(in Python v2.7)"><tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">str</span></tt></a> and <tt data-mce-style="padding: 0px 1px; font-size: 0.95em; background-color: transparent; font-weight: bold;" style="background-color: transparent; font-size: 0.95em; font-weight: bold; line-height: 1.428571em; padding: 0px 1px;"><span style="line-height: 1.428571em;">unicode</span></tt> strings in your program. Here’s a short example that uses many kitchen functions to do its work:</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<div data-mce-style="background: #eeffcc;" style="background: rgb(238, 255, 204); border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<pre data-mce-style="overflow-x: auto; overflow-y: hidden; padding: 5px; color: #333333; line-height: 120%; border-top-width: 1px; border-bottom-width: 1px; border-style: solid none; border-top-color: #aacc99; border-bottom-color: #aacc99; background-color: #eeffcc;" style="border-bottom-color: rgb(170, 204, 153); border-style: solid none; border-top-color: rgb(170, 204, 153); border-width: 1px 0px; color: #333333; font-family: Monaco, Courier, monospace; line-height: 16.7999992370605px; overflow-x: auto; overflow-y: hidden; padding: 5px;" xml:space="preserve"><span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;">#!/usr/bin/<span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">python</span> -tt</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># -*- coding: utf-8 -*-</span>
<span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">import</span> <span data-mce-style="color: #0e84b5; font-weight: bold;" style="color: #0e84b5; font-weight: bold; line-height: 1.428571em;">locale</span>
<span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">import</span> <span data-mce-style="color: #0e84b5; font-weight: bold;" style="color: #0e84b5; font-weight: bold; line-height: 1.428571em;">os</span>
<span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">import</span> <span data-mce-style="color: #0e84b5; font-weight: bold;" style="color: #0e84b5; font-weight: bold; line-height: 1.428571em;">sys</span>
<span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">import</span> <span data-mce-style="color: #0e84b5; font-weight: bold;" style="color: #0e84b5; font-weight: bold; line-height: 1.428571em;">unicodedata</span>
<span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">from</span> <span data-mce-style="color: #0e84b5; font-weight: bold;" style="color: #0e84b5; font-weight: bold; line-height: 1.428571em;">kitchen.text.converters</span> <span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">import</span> <span style="line-height: 1.428571em;">getwriter</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">to_bytes</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">to_unicode</span>
<span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">from</span> <span data-mce-style="color: #0e84b5; font-weight: bold;" style="color: #0e84b5; font-weight: bold; line-height: 1.428571em;">kitchen.i18n</span> <span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">import</span> <span style="line-height: 1.428571em;">get_translation_object</span>
<span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">if</span> <span style="line-height: 1.428571em;">__name__</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">==</span> <span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'__main__'</span><span style="line-height: 1.428571em;">:</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># Setup gettext driven translations but use the kitchen functions so</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># we don't have the mismatched bytes-unicode issues.</span>
<span style="line-height: 1.428571em;">translations</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">get_translation_object</span><span style="line-height: 1.428571em;">(</span><span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'example'</span><span style="line-height: 1.428571em;">)</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># We use _() for marking strings that we operate on as unicode</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># This is pretty much everything</span>
<span style="line-height: 1.428571em;">_</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">translations</span><span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">ugettext</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># And b_() for marking strings that we operate on as bytes.</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># This is limited to exceptions</span>
<span style="line-height: 1.428571em;">b_</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">translations</span><span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">lgettext</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># Setup stdout</span>
<span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;"></span><span style="line-height: 1.428571em;"><span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">encoding</span></span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">locale</span><span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">getpreferredencoding</span><span style="line-height: 1.428571em;">()</span>
<span style="line-height: 1.428571em;">Writer</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">getwriter</span><span style="line-height: 1.428571em;">(<span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;"></span></span><span style="line-height: 1.428571em;"><span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">encoding</span></span><span style="line-height: 1.428571em;">)</span>
<span style="line-height: 1.428571em;">sys</span><span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">stdout</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">Writer</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">sys</span><span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">stdout</span><span style="line-height: 1.428571em;">)</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># Load data. Format is filename\0description</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># description should be utf-8 but filename can be any legal filename</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># on the filesystem</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># Sample datafile.txt:</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># /etc/shells\x00Shells available on caf\xc3\xa9.lan</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># /var/tmp/file\xff\x00File with non-utf8 data in the filename</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;">#</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># And to create /var/tmp/file\xff (under bash or zsh) do:</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># echo 'Some data' > /var/tmp/file$'\377'</span>
<span style="line-height: 1.428571em;">datafile</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span data-mce-style="color: #007020;" style="color: #007020; line-height: 1.428571em;">open</span><span style="line-height: 1.428571em;">(</span><span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'datafile.txt'</span><span style="line-height: 1.428571em;">,</span> <span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'r'</span><span style="line-height: 1.428571em;">)</span>
<span style="line-height: 1.428571em;">data</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">{}</span>
<span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">for</span> <span style="line-height: 1.428571em;">line</span> <span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">in</span> <span style="line-height: 1.428571em;">datafile</span><span style="line-height: 1.428571em;">:</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># We're going to keep filename as bytes because we will need the</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># exact bytes to access files on a POSIX operating system.</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># description, we'll immediately transform into unicode type.</span>
<span style="line-height: 1.428571em;">b_filename</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">description</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">line</span><span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">split</span><span style="line-height: 1.428571em;">(</span><span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'</span><span data-mce-style="color: #4070a0; font-weight: bold;" style="color: #4070a0; font-weight: bold; line-height: 1.428571em;">\0</span><span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'</span><span style="line-height: 1.428571em;">,</span> <span data-mce-style="color: #208050;" style="color: #208050; line-height: 1.428571em;">1</span><span style="line-height: 1.428571em;">)</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># to_unicode defaults to decoding output from utf-8 and replacing</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># any problematic bytes with the unicode replacement character</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># We accept mangling of the description here knowing that our file</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># format is supposed to use utf-8 in that field and that the</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># description will only be displayed to the user, not used as</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># a key value.</span>
<span style="line-height: 1.428571em;">description</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">to_unicode</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">description</span><span style="line-height: 1.428571em;">,</span> <span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'utf-8'</span><span style="line-height: 1.428571em;">)</span><span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">strip</span><span style="line-height: 1.428571em;">()</span>
<span style="line-height: 1.428571em;">data</span><span style="line-height: 1.428571em;">[</span><span style="line-height: 1.428571em;">b_filename</span><span style="line-height: 1.428571em;">]</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">description</span>
<span style="line-height: 1.428571em;">datafile</span><span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">close</span><span style="line-height: 1.428571em;">()</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># We're going to add a pair of extra fields onto our data to show the</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># length of the description and the filesize. We put those between</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># the filename and description because we haven't checked that the</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># description is free of NULLs.</span>
<span style="line-height: 1.428571em;">datafile</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span data-mce-style="color: #007020;" style="color: #007020; line-height: 1.428571em;">open</span><span style="line-height: 1.428571em;">(</span><span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'newdatafile.txt'</span><span style="line-height: 1.428571em;">,</span> <span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'w'</span><span style="line-height: 1.428571em;">)</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># Name filename with a b_ prefix to denote byte string of unknown <span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">encoding</span></span>
<span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">for</span> <span style="line-height: 1.428571em;">b_filename</span> <span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">in</span> <span style="line-height: 1.428571em;">data</span><span style="line-height: 1.428571em;">:</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># Since we have the byte representation of filename, we can read any</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># filename</span>
<span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">if</span> <span style="line-height: 1.428571em;">os</span><span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">access</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">b_filename</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">os</span><span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">F_OK</span><span style="line-height: 1.428571em;">):</span>
<span style="line-height: 1.428571em;">size</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span style="line-height: 1.428571em;">os</span><span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">path</span><span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">getsize</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">b_filename</span><span style="line-height: 1.428571em;">)</span>
<span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">else</span><span style="line-height: 1.428571em;">:</span>
<span style="line-height: 1.428571em;">size</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span data-mce-style="color: #208050;" style="color: #208050; line-height: 1.428571em;">0</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># Because the description is unicode type, we know the number of</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># characters corresponds to the length of the normalized unicode</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># string.</span>
<span style="line-height: 1.428571em;">length</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span data-mce-style="color: #007020;" style="color: #007020; line-height: 1.428571em;">len</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">unicodedata</span><span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">normalize</span><span style="line-height: 1.428571em;">(</span><span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'NFC'</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">description</span><span style="line-height: 1.428571em;">))</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># Print a summary to the screen</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># Note that we do not let implici type conversion from str to</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># unicode transform b_filename into a unicode string. That might</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># fail as <span class="highlight" style="background: rgb(246, 238, 150); border: 1px solid rgb(212, 205, 126); line-height: 1.428571em;">python</span> would use the ASCII filename. Instead we use</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># to_unicode() to explictly transform in a way that we know will</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># not traceback.</span>
<span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">print</span> <span style="line-height: 1.428571em;">_</span><span style="line-height: 1.428571em;">(</span><span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">u'filename: </span><span data-mce-style="color: #70a0d0; font-style: italic;" style="color: #70a0d0; font-style: italic; line-height: 1.428571em;">%s</span><span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'</span><span style="line-height: 1.428571em;">)</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">%</span> <span style="line-height: 1.428571em;">to_unicode</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">b_filename</span><span style="line-height: 1.428571em;">)</span>
<span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">print</span> <span style="line-height: 1.428571em;">_</span><span style="line-height: 1.428571em;">(</span><span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">u'file size: </span><span data-mce-style="color: #70a0d0; font-style: italic;" style="color: #70a0d0; font-style: italic; line-height: 1.428571em;">%s</span><span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'</span><span style="line-height: 1.428571em;">)</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">%</span> <span style="line-height: 1.428571em;">size</span>
<span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">print</span> <span style="line-height: 1.428571em;">_</span><span style="line-height: 1.428571em;">(</span><span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">u'desc length: </span><span data-mce-style="color: #70a0d0; font-style: italic;" style="color: #70a0d0; font-style: italic; line-height: 1.428571em;">%s</span><span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'</span><span style="line-height: 1.428571em;">)</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">%</span> <span style="line-height: 1.428571em;">length</span>
<span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">print</span> <span style="line-height: 1.428571em;">_</span><span style="line-height: 1.428571em;">(</span><span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">u'description: </span><span data-mce-style="color: #70a0d0; font-style: italic;" style="color: #70a0d0; font-style: italic; line-height: 1.428571em;">%s</span><span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'</span><span style="line-height: 1.428571em;">)</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">%</span> <span style="line-height: 1.428571em;">data</span><span style="line-height: 1.428571em;">[</span><span style="line-height: 1.428571em;">b_filename</span><span style="line-height: 1.428571em;">]</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># First combine the unicode portion</span>
<span style="line-height: 1.428571em;">line</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">u'</span><span data-mce-style="color: #70a0d0; font-style: italic;" style="color: #70a0d0; font-style: italic; line-height: 1.428571em;">%s</span><span data-mce-style="color: #4070a0; font-weight: bold;" style="color: #4070a0; font-weight: bold; line-height: 1.428571em;">\0</span><span data-mce-style="color: #70a0d0; font-style: italic;" style="color: #70a0d0; font-style: italic; line-height: 1.428571em;">%s</span><span data-mce-style="color: #4070a0; font-weight: bold;" style="color: #4070a0; font-weight: bold; line-height: 1.428571em;">\0</span><span data-mce-style="color: #70a0d0; font-style: italic;" style="color: #70a0d0; font-style: italic; line-height: 1.428571em;">%s</span><span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">%</span> <span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">size</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">length</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">data</span><span style="line-height: 1.428571em;">[</span><span style="line-height: 1.428571em;">b_filename</span><span style="line-height: 1.428571em;">])</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># Since the filenames are bytes, turn everything else to bytes before combining</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># Turning into unicode first would be wrong as the bytes in b_filename</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># might not convert</span>
<span style="line-height: 1.428571em;">b_line</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'</span><span data-mce-style="color: #70a0d0; font-style: italic;" style="color: #70a0d0; font-style: italic; line-height: 1.428571em;">%s</span><span data-mce-style="color: #4070a0; font-weight: bold;" style="color: #4070a0; font-weight: bold; line-height: 1.428571em;">\0</span><span data-mce-style="color: #70a0d0; font-style: italic;" style="color: #70a0d0; font-style: italic; line-height: 1.428571em;">%s</span><span data-mce-style="color: #4070a0; font-weight: bold;" style="color: #4070a0; font-weight: bold; line-height: 1.428571em;">\n</span><span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">%</span> <span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">b_filename</span><span style="line-height: 1.428571em;">,</span> <span style="line-height: 1.428571em;">to_bytes</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">line</span><span style="line-height: 1.428571em;">))</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># Just to demonstrate that getwriter will pass bytes through fine</span>
<span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">print</span> <span style="line-height: 1.428571em;">b_</span><span style="line-height: 1.428571em;">(</span><span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'Wrote: </span><span data-mce-style="color: #70a0d0; font-style: italic;" style="color: #70a0d0; font-style: italic; line-height: 1.428571em;">%s</span><span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'</span><span style="line-height: 1.428571em;">)</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">%</span> <span style="line-height: 1.428571em;">b_line</span>
<span style="line-height: 1.428571em;">datafile</span><span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">write</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">b_line</span><span style="line-height: 1.428571em;">)</span>
<span style="line-height: 1.428571em;">datafile</span><span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">.</span><span style="line-height: 1.428571em;">close</span><span style="line-height: 1.428571em;">()</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># And just to show how to properly deal with an exception.</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># Note two things about this:</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># 1) We use the b_() function to translate the string. This returns a</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># byte string instead of a unicode string</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># 2) We're using the b_() function returned by kitchen. If we had</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># used the one from gettext we would need to convert the message to</span>
<span data-mce-style="color: #408090; font-style: italic;" style="color: #408090; font-style: italic; line-height: 1.428571em;"># a byte str first</span>
<span style="line-height: 1.428571em;">message</span> <span data-mce-style="color: #666666;" style="color: #666666; line-height: 1.428571em;">=</span> <span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">u'Demonstrate the proper way to raise exceptions. Sincerely, </span><span data-mce-style="color: #4070a0; font-weight: bold;" style="color: #4070a0; font-weight: bold; line-height: 1.428571em;">\u3068\u3057\u304a</span><span data-mce-style="color: #4070a0;" style="color: #4070a0; line-height: 1.428571em;">'</span>
<span data-mce-style="color: #007020; font-weight: bold;" style="color: #007020; font-weight: bold; line-height: 1.428571em;">raise</span> <span data-mce-style="color: #007020;" style="color: #007020; line-height: 1.428571em;">Exception</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">b_</span><span style="line-height: 1.428571em;">(</span><span style="line-height: 1.428571em;">message</span><span style="line-height: 1.428571em;">))</span></pre>
</div>
</div>
</div>
</div>
Jeffyehttp://www.blogger.com/profile/07633630042179070371noreply@blogger.com0tag:blogger.com,1999:blog-5448019410873028021.post-74997693409548314232015-06-04T07:04:00.001-07:002015-06-04T07:04:20.843-07:00Saving utf-8 texts in json.dumps as UTF8, not as \u escape sequence<div>
<h1 itemprop="name" style="background-color: white; border: 0px; color: #222222; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 22px; line-height: 1.3; margin: 0px 0px 0.5em; padding: 0px;">
Saving utf-8 texts in json.dumps as UTF8, not as \u escape sequence</h1>
</div>
<div>
<br /></div>
<div>
<pre class="lang-py prettyprint prettyprinted" style="background-color: #eeeeee; border: 0px; color: #393318; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; font-size: 13px; margin-bottom: 1em; max-height: 600px; overflow: auto; padding: 5px; width: auto; word-wrap: normal;"><code style="border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; margin: 0px; padding: 0px; white-space: inherit;"><span class="pun" style="border: 0px; color: black; margin: 0px; padding: 0px;">>>></span><span class="pln" style="border: 0px; color: black; margin: 0px; padding: 0px;"> d </span><span class="pun" style="border: 0px; color: black; margin: 0px; padding: 0px;">=</span><span class="pln" style="border: 0px; color: black; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; color: black; margin: 0px; padding: 0px;">{</span><span class="lit" style="border: 0px; color: maroon; margin: 0px; padding: 0px;">1</span><span class="pun" style="border: 0px; color: black; margin: 0px; padding: 0px;">:</span><span class="pln" style="border: 0px; color: black; margin: 0px; padding: 0px;"> </span><span class="str" style="border: 0px; color: maroon; margin: 0px; padding: 0px;">"ברי צקלה"</span><span class="pun" style="border: 0px; color: black; margin: 0px; padding: 0px;">,</span><span class="pln" style="border: 0px; color: black; margin: 0px; padding: 0px;"> </span><span class="lit" style="border: 0px; color: maroon; margin: 0px; padding: 0px;">2</span><span class="pun" style="border: 0px; color: black; margin: 0px; padding: 0px;">:</span><span class="pln" style="border: 0px; color: black; margin: 0px; padding: 0px;"> u</span><span class="str" style="border: 0px; color: maroon; margin: 0px; padding: 0px;">"ברי צקלה"</span><span class="pun" style="border: 0px; color: black; margin: 0px; padding: 0px;">}</span><span class="pln" style="border: 0px; color: black; margin: 0px; padding: 0px;">
</span><span class="pun" style="border: 0px; color: black; margin: 0px; padding: 0px;">>>></span><span class="pln" style="border: 0px; color: black; margin: 0px; padding: 0px;"> json_str </span><span class="pun" style="border: 0px; color: black; margin: 0px; padding: 0px;">=</span><span class="pln" style="border: 0px; color: black; margin: 0px; padding: 0px;"> json</span><span class="pun" style="border: 0px; color: black; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: black; margin: 0px; padding: 0px;">dumps</span><span class="pun" style="border: 0px; color: black; margin: 0px; padding: 0px;">(</span><span class="pln" style="border: 0px; color: black; margin: 0px; padding: 0px;">d</span><span class="pun" style="border: 0px; color: black; margin: 0px; padding: 0px;">).</span><span class="pln" style="border: 0px; color: black; margin: 0px; padding: 0px;">decode</span><span class="pun" style="border: 0px; color: black; margin: 0px; padding: 0px;">(</span><span class="str" style="border: 0px; color: maroon; margin: 0px; padding: 0px;">'unicode-escape'</span><span class="pun" style="border: 0px; color: black; margin: 0px; padding: 0px;">).</span><span class="pln" style="border: 0px; color: black; margin: 0px; padding: 0px;">encode</span><span class="pun" style="border: 0px; color: black; margin: 0px; padding: 0px;">(</span><span class="str" style="border: 0px; color: maroon; margin: 0px; padding: 0px;">'utf8'</span><span class="pun" style="border: 0px; color: black; margin: 0px; padding: 0px;">)</span><span class="pln" style="border: 0px; color: black; margin: 0px; padding: 0px;">
</span><span class="pun" style="border: 0px; color: black; margin: 0px; padding: 0px;">>>></span><span class="pln" style="border: 0px; color: black; margin: 0px; padding: 0px;"> </span><span class="kwd" style="border: 0px; color: darkblue; margin: 0px; padding: 0px;">print</span><span class="pln" style="border: 0px; color: black; margin: 0px; padding: 0px;"> json_str
</span><span class="pun" style="border: 0px; color: black; margin: 0px; padding: 0px;">{</span><span class="str" style="border: 0px; color: maroon; margin: 0px; padding: 0px;">"1"</span><span class="pun" style="border: 0px; color: black; margin: 0px; padding: 0px;">:</span><span class="pln" style="border: 0px; color: black; margin: 0px; padding: 0px;"> </span><span class="str" style="border: 0px; color: maroon; margin: 0px; padding: 0px;">"ברי צקלה"</span><span class="pun" style="border: 0px; color: black; margin: 0px; padding: 0px;">,</span><span class="pln" style="border: 0px; color: black; margin: 0px; padding: 0px;"> </span><span class="str" style="border: 0px; color: maroon; margin: 0px; padding: 0px;">"2"</span><span class="pun" style="border: 0px; color: black; margin: 0px; padding: 0px;">:</span><span class="pln" style="border: 0px; color: black; margin: 0px; padding: 0px;"> </span><span class="str" style="border: 0px; color: maroon; margin: 0px; padding: 0px;">"ברי צקלה"</span><span class="pun" style="border: 0px; color: black; margin: 0px; padding: 0px;">}</span></code></pre>
<pre class="lang-py prettyprint prettyprinted" style="background-color: #eeeeee; border: 0px; color: #393318; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; font-size: 13px; margin-bottom: 1em; max-height: 600px; overflow: auto; padding: 5px; width: auto; word-wrap: normal;"><code style="border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; margin: 0px; padding: 0px; white-space: inherit;"><span class="pun" style="border: 0px; color: black; margin: 0px; padding: 0px;">
</span></code></pre>
<pre class="lang-py prettyprint prettyprinted" style="background-color: #eeeeee; border: 0px; color: #393318; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; font-size: 13px; margin-bottom: 1em; max-height: 600px; overflow: auto; padding: 5px; width: auto; word-wrap: normal;"><code style="border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; margin: 0px; padding: 0px; white-space: inherit;"><span class="pun" style="border: 0px; color: black; margin: 0px; padding: 0px;">
</span></code></pre>
</div>
Jeffyehttp://www.blogger.com/profile/07633630042179070371noreply@blogger.com0tag:blogger.com,1999:blog-5448019410873028021.post-65998537068288608332015-06-02T22:01:00.002-07:002015-06-02T22:01:14.571-07:00manully Update Drupal Core<div id="page-heading" style="background-color: white; border: 0px; box-sizing: border-box; color: #222222; font-family: 'Lucida Grande', 'DejaVu Sans', 'Bitstream Vera Sans', Verdana, Arial, sans-serif; font-size: 13px; font-stretch: inherit; line-height: 17.9999942779541px; margin: 0px; padding: 0px; vertical-align: baseline;">
<h1 id="page-subtitle" style="border: 0px; box-sizing: border-box; color: black; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: normal; line-height: 1.385em; margin: 0px 0px 0.692em; padding: 0px; vertical-align: baseline;">
Manully Update Drupal Core</h1>
</div>
<div id="sidebar-first" role="complementary" style="background-color: white; border: 0px; box-sizing: border-box; color: #222222; display: inline; float: right; font-family: 'Lucida Grande', 'DejaVu Sans', 'Bitstream Vera Sans', Verdana, Arial, sans-serif; font-size: 13px; font-stretch: inherit; line-height: 17.9999942779541px; margin: 0px; padding: 0px; vertical-align: baseline; width: 299.989593505859px;">
<div id="sidebar-first-region" style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px 0px 13px; padding: 0px; vertical-align: baseline;">
</div>
</div>
<div id="main" role="main" style="background-color: white; border: 0px; box-sizing: border-box; color: #222222; display: inline; float: left; font-family: 'Lucida Grande', 'DejaVu Sans', 'Bitstream Vera Sans', Verdana, Arial, sans-serif; font-size: 13px; font-stretch: inherit; line-height: 17.9999942779541px; margin: 0px 20px 0px 0px; padding: 0px; vertical-align: baseline; width: 620px; z-index: 1;">
<div class="clearfix" id="content" style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px 0px 13px; padding: 0px; vertical-align: baseline;">
<div class="clearfix" id="content-inner" style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
<div class="region region-content" style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
<div class="block block-system" id="block-system-main" style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
<div class="block-inner" style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
<div class="content" style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
<div class="node node-book node-content-1494290 clearfix" id="node-1494290" style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px 0px 1.385em; padding: 0px; vertical-align: baseline;">
<div class="content" style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
<div class="updated" style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin-bottom: 1.385em; padding: 0px; vertical-align: baseline;">
<em style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Last updated June 2, 2015. Created on March 22, 2012.<br style="box-sizing: border-box;" />Edited by <a class="username" data-uid="801668" href="https://www.drupal.org/u/steve-cc" style="border: 0px; box-sizing: border-box; color: #0678be; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;" title="View user profile.">Steve -cc</a>, <a class="username" data-uid="391085" href="https://www.drupal.org/u/ssiruguri" style="border: 0px; box-sizing: border-box; color: #0678be; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;" title="View user profile.">ssiruguri</a>, <a class="username" data-uid="2018902" href="https://www.drupal.org/user/2018902" style="border: 0px; box-sizing: border-box; color: #0678be; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;" title="View user profile.">talvi</a>, <a class="username" data-uid="1847164" href="https://www.drupal.org/u/shamio" style="border: 0px; box-sizing: border-box; color: #0678be; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;" title="View user profile.">shamio</a>. <a href="https://www.drupal.org/user?destination=node/1494290" style="border: 0px; box-sizing: border-box; color: #0678be; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">Log in to edit this page</a>.</em></div>
<div class="field field-name-body field-type-text-with-summary field-label-hidden" style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
<div class="field-items" style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
<div class="field-item even" style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
<div style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin-bottom: 1.385em; padding: 0px; vertical-align: baseline;">
<strong style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><em style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">These instructions are for updating - not upgrading. Documentation for upgrading (changing from D6 to D7 etc.) an existing Drupal site, see<a href="https://www.drupal.org/upgrade" rel="nofollow" style="border: 0px; box-sizing: border-box; color: #0678be; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">http://drupal.org/upgrade</a>.</em></strong></div>
<div style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin-bottom: 1.385em; padding: 0px; vertical-align: baseline;">
Instructions on how to update Drupal core with a newer version of Drupal of the same category or same base version [ ie Drupal 7.1 to 7.2 etc ]</div>
<ol style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; list-style-position: outside; margin: 0px 0px 0.692em; padding: 0px 0px 0px 2em; vertical-align: baseline;">
<li style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Make a backup of your Drupal instance (database). (For ex: <a href="http://www.thegeekstuff.com/2008/09/backup-and-restore-mysql-database-using-mysqldump/" rel="nofollow" style="border: 0px; box-sizing: border-box; color: #0678be; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">with MySQL</a>)</li>
<li style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Download the latest release of your current Drupal version.</li>
<li style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Extract the [tar ball or zip] Drupal package.</li>
<li style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Set your site on maintenance mode (For ex: <a href="http://www.ostraining.com/blog/drupal/how-to-put-drupal-7-in-maintenance-mode/" rel="nofollow" style="border: 0px; box-sizing: border-box; color: #0678be; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">How on D7</a>)</li>
<li style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Delete all the files & folders inside your original Drupal instance except for <em style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">/sites and its sub-folders. This assumes that all of your custom work - including themes - is in the recommended spot - the /sites folder tree. If you have made custom changes elsewhere you will need to either preserve them or plan to replace the later. </em>.</li>
<li style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Copy all the folders and files except <em style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">/sites</em> from inside the extracted Drupal package [tar ball or zip package] into your original Drupal instance location.</li>
<li style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Some updates do not include changes to setting.php and some do. See <a href="https://www.drupal.org/node/1494290#comment-9488185" rel="nofollow" style="border: 0px; box-sizing: border-box; color: #0678be; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">There is a way - Comment below</a> for more on this. If the update release includes changes to <em style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">settings.php</em>you will need to replace your old settings.php in <em style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.../sites/default/</em> with the new one, and then edit the site-specific entries (eg database name, user, and password) in the setting file.</li>
<li style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">If you have modified files such as .htaccess or robots.txt re-apply those changes to the new files.</li>
<li style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">If you have a favicon.ico file that was deleted replace it too</li>
<li style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Login to your site as administrator or as user no 1</li>
<li style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Run update.php by navigating to <em style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><a href="http://...yourdrupalsitename/update.php" style="border: 0px; box-sizing: border-box; color: #0678be; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">http://...yourdrupalsitename/update.php</a></em></li>
<li style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Follow the process to update your Drupal instance</li>
<li style="border: 0px; box-sizing: border-box; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Disable maintenance mode</li>
</ol>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Jeffyehttp://www.blogger.com/profile/07633630042179070371noreply@blogger.com0tag:blogger.com,1999:blog-5448019410873028021.post-23038047822475952562015-05-24T21:36:00.004-07:002015-05-24T21:36:57.601-07:00Designing a RESTful API using Flask-RESTful<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
This is the third article in which I explore different aspects of writing RESTful APIs using the <a href="http://flask.pocoo.org/" style="background-color: transparent; box-sizing: border-box; color: #6666aa; text-decoration: none;">Flask</a> microframework.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
The example RESTful server I wrote before used only Flask as a dependency. Today I will show you how to write the same server using <a href="http://flask-restful.readthedocs.org/en/latest/" style="background-color: transparent; box-sizing: border-box; color: #6666aa; text-decoration: none;">Flask-RESTful</a>, a Flask extension that simplifies the creation of APIs.</div>
<h2 style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 25.2000007629395px; font-weight: normal; line-height: 1.1; margin-bottom: 10px; margin-top: 16px;">
The RESTful server</h2>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
As a reminder, here is the definition of the ToDo List web service that has been serving as an example in my RESTful articles:</div>
<table class="data" style="background-color: white; border-collapse: collapse; border-spacing: 0px; border: 1px solid rgb(136, 136, 136); box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 21px; margin: 0px 0px 16px; padding: 0px;"><tbody style="box-sizing: border-box;">
<tr style="box-sizing: border-box;"><th style="background-color: #cccccc; border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">HTTP Method</th><th style="background-color: #cccccc; border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">URI</th><th style="background-color: #cccccc; border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">Action</th></tr>
<tr style="background-color: #eeeeff; box-sizing: border-box;"><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">GET</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">http://[hostname]/todo/api/v1.0/tasks</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">Retrieve list of tasks</td></tr>
<tr style="box-sizing: border-box;"><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">GET</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">http://[hostname]/todo/api/v1.0/tasks/[task_id]</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">Retrieve a task</td></tr>
<tr style="background-color: #eeeeff; box-sizing: border-box;"><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">POST</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">http://[hostname]/todo/api/v1.0/tasks</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">Create a new task</td></tr>
<tr style="box-sizing: border-box;"><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">PUT</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">http://[hostname]/todo/api/v1.0/tasks/[task_id]</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">Update an existing task</td></tr>
<tr style="background-color: #eeeeff; box-sizing: border-box;"><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">DELETE</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">http://[hostname]/todo/api/v1.0/tasks/[task_id]</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">Delete a task</td></tr>
</tbody></table>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
The only resource exposed by this service is a "task", which has the following data fields:</div>
<ul style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 20px; margin-top: 0px;">
<li style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">uri</span>: unique URI for the task. String type.</li>
<li style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">title</span>: short task description. String type.</li>
<li style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">description</span>: long task description. Text type.</li>
<li style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">done</span>: task completion state. Boolean type.</li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 25.2000007629395px; font-weight: normal; line-height: 1.1; margin-bottom: 10px; margin-top: 16px;">
Routing</h2>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
In my first RESTful server example (source code <a href="https://github.com/miguelgrinberg/REST-tutorial/blob/master/rest-server.py" style="background-color: transparent; box-sizing: border-box; color: #6666aa; text-decoration: none;">here</a>) I have used regular Flask view functions to define all the routes.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Flask-RESTful provides a <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">Resource</code> base class that can define the routing for one or more HTTP methods for a given URL. For example, to define a <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">User</code>resource with <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">GET</code>, <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">PUT</code> and <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">DELETE</code> methods you would write:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="kwd" style="box-sizing: border-box; color: #8959a8;">from</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> flask </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">import</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Flask</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">from</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> flask</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">ext</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">restful </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">import</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Api</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Resource</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
app </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Flask</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">__name__</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
api </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Api</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">app</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">class</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">UserAPI</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Resource</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">):</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">def</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">get</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">self</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> id</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">):</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">pass</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">def</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> put</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">self</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> id</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">):</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">pass</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">def</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">delete</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">self</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> id</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">):</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">pass</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
api</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">add_resource</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">UserAPI</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'/users/<int:id>'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> endpoint </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'user'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
The <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">add_resource</code> function registers the routes with the framework using the given endpoint. If an endpoint isn't given then Flask-RESTful generates one for you from the class name, but since sometimes the endpoint is needed for functions such as <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">url_for</code> I prefer to make it explicit.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
My ToDo API defines two URLs: <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">/todo/api/v1.0/tasks</code> for the list of tasks, and <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">/todo/api/v1.0/tasks/<int:id></code> for an individual task. Since Flask-RESTful's <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">Resource</code> class can wrap a single URL this server will need two resources:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="kwd" style="box-sizing: border-box; color: #8959a8;">class</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">TaskListAPI</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Resource</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">):</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">def</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">get</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">self</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">):</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">pass</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">def</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> post</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">self</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">):</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">pass</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">class</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">TaskAPI</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Resource</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">):</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">def</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">get</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">self</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> id</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">):</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">pass</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">def</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> put</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">self</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> id</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">):</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">pass</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">def</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">delete</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">self</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> id</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">):</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">pass</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
api</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">add_resource</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">TaskListAPI</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'/todo/api/v1.0/tasks'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> endpoint </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'tasks'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
api</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">add_resource</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">TaskAPI</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'/todo/api/v1.0/tasks/<int:id>'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> endpoint </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'task'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Note that while the method views of <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">TaskListAPI</code> receive no arguments the ones in <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">TaskAPI</code> all receive the <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">id</code>, as specified in the URL under which the resource is registered.</div>
<h2 style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 25.2000007629395px; font-weight: normal; line-height: 1.1; margin-bottom: 10px; margin-top: 16px;">
Request Parsing and Validation</h2>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
When I implemented this server in the previous article I did my own validation of the request data. For example, look at how long the <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">PUT</code> handler is in that version:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="lit" style="box-sizing: border-box; color: #f5871f;">@app</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">route</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="str" style="box-sizing: border-box; color: #718c00;">'/todo/api/v1.0/tasks/<int:task_id>'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> methods </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="str" style="box-sizing: border-box; color: #718c00;">'PUT'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">])</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">@auth</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">login_required
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">def</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> update_task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">task_id</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">):</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
task </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> filter</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">lambda</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> t</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> t</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="str" style="box-sizing: border-box; color: #718c00;">'id'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">==</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> task_id</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> tasks</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">if</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> len</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">==</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
abort</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">404</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">if</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">not</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> request</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
abort</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">400</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">if</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'title'</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">in</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> request</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">and</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> type</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">request</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="str" style="box-sizing: border-box; color: #718c00;">'title'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">])</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">!=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> unicode</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
abort</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">400</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">if</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'description'</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">in</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> request</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">and</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> type</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">request</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="str" style="box-sizing: border-box; color: #718c00;">'description'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">])</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">is</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">not</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> unicode</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
abort</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">400</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">if</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'done'</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">in</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> request</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">and</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> type</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">request</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="str" style="box-sizing: border-box; color: #718c00;">'done'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">])</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">is</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">not</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">bool</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
abort</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">400</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">][</span><span class="str" style="box-sizing: border-box; color: #718c00;">'title'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> request</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">get</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="str" style="box-sizing: border-box; color: #718c00;">'title'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">][</span><span class="str" style="box-sizing: border-box; color: #718c00;">'title'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">])</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">][</span><span class="str" style="box-sizing: border-box; color: #718c00;">'description'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> request</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">get</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="str" style="box-sizing: border-box; color: #718c00;">'description'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">][</span><span class="str" style="box-sizing: border-box; color: #718c00;">'description'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">])</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">][</span><span class="str" style="box-sizing: border-box; color: #718c00;">'done'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> request</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">get</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="str" style="box-sizing: border-box; color: #718c00;">'done'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">][</span><span class="str" style="box-sizing: border-box; color: #718c00;">'done'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">])</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">return</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> jsonify</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'task'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> make_public_task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">])</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">}</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Here I have to make sure the data given with the request is valid before using it, and that makes the function pretty long.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Flask-RESTful provides a much better way to handle this with the<code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">RequestParser</code> class. This class works in a similar way as <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">argparse</code> for command line arguments.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
First, for each resource I define the arguments and how to validate them:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="kwd" style="box-sizing: border-box; color: #8959a8;">from</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> flask</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">ext</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">restful </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">import</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> reqparse
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">class</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">TaskListAPI</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Resource</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">):</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">def</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> __init__</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">self</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">):</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">self</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">reqparse </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> reqparse</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">RequestParser</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">()</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">self</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">reqparse</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">add_argument</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="str" style="box-sizing: border-box; color: #718c00;">'title'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> type </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> str</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> required </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">True</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
help </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'No task title provided'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> location </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'json'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">self</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">reqparse</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">add_argument</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="str" style="box-sizing: border-box; color: #718c00;">'description'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> type </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> str</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">default</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">""</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> location </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'json'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">super</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">TaskListAPI</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">self</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">).</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">__init__</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">()</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="com" style="box-sizing: border-box; color: #8e908c;"># ...</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">class</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">TaskAPI</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Resource</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">):</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">def</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> __init__</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">self</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">):</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">self</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">reqparse </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> reqparse</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">RequestParser</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">()</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">self</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">reqparse</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">add_argument</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="str" style="box-sizing: border-box; color: #718c00;">'title'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> type </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> str</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> location </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'json'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">self</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">reqparse</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">add_argument</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="str" style="box-sizing: border-box; color: #718c00;">'description'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> type </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> str</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> location </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'json'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">self</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">reqparse</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">add_argument</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="str" style="box-sizing: border-box; color: #718c00;">'done'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> type </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">bool</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> location </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'json'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">super</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">TaskAPI</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">self</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">).</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">__init__</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">()</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="com" style="box-sizing: border-box; color: #8e908c;"># ...</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
In the <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">TaskListAPI</code> resource the <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">POST</code> method is the only one the receives arguments. The <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">title</code> argument is required here, so I included an error message that Flask-RESTful will send as a response to the client when the field is missing. The <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">description</code> field is optional, and when it is missing a default value of an empty string will be used. One interesting aspect of the<code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">RequestParser</code> class is that by default it looks for fields in <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">request.values</code>, so the <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">location</code> optional argument must be set to indicate that the fields are coming in <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">request.json</code>.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
The request parser for the <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">TaskAPI</code> is constructed in a similar way, but has a few differences. In this case it is the <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">PUT</code> method that will need to parse arguments, and for this method all the arguments are optional, including the<code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">done</code> field that was not part of the request in the other resource.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Now that the request parsers are initialized, parsing and validating a request is pretty easy. For example, note how much simpler the <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">TaskAPI.put()</code> method becomes:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">def</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> put</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">self</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> id</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">):</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
task </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> filter</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">lambda</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> t</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> t</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="str" style="box-sizing: border-box; color: #718c00;">'id'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">==</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> id</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> tasks</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">if</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> len</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">==</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
abort</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">404</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
task </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
args </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">self</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">reqparse</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">parse_args</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">()</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">for</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> k</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> v </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">in</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> args</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">iteritems</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">():</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">if</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> v </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">!=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">None</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">k</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> v
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">return</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> jsonify</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'task'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> make_public_task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">}</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
A side benefit of letting Flask-RESTful do the validation is that now there is no need to have a handler for the bad request code 400 error, this is all taken care of by the extension.</div>
<h2 style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 25.2000007629395px; font-weight: normal; line-height: 1.1; margin-bottom: 10px; margin-top: 16px;">
Generating Responses</h2>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
My original REST server generates the responses using Flask's <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">jsonify</code>helper function. Flask-RESTful automatically handles the conversion to JSON, so instead of this:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">return</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> jsonify</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'task'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> make_public_task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">}</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
I can do this:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">return</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'task'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> make_public_task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">}</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Flask-RESTful also supports passing a custom status code back when necessary:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">return</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'task'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> make_public_task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">},</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">201</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
But there is more. The <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">make_public_task</code> wrapper from the original server converted a task from its internal representation to the external representation that clients expected. The conversion included removing the <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">id</code> field and adding a <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">uri</code> field in its place. Flask-RESTful provides a helper function to do this in a much more elegant way that not only generates the <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">uri</code> but also does type conversion on the remaining fields:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="kwd" style="box-sizing: border-box; color: #8959a8;">from</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> flask</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">ext</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">restful </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">import</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> fields</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> marshal
task_fields </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">'title'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> fields</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">String</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">'description'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> fields</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">String</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">'done'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> fields</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Boolean</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">'uri'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> fields</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Url</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="str" style="box-sizing: border-box; color: #718c00;">'task'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">}</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">class</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">TaskAPI</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Resource</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">):</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="com" style="box-sizing: border-box; color: #8e908c;"># ...</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">def</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> put</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">self</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> id</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">):</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="com" style="box-sizing: border-box; color: #8e908c;"># ...</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">return</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'task'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> marshal</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> task_fields</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">}</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
The <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">task_fields</code> structure serves as a template for the <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">marshal</code> function. The <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">fields.Uri</code> type is a special type that generates a URL. The argument it takes is the endpoint (recall that I have used explicit endpoints when I registered the resources specifically so that I can refer to them when needed).</div>
<h2 style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 25.2000007629395px; font-weight: normal; line-height: 1.1; margin-bottom: 10px; margin-top: 16px;">
Authentication</h2>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
The routes in the REST server are all protected with HTTP basic authentication. In the original server the protection was added using the decorator provided by the <a href="https://github.com/miguelgrinberg/flask-httpauth" style="background-color: transparent; box-sizing: border-box; color: #6666aa; text-decoration: none;">Flask-HTTPAuth</a> extension.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Since the <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">Resouce</code> class inherits from Flask's <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">MethodView</code>, it is possible to attach decorators to the methods by defining a <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">decorators</code> class variable:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="kwd" style="box-sizing: border-box; color: #8959a8;">from</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> flask</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">ext</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">httpauth </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">import</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">HTTPBasicAuth</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="com" style="box-sizing: border-box; color: #8e908c;"># ...</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
auth </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">HTTPBasicAuth</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">()</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="com" style="box-sizing: border-box; color: #8e908c;"># ...</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">class</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">TaskAPI</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Resource</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">):</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
decorators </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">auth</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">login_required</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="com" style="box-sizing: border-box; color: #8e908c;"># ...</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">class</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">TaskAPI</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Resource</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">):</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
decorators </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">auth</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">login_required</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="com" style="box-sizing: border-box; color: #8e908c;"># ...</span></code></pre>
<h2 style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 25.2000007629395px; font-weight: normal; line-height: 1.1; margin-bottom: 10px; margin-top: 16px;">
Conclusion</h2>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
The complete server implementation based on Flask-RESTful is available in my<a href="https://github.com/miguelgrinberg/REST-tutorial" style="background-color: transparent; box-sizing: border-box; color: #6666aa; text-decoration: none;">REST-tutorial</a> project on github. The file with the Flask-RESTful server is <a href="https://github.com/miguelgrinberg/REST-tutorial/blob/master/rest-server-v2.py" style="background-color: transparent; box-sizing: border-box; color: #6666aa; text-decoration: none;">rest-server-v2.py</a>.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
You can also download the entire project including both server implementations and a javascript client to test it:</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
<a href="https://github.com/miguelgrinberg/REST-tutorial/archive/master.zip" style="background-color: transparent; box-sizing: border-box; color: #6666aa; text-decoration: none;">Download REST-tutorial project</a>.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Let me know if you have any questions in the comments below.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
from Miguel at http://blog.miguelgrinberg.com/post/designing-a-restful-api-using-flask-restful</div>
Jeffyehttp://www.blogger.com/profile/07633630042179070371noreply@blogger.com0tag:blogger.com,1999:blog-5448019410873028021.post-43297455519228112112015-05-24T21:34:00.002-07:002015-05-24T21:34:57.226-07:00Designing a RESTful API with Python and Flask<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
In recent years <a href="https://en.wikipedia.org/wiki/Representational_state_transfer" style="background-color: transparent; box-sizing: border-box; color: #6666aa; text-decoration: none;">REST</a> (REpresentational State Transfer) has emerged as the standard architectural design for web services and web APIs.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
In this article I'm going to show you how easy it is to create a RESTful web service using <a href="http://www.python.org/" style="background-color: transparent; box-sizing: border-box; color: #6666aa; text-decoration: none;">Python</a> and the <a href="http://flask.pocoo.org/" style="background-color: transparent; box-sizing: border-box; color: #6666aa; text-decoration: none;">Flask</a> microframework.</div>
<h2 style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 25.2000007629395px; font-weight: normal; line-height: 1.1; margin-bottom: 10px; margin-top: 16px;">
What is REST?</h2>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
The characteristics of a REST system are defined by six design rules:</div>
<ul style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 20px; margin-top: 0px;">
<li style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">Client-Server</span>: There should be a separation between the server that offers a service, and the client that consumes it.</li>
<li style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">Stateless</span>: Each request from a client must contain all the information required by the server to carry out the request. In other words, the server cannot store information provided by the client in one request and use it in another request.</li>
<li style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">Cacheable</span>: The server must indicate to the client if requests can be cached or not.</li>
<li style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">Layered System</span>: Communication between a client and a server should be standardized in such a way that allows intermediaries to respond to requests instead of the end server, without the client having to do anything different.</li>
<li style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">Uniform Interface</span>: The method of communication between a client and a server must be uniform.</li>
<li style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">Code on demand</span>: Servers can provide executable code or scripts for clients to execute in their context. This constraint is the only one that is optional.</li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 25.2000007629395px; font-weight: normal; line-height: 1.1; margin-bottom: 10px; margin-top: 16px;">
What is a RESTful web service?</h2>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
The REST architecture was originally designed to fit the <a href="http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol" style="background-color: transparent; box-sizing: border-box; color: #6666aa; text-decoration: none;">HTTP protocol</a> that the world wide web uses.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Central to the concept of RESTful web services is the notion of resources. Resources are represented by <a href="https://en.wikipedia.org/wiki/Uniform_Resource_Identifier" style="background-color: transparent; box-sizing: border-box; color: #6666aa; text-decoration: none;">URIs</a>. The clients send requests to these URIs using the methods defined by the HTTP protocol, and possibly as a result of that the state of the affected resource changes.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
The HTTP request methods are typically designed to affect a given resource in standard ways:</div>
<table class="data" style="background-color: white; border-collapse: collapse; border-spacing: 0px; border: 1px solid rgb(136, 136, 136); box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 21px; margin: 0px 0px 16px; padding: 0px;"><tbody style="box-sizing: border-box;">
<tr style="box-sizing: border-box;"><th style="background-color: #cccccc; border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">HTTP Method</th><th style="background-color: #cccccc; border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">Action</th><th style="background-color: #cccccc; border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">Examples</th></tr>
<tr style="background-color: #eeeeff; box-sizing: border-box;"><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">GET</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">Obtain information about a resource</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">http://example.com/api/orders<br style="box-sizing: border-box;" />(retrieve order list)</td></tr>
<tr style="box-sizing: border-box;"><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">GET</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">Obtain information about a resource</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">http://example.com/api/orders/123<br style="box-sizing: border-box;" />(retrieve order #123)</td></tr>
<tr style="background-color: #eeeeff; box-sizing: border-box;"><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">POST</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">Create a new resource</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">http://example.com/api/orders<br style="box-sizing: border-box;" />(create a new order, from data provided with the request)</td></tr>
<tr style="box-sizing: border-box;"><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">PUT</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">Update a resource</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">http://example.com/api/orders/123<br style="box-sizing: border-box;" />(update order #123, from data provided with the request)</td></tr>
<tr style="background-color: #eeeeff; box-sizing: border-box;"><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">DELETE</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">Delete a resource</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">http://example.com/api/orders/123<br style="box-sizing: border-box;" />(delete order #123)</td></tr>
</tbody></table>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
The REST design does not require a specific format for the data provided with the requests. In general data is provided in the request body as a <a href="http://en.wikipedia.org/wiki/JSON" style="background-color: transparent; box-sizing: border-box; color: #6666aa; text-decoration: none;">JSON</a> blob, or sometimes as arguments in the <a href="http://en.wikipedia.org/wiki/Query_string" style="background-color: transparent; box-sizing: border-box; color: #6666aa; text-decoration: none;">query string</a> portion of the URL.</div>
<h2 style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 25.2000007629395px; font-weight: normal; line-height: 1.1; margin-bottom: 10px; margin-top: 16px;">
Designing a simple web service</h2>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
The task of designing a web service or API that adheres to the REST guidelines then becomes an exercise in identifying the resources that will be exposed and how they will be affected by the different request methods.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Let's say we want to write a To Do List application and we want to design a web service for it. The first thing to do is to decide what is the root URL to access this service. For example, we could expose this service as:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">http</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="com" style="box-sizing: border-box; color: #8e908c;">//[hostname]/todo/api/v1.0/</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Here I have decided to include the name of the application and the version of the API in the URL. Including the application name in the URL is useful to provide a namespace that separates this service from others that can be running on the same system. Including the version in the URL can help with making updates in the future, since new and potentially incompatible functions can be added under a new version, without affecting applications that rely on the older functions.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
The next step is to select the resources that will be exposed by this service. This is an extremely simple application, we only have tasks, so our only resource will be the tasks in our to do list.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Our tasks resource will use HTTP methods as follows:</div>
<table class="data" style="background-color: white; border-collapse: collapse; border-spacing: 0px; border: 1px solid rgb(136, 136, 136); box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 21px; margin: 0px 0px 16px; padding: 0px;"><tbody style="box-sizing: border-box;">
<tr style="box-sizing: border-box;"><th style="background-color: #cccccc; border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">HTTP Method</th><th style="background-color: #cccccc; border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">URI</th><th style="background-color: #cccccc; border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">Action</th></tr>
<tr style="background-color: #eeeeff; box-sizing: border-box;"><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">GET</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">http://[hostname]/todo/api/v1.0/tasks</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">Retrieve list of tasks</td></tr>
<tr style="box-sizing: border-box;"><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">GET</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">http://[hostname]/todo/api/v1.0/tasks/[task_id]</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">Retrieve a task</td></tr>
<tr style="background-color: #eeeeff; box-sizing: border-box;"><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">POST</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">http://[hostname]/todo/api/v1.0/tasks</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">Create a new task</td></tr>
<tr style="box-sizing: border-box;"><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">PUT</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">http://[hostname]/todo/api/v1.0/tasks/[task_id]</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">Update an existing task</td></tr>
<tr style="background-color: #eeeeff; box-sizing: border-box;"><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">DELETE</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">http://[hostname]/todo/api/v1.0/tasks/[task_id]</td><td style="border: 1px solid rgb(136, 136, 136); box-sizing: border-box; margin: 0px; padding: 4px 16px;">Delete a task</td></tr>
</tbody></table>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
We can define a task as having the following fields:</div>
<ul style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 20px; margin-top: 0px;">
<li style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">id</span>: unique identifier for tasks. Numeric type.</li>
<li style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">title</span>: short task description. String type.</li>
<li style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">description</span>: long task description. Text type.</li>
<li style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">done</span>: task completion state. Boolean type.</li>
</ul>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
And with this we are basically done with the design part of our web service. All that is left is to implement it!</div>
<h2 style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 25.2000007629395px; font-weight: normal; line-height: 1.1; margin-bottom: 10px; margin-top: 16px;">
A brief introduction to the Flask microframework</h2>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
If you read my <a href="http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world" style="background-color: transparent; box-sizing: border-box; color: #6666aa; text-decoration: none;">Flask Mega-Tutorial series</a> you know that Flask is a simple, yet very powerful Python web framework.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Before we delve into the specifics of web services let's review how a regular Flask web application is structured.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
I will assume you know the basics of working with Python in your platform. The example command lines I will show below are for a Unix-like operating system. In short, that means that they will work on Linux, Mac OS X and also on Windows if you use <a href="http://www.cygwin.com/" style="background-color: transparent; box-sizing: border-box; color: #6666aa; text-decoration: none;">Cygwin</a>. The commands are slightly different if you use the Windows native version of Python.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Let's begin by installing Flask in a virtual environment. If you don't have<code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">virtualenv</code> installed in your system, you can download it from<a href="https://pypi.python.org/pypi/virtualenv" style="background-color: transparent; box-sizing: border-box; color: #6666aa; text-decoration: none;">https://pypi.python.org/pypi/virtualenv</a>.</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">$ mkdir todo</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">api
$ cd todo</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">api
$ virtualenv flask
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">New</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> python executable </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">in</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> flask</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">bin</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">python
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Installing</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> setuptools</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">............................</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">done</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Installing</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> pip</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">...................</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">done</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
$ flask</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">bin</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">pip install flask</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Now that we have Flask installed let's create a simple web application, which we will put in a file called <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">app.py</code>:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="com" style="box-sizing: border-box; color: #8e908c;">#!flask/bin/python</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">from</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> flask </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">import</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Flask</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
app </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Flask</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">__name__</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">@app</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">route</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="str" style="box-sizing: border-box; color: #718c00;">'/'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">def</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> index</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">():</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">return</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"Hello, World!"</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">if</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> __name__ </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">==</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'__main__'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
app</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">run</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">debug</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">True</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
To run this application we have to execute <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">app.py</code>:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">$ chmod a</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">+</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">x app</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">py
$ </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">./</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">app</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">py
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">*</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Running</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> on http</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="com" style="box-sizing: border-box; color: #8e908c;">//127.0.0.1:5000/</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">*</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Restarting</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">with</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> reloader</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
And now you can launch your web browser and type <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">http://localhost:5000</code>to see this tiny application in action.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Simple, right? Now we will convert this app into our RESTful service!</div>
<h2 style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 25.2000007629395px; font-weight: normal; line-height: 1.1; margin-bottom: 10px; margin-top: 16px;">
Implementing RESTful services in Python and Flask</h2>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Building web services with Flask is surprisingly simple, much simpler than building complete server side applications like the one I built in the <a href="http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world" style="background-color: transparent; box-sizing: border-box; color: #6666aa; text-decoration: none;">Mega-Tutorial</a>.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
There are a couple of Flask extensions that help with building RESTful services with Flask, but the task is so simple that in my opinion there is no need to use an extension.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
The clients of our web service will be asking the service to add, remove and modify tasks, so clearly we need to have a way to store tasks. The obvious way to do that is to build a small database, but because databases are not the topic of this article we are going to take a much simpler approach. To learn about proper use of databases with Flask once again I recommend that you read my<a href="http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world" style="background-color: transparent; box-sizing: border-box; color: #6666aa; text-decoration: none;">Mega-Tutorial</a>.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
In place of a database we will store our task list in a memory structure. This will only work when the web server that runs our application is single process and single threaded. This is okay for Flask's own development web server. It is not okay to use this technique on a production web server, for that a proper database setup must be used.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Using the base Flask application we are now ready to implement the first entry point of our web service:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="com" style="box-sizing: border-box; color: #8e908c;">#!flask/bin/python</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">from</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> flask </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">import</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Flask</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> jsonify
app </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Flask</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">__name__</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
tasks </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">'id'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">1</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">'title'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> u</span><span class="str" style="box-sizing: border-box; color: #718c00;">'Buy groceries'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">'description'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> u</span><span class="str" style="box-sizing: border-box; color: #718c00;">'Milk, Cheese, Pizza, Fruit, Tylenol'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">'done'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">False</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">},</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">'id'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">2</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">'title'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> u</span><span class="str" style="box-sizing: border-box; color: #718c00;">'Learn Python'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">'description'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> u</span><span class="str" style="box-sizing: border-box; color: #718c00;">'Need to find a good Python tutorial on the web'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">'done'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">False</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">}</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">@app</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">route</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="str" style="box-sizing: border-box; color: #718c00;">'/todo/api/v1.0/tasks'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> methods</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=[</span><span class="str" style="box-sizing: border-box; color: #718c00;">'GET'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">])</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">def</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> get_tasks</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">():</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">return</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> jsonify</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">({</span><span class="str" style="box-sizing: border-box; color: #718c00;">'tasks'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> tasks</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">})</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">if</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> __name__ </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">==</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'__main__'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
app</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">run</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">debug</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">True</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
As you can see, not much has changed. We created a memory database of tasks, which is nothing more than a plain and simple array of dictionaries. Each entry in the array has the fields that we defined above for our tasks.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Instead of the <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">index</code> entry point we now have a <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">get_tasks</code> function that is associated with the <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">/todo/api/v1.0/tasks</code> URI, and only for the <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">GET</code> HTTP method.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
The response of this function is not text, we are now replying with JSON data, which Flask's <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">jsonify</code> function generates for us from our data structure.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Using a web browser to test a web service isn't the best idea since web browsers cannot easily generate all types of HTTP requests. Instead, we will use <a href="http://curl.haxx.se/" style="background-color: transparent; box-sizing: border-box; color: #6666aa; text-decoration: none;">curl</a>. If you don't have <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">curl</code> installed, go ahead and install it now.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Start the web service in the same way we started the sample application, by running <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">app.py</code>. Then open a new console window and run the following command:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">$ curl </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">i http</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="com" style="box-sizing: border-box; color: #8e908c;">//localhost:5000/todo/api/v1.0/tasks</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
HTTP</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">1.0</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">200</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> OK
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Content</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Type</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> application</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Content</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Length</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">294</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Server</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Werkzeug</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0.8</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">3</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Python</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">2.7</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">3</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Date</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Mon</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">20</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">May</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">2013</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">04</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">53</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">53</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> GMT
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"tasks"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"description"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"Milk, Cheese, Pizza, Fruit, Tylenol"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"done"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">false</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"id"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">1</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"title"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"Buy groceries"</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">},</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"description"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"Need to find a good Python tutorial on the web"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"done"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">false</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"id"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">2</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"title"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"Learn Python"</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">}</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">}</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
We just have invoked a function in our RESTful service!</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Now let's write the second version of the GET method for our tasks resource. If you look at the table above this will be the one that is used to return the data of a single task:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="kwd" style="box-sizing: border-box; color: #8959a8;">from</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> flask </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">import</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> abort
</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">@app</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">route</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="str" style="box-sizing: border-box; color: #718c00;">'/todo/api/v1.0/tasks/<int:task_id>'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> methods</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=[</span><span class="str" style="box-sizing: border-box; color: #718c00;">'GET'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">])</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">def</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> get_task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">task_id</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">):</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
task </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">task </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">for</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> task </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">in</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> tasks </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">if</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="str" style="box-sizing: border-box; color: #718c00;">'id'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">==</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> task_id</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">if</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> len</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">==</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
abort</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">404</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">return</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> jsonify</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">({</span><span class="str" style="box-sizing: border-box; color: #718c00;">'task'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]})</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
This second function is a little bit more interesting. Here we get the id of the task in the URL, and Flask translates it into the <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">task_id</code> argument that we receive in the function.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
With this argument we search our <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">tasks</code> array. If the id that we were given does not exist in our database then we return the familiar error code 404, which according to the HTTP specification means "Resource Not Found", which is exactly our case.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
If we find the task then we just package it as JSON with <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">jsonify</code> and send it as a response, just like we did before for the entire collection.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Here is how this function looks when invoked from <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">curl</code>:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">$ curl </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">i http</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="com" style="box-sizing: border-box; color: #8e908c;">//localhost:5000/todo/api/v1.0/tasks/2</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
HTTP</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">1.0</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">200</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> OK
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Content</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Type</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> application</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Content</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Length</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">151</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Server</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Werkzeug</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0.8</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">3</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Python</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">2.7</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">3</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Date</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Mon</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">20</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">May</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">2013</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">05</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">21</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">50</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> GMT
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"task"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"description"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"Need to find a good Python tutorial on the web"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"done"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">false</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"id"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">2</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"title"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"Learn Python"</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">}</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">}</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
$ curl </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">i http</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="com" style="box-sizing: border-box; color: #8e908c;">//localhost:5000/todo/api/v1.0/tasks/3</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
HTTP</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">1.0</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">404</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> NOT FOUND
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Content</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Type</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> text</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">html
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Content</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Length</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">238</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Server</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Werkzeug</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0.8</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">3</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Python</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">2.7</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">3</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Date</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Mon</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">20</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">May</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">2013</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">05</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">21</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">52</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> GMT
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;"><!</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">DOCTYPE HTML PUBLIC </span><span class="str" style="box-sizing: border-box; color: #718c00;">"-//W3C//DTD HTML 3.2 Final//EN"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">></span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;"><title></span><span class="lit" style="box-sizing: border-box; color: #f5871f;">404</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Not</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Found</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;"><</span><span class="str" style="box-sizing: border-box; color: #718c00;">/title>
<h1>Not Found</</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">h1</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">></span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;"><p></span><span class="typ" style="box-sizing: border-box; color: #4271ae;">The</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> requested URL was </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">not</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> found on the server</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">p</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">><</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">p</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">></span><span class="typ" style="box-sizing: border-box; color: #4271ae;">If</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> you entered the URL manually please check your spelling </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">and</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">try</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> again</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">p</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">></span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
When we ask for resource id #2 we get it, but when we ask for #3 we get back the 404 error. The odd thing about the error is that it came back with an HTML message instead of JSON, because that is how Flask generates the 404 response by default. Since this is a web service client applications will expect that we always respond with JSON, so we need to improve our 404 error handler:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="kwd" style="box-sizing: border-box; color: #8959a8;">from</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> flask </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">import</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> make_response
</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">@app</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">errorhandler</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">404</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">def</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> not_found</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">error</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">):</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">return</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> make_response</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">jsonify</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">({</span><span class="str" style="box-sizing: border-box; color: #718c00;">'error'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'Not found'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">}),</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">404</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
And we get a much more API friendly error response:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">$ curl </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">i http</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="com" style="box-sizing: border-box; color: #8e908c;">//localhost:5000/todo/api/v1.0/tasks/3</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
HTTP</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">1.0</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">404</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> NOT FOUND
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Content</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Type</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> application</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Content</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Length</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">26</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Server</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Werkzeug</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0.8</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">3</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Python</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">2.7</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">3</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Date</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Mon</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">20</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">May</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">2013</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">05</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">36</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">54</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> GMT
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"error"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"Not found"</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">}</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Next in our list is the <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">POST</code> method, which we will use to insert a new item in our task database:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="kwd" style="box-sizing: border-box; color: #8959a8;">from</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> flask </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">import</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> request
</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">@app</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">route</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="str" style="box-sizing: border-box; color: #718c00;">'/todo/api/v1.0/tasks'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> methods</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=[</span><span class="str" style="box-sizing: border-box; color: #718c00;">'POST'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">])</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">def</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> create_task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">():</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">if</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">not</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> request</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">or</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">not</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'title'</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">in</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> request</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
abort</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">400</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
task </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">'id'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> tasks</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[-</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">1</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">][</span><span class="str" style="box-sizing: border-box; color: #718c00;">'id'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">+</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">1</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">'title'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> request</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="str" style="box-sizing: border-box; color: #718c00;">'title'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">],</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">'description'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> request</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">get</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="str" style="box-sizing: border-box; color: #718c00;">'description'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">""</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">),</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">'done'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">False</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">}</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
tasks</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">append</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">return</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> jsonify</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">({</span><span class="str" style="box-sizing: border-box; color: #718c00;">'task'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">}),</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">201</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Adding a new task is also pretty easy. The <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">request.json</code> will have the request data, but only if it came marked as JSON. If the data isn't there, or if it is there, but we are missing a <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">title</code> item then we return an error code 400, which is the code for the bad request.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
We then create a new task dictionary, using the id of the last task plus one (a cheap way to guarantee unique ids in our simple database). We tolerate a missing <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">description</code> field, and we assume the <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">done</code> field will always start set to <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">False</code>.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
We append the new task to our <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">tasks</code> array, and then respond to the client with the added task and send back a status code 201, which HTTP defines as the code for "Created".</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
To test this new function we can use the following <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">curl</code> command:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">$ curl </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">i </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">H </span><span class="str" style="box-sizing: border-box; color: #718c00;">"Content-Type: application/json"</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">X POST </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">d </span><span class="str" style="box-sizing: border-box; color: #718c00;">'{"title":"Read a book"}'</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> http</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="com" style="box-sizing: border-box; color: #8e908c;">//localhost:5000/todo/api/v1.0/tasks</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
HTTP</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">1.0</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">201</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Created</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Content</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Type</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> application</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Content</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Length</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">104</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Server</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Werkzeug</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0.8</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">3</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Python</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">2.7</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">3</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Date</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Mon</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">20</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">May</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">2013</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">05</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">56</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">21</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> GMT
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"task"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"description"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">""</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"done"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">false</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"id"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">3</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"title"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"Read a book"</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">}</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">}</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Note: if you are on Windows and use the Cygwin version of <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">curl</code> from <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">bash</code>then the above command will work just fine. However, if you are using the native version of <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">curl</code> from the regular command prompt there is a little dance that needs to be done to send double quotes inside the body of a request:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">curl </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">i </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">H </span><span class="str" style="box-sizing: border-box; color: #718c00;">"Content-Type: application/json"</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">X POST </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">d </span><span class="str" style="box-sizing: border-box; color: #718c00;">"{"""</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">title</span><span class="str" style="box-sizing: border-box; color: #718c00;">""":"""</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Read</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> a book</span><span class="str" style="box-sizing: border-box; color: #718c00;">"""}"</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> http</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="com" style="box-sizing: border-box; color: #8e908c;">//localhost:5000/todo/api/v1.0/tasks</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Essentially on Windows you have to use double quotes to enclose the body of the request, and then inside it you escape a double quote by writing three of them in sequence.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Of course after this request completed we can obtain the updated list of tasks:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">$ curl </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">i http</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="com" style="box-sizing: border-box; color: #8e908c;">//localhost:5000/todo/api/v1.0/tasks</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
HTTP</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">1.0</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">200</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> OK
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Content</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Type</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> application</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Content</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Length</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">423</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Server</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Werkzeug</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0.8</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">3</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Python</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">2.7</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">3</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Date</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Mon</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">20</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">May</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">2013</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">05</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">57</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">44</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> GMT
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"tasks"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"description"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"Milk, Cheese, Pizza, Fruit, Tylenol"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"done"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">false</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"id"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">1</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"title"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"Buy groceries"</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">},</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"description"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"Need to find a good Python tutorial on the web"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"done"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">false</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"id"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">2</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"title"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"Learn Python"</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">},</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"description"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">""</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"done"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">false</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"id"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">3</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"title"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"Read a book"</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">}</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">}</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
The remaining two functions of our web service are shown below:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="lit" style="box-sizing: border-box; color: #f5871f;">@app</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">route</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="str" style="box-sizing: border-box; color: #718c00;">'/todo/api/v1.0/tasks/<int:task_id>'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> methods</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=[</span><span class="str" style="box-sizing: border-box; color: #718c00;">'PUT'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">])</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">def</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> update_task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">task_id</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">):</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
task </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">task </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">for</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> task </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">in</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> tasks </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">if</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="str" style="box-sizing: border-box; color: #718c00;">'id'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">==</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> task_id</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">if</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> len</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">==</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
abort</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">404</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">if</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">not</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> request</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
abort</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">400</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">if</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'title'</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">in</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> request</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">and</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> type</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">request</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="str" style="box-sizing: border-box; color: #718c00;">'title'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">])</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">!=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> unicode</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
abort</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">400</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">if</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'description'</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">in</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> request</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">and</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> type</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">request</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="str" style="box-sizing: border-box; color: #718c00;">'description'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">])</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">is</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">not</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> unicode</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
abort</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">400</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">if</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'done'</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">in</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> request</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">and</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> type</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">request</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="str" style="box-sizing: border-box; color: #718c00;">'done'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">])</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">is</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">not</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">bool</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
abort</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">400</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">][</span><span class="str" style="box-sizing: border-box; color: #718c00;">'title'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> request</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">get</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="str" style="box-sizing: border-box; color: #718c00;">'title'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">][</span><span class="str" style="box-sizing: border-box; color: #718c00;">'title'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">])</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">][</span><span class="str" style="box-sizing: border-box; color: #718c00;">'description'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> request</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">get</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="str" style="box-sizing: border-box; color: #718c00;">'description'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">][</span><span class="str" style="box-sizing: border-box; color: #718c00;">'description'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">])</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">][</span><span class="str" style="box-sizing: border-box; color: #718c00;">'done'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> request</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">get</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="str" style="box-sizing: border-box; color: #718c00;">'done'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">][</span><span class="str" style="box-sizing: border-box; color: #718c00;">'done'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">])</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">return</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> jsonify</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">({</span><span class="str" style="box-sizing: border-box; color: #718c00;">'task'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]})</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">@app</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">route</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="str" style="box-sizing: border-box; color: #718c00;">'/todo/api/v1.0/tasks/<int:task_id>'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> methods</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=[</span><span class="str" style="box-sizing: border-box; color: #718c00;">'DELETE'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">])</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">def</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> delete_task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">task_id</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">):</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
task </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">task </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">for</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> task </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">in</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> tasks </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">if</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="str" style="box-sizing: border-box; color: #718c00;">'id'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">==</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> task_id</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">if</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> len</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">==</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
abort</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">404</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
tasks</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">remove</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">])</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">return</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> jsonify</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">({</span><span class="str" style="box-sizing: border-box; color: #718c00;">'result'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">True</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">})</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
The <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">delete_task</code> function should have no surprises. For the <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">update_task</code>function we are trying to prevent bugs by doing exhaustive checking of the input arguments. We need to make sure that anything that the client provided us is in the expected format before we incorporate it into our database.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
A function call that updates task #2 as being done would be done as follows:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">$ curl </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">i </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">H </span><span class="str" style="box-sizing: border-box; color: #718c00;">"Content-Type: application/json"</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">X PUT </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">d </span><span class="str" style="box-sizing: border-box; color: #718c00;">'{"done":true}'</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> http</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="com" style="box-sizing: border-box; color: #8e908c;">//localhost:5000/todo/api/v1.0/tasks/2</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
HTTP</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">1.0</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">200</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> OK
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Content</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Type</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> application</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Content</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Length</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">170</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Server</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Werkzeug</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0.8</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">3</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Python</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">2.7</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">3</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Date</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Mon</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">20</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">May</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">2013</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">07</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">10</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">16</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> GMT
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"task"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"description"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"Need to find a good Python tutorial on the web"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"done"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">true</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"id"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">2</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"title"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"Learn Python"</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">}</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">}</span></code></pre>
<h2 style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 25.2000007629395px; font-weight: normal; line-height: 1.1; margin-bottom: 10px; margin-top: 16px;">
Improving the web service interface</h2>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
The problem with the current design of the API is that clients are forced to construct URIs from the task identifiers that are returned. This is pretty easy in itself, but it indirectly forces clients to know how these URIs need to be built, and this will prevent us from making changes to URIs in the future.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Instead of returning task ids we can return the full URI that controls the task, so that clients get the URIs ready to be used. For this we can write a small helper function that generates a "public" version of a task to send to the client:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="kwd" style="box-sizing: border-box; color: #8959a8;">from</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> flask </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">import</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> url_for
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">def</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> make_public_task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">):</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
new_task </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{}</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">for</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> field </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">in</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">if</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> field </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">==</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'id'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
new_task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="str" style="box-sizing: border-box; color: #718c00;">'uri'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> url_for</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="str" style="box-sizing: border-box; color: #718c00;">'get_task'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> task_id</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="str" style="box-sizing: border-box; color: #718c00;">'id'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">],</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> _external</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">True</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">else</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
new_task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">field</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">field</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">return</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> new_task</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
All we are doing here is taking a task from our database and creating a new task that has all the fields except <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">id</code>, which gets replaced with another field called<code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">uri</code>, generated with Flask's <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">url_for</code>.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
When we return the list of tasks we pass them through this function before sending them to the client:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="lit" style="box-sizing: border-box; color: #f5871f;">@app</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">route</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="str" style="box-sizing: border-box; color: #718c00;">'/todo/api/v1.0/tasks'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> methods</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=[</span><span class="str" style="box-sizing: border-box; color: #718c00;">'GET'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">])</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">def</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> get_tasks</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">():</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">return</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> jsonify</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">({</span><span class="str" style="box-sizing: border-box; color: #718c00;">'tasks'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">make_public_task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">task</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">for</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> task </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">in</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> tasks</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]})</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
So now this is what the client gets when it retrieves the list of tasks:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">$ curl </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">i http</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="com" style="box-sizing: border-box; color: #8e908c;">//localhost:5000/todo/api/v1.0/tasks</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
HTTP</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">1.0</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">200</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> OK
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Content</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Type</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> application</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Content</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Length</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">406</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Server</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Werkzeug</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0.8</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">3</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Python</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">2.7</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">3</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Date</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Mon</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">20</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">May</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">2013</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">18</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">16</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">28</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> GMT
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"tasks"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"title"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"Buy groceries"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"done"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">false</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"description"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"Milk, Cheese, Pizza, Fruit, Tylenol"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"uri"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"http://localhost:5000/todo/api/v1.0/tasks/1"</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">},</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"title"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"Learn Python"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"done"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">false</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"description"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"Need to find a good Python tutorial on the web"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"uri"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"http://localhost:5000/todo/api/v1.0/tasks/2"</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">}</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">}</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
We apply this technique to all the other functions and with this we ensure that the client always sees URIs instead of ids.</div>
<h2 style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 25.2000007629395px; font-weight: normal; line-height: 1.1; margin-bottom: 10px; margin-top: 16px;">
Securing a RESTful web service</h2>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Can you believe we are done? Well, we are done with the functionality of our service, but we still have a problem. Our service is open to anybody, and that is a bad thing.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
We have a complete web service that can manage our to do list, but the service in its current state is open to any clients. If a stranger figures out how our API works he or she can write a new client that can access our service and mess with our data.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Most entry level tutorials ignore security and stop here. In my opinion this is a serious problem that should always be addressed.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
The easiest way to secure our web service is to require clients to provide a username and a password. In a regular web application you would have a login form that posts the credentials, and at that point the server would create a session for the logged in user to continue working, with the session id stored in a cookie in the client browser. Unfortunately doing that here would violate the stateless requirement of REST, so instead we have to ask clients to send their authentication information with every request they send to us.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
With REST we always try to adhere to the HTTP protocol as much as we can. Now that we need to implement authentication we should do so in the context of HTTP, which provides two forms of authentication called <a href="http://en.wikipedia.org/wiki/Basic_access_authentication" style="background-color: transparent; box-sizing: border-box; color: #6666aa; text-decoration: none;">Basic</a> and <a href="http://en.wikipedia.org/wiki/Digest_access_authentication" style="background-color: transparent; box-sizing: border-box; color: #6666aa; text-decoration: none;">Digest</a>.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
There is a small Flask extension that can help with this, written by no other than yours truly. So let's go ahead and install <a href="https://github.com/miguelgrinberg/flask-httpauth" style="background-color: transparent; box-sizing: border-box; color: #6666aa; text-decoration: none;">Flask-HTTPAuth</a>:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">$ flask</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">bin</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">pip install flask</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">httpauth</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Let's say we want our web service to only be accessible to username <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">miguel</code>and password <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">python</code>. We can setup a Basic HTTP authentication as follows:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="kwd" style="box-sizing: border-box; color: #8959a8;">from</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> flask</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">ext</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">httpauth </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">import</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">HTTPBasicAuth</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
auth </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">HTTPBasicAuth</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">()</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">@auth</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">get_password
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">def</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> get_password</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">username</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">):</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">if</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> username </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">==</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'miguel'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">return</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'python'</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">return</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">None</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">@auth</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">error_handler
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">def</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> unauthorized</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">():</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">return</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> make_response</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">jsonify</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">({</span><span class="str" style="box-sizing: border-box; color: #718c00;">'error'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'Unauthorized access'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">}),</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">401</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
The <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">get_password</code> function is a callback function that the extension will use to obtain the password for a given user. In a more complex system this function could check a user database, but in this case we just have a single user so there is no need for that.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
The <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">error_handler</code> callback will be used by the extension when it needs to send the unauthorized error code back to the client. Like we did with other error codes, here we customize the response so that is contains JSON instead of HTML.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
With the authentication system setup, all that is left is to indicate which functions need to be protected, by adding the <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">@auth.login_required</code> decorator. For example:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="lit" style="box-sizing: border-box; color: #f5871f;">@app</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">route</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="str" style="box-sizing: border-box; color: #718c00;">'/todo/api/v1.0/tasks'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> methods</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=[</span><span class="str" style="box-sizing: border-box; color: #718c00;">'GET'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">])</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">@auth</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">login_required
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">def</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> get_tasks</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">():</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">return</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> jsonify</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">({</span><span class="str" style="box-sizing: border-box; color: #718c00;">'tasks'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> tasks</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">})</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
If we now try to invoke this function with <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">curl</code> this is what we get:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">$ curl </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">i http</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="com" style="box-sizing: border-box; color: #8e908c;">//localhost:5000/todo/api/v1.0/tasks</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
HTTP</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">1.0</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">401</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> UNAUTHORIZED
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Content</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Type</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> application</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Content</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Length</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">36</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
WWW</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Authenticate</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Basic</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> realm</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">=</span><span class="str" style="box-sizing: border-box; color: #718c00;">"Authentication Required"</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Server</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Werkzeug</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0.8</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">3</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Python</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">2.7</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">3</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Date</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Mon</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">20</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">May</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">2013</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">06</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">41</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">14</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> GMT
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"error"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"Unauthorized access"</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">}</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
To be able to invoke this function we have to send our credentials:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">$ curl </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">u miguel</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">python </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">i http</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="com" style="box-sizing: border-box; color: #8e908c;">//localhost:5000/todo/api/v1.0/tasks</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
HTTP</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">1.0</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">200</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> OK
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Content</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Type</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> application</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">json
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Content</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">-</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Length</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">316</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Server</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Werkzeug</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">0.8</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">3</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Python</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">/</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">2.7</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">3</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Date</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">Mon</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">20</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="typ" style="box-sizing: border-box; color: #4271ae;">May</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">2013</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">06</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">46</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="lit" style="box-sizing: border-box; color: #f5871f;">45</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> GMT
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"tasks"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">[</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"title"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"Buy groceries"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"done"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">false</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"description"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"Milk, Cheese, Pizza, Fruit, Tylenol"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"uri"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"http://localhost:5000/todo/api/v1.0/tasks/1"</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">},</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">{</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"title"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"Learn Python"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"done"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">false</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"description"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"Need to find a good Python tutorial on the web"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">,</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="str" style="box-sizing: border-box; color: #718c00;">"uri"</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">"http://localhost:5000/todo/api/v1.0/tasks/2"</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">}</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">]</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">}</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
The authentication extension gives us the freedom to choose which functions in the service are open and which are protected.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
To ensure the login information is secure the web service should be exposed in a HTTP Secure server (i.e. https://...) as this encrypts all the communications between client and server and prevents a third party from seeing the authentication credentials in transit.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Unfortunately web browsers have the nasty habit of showing an ugly login dialog box when a request comes back with a 401 error code. This happens even for background requests, so if we were to implement a web browser client with our current web server we would need to jump through hoops to prevent browsers from showing their authentication dialogs and let our client application handle the login.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
A simple trick to distract web browsers is to return an error code other than 401. An alternative error code favored by many is 403, which is the "Forbidden" error. While this is a close enough error, it sort of violates the HTTP standard, so it is not the proper thing to do if full compliance is necessary. In particular this would be a bad idea if the client application is not a web browser. But for cases where server and client are developed together it saves a lot of trouble. The simple change that we can make to implement this trick is to replace the 401 with a 403:</div>
<pre class="prettyprint" style="background: rgb(248, 248, 248); border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace; font-size: 12px; line-height: 1.5; margin-bottom: 10px; overflow: auto; padding: 10px; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;"><span class="lit" style="box-sizing: border-box; color: #f5871f;">@auth</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">.</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">error_handler
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">def</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> unauthorized</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">():</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">
</span><span class="kwd" style="box-sizing: border-box; color: #8959a8;">return</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> make_response</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">(</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;">jsonify</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">({</span><span class="str" style="box-sizing: border-box; color: #718c00;">'error'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">:</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="str" style="box-sizing: border-box; color: #718c00;">'Unauthorized access'</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">}),</span><span class="pln" style="box-sizing: border-box; color: #4d4d4c;"> </span><span class="lit" style="box-sizing: border-box; color: #f5871f;">403</span><span class="pun" style="box-sizing: border-box; color: #4d4d4c;">)</span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Of course if we do this we will need the client application to look for 403 errors as well.</div>
<h2 style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 25.2000007629395px; font-weight: normal; line-height: 1.1; margin-bottom: 10px; margin-top: 16px;">
Possible improvements</h2>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
There are a number of ways in which this little web service we have built today can be improved.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
For starters, a real web service should be backed by a real database. The memory data structure that we are using is very limited in functionality and should not be used for a real application.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
Another area in which an improvement could be made is in handling multiple users. If the system supports multiple users the authentication credentials sent by the client could be used to obtain user specific to do lists. In such a system we would have a second resource, which would be the users. A <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">POST</code> request on the users resource would represent a new user registering for the service. A<code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">GET</code> request would return user information back to the client. A <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">PUT</code> request would update the user information, maybe updating an email address. A<code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">DELETE</code> request would delete the user account.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
The <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #dd0000; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15.1199998855591px; padding: 2px 4px;">GET</code> request that retrieves the task list could be expanded in a couple of ways. First, this request could take optional pagination arguments, so that a client can request a portion of the list. Another way to make this function more useful would be to allow filtering by certain criteria. For example, a client might want to see only completed tasks, or only tasks with a title that begins with the letter A. All these elements can be added to the URL as arguments.</div>
<h2 style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 25.2000007629395px; font-weight: normal; line-height: 1.1; margin-bottom: 10px; margin-top: 16px;">
Conclusion</h2>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
The complete code for the To Do List web service is here:<a href="https://gist.github.com/miguelgrinberg/5614326" style="background-color: transparent; box-sizing: border-box; color: #6666aa; text-decoration: none;">https://gist.github.com/miguelgrinberg/5614326</a>.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16.7999992370605px; line-height: 25.1999988555908px; margin-bottom: 20px;">
I hope this was a simple and friendly introduction to RESTful APIs. If there is enough interest I could write a second part to this article in which we can develop a simple web client that uses this service for a complete To Do List application. Let me know what you think below in the comments!</div>
<div style="background-color: white; box-sizing: border-box; margin-bottom: 20px;">
<span style="color: #333333; font-family: Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="font-size: 16.7999992370605px; line-height: 25.1999988555908px;">from http://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask</span></span></div>
Jeffyehttp://www.blogger.com/profile/07633630042179070371noreply@blogger.com0tag:blogger.com,1999:blog-5448019410873028021.post-27455110266194096892015-05-24T21:15:00.001-07:002015-05-24T21:15:28.276-07:00Resetting the Drupal administrator password with sql-query<br />
<div style="background-color: white; border: 0px; box-sizing: border-box; color: #222222; font-family: 'Lucida Grande', 'DejaVu Sans', 'Bitstream Vera Sans', Verdana, Arial, sans-serif; font-size: 13px; font-stretch: inherit; line-height: 17.9999942779541px; margin-bottom: 1.385em; padding: 0px; vertical-align: baseline;">
When, in Drupal 7, the password for user 1 (the administrator) is lost and the <a href="https://www.drupal.org/node/44164" rel="nofollow" style="border: 0px; box-sizing: border-box; color: #0678be; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">email notification or drush methods don't work</a>, it is possible to set the password via a database query.</div>
<div style="background-color: white; border: 0px; box-sizing: border-box; color: #222222; font-family: 'Lucida Grande', 'DejaVu Sans', 'Bitstream Vera Sans', Verdana, Arial, sans-serif; font-size: 13px; font-stretch: inherit; line-height: 17.9999942779541px; margin-bottom: 1.385em; padding: 0px; vertical-align: baseline;">
But first, you have to generate a password hash that is valid for your site.</div>
<div style="background-color: white; border: 0px; box-sizing: border-box; color: #222222; font-family: 'Lucida Grande', 'DejaVu Sans', 'Bitstream Vera Sans', Verdana, Arial, sans-serif; font-size: 13px; font-stretch: inherit; line-height: 17.9999942779541px; margin-bottom: 1.385em; padding: 0px; vertical-align: baseline;">
Execute the following commands from the command line, in the Drupal root directory:</div>
<div style="background-color: white; border: 0px; box-sizing: border-box; color: #222222; font-family: 'Lucida Grande', 'DejaVu Sans', 'Bitstream Vera Sans', Verdana, Arial, sans-serif; font-size: 13px; font-stretch: inherit; line-height: 17.9999942779541px; margin-bottom: 1.385em; padding: 0px; vertical-align: baseline;">
<code style="background-color: #f6f6f2; border: 0px; box-sizing: border-box; font-family: 'Bitstream Vera Sans Mono', Monaco, 'Lucida Console', monospace; font-size: 0.923em; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 1px; vertical-align: baseline; white-space: pre-wrap;"><span style="color: black; font-family: 'Times New Roman'; font-size: small; line-height: normal; white-space: normal;">go to your drupal home folder, probably /var/www/drupal-7.22</span></code></div>
<div style="background-color: white; border: 0px; box-sizing: border-box; color: #222222; font-family: 'Lucida Grande', 'DejaVu Sans', 'Bitstream Vera Sans', Verdana, Arial, sans-serif; font-size: 13px; font-stretch: inherit; line-height: 17.9999942779541px; margin-bottom: 1.385em; padding: 0px; vertical-align: baseline;">
<code style="background-color: #f6f6f2; border: 0px; box-sizing: border-box; font-family: 'Bitstream Vera Sans Mono', Monaco, 'Lucida Console', monospace; font-size: 0.923em; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 1px; vertical-align: baseline; white-space: pre-wrap;">./scripts/password-hash.sh newpwd</code></div>
<div style="background-color: white; border: 0px; box-sizing: border-box; color: #222222; font-family: 'Lucida Grande', 'DejaVu Sans', 'Bitstream Vera Sans', Verdana, Arial, sans-serif; font-size: 13px; font-stretch: inherit; line-height: 17.9999942779541px; margin-bottom: 1.385em; padding: 0px; vertical-align: baseline;">
Check the first line of this script. It will read something like <code style="background-color: #f6f6f2; border: 0px; box-sizing: border-box; font-family: 'Bitstream Vera Sans Mono', Monaco, 'Lucida Console', monospace; font-size: 0.923em; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 1px; vertical-align: baseline; white-space: pre-wrap;">#!/usr/bin/php</code>. Confirm that the filename listed in this line is located on your machine. Typically if <code style="background-color: #f6f6f2; border: 0px; box-sizing: border-box; font-family: 'Bitstream Vera Sans Mono', Monaco, 'Lucida Console', monospace; font-size: 0.923em; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 1px; vertical-align: baseline; white-space: pre-wrap;">/usr/bin/php</code> is not available, then <code style="background-color: #f6f6f2; border: 0px; box-sizing: border-box; font-family: 'Bitstream Vera Sans Mono', Monaco, 'Lucida Console', monospace; font-size: 0.923em; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 1px; vertical-align: baseline; white-space: pre-wrap;">/usr/local/bin/php</code> will be.</div>
<div style="background-color: white; border: 0px; box-sizing: border-box; color: #222222; font-family: 'Lucida Grande', 'DejaVu Sans', 'Bitstream Vera Sans', Verdana, Arial, sans-serif; font-size: 13px; font-stretch: inherit; line-height: 17.9999942779541px; margin-bottom: 1.385em; padding: 0px; vertical-align: baseline;">
or for Windows:</div>
<br />
<blockquote style="background: url(https://www.drupal.org/sites/all/themes/bluecheese/images/blockquote-66.png) 0px 0px no-repeat white; border: 0px; box-sizing: border-box; color: #222222; font-family: 'Lucida Grande', 'DejaVu Sans', 'Bitstream Vera Sans', Verdana, Arial, sans-serif; font-size: 13px; font-stretch: inherit; line-height: 17.9999942779541px; margin: 1.384em 40px 1.384em 20px; padding: 0px 0px 0px 30px; quotes: none; vertical-align: baseline;">
</blockquote>
<br />
<div class="codeblock" style="-webkit-text-stroke-width: 0px; background-color: #f6f6f2; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #222222; font-family: 'Lucida Grande', 'DejaVu Sans', 'Bitstream Vera Sans', Verdana, Arial, sans-serif; font-size: 13px; font-stretch: inherit; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 17.9999942779541px; margin: 0px 0px 0.5em; orphans: auto; padding: 5px; text-align: start; text-indent: 0px; text-transform: none; vertical-align: baseline; white-space: normal; widows: 1; word-spacing: 0px;">
<code style="background-color: #f6f6f2; border: 0px; box-sizing: border-box; font-family: 'Bitstream Vera Sans Mono', Monaco, 'Lucida Console', monospace; font-size: 0.923em; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 1px; vertical-align: baseline; white-space: pre-wrap;">php .\scripts\password-hash.sh newpwd</code></div>
Jeffyehttp://www.blogger.com/profile/07633630042179070371noreply@blogger.com0tag:blogger.com,1999:blog-5448019410873028021.post-84095257002780004462014-09-08T17:37:00.001-07:002014-09-09T07:30:02.445-07:00concise tutorial of itertools in Python<br />
<div class="section" style="-webkit-text-stroke-width: 0px; background-color: #f4f4f4; box-sizing: border-box; color: #222222; direction: ltr; font-family: ProximaNova-Reg; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 12.8000001907349px; margin: 0px; orphans: auto; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;" title="14.5. Lab">
<div class="titlepage" style="box-sizing: border-box; direction: ltr; margin: 0px; padding: 0px;">
<div style="box-sizing: border-box; direction: ltr; margin: 0px; padding: 0px;">
<div style="box-sizing: border-box; direction: ltr; margin: 0px; padding: 0px;">
</div>
</div>
</div>
</div>
<table class="docutils field-list" frame="void" rules="none" style="background-color: white; border: 0px; color: black; font-family: 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif; font-size: 12px; line-height: 19.2000007629395px;"><tbody valign="top">
<tr class="field-odd field"><th class="field-name" style="background-color: whitesmoke; border-color: rgb(204, 204, 204); border-style: none none solid; border-width: 1px; padding: 1px 8px;">Purpose:</th><td class="field-body" style="border-color: rgb(204, 204, 204); border-style: none none solid; border-width: 1px; padding: 1px 8px;">The itertools module includes a set of functions for working with iterable (sequence-like) data sets.</td></tr>
</tbody></table>
<div class="section" style="-webkit-text-stroke-width: 0px; background-color: #f4f4f4; box-sizing: border-box; color: #222222; direction: ltr; font-family: ProximaNova-Reg; font-size: 16px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 12.8000001907349px; margin: 0px; orphans: auto; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;" title="14.4. Iteration Helpers: itertools">
<div class="titlepage" style="box-sizing: border-box; direction: ltr; font-weight: normal; margin: 0px; padding: 0px;">
<div style="box-sizing: border-box; direction: ltr; margin: 0px; padding: 0px;">
<div style="box-sizing: border-box; direction: ltr; margin: 0px; padding: 0px;">
<h3 class="title" style="box-sizing: border-box; color: #5e7e90; direction: ltr; font-family: ProximaNova-Bold; font-size: 1em; font-style: normal; font-weight: normal; line-height: 19.2000007629395px; margin: 20px 0px 10px; padding: 8px 0px; text-rendering: optimizelegibility;">
Iteration Helpers: <code class="literal" style="background: none; box-sizing: border-box; color: #7f0a0c; display: inline; font-family: Courier, monospace; font-size: 1em; font-weight: bold; line-height: 16px; margin: 0px; padding: 0px;">itertools</code></h3>
</div>
</div>
</div>
<div style="-webkit-font-smoothing: antialiased; box-sizing: border-box; direction: ltr; font-family: inherit; font-size: 0.9em; font-weight: normal; line-height: 18px; margin: 0px; padding: 8px 0px; text-rendering: optimizelegibility;">
Iteration is a big part of the flow of Python and aside from the builtin syntax, there are some handy tools in the itertools package to make things easier. They also tend to make things run faster.</div>
<div class="section" style="box-sizing: border-box; direction: ltr; font-weight: normal; margin: 0px; padding: 0px;" title="14.4.1. chain()">
<div class="titlepage" style="box-sizing: border-box; direction: ltr; margin: 0px; padding: 0px;">
<div style="box-sizing: border-box; direction: ltr; margin: 0px; padding: 0px;">
<div style="box-sizing: border-box; direction: ltr; margin: 0px; padding: 0px;">
<h4 class="title" style="box-sizing: border-box; color: #222222; direction: ltr; float: left; font-family: ProximaNova-CondSemibold; font-size: 0.75em; font-style: normal; font-weight: normal; line-height: 14.3999996185303px; margin: 20px 0px 10px; padding: 0px; text-rendering: optimizelegibility; text-transform: uppercase; width: 516px;">
<a href="https://www.blogger.com/null" id="_literal_chain_literal" style="box-sizing: border-box; color: black; cursor: pointer; line-height: inherit; text-decoration: underline;"></a>14.4.1. <code class="literal" style="background: none; box-sizing: border-box; color: #7f0a0c; display: inline; font-family: Courier, monospace; font-size: 1em; font-weight: bold; line-height: 12px; margin: 0px; padding: 0px;">CHAIN()</code></h4>
</div>
</div>
</div>
<div style="-webkit-font-smoothing: antialiased; box-sizing: border-box; direction: ltr; font-family: inherit; font-size: 0.9em; font-weight: normal; line-height: 18px; margin: 0px; padding: 8px 0px; text-rendering: optimizelegibility;">
The <code class="literal" style="background: none; box-sizing: border-box; color: #7f0a0c; display: inline; font-family: Courier, monospace; font-size: 1em; font-weight: bold; line-height: 14.3999996185303px; margin: 0px; padding: 0px;">chain()</code>method accepts an arbitrary number of iterable objects as arguments and returns an iterator that will iterate over each iterable in turn. Once the first is exhausted, it will move onto the next.</div>
<div style="-webkit-font-smoothing: antialiased; box-sizing: border-box; direction: ltr; font-family: inherit; font-size: 0.9em; font-weight: normal; line-height: 18px; margin: 0px; padding: 8px 0px; text-rendering: optimizelegibility;">
Without the <code class="literal" style="background: none; box-sizing: border-box; color: #7f0a0c; display: inline; font-family: Courier, monospace; font-size: 1em; font-weight: bold; line-height: 14.3999996185303px; margin: 0px; padding: 0px;">chain()</code> function, iterating over two lists would require creating a copy with the contents of both or adding the contents of one to the other.</div>
<pre class="screen" style="box-sizing: border-box; direction: ltr; font-family: monospace, serif; font-size: 1em; margin: 0px; padding: 0px; white-space: pre-wrap;">>>> l1 = ['a', 'b', 'c']
>>> l2 = ['d', 'e', 'f']
>>> l1.extend(l2)
>>> l1
['a', 'b', 'c', 'd', 'e', 'f']</pre>
<div style="-webkit-font-smoothing: antialiased; box-sizing: border-box; direction: ltr; font-family: inherit; font-size: 0.9em; font-weight: normal; line-height: 18px; margin: 0px; padding: 8px 0px; text-rendering: optimizelegibility;">
It?s much more efficient to use the <code class="literal" style="background: none; box-sizing: border-box; color: #7f0a0c; display: inline; font-family: Courier, monospace; font-size: 1em; font-weight: bold; line-height: 14.3999996185303px; margin: 0px; padding: 0px;">chain()</code> function which only allocates additional storage for some housekeeping data in the iterator itself.</div>
<pre class="screen" style="box-sizing: border-box; direction: ltr; font-family: monospace, serif; font-size: 1em; margin: 0px; padding: 0px; white-space: pre-wrap;">>>> import itertools
>>> l1 = ['a', 'b', 'c']
>>> l2 = ['d', 'e', 'f']
>>> chained = itertools.chain(l1, l2)
>>> chained
<itertools.chain object at 0x100431250>
>>> [l for l in chained]
['a', 'b', 'c', 'd', 'e', 'f']</pre>
</div>
<div class="section" style="box-sizing: border-box; direction: ltr; margin: 0px; padding: 0px;" title="14.4.2. izip()">
<div class="titlepage" style="box-sizing: border-box; direction: ltr; font-weight: normal; margin: 0px; padding: 0px;">
<div style="box-sizing: border-box; direction: ltr; margin: 0px; padding: 0px;">
<div style="box-sizing: border-box; direction: ltr; margin: 0px; padding: 0px;">
<h4 class="title" style="box-sizing: border-box; color: #222222; direction: ltr; float: left; font-family: ProximaNova-CondSemibold; font-size: 0.75em; font-style: normal; font-weight: normal; line-height: 14.3999996185303px; margin: 20px 0px 10px; padding: 0px; text-rendering: optimizelegibility; text-transform: uppercase; width: 516px;">
<a href="https://www.blogger.com/null" id="_literal_izip_literal" style="box-sizing: border-box; color: black; cursor: pointer; line-height: inherit; text-decoration: underline;"></a>14.4.2. <code class="literal" style="background: none; box-sizing: border-box; color: #7f0a0c; display: inline; font-family: Courier, monospace; font-size: 1em; font-weight: bold; line-height: 12px; margin: 0px; padding: 0px;">IZIP()</code></h4>
</div>
</div>
</div>
<div style="-webkit-font-smoothing: antialiased; box-sizing: border-box; direction: ltr; font-family: inherit; font-size: 0.9em; font-weight: normal; line-height: 18px; margin: 0px; padding: 8px 0px; text-rendering: optimizelegibility;">
<code class="literal" style="background: none; box-sizing: border-box; color: #7f0a0c; display: inline; font-family: Courier, monospace; font-size: 1em; font-weight: bold; line-height: 14.3999996185303px; margin: 0px; padding: 0px;">izip()</code> is almost identical to the <code class="literal" style="background: none; box-sizing: border-box; color: #7f0a0c; display: inline; font-family: Courier, monospace; font-size: 1em; font-weight: bold; line-height: 14.3999996185303px; margin: 0px; padding: 0px;">zip()</code> builtin, in that it pairs up the contents of two lists into an iterable of 2-tuples. However, where <code class="literal" style="background: none; box-sizing: border-box; color: #7f0a0c; display: inline; font-family: Courier, monospace; font-size: 1em; font-weight: bold; line-height: 14.3999996185303px; margin: 0px; padding: 0px;">zip()</code> allocates a new list, <code class="literal" style="background: none; box-sizing: border-box; color: #7f0a0c; display: inline; font-family: Courier, monospace; font-size: 1em; font-weight: bold; line-height: 14.3999996185303px; margin: 0px; padding: 0px;">izip()</code> only returns an iterator.</div>
<pre class="screen" style="box-sizing: border-box; direction: ltr; font-family: monospace, serif; font-size: 1em; font-weight: normal; margin: 0px; padding: 0px; white-space: pre-wrap;">>>> name = ['Jimmy', 'Robert', 'John Paul', 'John']
>>> instruments = ['Guitar', 'Vocals', 'Bass', 'Drums']
>>> zepp = zip(name, instruments)
>>> zepp
[('Jimmy', 'Guitar'), ('Robert', 'Vocals'), ('John Paul', 'Bass'), ('John', 'Drums')]
>>> zepp = itertools.izip(name, instruments)
>>> zepp
<itertools.izip object at 0x100430998>
>>> [musician for musician in zepp]
[('Jimmy', 'Guitar'), ('Robert', 'Vocals'), ('John Paul', 'Bass'), ('John', 'Drums')]</pre>
<pre class="screen" style="box-sizing: border-box; direction: ltr; font-family: monospace, serif; font-size: 1em; font-weight: normal; margin: 0px; padding: 0px; white-space: pre-wrap;">
</pre>
<pre class="screen" style="box-sizing: border-box; direction: ltr; font-family: monospace, serif; font-size: 1em; font-weight: normal; margin: 0px; padding: 0px; white-space: pre-wrap;">
</pre>
<pre class="screen" style="box-sizing: border-box; direction: ltr; font-family: monospace, serif; font-size: 1em; margin: 0px; padding: 0px; white-space: pre-wrap;"><div style="border-bottom-width: 0px; font-weight: normal; padding-bottom: 0px;">
</div>
<h2 style="border-bottom-width: 0px; padding-bottom: 0px;">
<b>More</b></h2>
<div style="border-bottom-width: 0px; font-weight: normal; padding-bottom: 0px;">
</div>
<div style="border-bottom-width: 0px; font-weight: normal; padding-bottom: 0px;">
T<span style="font-size: 1em; line-height: 12.8000001907349px;">he functions provided are inspired by similar features of the “lazy functional programming language” Haskell and SML. They are intended to be fast and use memory efficiently, but also to be hooked together to express more complicated iteration-based algorithms.</span></div>
<div style="border-bottom-width: 0px; font-weight: normal; padding-bottom: 0px;">
Iterator-based code may be preferred over code which uses lists for several reasons. Since data is not produced from the iterator until it is needed, all of the data is not stored in memory at the same time. Reducing memory usage can reduce swapping and other side-effects of large data sets, increasing performance.</div>
<div class="section" id="merging-and-splitting-iterators" style="font-weight: normal;">
<h2 style="background-color: white; border-bottom-color: rgb(221, 221, 221); border-bottom-style: solid; border-bottom-width: 1px; color: #666666; font-family: Arial, Helvetica, sans-serif; font-size: 18px;">
Merging and Splitting Iterators<a class="headerlink" href="http://pymotw.com/2/itertools/#merging-and-splitting-iterators" style="color: #cc6714; font-size: 0.8em; padding: 0px 4px; text-decoration: none; visibility: hidden;" title="Permalink to this headline"></a></h2>
<div style="border-bottom-width: 0px; padding-bottom: 0px;">
The <tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">chain()</span></tt> function takes several iterators as arguments and returns a single iterator that produces the contents of all of them as though they came from a single sequence.</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;"><span class="kn">from</span> <span class="nn" style="color: blue; font-weight: bold;">itertools</span> <span class="kn">import</span> <span class="o" style="color: #666666;">*</span>
<span class="k" style="color: green; font-weight: bold;">for</span> <span class="n">i</span> <span class="ow" style="color: #aa22ff; font-weight: bold;">in</span> <span class="n">chain</span><span class="p">([</span><span class="mi" style="color: #666666;">1</span><span class="p">,</span> <span class="mi" style="color: #666666;">2</span><span class="p">,</span> <span class="mi" style="color: #666666;">3</span><span class="p">],</span> <span class="p">[</span><span class="s" style="color: #ba2121;">'a'</span><span class="p">,</span> <span class="s" style="color: #ba2121;">'b'</span><span class="p">,</span> <span class="s" style="color: #ba2121;">'c'</span><span class="p">]):</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="n">i</span>
</pre>
</div>
</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;">$ python itertools_chain.py
1
2
3
a
b
c
</pre>
</div>
</div>
<div style="border-bottom-width: 0px; padding-bottom: 0px;">
<tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">izip()</span></tt> returns an iterator that combines the elements of several iterators into tuples. It works like the built-in function <tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">zip()</span></tt>, except that it returns an iterator instead of a list.</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;"><span class="kn">from</span> <span class="nn" style="color: blue; font-weight: bold;">itertools</span> <span class="kn">import</span> <span class="o" style="color: #666666;">*</span>
<span class="k" style="color: green; font-weight: bold;">for</span> <span class="n">i</span> <span class="ow" style="color: #aa22ff; font-weight: bold;">in</span> <span class="n">izip</span><span class="p">([</span><span class="mi" style="color: #666666;">1</span><span class="p">,</span> <span class="mi" style="color: #666666;">2</span><span class="p">,</span> <span class="mi" style="color: #666666;">3</span><span class="p">],</span> <span class="p">[</span><span class="s" style="color: #ba2121;">'a'</span><span class="p">,</span> <span class="s" style="color: #ba2121;">'b'</span><span class="p">,</span> <span class="s" style="color: #ba2121;">'c'</span><span class="p">]):</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="n">i</span>
</pre>
</div>
</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;">$ python itertools_izip.py
(1, 'a')
(2, 'b')
(3, 'c')
</pre>
</div>
</div>
<div style="border-bottom-width: 0px; padding-bottom: 0px;">
The <tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">islice()</span></tt> function returns an iterator which returns selected items from the input iterator, by index. It takes the same arguments as the slice operator for lists: start, stop, and step. The start and step arguments are optional.</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;"><span class="kn">from</span> <span class="nn" style="color: blue; font-weight: bold;">itertools</span> <span class="kn">import</span> <span class="o" style="color: #666666;">*</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="s" style="color: #ba2121;">'Stop at 5:'</span>
<span class="k" style="color: green; font-weight: bold;">for</span> <span class="n">i</span> <span class="ow" style="color: #aa22ff; font-weight: bold;">in</span> <span class="n">islice</span><span class="p">(</span><span class="n">count</span><span class="p">(),</span> <span class="mi" style="color: #666666;">5</span><span class="p">):</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="n">i</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="s" style="color: #ba2121;">'Start at 5, Stop at 10:'</span>
<span class="k" style="color: green; font-weight: bold;">for</span> <span class="n">i</span> <span class="ow" style="color: #aa22ff; font-weight: bold;">in</span> <span class="n">islice</span><span class="p">(</span><span class="n">count</span><span class="p">(),</span> <span class="mi" style="color: #666666;">5</span><span class="p">,</span> <span class="mi" style="color: #666666;">10</span><span class="p">):</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="n">i</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="s" style="color: #ba2121;">'By tens to 100:'</span>
<span class="k" style="color: green; font-weight: bold;">for</span> <span class="n">i</span> <span class="ow" style="color: #aa22ff; font-weight: bold;">in</span> <span class="n">islice</span><span class="p">(</span><span class="n">count</span><span class="p">(),</span> <span class="mi" style="color: #666666;">0</span><span class="p">,</span> <span class="mi" style="color: #666666;">100</span><span class="p">,</span> <span class="mi" style="color: #666666;">10</span><span class="p">):</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="n">i</span>
</pre>
</div>
</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;">$ python itertools_islice.py
Stop at 5:
0
1
2
3
4
Start at 5, Stop at 10:
5
6
7
8
9
By tens to 100:
0
10
20
30
40
50
60
70
80
90
</pre>
</div>
</div>
<div style="border-bottom-width: 0px; padding-bottom: 0px;">
The <tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">tee()</span></tt> function returns several independent iterators (defaults to 2) based on a single original input. It has semantics similar to the Unix <a class="reference external" href="http://unixhelp.ed.ac.uk/CGI/man-cgi?tee" style="color: #cc6714; text-decoration: none;">tee</a> utility, which repeats the values it reads from its input and writes them to a named file and standard output.</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;"><span class="kn">from</span> <span class="nn" style="color: blue; font-weight: bold;">itertools</span> <span class="kn">import</span> <span class="o" style="color: #666666;">*</span>
<span class="n">r</span> <span class="o" style="color: #666666;">=</span> <span class="n">islice</span><span class="p">(</span><span class="n">count</span><span class="p">(),</span> <span class="mi" style="color: #666666;">5</span><span class="p">)</span>
<span class="n">i1</span><span class="p">,</span> <span class="n">i2</span> <span class="o" style="color: #666666;">=</span> <span class="n">tee</span><span class="p">(</span><span class="n">r</span><span class="p">)</span>
<span class="k" style="color: green; font-weight: bold;">for</span> <span class="n">i</span> <span class="ow" style="color: #aa22ff; font-weight: bold;">in</span> <span class="n">i1</span><span class="p">:</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="s" style="color: #ba2121;">'i1:'</span><span class="p">,</span> <span class="n">i</span>
<span class="k" style="color: green; font-weight: bold;">for</span> <span class="n">i</span> <span class="ow" style="color: #aa22ff; font-weight: bold;">in</span> <span class="n">i2</span><span class="p">:</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="s" style="color: #ba2121;">'i2:'</span><span class="p">,</span> <span class="n">i</span>
</pre>
</div>
</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;">$ python itertools_tee.py
i1: 0
i1: 1
i1: 2
i1: 3
i1: 4
i2: 0
i2: 1
i2: 2
i2: 3
i2: 4
</pre>
</div>
</div>
<div style="border-bottom-width: 0px; padding-bottom: 0px;">
Since the new iterators created by <tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">tee()</span></tt> share the input, you should not use the original iterator any more. If you do consume values from the original input, the new iterators will not produce those values:</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;"><span class="kn">from</span> <span class="nn" style="color: blue; font-weight: bold;">itertools</span> <span class="kn">import</span> <span class="o" style="color: #666666;">*</span>
<span class="n">r</span> <span class="o" style="color: #666666;">=</span> <span class="n">islice</span><span class="p">(</span><span class="n">count</span><span class="p">(),</span> <span class="mi" style="color: #666666;">5</span><span class="p">)</span>
<span class="n">i1</span><span class="p">,</span> <span class="n">i2</span> <span class="o" style="color: #666666;">=</span> <span class="n">tee</span><span class="p">(</span><span class="n">r</span><span class="p">)</span>
<span class="k" style="color: green; font-weight: bold;">for</span> <span class="n">i</span> <span class="ow" style="color: #aa22ff; font-weight: bold;">in</span> <span class="n">r</span><span class="p">:</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="s" style="color: #ba2121;">'r:'</span><span class="p">,</span> <span class="n">i</span>
<span class="k" style="color: green; font-weight: bold;">if</span> <span class="n">i</span> <span class="o" style="color: #666666;">></span> <span class="mi" style="color: #666666;">1</span><span class="p">:</span>
<span class="k" style="color: green; font-weight: bold;">break</span>
<span class="k" style="color: green; font-weight: bold;">for</span> <span class="n">i</span> <span class="ow" style="color: #aa22ff; font-weight: bold;">in</span> <span class="n">i1</span><span class="p">:</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="s" style="color: #ba2121;">'i1:'</span><span class="p">,</span> <span class="n">i</span>
<span class="k" style="color: green; font-weight: bold;">for</span> <span class="n">i</span> <span class="ow" style="color: #aa22ff; font-weight: bold;">in</span> <span class="n">i2</span><span class="p">:</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="s" style="color: #ba2121;">'i2:'</span><span class="p">,</span> <span class="n">i</span>
</pre>
</div>
</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;">$ python itertools_tee_error.py
r: 0
r: 1
r: 2
i1: 3
i1: 4
i2: 3
i2: 4
</pre>
</div>
</div>
</div>
<div class="section" id="converting-inputs" style="font-weight: normal;">
<h2 style="background-color: white; border-bottom-color: rgb(221, 221, 221); border-bottom-style: solid; border-bottom-width: 1px; color: #666666; font-family: Arial, Helvetica, sans-serif; font-size: 18px;">
Converting Inputs<a class="headerlink" href="http://pymotw.com/2/itertools/#converting-inputs" style="color: #cc6714; font-size: 0.8em; padding: 0px 4px; text-decoration: none; visibility: hidden;" title="Permalink to this headline"></a></h2>
<div style="border-bottom-width: 0px; padding-bottom: 0px;">
The <tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">imap()</span></tt> function returns an iterator that calls a function on the values in the input iterators, and returns the results. It works like the built-in <tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">map()</span></tt>, except that it stops when any input iterator is exhausted (instead of inserting <tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">None</span></tt> values to completely consume all of the inputs).</div>
<div style="border-bottom-width: 0px; padding-bottom: 0px;">
In the first example, the lambda function multiplies the input values by 2. In a second example, the lambda function multiplies 2 arguments, taken from separate iterators, and returns a tuple with the original arguments and the computed value.</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;"><span class="kn">from</span> <span class="nn" style="color: blue; font-weight: bold;">itertools</span> <span class="kn">import</span> <span class="o" style="color: #666666;">*</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="s" style="color: #ba2121;">'Doubles:'</span>
<span class="k" style="color: green; font-weight: bold;">for</span> <span class="n">i</span> <span class="ow" style="color: #aa22ff; font-weight: bold;">in</span> <span class="n">imap</span><span class="p">(</span><span class="k" style="color: green; font-weight: bold;">lambda</span> <span class="n">x</span><span class="p">:</span><span class="mi" style="color: #666666;">2</span><span class="o" style="color: #666666;">*</span><span class="n">x</span><span class="p">,</span> <span class="nb" style="color: green;">xrange</span><span class="p">(</span><span class="mi" style="color: #666666;">5</span><span class="p">)):</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="n">i</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="s" style="color: #ba2121;">'Multiples:'</span>
<span class="k" style="color: green; font-weight: bold;">for</span> <span class="n">i</span> <span class="ow" style="color: #aa22ff; font-weight: bold;">in</span> <span class="n">imap</span><span class="p">(</span><span class="k" style="color: green; font-weight: bold;">lambda</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">:(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">x</span><span class="o" style="color: #666666;">*</span><span class="n">y</span><span class="p">),</span> <span class="nb" style="color: green;">xrange</span><span class="p">(</span><span class="mi" style="color: #666666;">5</span><span class="p">),</span> <span class="nb" style="color: green;">xrange</span><span class="p">(</span><span class="mi" style="color: #666666;">5</span><span class="p">,</span><span class="mi" style="color: #666666;">10</span><span class="p">)):</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="s" style="color: #ba2121;">'</span><span class="si" style="color: #bb6688; font-weight: bold;">%d</span><span class="s" style="color: #ba2121;"> * </span><span class="si" style="color: #bb6688; font-weight: bold;">%d</span><span class="s" style="color: #ba2121;"> = </span><span class="si" style="color: #bb6688; font-weight: bold;">%d</span><span class="s" style="color: #ba2121;">'</span> <span class="o" style="color: #666666;">%</span> <span class="n">i</span>
</pre>
</div>
</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;">$ python itertools_imap.py
Doubles:
0
2
4
6
8
Multiples:
0 * 5 = 0
1 * 6 = 6
2 * 7 = 14
3 * 8 = 24
4 * 9 = 36
</pre>
</div>
</div>
<div style="border-bottom-width: 0px; padding-bottom: 0px;">
The <tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">starmap()</span></tt> function is similar to <tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">imap()</span></tt>, but instead of constructing a tuple from multiple iterators it splits up the items in a single iterator as arguments to the mapping function using the <tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">*</span></tt>syntax. Where the mapping function to imap() is called f(i1, i2), the mapping function to starmap() is called <tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">f(*i)</span></tt>.</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;"><span class="kn">from</span> <span class="nn" style="color: blue; font-weight: bold;">itertools</span> <span class="kn">import</span> <span class="o" style="color: #666666;">*</span>
<span class="n">values</span> <span class="o" style="color: #666666;">=</span> <span class="p">[(</span><span class="mi" style="color: #666666;">0</span><span class="p">,</span> <span class="mi" style="color: #666666;">5</span><span class="p">),</span> <span class="p">(</span><span class="mi" style="color: #666666;">1</span><span class="p">,</span> <span class="mi" style="color: #666666;">6</span><span class="p">),</span> <span class="p">(</span><span class="mi" style="color: #666666;">2</span><span class="p">,</span> <span class="mi" style="color: #666666;">7</span><span class="p">),</span> <span class="p">(</span><span class="mi" style="color: #666666;">3</span><span class="p">,</span> <span class="mi" style="color: #666666;">8</span><span class="p">),</span> <span class="p">(</span><span class="mi" style="color: #666666;">4</span><span class="p">,</span> <span class="mi" style="color: #666666;">9</span><span class="p">)]</span>
<span class="k" style="color: green; font-weight: bold;">for</span> <span class="n">i</span> <span class="ow" style="color: #aa22ff; font-weight: bold;">in</span> <span class="n">starmap</span><span class="p">(</span><span class="k" style="color: green; font-weight: bold;">lambda</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">:(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">x</span><span class="o" style="color: #666666;">*</span><span class="n">y</span><span class="p">),</span> <span class="n">values</span><span class="p">):</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="s" style="color: #ba2121;">'</span><span class="si" style="color: #bb6688; font-weight: bold;">%d</span><span class="s" style="color: #ba2121;"> * </span><span class="si" style="color: #bb6688; font-weight: bold;">%d</span><span class="s" style="color: #ba2121;"> = </span><span class="si" style="color: #bb6688; font-weight: bold;">%d</span><span class="s" style="color: #ba2121;">'</span> <span class="o" style="color: #666666;">%</span> <span class="n">i</span>
</pre>
</div>
</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;">$ python itertools_starmap.py
0 * 5 = 0
1 * 6 = 6
2 * 7 = 14
3 * 8 = 24
4 * 9 = 36
</pre>
</div>
</div>
</div>
<div class="section" id="producing-new-values" style="font-weight: normal;">
<h2 style="background-color: white; border-bottom-color: rgb(221, 221, 221); border-bottom-style: solid; border-bottom-width: 1px; color: #666666; font-family: Arial, Helvetica, sans-serif; font-size: 18px;">
Producing New Values<a class="headerlink" href="http://pymotw.com/2/itertools/#producing-new-values" style="color: #cc6714; font-size: 0.8em; padding: 0px 4px; text-decoration: none; visibility: hidden;" title="Permalink to this headline"></a></h2>
<div style="border-bottom-width: 0px; padding-bottom: 0px;">
The <tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">count()</span></tt> function returns an interator that produces consecutive integers, indefinitely. The first number can be passed as an argument, the default is zero. There is no upper bound argument (see the built-in <tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">xrange()</span></tt> for more control over the result set). In this example, the iteration stops because the list argument is consumed.</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;"><span class="kn">from</span> <span class="nn" style="color: blue; font-weight: bold;">itertools</span> <span class="kn">import</span> <span class="o" style="color: #666666;">*</span>
<span class="k" style="color: green; font-weight: bold;">for</span> <span class="n">i</span> <span class="ow" style="color: #aa22ff; font-weight: bold;">in</span> <span class="n">izip</span><span class="p">(</span><span class="n">count</span><span class="p">(</span><span class="mi" style="color: #666666;">1</span><span class="p">),</span> <span class="p">[</span><span class="s" style="color: #ba2121;">'a'</span><span class="p">,</span> <span class="s" style="color: #ba2121;">'b'</span><span class="p">,</span> <span class="s" style="color: #ba2121;">'c'</span><span class="p">]):</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="n">i</span>
</pre>
</div>
</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;">$ python itertools_count.py
(1, 'a')
(2, 'b')
(3, 'c')
</pre>
</div>
</div>
<div style="border-bottom-width: 0px; padding-bottom: 0px;">
The <tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">cycle()</span></tt> function returns an iterator that repeats the contents of the arguments it is given indefinitely. Since it has to remember the entire contents of the input iterator, it may consume quite a bit of memory if the iterator is long. In this example, a counter variable is used to break out of the loop after a few cycles.</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;"><span class="kn">from</span> <span class="nn" style="color: blue; font-weight: bold;">itertools</span> <span class="kn">import</span> <span class="o" style="color: #666666;">*</span>
<span class="n">i</span> <span class="o" style="color: #666666;">=</span> <span class="mi" style="color: #666666;">0</span>
<span class="k" style="color: green; font-weight: bold;">for</span> <span class="n">item</span> <span class="ow" style="color: #aa22ff; font-weight: bold;">in</span> <span class="n">cycle</span><span class="p">([</span><span class="s" style="color: #ba2121;">'a'</span><span class="p">,</span> <span class="s" style="color: #ba2121;">'b'</span><span class="p">,</span> <span class="s" style="color: #ba2121;">'c'</span><span class="p">]):</span>
<span class="n">i</span> <span class="o" style="color: #666666;">+=</span> <span class="mi" style="color: #666666;">1</span>
<span class="k" style="color: green; font-weight: bold;">if</span> <span class="n">i</span> <span class="o" style="color: #666666;">==</span> <span class="mi" style="color: #666666;">10</span><span class="p">:</span>
<span class="k" style="color: green; font-weight: bold;">break</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">item</span><span class="p">)</span>
</pre>
</div>
</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;">$ python itertools_cycle.py
(1, 'a')
(2, 'b')
(3, 'c')
(4, 'a')
(5, 'b')
(6, 'c')
(7, 'a')
(8, 'b')
(9, 'c')
</pre>
</div>
</div>
<div style="border-bottom-width: 0px; padding-bottom: 0px;">
The <tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">repeat()</span></tt> function returns an iterator that produces the same value each time it is accessed. It keeps going forever, unless the optional times argument is provided to limit it.</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;"><span class="kn">from</span> <span class="nn" style="color: blue; font-weight: bold;">itertools</span> <span class="kn">import</span> <span class="o" style="color: #666666;">*</span>
<span class="k" style="color: green; font-weight: bold;">for</span> <span class="n">i</span> <span class="ow" style="color: #aa22ff; font-weight: bold;">in</span> <span class="n">repeat</span><span class="p">(</span><span class="s" style="color: #ba2121;">'over-and-over'</span><span class="p">,</span> <span class="mi" style="color: #666666;">5</span><span class="p">):</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="n">i</span>
</pre>
</div>
</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;">$ python itertools_repeat.py
over-and-over
over-and-over
over-and-over
over-and-over
over-and-over
</pre>
</div>
</div>
<div style="border-bottom-width: 0px; padding-bottom: 0px;">
It is useful to combine <tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">repeat()</span></tt> with <tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">izip()</span></tt> or <tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">imap()</span></tt> when invariant values need to be included with the values from the other iterators.</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;"><span class="kn">from</span> <span class="nn" style="color: blue; font-weight: bold;">itertools</span> <span class="kn">import</span> <span class="o" style="color: #666666;">*</span>
<span class="k" style="color: green; font-weight: bold;">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">s</span> <span class="ow" style="color: #aa22ff; font-weight: bold;">in</span> <span class="n">izip</span><span class="p">(</span><span class="n">count</span><span class="p">(),</span> <span class="n">repeat</span><span class="p">(</span><span class="s" style="color: #ba2121;">'over-and-over'</span><span class="p">,</span> <span class="mi" style="color: #666666;">5</span><span class="p">)):</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="n">i</span><span class="p">,</span> <span class="n">s</span>
</pre>
</div>
</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;">$ python itertools_repeat_izip.py
0 over-and-over
1 over-and-over
2 over-and-over
3 over-and-over
4 over-and-over
</pre>
</div>
</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;"><span class="kn">from</span> <span class="nn" style="color: blue; font-weight: bold;">itertools</span> <span class="kn">import</span> <span class="o" style="color: #666666;">*</span>
<span class="k" style="color: green; font-weight: bold;">for</span> <span class="n">i</span> <span class="ow" style="color: #aa22ff; font-weight: bold;">in</span> <span class="n">imap</span><span class="p">(</span><span class="k" style="color: green; font-weight: bold;">lambda</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">:(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">x</span><span class="o" style="color: #666666;">*</span><span class="n">y</span><span class="p">),</span> <span class="n">repeat</span><span class="p">(</span><span class="mi" style="color: #666666;">2</span><span class="p">),</span> <span class="nb" style="color: green;">xrange</span><span class="p">(</span><span class="mi" style="color: #666666;">5</span><span class="p">)):</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="s" style="color: #ba2121;">'</span><span class="si" style="color: #bb6688; font-weight: bold;">%d</span><span class="s" style="color: #ba2121;"> * </span><span class="si" style="color: #bb6688; font-weight: bold;">%d</span><span class="s" style="color: #ba2121;"> = </span><span class="si" style="color: #bb6688; font-weight: bold;">%d</span><span class="s" style="color: #ba2121;">'</span> <span class="o" style="color: #666666;">%</span> <span class="n">i</span>
</pre>
</div>
</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;">$ python itertools_repeat_imap.py
2 * 0 = 0
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
</pre>
</div>
</div>
</div>
<div class="section" id="filtering" style="font-weight: normal;">
<h2 style="background-color: white; border-bottom-color: rgb(221, 221, 221); border-bottom-style: solid; border-bottom-width: 1px; color: #666666; font-family: Arial, Helvetica, sans-serif; font-size: 18px;">
Filtering<a class="headerlink" href="http://pymotw.com/2/itertools/#filtering" style="color: #cc6714; font-size: 0.8em; padding: 0px 4px; text-decoration: none; visibility: hidden;" title="Permalink to this headline"></a></h2>
<div style="border-bottom-width: 0px; padding-bottom: 0px;">
The <tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">dropwhile()</span></tt> function returns an iterator that returns elements of the input iterator after a condition becomes false for the first time. It does not filter every item of the input; after the condition is false the first time, all of the remaining items in the input are returned.</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;"><span class="kn">from</span> <span class="nn" style="color: blue; font-weight: bold;">itertools</span> <span class="kn">import</span> <span class="o" style="color: #666666;">*</span>
<span class="k" style="color: green; font-weight: bold;">def</span> <span class="nf" style="color: blue;">should_drop</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="s" style="color: #ba2121;">'Testing:'</span><span class="p">,</span> <span class="n">x</span>
<span class="k" style="color: green; font-weight: bold;">return</span> <span class="p">(</span><span class="n">x</span><span class="o" style="color: #666666;"><</span><span class="mi" style="color: #666666;">1</span><span class="p">)</span>
<span class="k" style="color: green; font-weight: bold;">for</span> <span class="n">i</span> <span class="ow" style="color: #aa22ff; font-weight: bold;">in</span> <span class="n">dropwhile</span><span class="p">(</span><span class="n">should_drop</span><span class="p">,</span> <span class="p">[</span> <span class="o" style="color: #666666;">-</span><span class="mi" style="color: #666666;">1</span><span class="p">,</span> <span class="mi" style="color: #666666;">0</span><span class="p">,</span> <span class="mi" style="color: #666666;">1</span><span class="p">,</span> <span class="mi" style="color: #666666;">2</span><span class="p">,</span> <span class="mi" style="color: #666666;">3</span><span class="p">,</span> <span class="mi" style="color: #666666;">4</span><span class="p">,</span> <span class="mi" style="color: #666666;">1</span><span class="p">,</span> <span class="o" style="color: #666666;">-</span><span class="mi" style="color: #666666;">2</span> <span class="p">]):</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="s" style="color: #ba2121;">'Yielding:'</span><span class="p">,</span> <span class="n">i</span>
</pre>
</div>
</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;">$ python itertools_dropwhile.py
Testing: -1
Testing: 0
Testing: 1
Yielding: 1
Yielding: 2
Yielding: 3
Yielding: 4
Yielding: 1
Yielding: -2
</pre>
</div>
</div>
<div style="border-bottom-width: 0px; padding-bottom: 0px;">
The opposite of <tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">dropwhile()</span></tt>, <tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">takewhile()</span></tt> returns an iterator that returns items from the input iterator as long as the test function returns true.</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;"><span class="kn">from</span> <span class="nn" style="color: blue; font-weight: bold;">itertools</span> <span class="kn">import</span> <span class="o" style="color: #666666;">*</span>
<span class="k" style="color: green; font-weight: bold;">def</span> <span class="nf" style="color: blue;">should_take</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="s" style="color: #ba2121;">'Testing:'</span><span class="p">,</span> <span class="n">x</span>
<span class="k" style="color: green; font-weight: bold;">return</span> <span class="p">(</span><span class="n">x</span><span class="o" style="color: #666666;"><</span><span class="mi" style="color: #666666;">2</span><span class="p">)</span>
<span class="k" style="color: green; font-weight: bold;">for</span> <span class="n">i</span> <span class="ow" style="color: #aa22ff; font-weight: bold;">in</span> <span class="n">takewhile</span><span class="p">(</span><span class="n">should_take</span><span class="p">,</span> <span class="p">[</span> <span class="o" style="color: #666666;">-</span><span class="mi" style="color: #666666;">1</span><span class="p">,</span> <span class="mi" style="color: #666666;">0</span><span class="p">,</span> <span class="mi" style="color: #666666;">1</span><span class="p">,</span> <span class="mi" style="color: #666666;">2</span><span class="p">,</span> <span class="mi" style="color: #666666;">3</span><span class="p">,</span> <span class="mi" style="color: #666666;">4</span><span class="p">,</span> <span class="mi" style="color: #666666;">1</span><span class="p">,</span> <span class="o" style="color: #666666;">-</span><span class="mi" style="color: #666666;">2</span> <span class="p">]):</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="s" style="color: #ba2121;">'Yielding:'</span><span class="p">,</span> <span class="n">i</span>
</pre>
</div>
</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;">$ python itertools_takewhile.py
Testing: -1
Yielding: -1
Testing: 0
Yielding: 0
Testing: 1
Yielding: 1
Testing: 2
</pre>
</div>
</div>
<div style="border-bottom-width: 0px; padding-bottom: 0px;">
<tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">ifilter()</span></tt> returns an iterator that works like the built-in <tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">filter()</span></tt> does for lists, including only items for which the test function returns true. It is different from <tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">dropwhile()</span></tt> in that every item is tested before it is returned.</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;"><span class="kn">from</span> <span class="nn" style="color: blue; font-weight: bold;">itertools</span> <span class="kn">import</span> <span class="o" style="color: #666666;">*</span>
<span class="k" style="color: green; font-weight: bold;">def</span> <span class="nf" style="color: blue;">check_item</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="s" style="color: #ba2121;">'Testing:'</span><span class="p">,</span> <span class="n">x</span>
<span class="k" style="color: green; font-weight: bold;">return</span> <span class="p">(</span><span class="n">x</span><span class="o" style="color: #666666;"><</span><span class="mi" style="color: #666666;">1</span><span class="p">)</span>
<span class="k" style="color: green; font-weight: bold;">for</span> <span class="n">i</span> <span class="ow" style="color: #aa22ff; font-weight: bold;">in</span> <span class="n">ifilter</span><span class="p">(</span><span class="n">check_item</span><span class="p">,</span> <span class="p">[</span> <span class="o" style="color: #666666;">-</span><span class="mi" style="color: #666666;">1</span><span class="p">,</span> <span class="mi" style="color: #666666;">0</span><span class="p">,</span> <span class="mi" style="color: #666666;">1</span><span class="p">,</span> <span class="mi" style="color: #666666;">2</span><span class="p">,</span> <span class="mi" style="color: #666666;">3</span><span class="p">,</span> <span class="mi" style="color: #666666;">4</span><span class="p">,</span> <span class="mi" style="color: #666666;">1</span><span class="p">,</span> <span class="o" style="color: #666666;">-</span><span class="mi" style="color: #666666;">2</span> <span class="p">]):</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="s" style="color: #ba2121;">'Yielding:'</span><span class="p">,</span> <span class="n">i</span>
</pre>
</div>
</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;">$ python itertools_ifilter.py
Testing: -1
Yielding: -1
Testing: 0
Yielding: 0
Testing: 1
Testing: 2
Testing: 3
Testing: 4
Testing: 1
Testing: -2
Yielding: -2
</pre>
</div>
</div>
<div style="border-bottom-width: 0px; padding-bottom: 0px;">
The opposite of <tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">ifilter()</span></tt>, <tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">ifilterfalse()</span></tt> returns an iterator that includes only items where the test function returns false.</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;"><span class="kn">from</span> <span class="nn" style="color: blue; font-weight: bold;">itertools</span> <span class="kn">import</span> <span class="o" style="color: #666666;">*</span>
<span class="k" style="color: green; font-weight: bold;">def</span> <span class="nf" style="color: blue;">check_item</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="s" style="color: #ba2121;">'Testing:'</span><span class="p">,</span> <span class="n">x</span>
<span class="k" style="color: green; font-weight: bold;">return</span> <span class="p">(</span><span class="n">x</span><span class="o" style="color: #666666;"><</span><span class="mi" style="color: #666666;">1</span><span class="p">)</span>
<span class="k" style="color: green; font-weight: bold;">for</span> <span class="n">i</span> <span class="ow" style="color: #aa22ff; font-weight: bold;">in</span> <span class="n">ifilterfalse</span><span class="p">(</span><span class="n">check_item</span><span class="p">,</span> <span class="p">[</span> <span class="o" style="color: #666666;">-</span><span class="mi" style="color: #666666;">1</span><span class="p">,</span> <span class="mi" style="color: #666666;">0</span><span class="p">,</span> <span class="mi" style="color: #666666;">1</span><span class="p">,</span> <span class="mi" style="color: #666666;">2</span><span class="p">,</span> <span class="mi" style="color: #666666;">3</span><span class="p">,</span> <span class="mi" style="color: #666666;">4</span><span class="p">,</span> <span class="mi" style="color: #666666;">1</span><span class="p">,</span> <span class="o" style="color: #666666;">-</span><span class="mi" style="color: #666666;">2</span> <span class="p">]):</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="s" style="color: #ba2121;">'Yielding:'</span><span class="p">,</span> <span class="n">i</span>
</pre>
</div>
</div>
<div class="highlight-python" style="margin-left: 2em; margin-right: 2em;">
<div class="highlight" style="background-color: white; border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;">$ python itertools_ifilterfalse.py
Testing: -1
Testing: 0
Testing: 1
Yielding: 1
Testing: 2
Yielding: 2
Testing: 3
Yielding: 3
Testing: 4
Yielding: 4
Testing: 1
Yielding: 1
Testing: -2
</pre>
</div>
</div>
</div>
<div class="section" id="grouping-data" style="font-weight: normal;">
<span id="itertools-groupby" style="background-color: white; color: black; font-family: 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif; font-size: 12px; line-height: 19.2000007629395px; white-space: normal;"></span><h2 style="background-color: white; border-bottom-color: rgb(221, 221, 221); border-bottom-style: solid; border-bottom-width: 1px; color: #666666; font-family: Arial, Helvetica, sans-serif; font-size: 18px; line-height: 19.2000007629395px; white-space: normal;">
Grouping Data<a class="headerlink" href="http://pymotw.com/2/itertools/#grouping-data" style="color: #cc6714; font-size: 0.8em; padding: 0px 4px; text-decoration: none; visibility: hidden;" title="Permalink to this headline"></a></h2>
<div style="background-color: white; border-bottom-width: 0px; color: black; font-family: 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif; font-size: 12px; line-height: 19.2000007629395px; padding-bottom: 0px; white-space: normal;">
The <tt class="docutils literal" style="background-color: #ecf0f3; font-size: 0.95em; padding: 0px 1px;"><span class="pre">groupby()</span></tt> function returns an iterator that produces sets of values grouped by a common key.</div>
<div style="background-color: white; border-bottom-width: 0px; color: black; font-family: 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif; font-size: 12px; line-height: 19.2000007629395px; padding-bottom: 0px; white-space: normal;">
This example from the standard library documentation shows how to group keys in a dictionary which have the same value:</div>
<div class="highlight-python" style="background-color: white; color: black; font-family: 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif; font-size: 12px; line-height: 19.2000007629395px; margin-left: 2em; margin-right: 2em; white-space: normal;">
<div class="highlight" style="border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;"><span class="kn">from</span> <span class="nn" style="color: blue; font-weight: bold;">itertools</span> <span class="kn">import</span> <span class="o" style="color: #666666;">*</span>
<span class="kn">from</span> <span class="nn" style="color: blue; font-weight: bold;">operator</span> <span class="kn">import</span> <span class="n">itemgetter</span>
<span class="n">d</span> <span class="o" style="color: #666666;">=</span> <span class="nb" style="color: green;">dict</span><span class="p">(</span><span class="n">a</span><span class="o" style="color: #666666;">=</span><span class="mi" style="color: #666666;">1</span><span class="p">,</span> <span class="n">b</span><span class="o" style="color: #666666;">=</span><span class="mi" style="color: #666666;">2</span><span class="p">,</span> <span class="n">c</span><span class="o" style="color: #666666;">=</span><span class="mi" style="color: #666666;">1</span><span class="p">,</span> <span class="n">d</span><span class="o" style="color: #666666;">=</span><span class="mi" style="color: #666666;">2</span><span class="p">,</span> <span class="n">e</span><span class="o" style="color: #666666;">=</span><span class="mi" style="color: #666666;">1</span><span class="p">,</span> <span class="n">f</span><span class="o" style="color: #666666;">=</span><span class="mi" style="color: #666666;">2</span><span class="p">,</span> <span class="n">g</span><span class="o" style="color: #666666;">=</span><span class="mi" style="color: #666666;">3</span><span class="p">)</span>
<span class="n">di</span> <span class="o" style="color: #666666;">=</span> <span class="nb" style="color: green;">sorted</span><span class="p">(</span><span class="n">d</span><span class="o" style="color: #666666;">.</span><span class="n">iteritems</span><span class="p">(),</span> <span class="n">key</span><span class="o" style="color: #666666;">=</span><span class="n">itemgetter</span><span class="p">(</span><span class="mi" style="color: #666666;">1</span><span class="p">))</span>
<span class="k" style="color: green; font-weight: bold;">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">g</span> <span class="ow" style="color: #aa22ff; font-weight: bold;">in</span> <span class="n">groupby</span><span class="p">(</span><span class="n">di</span><span class="p">,</span> <span class="n">key</span><span class="o" style="color: #666666;">=</span><span class="n">itemgetter</span><span class="p">(</span><span class="mi" style="color: #666666;">1</span><span class="p">)):</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="n">k</span><span class="p">,</span> <span class="nb" style="color: green;">map</span><span class="p">(</span><span class="n">itemgetter</span><span class="p">(</span><span class="mi" style="color: #666666;">0</span><span class="p">),</span> <span class="n">g</span><span class="p">)</span>
</pre>
</div>
</div>
<div class="highlight-python" style="background-color: white; color: black; font-family: 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif; font-size: 12px; line-height: 19.2000007629395px; margin-left: 2em; margin-right: 2em; white-space: normal;">
<div class="highlight" style="border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;">$ python itertools_groupby.py
1 ['a', 'c', 'e']
2 ['b', 'd', 'f']
3 ['g']
</pre>
</div>
</div>
<div style="background-color: white; border-bottom-width: 0px; color: black; font-family: 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif; font-size: 12px; line-height: 19.2000007629395px; padding-bottom: 0px; white-space: normal;">
This more complicated example illustrates grouping related values based on some attribute. Notice that the input sequence needs to be sorted on the key in order for the groupings to work out as expected.</div>
<div class="highlight-python" style="background-color: white; color: black; font-family: 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif; font-size: 12px; line-height: 19.2000007629395px; margin-left: 2em; margin-right: 2em; white-space: normal;">
<div class="highlight" style="border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;"><span class="kn">from</span> <span class="nn" style="color: blue; font-weight: bold;">itertools</span> <span class="kn">import</span> <span class="o" style="color: #666666;">*</span>
<span class="k" style="color: green; font-weight: bold;">class</span> <span class="nc" style="color: blue; font-weight: bold;">Point</span><span class="p">:</span>
<span class="k" style="color: green; font-weight: bold;">def</span> <span class="nf" style="color: blue;">__init__</span><span class="p">(</span><span class="bp" style="color: green;">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="bp" style="color: green;">self</span><span class="o" style="color: #666666;">.</span><span class="n">x</span> <span class="o" style="color: #666666;">=</span> <span class="n">x</span>
<span class="bp" style="color: green;">self</span><span class="o" style="color: #666666;">.</span><span class="n">y</span> <span class="o" style="color: #666666;">=</span> <span class="n">y</span>
<span class="k" style="color: green; font-weight: bold;">def</span> <span class="nf" style="color: blue;">__repr__</span><span class="p">(</span><span class="bp" style="color: green;">self</span><span class="p">):</span>
<span class="k" style="color: green; font-weight: bold;">return</span> <span class="s" style="color: #ba2121;">'Point(</span><span class="si" style="color: #bb6688; font-weight: bold;">%s</span><span class="s" style="color: #ba2121;">, </span><span class="si" style="color: #bb6688; font-weight: bold;">%s</span><span class="s" style="color: #ba2121;">)'</span> <span class="o" style="color: #666666;">%</span> <span class="p">(</span><span class="bp" style="color: green;">self</span><span class="o" style="color: #666666;">.</span><span class="n">x</span><span class="p">,</span> <span class="bp" style="color: green;">self</span><span class="o" style="color: #666666;">.</span><span class="n">y</span><span class="p">)</span>
<span class="k" style="color: green; font-weight: bold;">def</span> <span class="nf" style="color: blue;">__cmp__</span><span class="p">(</span><span class="bp" style="color: green;">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
<span class="k" style="color: green; font-weight: bold;">return</span> <span class="nb" style="color: green;">cmp</span><span class="p">((</span><span class="bp" style="color: green;">self</span><span class="o" style="color: #666666;">.</span><span class="n">x</span><span class="p">,</span> <span class="bp" style="color: green;">self</span><span class="o" style="color: #666666;">.</span><span class="n">y</span><span class="p">),</span> <span class="p">(</span><span class="n">other</span><span class="o" style="color: #666666;">.</span><span class="n">x</span><span class="p">,</span> <span class="n">other</span><span class="o" style="color: #666666;">.</span><span class="n">y</span><span class="p">))</span>
<span class="c" style="color: #408080; font-style: italic;"># Create a dataset of Point instances</span>
<span class="n">data</span> <span class="o" style="color: #666666;">=</span> <span class="nb" style="color: green;">list</span><span class="p">(</span><span class="n">imap</span><span class="p">(</span><span class="n">Point</span><span class="p">,</span>
<span class="n">cycle</span><span class="p">(</span><span class="n">islice</span><span class="p">(</span><span class="n">count</span><span class="p">(),</span> <span class="mi" style="color: #666666;">3</span><span class="p">)),</span>
<span class="n">islice</span><span class="p">(</span><span class="n">count</span><span class="p">(),</span> <span class="mi" style="color: #666666;">10</span><span class="p">),</span>
<span class="p">)</span>
<span class="p">)</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="s" style="color: #ba2121;">'Data:'</span><span class="p">,</span> <span class="n">data</span>
<span class="k" style="color: green; font-weight: bold;">print</span>
<span class="c" style="color: #408080; font-style: italic;"># Try to group the unsorted data based on X values</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="s" style="color: #ba2121;">'Grouped, unsorted:'</span>
<span class="k" style="color: green; font-weight: bold;">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">g</span> <span class="ow" style="color: #aa22ff; font-weight: bold;">in</span> <span class="n">groupby</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="k" style="color: green; font-weight: bold;">lambda</span> <span class="n">o</span><span class="p">:</span><span class="n">o</span><span class="o" style="color: #666666;">.</span><span class="n">x</span><span class="p">):</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="n">k</span><span class="p">,</span> <span class="nb" style="color: green;">list</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>
<span class="k" style="color: green; font-weight: bold;">print</span>
<span class="c" style="color: #408080; font-style: italic;"># Sort the data</span>
<span class="n">data</span><span class="o" style="color: #666666;">.</span><span class="n">sort</span><span class="p">()</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="s" style="color: #ba2121;">'Sorted:'</span><span class="p">,</span> <span class="n">data</span>
<span class="k" style="color: green; font-weight: bold;">print</span>
<span class="c" style="color: #408080; font-style: italic;"># Group the sorted data based on X values</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="s" style="color: #ba2121;">'Grouped, sorted:'</span>
<span class="k" style="color: green; font-weight: bold;">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">g</span> <span class="ow" style="color: #aa22ff; font-weight: bold;">in</span> <span class="n">groupby</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="k" style="color: green; font-weight: bold;">lambda</span> <span class="n">o</span><span class="p">:</span><span class="n">o</span><span class="o" style="color: #666666;">.</span><span class="n">x</span><span class="p">):</span>
<span class="k" style="color: green; font-weight: bold;">print</span> <span class="n">k</span><span class="p">,</span> <span class="nb" style="color: green;">list</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>
<span class="k" style="color: green; font-weight: bold;">print</span>
</pre>
</div>
</div>
<div class="highlight-python" style="background-color: white; color: black; font-family: 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif; font-size: 12px; line-height: 19.2000007629395px; margin-left: 2em; margin-right: 2em; white-space: normal;">
<div class="highlight" style="border: 0px;">
<pre style="border: 1px solid rgb(221, 221, 221); color: #333333; line-height: 14.3999996185303px; overflow-x: auto; overflow-y: hidden; padding: 5px 1em;">$ python itertools_groupby_seq.py
Data: [Point(0, 0), Point(1, 1), Point(2, 2), Point(0, 3), Point(1, 4), Point(2, 5), Point(0, 6), Point(1, 7), Point(2, 8), Point(0, 9)]
Grouped, unsorted:
0 [Point(0, 0)]
1 [Point(1, 1)]
2 [Point(2, 2)]
0 [Point(0, 3)]
1 [Point(1, 4)]
2 [Point(2, 5)]
0 [Point(0, 6)]
1 [Point(1, 7)]
2 [Point(2, 8)]
0 [Point(0, 9)]
Sorted: [Point(0, 0), Point(0, 3), Point(0, 6), Point(0, 9), Point(1, 1), Point(1, 4), Point(1, 7), Point(2, 2), Point(2, 5), Point(2, 8)]
Grouped, sorted:
0 [Point(0, 0), Point(0, 3), Point(0, 6), Point(0, 9)]
1 [Point(1, 1), Point(1, 4), Point(1, 7)]
2 [Point(2, 2), Point(2, 5), Point(2, 8)]
</pre>
<div>
</div>
</div>
</div>
</div>
</pre>
<pre class="screen" style="box-sizing: border-box; direction: ltr; font-family: monospace, serif; font-size: 1em; font-weight: normal; margin: 0px; padding: 0px; white-space: pre-wrap;">
</pre>
</div>
</div>
Jeffyehttp://www.blogger.com/profile/07633630042179070371noreply@blogger.com0tag:blogger.com,1999:blog-5448019410873028021.post-73734206244363567112014-09-05T00:44:00.001-07:002014-09-09T07:25:45.597-07:00the pitfall of not using __slots__ in pythonRecently, I try to create a trie tree with python. My data has 3M+ different words, which ate 2G+ of my memory. It made me rather disappointed, so I searched online for solutions. <span style="color: red;">And I found the greatest improvement is to using __slots__ property. </span>It saves me 50% of the memory, so I think this a damn pitfall. Watch out, really, if you've not been familiar with it yet.<br />
<br />
Example:<br />
<br />
<span style="background-color: #999999;">class Node(object):</span><br />
<span style="background-color: #999999;"> '''without __slots__</span><br />
<span style="background-color: #999999;"> '''</span><br />
<span style="background-color: #999999;"><br /></span>
<span style="background-color: #999999;"> ChildrenFactory = dict</span><br />
<span style="background-color: #999999;"><br /></span>
<span style="background-color: #999999;"> def __init__(self, value=NULL):</span><br />
<span style="background-color: #999999;"> self.value = value</span><br />
<span style="background-color: #999999;"> self.children = self.ChildrenFactory()</span><br />
<br />
<br />
<span style="background-color: #999999;">class Node(object):</span><br />
<span style="background-color: #999999;"> '''</span><br />
<span style="background-color: #999999;"> :ivar value: The value of the key corresponding to this node or :const:`NULL`</span><br />
<span style="background-color: #999999;"> if there is no such key.</span><br />
<span style="background-color: #999999;"> :ivar children: A ``{key-part : child-node}`` mapping.</span><br />
<span style="background-color: #999999;"> '''</span><br />
<span style="background-color: #999999;"> __slots__ = ('value', 'children')</span><br />
<span style="background-color: #999999;"><br /></span>
<span style="background-color: #999999;"> ChildrenFactory = dict</span><br />
<span style="background-color: #999999;"><br /></span>
<span style="background-color: #999999;"> def __init__(self, value=NULL):</span><br />
<span style="background-color: #999999;"> self.value = value</span><br />
<span style="background-color: #999999;"> self.children = self.ChildrenFactory()</span><br />
<br />
<br />
So what is __slots__ doing here?<br />
<br />
<div style="background: rgb(255, 255, 255); border: 0px; clear: both; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17.8048000335693px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;">
Quoting <a href="http://code.activestate.com/lists/python-list/531365/" style="background: transparent; border: 0px; color: #4a6b82; cursor: pointer; font-size: 14px; margin: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">Jacob Hallen</a>:</div>
<blockquote style="background: rgb(238, 238, 238); border: 0px; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17.8048000335693px; margin: 0px 0px 10px; padding: 10px 10px 1px; quotes: none; vertical-align: baseline;">
<div style="background: transparent; border: 0px; clear: both; font-size: 14px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;">
The proper use of <code style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; margin: 0px; padding: 1px 5px; vertical-align: baseline; white-space: pre-wrap;">__slots__</code> is to save space in objects. Instead of having a dynamic dict that allows adding attributes to objects at anytime, there is a static structure which does not allow additions after creation. This saves the overhead of one dict for every object that uses slots. While this is sometimes a useful optimization, it would be completely unnecessary if the Python interpreter was dynamic enough so that it would only require the dict when there actually were additions to the object.</div>
<div style="background: transparent; border: 0px; clear: both; font-size: 14px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;">
Unfortunately there is a side effect to slots. They change the behavior of the objects that have slots in a way that can be abused by control freaks and static typing weenies. This is bad, because the control freaks should be abusing the metaclasses and the static typing weenies should be abusing decorators, since in Python, there should be only one obvious way of doing something.</div>
<div style="background: transparent; border: 0px; clear: both; font-size: 14px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;">
Making CPython smart enough to handle saving space without <code style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; margin: 0px; padding: 1px 5px; vertical-align: baseline; white-space: pre-wrap;">__slots__</code> is a major undertaking, which is probably why it is not on the list of changes for P3k (yet).</div>
</blockquote>
Jeffyehttp://www.blogger.com/profile/07633630042179070371noreply@blogger.com0tag:blogger.com,1999:blog-5448019410873028021.post-92170218459111068292014-09-03T13:00:00.003-07:002014-09-03T13:00:23.329-07:00How to set up shared folders in a Windows XP virtualbox guest?<div style="background-color: white; border: 0px; clear: both; margin-bottom: 1em; padding: 0px; vertical-align: baseline;">
<span style="color: #333333; font-family: UbuntuRegular, Ubuntu, Bitstream Vera Sans, DejaVu Sans, Tahoma, sans-serif;"><span style="line-height: 12.1333341598511px;">How to set up shared folders in a Windows XP virtualbox guest (say ubuntu host)?</span></span></div>
<div style="background-color: white; border: 0px; clear: both; margin-bottom: 1em; padding: 0px; vertical-align: baseline;">
<span style="color: #333333; font-family: UbuntuRegular, Ubuntu, Bitstream Vera Sans, DejaVu Sans, Tahoma, sans-serif;"><span style="line-height: 12.1333341598511px;"><br /></span></span></div>
<div style="background-color: white; border: 0px; clear: both; color: #333333; font-family: UbuntuRegular, Ubuntu, 'Bitstream Vera Sans', 'DejaVu Sans', Tahoma, sans-serif; line-height: 12.1333341598511px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;">
You have to install the Virtualbox Guest Addition (GA) to be able to share with the host if using Windows.</div>
<div style="background-color: white; border: 0px; clear: both; font-family: UbuntuRegular, Ubuntu, 'Bitstream Vera Sans', 'DejaVu Sans', Tahoma, sans-serif; line-height: 12.1333341598511px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;">
<span style="color: #333333;">First create a folder to share in with the host. For example, </span><span style="color: red;">create a folder called X </span><span style="color: #333333;">in your Host's Desktop and before or after installing the Guest Addition right click on the guest os in your Virtualbox main window. Go to settings and look for the </span><strong style="background-color: transparent; border: 0px; color: #333333; margin: 0px; padding: 0px; vertical-align: baseline;">Shared Folder</strong><span style="color: #333333;"> option.</span></div>
<div style="background-color: white; border: 0px; clear: both; font-family: UbuntuRegular, Ubuntu, 'Bitstream Vera Sans', 'DejaVu Sans', Tahoma, sans-serif; line-height: 12.1333341598511px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;">
<span style="color: #333333;">Select it and choose the X folder you just made. Now boot the guest OS with the Guest Addition already installed (if you haven't installed it yet do so) and on the terminal type </span><code style="background-color: #eeeeee; border: 0px; color: #222222; font-family: 'Ubuntu Mono', 'Ubuntu Beta Mono A', Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; margin: 0px; padding: 1px 5px; vertical-align: baseline; white-space: pre-wrap;">net </code><code style="background-color: #eeeeee; border: 0px; font-family: 'Ubuntu Mono', 'Ubuntu Beta Mono A', Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; margin: 0px; padding: 1px 5px; vertical-align: baseline; white-space: pre-wrap;"><span style="color: red;">use x: \\vboxsrv\x</span></code><span style="color: #333333;"> that is all. It should tell you that you are now sharing.</span></div>
<div style="background-color: white; border: 0px; clear: both; color: #333333; font-family: UbuntuRegular, Ubuntu, 'Bitstream Vera Sans', 'DejaVu Sans', Tahoma, sans-serif; line-height: 12.1333341598511px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;">
<br /></div>
Jeffyehttp://www.blogger.com/profile/07633630042179070371noreply@blogger.com0tag:blogger.com,1999:blog-5448019410873028021.post-83845310618179034892014-07-01T14:28:00.004-07:002014-07-01T14:28:54.292-07:00How To Install Tinc and Set Up a Basic VPN on Ubuntu 14.04<h3 style="background-color: white; float: left; margin: 14px 0px 11px; padding: 0px; width: 715px;">
<span style="color: #2d2d2d; font-family: proxima-nova, sans-serif;"><span style="font-size: 22px; letter-spacing: -0.5px;"><span style="font-weight: normal;">From</span> <span style="font-weight: normal;">https://www.digitalocean.com/community/tutorials/how-to-install-tinc-and-set-up-a-basic-vpn-on-ubuntu-14-04</span></span></span></h3>
<h3 style="background-color: white; color: #2d2d2d; float: left; font-family: proxima-nova, sans-serif; font-size: 22px; letter-spacing: -0.5px; margin: 14px 0px 11px; padding: 0px; width: 715px;">
Introduction</h3>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
In this tutorial, we will go over how to use Tinc, an open source Virtual Private Network (VPN) daemon, to create a secure VPN that your servers can communicate on as if they were on a local network. We will also demonstrate how to use Tinc to set up a secure tunnel into a private network. We will be using Ubuntu 14.04 servers, but the configurations can be adapted for use with any other OS.</div>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
A few of the features that Tinc has that makes it useful include encryption, optional compression, automatic mesh routing (VPN traffic is routed directly between the communicating servers, if possible), and easy expansion. These features differentiate Tinc from other VPN solutions such as OpenVPN, and make it a good solution for creating a VPN out of many small networks that are geographically distributed. Tinc is supported on many operating systems, including Linux, Windows, and Mac OS X.</div>
<div data-unique="Prerequisites" name="Prerequisites" style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px;">
</div>
<h2 style="background-color: white; color: #2d2d2d; font-family: proxima-nova, sans-serif; font-size: 27px; letter-spacing: 0.2px; margin: 14px 0px 10px; padding: 0px;">
Prerequisites</h2>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
To complete this tutorial, you will require root access on at least three Ubuntu 14.04 servers. Instructions to set up root access can be found here (steps 3 and 4): <a href="https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-14-04" style="border-bottom-color: black; border-bottom-style: dotted; border-bottom-width: 1px; color: black; text-decoration: none;">Initial Server Setup with Ubuntu 14.04</a>.</div>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
If you are planning on using this in your own environment, you will have to plan out how your servers need to access each other, and adapt the examples presented in this tutorial to your own needs. If you are adapting this to your own setup, be sure to substitute the highlighted values in the examples with your own values.</div>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
If you would like to follow this tutorial exactly, create two VPSs in the same datacenter, with private networking, and create another VPS in a separate datacenter. We will create two VPSs in the NYC2 datacenter and one in AMS2 datacenter with the following names:</div>
<ul style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin: 0px 0px 28px 28px; padding: 0px;">
<li style="box-sizing: border-box; list-style: disc inside; margin: 0px 0px 3px; padding: 0px;"><span style="font-weight: 700;">externalnyc</span>: All of the VPN nodes will connect to this server, and the connection must be maintained for proper VPN functionality. Additional servers can be configured in a similarly to this one to provide redundancy, if desired.</li>
<li style="box-sizing: border-box; list-style: disc inside; margin: 0px 0px 3px; padding: 0px;"><span style="font-weight: 700;">internalnyc</span>: Connects to <em>externalnyc</em> VPN node using its <em>private</em> network interface</li>
<li style="box-sizing: border-box; list-style: disc inside; margin: 0px 0px 3px; padding: 0px;"><span style="font-weight: 700;">ams1</span>: Connects to <em>externalnyc</em> VPN node over the public Internet</li>
</ul>
<div data-unique="OurGoal" name="OurGoal" style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px;">
</div>
<h2 style="background-color: white; color: #2d2d2d; font-family: proxima-nova, sans-serif; font-size: 27px; letter-spacing: 0.2px; margin: 14px 0px 10px; padding: 0px;">
Our Goal</h2>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Here is a diagram of the VPN that we want to set up (described in Prerequisites):</div>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
<img alt="Tinc VPN Setup" src="https://assets.digitalocean.com/articles/tinc/tinc.png" style="border: 0px; height: auto; max-width: 100%;" /></div>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
The green represents our VPN, the gray represents the public Internet, and the orange represents the private network. All three servers can communicate on the VPN, even though the private network is inaccessible to ams1.</div>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Let's get started by installing Tinc!</div>
<div data-unique="InstallTinc" name="InstallTinc" style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px;">
</div>
<h2 style="background-color: white; color: #2d2d2d; font-family: proxima-nova, sans-serif; font-size: 27px; letter-spacing: 0.2px; margin: 14px 0px 10px; padding: 0px;">
Install Tinc</h2>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
On each VPS that you want to join the private network, install Tinc. Let's start by updating apt:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">sudo apt-get update
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Then install Tinc via apt:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">sudo apt-get install tinc
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Now that Tinc is installed, let's look at the Tinc configuration.</div>
<div data-unique="TincConfiguration" name="TincConfiguration" style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px;">
</div>
<h2 style="background-color: white; color: #2d2d2d; font-family: proxima-nova, sans-serif; font-size: 27px; letter-spacing: 0.2px; margin: 14px 0px 10px; padding: 0px;">
Tinc Configuration</h2>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Tinc uses a "netname" to distinguish one Tinc VPN from another (in case of multiple VPNs), and it is recommended to use a netname even if you are only planning on configuring one VPN. We will call our VPN "<span class="highlight" style="background: none; color: #de3939;">netname</span>" for simplicity.</div>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Every server that will be part of our VPN requires the following three configuration components:</div>
<ul style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin: 0px 0px 28px 28px; padding: 0px;">
<li style="box-sizing: border-box; list-style: disc inside; margin: 0px 0px 3px; padding: 0px;">Configuration files: tinc.conf, tinc-up, and tinc-down, for example</li>
<li style="box-sizing: border-box; list-style: disc inside; margin: 0px 0px 3px; padding: 0px;">Public/private key pairs: For encryption and node authentication</li>
<li style="box-sizing: border-box; list-style: disc inside; margin: 0px 0px 3px; padding: 0px;">Host configuration files: Which contain public keys and other VPN configuration</li>
</ul>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Let's start by configuring our <em>externalnyc</em> node.</div>
<div data-unique="Configureexternalnyc" name="Configureexternalnyc" style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px;">
</div>
<h2 style="background-color: white; color: #2d2d2d; font-family: proxima-nova, sans-serif; font-size: 27px; letter-spacing: 0.2px; margin: 14px 0px 10px; padding: 0px;">
Configure externalnyc</h2>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
On <span style="font-weight: 600;">externalnyc</span>, create the configuration directory structure for our VPN called "netname":</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">sudo mkdir -p /etc/tinc/<span class="highlight" style="background: none; color: #de3939;">netname</span>/hosts
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Now open tinc.conf for editing:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">sudo vi /etc/tinc/<span class="highlight" style="background: none; color: #de3939;">netname</span>/tinc.conf
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Now add the following lines:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">Name = <span class="highlight" style="background: none; color: #de3939;">externalnyc</span>
AddressFamily = ipv4
Interface = tun0
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
This simply configures a node called <span class="highlight" style="background: none; color: #de3939;">externalnyc</span>, with a network interface that will use IPv4 called "tun0". Save and quit.</div>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Next, let's create an <em>externalnyc</em> hosts configuration file:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">sudo vi /etc/tinc/<span class="highlight" style="background: none; color: #de3939;">netname</span>/hosts/<span class="highlight" style="background: none; color: #de3939;">externalnyc</span>
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Add the following lines to it (substitute the public IP address of your VPS here):</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">Address = <span class="highlight" style="background: none; color: #de3939;">externalnyc_public_IP</span>
Subnet = 10.0.0.1/32
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Ultimately, this file will be used on other servers to communicate with this server. The address specifies how other nodes will connect to this server, and the subnet specifies which subnet this daemon will serve. Save and quit.</div>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Now generate the public/private keypair for this host with the following command:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">sudo tincd -n <span class="highlight" style="background: none; color: #de3939;">netname</span> -K4096
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
This creates the private key (/etc/tinc/<em>netname</em>/rsa_key.priv) and appends the public key to the <em>externalnyc</em> hosts configuration file that we recently created (/etc/tinc/netname/hosts/<em>externalnyc</em>).</div>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Now we must create <code style="background-color: #f6f6f6; border-bottom-left-radius: 2px; border-bottom-right-radius: 2px; border-top-left-radius: 2px; border-top-right-radius: 2px; color: #111111; font-family: monospace, serif; font-size: 15px; margin: 0px 3px; padding: 3px;">tinc-up</code>, the script that will run whenever our <em>netname</em> VPN is started. Open the file for editing now:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">sudo vi /etc/tinc/<span class="highlight" style="background: none; color: #de3939;">netname</span>/tinc-up
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Add the following lines:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">#!/bin/sh
ifconfig $INTERFACE 10.0.0.1 netmask 255.255.255.0
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
When we start our VPN, this script will run to create the network interface that our VPN will use. On the VPN, this server will have an IP address of 10.0.0.1.</div>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Let's also create a script to remove network interface when our VPN is stopped:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">sudo vi /etc/tinc/<span class="highlight" style="background: none; color: #de3939;">netname</span>/tinc-down
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Add the following lines:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">#!/bin/sh
ifconfig $INTERFACE down
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Save and quit.</div>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Lastly, make tinc network scripts executable:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">sudo chmod 755 /etc/tinc/<span class="highlight" style="background: none; color: #de3939;">netname</span>/tinc-*
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Save and quit.</div>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Let's move on to our other nodes.</div>
<div data-unique="Configureinternalnycandams1" name="Configureinternalnycandams1" style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px;">
</div>
<h2 style="background-color: white; color: #2d2d2d; font-family: proxima-nova, sans-serif; font-size: 27px; letter-spacing: 0.2px; margin: 14px 0px 10px; padding: 0px;">
Configure internalnyc and ams1</h2>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
These steps are required on both <em>internalnyc</em> and <em>ams1</em>, with slight variations that will be noted.</div>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
On <span style="font-weight: 600;">internalnyc</span> and <span style="font-weight: 600;">ams1</span>, create the configuration directory structure for our VPN called "netname" and edit the Tinc configuration file:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">sudo mkdir -p /etc/tinc/<span class="highlight" style="background: none; color: #de3939;">netname</span>/hosts
sudo vi /etc/tinc/<span class="highlight" style="background: none; color: #de3939;">netname</span>/tinc.conf
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Add the following lines (substitute the name with the node name):</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">Name = <span class="highlight" style="background: none; color: #de3939;">node_name</span>
AddressFamily = ipv4
Interface = tun0
ConnectTo = <span class="highlight" style="background: none; color: #de3939;">externalnyc</span>
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
These nodes are configured to attempt to connect to "externalnyc" (the node we created prior to this). Save and quit.</div>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Next, let's create the hosts configuration file:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">sudo vi /etc/tinc/netname/hosts/<span class="highlight" style="background: none; color: #de3939;">node_name</span>
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
For <span style="font-weight: 600;">internalnyc</span>, add this line:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">Subnet = 10.0.0.<span class="highlight" style="background: none; color: #de3939;">2</span>/32
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
For <span style="font-weight: 600;">ams1</span>, add this line:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">Subnet = 10.0.0.<span class="highlight" style="background: none; color: #de3939;">3</span>/32
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Note that the numbers differ. Save and quit.</div>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Next, generate the keypairs:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">sudo tincd -n netname -K4096
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
And create the network interface start script:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">sudo vi /etc/tinc/netname/tinc-up
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
For <span style="font-weight: 600;">internalnyc</span>, add this line:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">ifconfig $INTERFACE 10.0.0.<span class="highlight" style="background: none; color: #de3939;">2</span> netmask 255.255.255.0
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
For <span style="font-weight: 600;">ams1</span>, add this line:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">ifconfig $INTERFACE 10.0.0.<span class="highlight" style="background: none; color: #de3939;">3</span> netmask 255.255.255.0
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
These IP addresses are how these nodes will be accessed on the VPN. Save and quit.</div>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Now create the network interface stop script:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">sudo vi /etc/tinc/netname/tinc-down
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
And add this line:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">ifconfig $INTERFACE down
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Save and quit.</div>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Lastly, make tinc network scripts executable:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">sudo chmod 755 /etc/tinc/<span class="highlight" style="background: none; color: #de3939;">netname</span>/tinc-*
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Save and quit.</div>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Now we must distribute the hosts configuration files to each node.</div>
<div data-unique="DistributetheKeys" name="DistributetheKeys" style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px;">
</div>
<h2 style="background-color: white; color: #2d2d2d; font-family: proxima-nova, sans-serif; font-size: 27px; letter-spacing: 0.2px; margin: 14px 0px 10px; padding: 0px;">
Distribute the Keys</h2>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
If you happen to use a configuration management system, here is a good application. Minimally, each node that wants communicate directly with another node must have exchanged public keys, which are inside of the hosts configuration files. In our case, for example, only <em>externalnyc</em> needs to exchange public keys with the other nodes. It is easier to manage if you just copy each public key to all members of the node. Note that you will want to change the "Address" value in <em>externalnyc</em>'s hosts configuration file to its private IP address when it is copied to <em>internalnyc</em>, so that connection is established over the private network.</div>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Because our VPN is called "netname", here is the location of the hosts configuration files: <code style="background-color: #f6f6f6; border-bottom-left-radius: 2px; border-bottom-right-radius: 2px; border-top-left-radius: 2px; border-top-right-radius: 2px; color: #111111; font-family: monospace, serif; font-size: 15px; margin: 0px 3px; padding: 3px;">/etc/tinc/<span class="highlight" style="background: none; color: #de3939;">netname</span>/hosts</code></div>
<h3 style="background-color: white; color: #2d2d2d; float: left; font-family: proxima-nova, sans-serif; font-size: 22px; letter-spacing: -0.5px; margin: 14px 0px 11px; padding: 0px; width: 715px;">
Exchange Keys Between externalnyc and internalnyc</h3>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
On <span style="font-weight: 600;">internalnyc</span>, copy its hosts configuration file to <em>externalnyc</em>:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">scp /etc/tinc/netname/hosts/<span class="highlight" style="background: none; color: #de3939;">internalnyc</span> <span class="highlight" style="background: none; color: #de3939;">user</span>@<span class="highlight" style="background: none; color: #de3939;">externalnyc_private_IP</span>:/tmp
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Then on <span style="font-weight: 600;">externalnyc</span>, copy the <em>internalnyc</em>'s file into the appropriate location:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">cd /etc/tinc/netname/hosts; sudo cp /tmp/<span class="highlight" style="background: none; color: #de3939;">internalnyc</span> .
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Then on <span style="font-weight: 600;">externalnyc</span> again, copy its hosts configuration file to <em>internalnyc</em>:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">scp /etc/tinc/netname/hosts/<span class="highlight" style="background: none; color: #de3939;">externalnyc</span> <span class="highlight" style="background: none; color: #de3939;">user</span>@<span class="highlight" style="background: none; color: #de3939;">internalnyc_private_IP</span>:/tmp
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
On <span style="font-weight: 600;">internalnyc</span>, copy <em>externalnyc</em>'s file to the appropriate location:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">cd /etc/tinc/netname/hosts; sudo cp /tmp/<span class="highlight" style="background: none; color: #de3939;">externalnyc</span> .
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
On <span style="font-weight: 600;">internalnyc</span>, let's edit <em>externalnyc</em>'s hosts configuration file so the "Address" field is set to <em>externalnyc</em>'s private IP address (so internalnyc will connect to the VPN via the private network). Edit <em>externalnyc</em>'s hosts configuration file:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">sudo vi /etc/tinc/netname/hosts/externalnyc
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Change the "Address" value to <em>externalnyc</em>'s private IP address:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">Address = <span class="highlight" style="background: none; color: #de3939;">externalnyc_private_IP</span>
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Save and quit. Now let's move on to our remaining node, ams1.</div>
<h3 style="background-color: white; color: #2d2d2d; float: left; font-family: proxima-nova, sans-serif; font-size: 22px; letter-spacing: -0.5px; margin: 14px 0px 11px; padding: 0px; width: 715px;">
Exchange Keys Between externalnyc and ams1</h3>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
On <span style="font-weight: 600;">ams1</span>, copy its hosts configuration file to <em>externalnyc</em>:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">scp /etc/tinc/netname/hosts/<span class="highlight" style="background: none; color: #de3939;">ams1</span> <span class="highlight" style="background: none; color: #de3939;">user</span>@<span class="highlight" style="background: none; color: #de3939;">externalnyc_public_IP</span>:/tmp
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Then on <span style="font-weight: 600;">externalnyc</span>, copy the <em>ams1</em>'s file into the appropriate location:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">cd /etc/tinc/netname/hosts; sudo cp /tmp/<span class="highlight" style="background: none; color: #de3939;">ams1</span> .
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Then on <span style="font-weight: 600;">externalnyc</span> again, copy its hosts configuration file to <em>ams1</em>:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">scp /etc/tinc/netname/hosts/<span class="highlight" style="background: none; color: #de3939;">externalnyc</span> <span class="highlight" style="background: none; color: #de3939;">user</span>@ams1_public_IP:/tmp
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
On <span style="font-weight: 600;">ams1</span>, copy <em>externalnyc</em>'s file to the appropriate location:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">cd /etc/tinc/netname/hosts; sudo cp /tmp/<span class="highlight" style="background: none; color: #de3939;">externalnyc</span> .
</code></pre>
<h3 style="background-color: white; color: #2d2d2d; float: left; font-family: proxima-nova, sans-serif; font-size: 22px; letter-spacing: -0.5px; margin: 14px 0px 11px; padding: 0px; width: 715px;">
Exchange Keys Between Additional Nodes</h3>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
If you are creating a larger VPN, now is a good time to exchange the keys between those other nodes. Remember that if you want two nodes to directly communicate with each other (without a forwarding server between), they need to have exchanged their keys/hosts configuration files, and they need to be able to access each other's real network interfaces. Also, it is fine to just copy each hosts configuration to every node in the VPN.</div>
<div data-unique="TestOurConfiguration" name="TestOurConfiguration" style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px;">
</div>
<h2 style="background-color: white; color: #2d2d2d; font-family: proxima-nova, sans-serif; font-size: 27px; letter-spacing: 0.2px; margin: 14px 0px 10px; padding: 0px;">
Test Our Configuration</h2>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
On <span style="font-weight: 600;">each</span> node, starting with <em>externalnyc</em>, start Tinc in debug mode like so (netname is the name of our VPN):</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">sudo tincd -n <span class="highlight" style="background: none; color: #de3939;">netname</span> -D -d3
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
After starting the daemon on each node, you should see output with the names of each node as they connect to externalnyc. Now let's test the connection over the VPN.</div>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
In a separate window, on <span style="font-weight: 600;">ams1</span>, ping internalnyc's VPN IP address (which we assigned to 10.0.0.2, earlier):</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">ping <span class="highlight" style="background: none; color: #de3939;">10.0.0.2</span>
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
The ping should work fine, and you should see some debug output in the other windows about the connection on the VPN. This indicates that ams1 is able to communicate over the VPN through externalnyc to internalnyc. Press CTRL-C to quit pinging.</div>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
You may also use the VPN interfaces to do any other network communication, like application connections, copying files, and SSH.</div>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
On each Tinc daemon debug window, quit the daemon by pressing CTRL-\.</div>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
<span style="font-weight: 600;">Note</span>: If the connections aren't working, ensure that your firewall is not blocking the connections or forwarding.</div>
<div data-unique="ConfigureTincToStartuponBoot" name="ConfigureTincToStartuponBoot" style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px;">
</div>
<h2 style="background-color: white; color: #2d2d2d; font-family: proxima-nova, sans-serif; font-size: 27px; letter-spacing: 0.2px; margin: 14px 0px 10px; padding: 0px;">
Configure Tinc To Startup on Boot</h2>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Before the Tinc init script will function properly, we have to put our VPN's name into the <code style="background-color: #f6f6f6; border-bottom-left-radius: 2px; border-bottom-right-radius: 2px; border-top-left-radius: 2px; border-top-right-radius: 2px; color: #111111; font-family: monospace, serif; font-size: 15px; margin: 0px 3px; padding: 3px;">nets.boot</code> configuration file.</div>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
On <span style="font-weight: 600;">each node</span>, edit nets.boot:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">sudo vi /etc/tinc/nets.boot
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Add the name of your VPN(s) into this file. Ours is "netname":</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;"># This file contains all names of the networks to be started on system startup.
netname
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Save and quit. Tinc is now configured to start on boot, and it can be controlled via the<code style="background-color: #f6f6f6; border-bottom-left-radius: 2px; border-bottom-right-radius: 2px; border-top-left-radius: 2px; border-top-right-radius: 2px; color: #111111; font-family: monospace, serif; font-size: 15px; margin: 0px 3px; padding: 3px;">service</code> command. If you would like to start it now run the following command on each of your nodes:</div>
<pre style="background-color: #f6f6f6; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; box-sizing: border-box; font-family: monospace, serif; font-size: 15px; margin-bottom: 28px; overflow: auto !important; padding: 18px; word-wrap: normal !important;"><code style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; color: #111111; font-family: monospace, serif; margin: 0px; padding: 0px;">sudo service tinc start
</code></pre>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Congrats! Your Tinc VPN is set up.</div>
<div data-unique="Conclusion" name="Conclusion" style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px;">
</div>
<h2 style="background-color: white; color: #2d2d2d; font-family: proxima-nova, sans-serif; font-size: 27px; letter-spacing: 0.2px; margin: 14px 0px 10px; padding: 0px;">
Conclusion</h2>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Now that you have gone through this tutorial, you should have a good foundation to build out your VPN to meet your needs. Tinc is very flexible, and any node can be configured to connect to any other node (that it can access over the network) so it can act as a mesh VPN, not relying on a single node.</div>
<div style="background-color: white; font-family: proxima-nova, sans-serif; font-size: 19px; line-height: 28.5px; margin-bottom: 28px; padding: 0px;">
Good luck!</div>
Jeffyehttp://www.blogger.com/profile/07633630042179070371noreply@blogger.com0tag:blogger.com,1999:blog-5448019410873028021.post-34787278795214116842014-04-06T15:14:00.000-07:002014-04-06T15:14:46.008-07:00fix a “E: The method driver /usr/lib/apt/methods/http could not be found.” error?how to fix a “E: The method driver /usr/lib/apt/methods/http could not be found.” error?<br />
Actually, it's not only this problem. Just now, I followed a damn tutorial (http://www.webupd8.org/2012/09/install-oracle-java-8-in-ubuntu-via-ppa.html) to install oracle Java8 on Ubuntu, which made me end with :<br />
"sudo: apt-get: command not found".<br />
<br />
I saw the installation note:<br />
<span style="color: #3d85c6;"><span style="background-color: white; font-family: arial; font-size: x-small;">Removing python-software-properties (0.92.35) ...</span><br style="font-family: arial; font-size: small;" /><span style="background-color: white; font-family: arial; font-size: x-small;">Removing jockey-gtk (0.9.7-0ubuntu15) ...</span><br style="font-family: arial; font-size: small;" /><span style="background-color: white; font-family: arial; font-size: x-small;">Removing ubuntu-minimal (1.324) ...</span><br style="font-family: arial; font-size: small;" /><span style="background-color: white; font-family: arial; font-size: x-small;">Removing nautilus-share (0.7.3-1ubuntu5) ...</span><br style="font-family: arial; font-size: small;" /><span style="background-color: white; font-family: arial; font-size: x-small;">Removing apturl (0.5.2ubuntu4) ...</span><br style="font-family: arial; font-size: small;" /><span style="background-color: white; font-family: arial; font-size: x-small;">Removing ubuntu-desktop (1.324) ...</span><br style="font-family: arial; font-size: small;" /><span style="background-color: white; font-family: arial; font-size: x-small;">Removing software-center (13.10-0ubuntu3) ...</span><br style="font-family: arial; font-size: small;" /><span style="background-color: white; font-family: arial; font-size: x-small;">Removing ubuntu-extras-keyring (2010.09.27) ...</span><br style="font-family: arial; font-size: small;" /><span style="background-color: white; font-family: arial; font-size: x-small;">OK</span><br style="font-family: arial; font-size: small;" /><span style="background-color: white; font-family: arial; font-size: x-small;">Removing software-properties-gtk (0.92.35) ...</span><br style="font-family: arial; font-size: small;" /><span style="background-color: white; font-family: arial; font-size: x-small;">Removing software-properties-common (0.92.35) ...</span><br style="font-family: arial; font-size: small;" /><span style="background-color: white; font-family: arial; font-size: x-small;">Removing python3-software-properties (0.92.35) ...</span><br style="font-family: arial; font-size: small;" /><span style="background-color: white; font-family: arial; font-size: x-small;">Removing unattended-upgrades (0.82.1ubuntu2) ...</span><br style="font-family: arial; font-size: small;" /><span style="background-color: white; font-family: arial; font-size: x-small;">Removing tasksel (2.88ubuntu15) ...</span><br style="font-family: arial; font-size: small;" /><span style="background-color: white; font-family: arial; font-size: x-small;">Removing tasksel-data (2.88ubuntu15) ...</span><br style="font-family: arial; font-size: small;" /><span style="background-color: white; font-family: arial; font-size: x-small;">Removing apt (0.9.15.4ubuntu4) ...</span></span><br />
<span style="background-color: white; color: #222222; font-family: arial; font-size: x-small;"><br /></span>
<span style="background-color: white; color: #222222; font-family: arial; font-size: x-small;">Why the hell installing java8 requires removing so many packages, including apt? </span><br />
<span style="background-color: white; color: #222222; font-family: arial; font-size: x-small;"><br /></span>
Having no "apt" will lead you cannot use apt-get.<br />
<br />
So I tried to use "sudo aptitude install apt" to get it back, which ends up with “E: The method driver /usr/lib/apt/methods/http could not be found.” In such a case, there is pretty much nothing you can do.<br />
<br />
So how did i fix a “E: The method driver /usr/lib/apt/methods/http could not be found.” error?<br />
<br />
<span style="color: red;">Solution is simple</span> : find a similar ubuntu machine and copy the "http" file back to your own one.<br />
<br />
The problem stems from the default jdk in Ubuntu is open-jdk. open-jdk is not suitable for many things, with which you even cannot use pycharm. What the hell is wrong with Ubuntu to choose open-jdk?<br />
<br />
Every time with a newly-install ubuntu system, the first thing for me to do is to install oracle jdk. I hate this.Jeffyehttp://www.blogger.com/profile/07633630042179070371noreply@blogger.com0