<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Incredible Vehicle &#187; python</title>
	<atom:link href="http://incrediblevehicle.com/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://incrediblevehicle.com</link>
	<description>(It's a blog.)</description>
	<lastBuildDate>Wed, 01 Feb 2012 16:00:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Python, Python, Python</title>
		<link>http://incrediblevehicle.com/2011/08/31/python-python-python/</link>
		<comments>http://incrediblevehicle.com/2011/08/31/python-python-python/#comments</comments>
		<pubDate>Thu, 01 Sep 2011 02:46:34 +0000</pubDate>
		<dc:creator>Matthew</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://incrediblevehicle.com/?p=978</guid>
		<description><![CDATA[Oh, god. I still have a bunch of rant left in me. So here we go, Internet: yet another angry rant to add to the pile. Sometimes, in the course of one&#8217;s life, one is left with a one-off task. In this case, I needed to call a binary a whole lot of times, and [...]]]></description>
			<content:encoded><![CDATA[<p>Oh, god. I still have a bunch of rant left in me. So here we go, Internet: yet another angry rant to add to the pile.</p>

<p>Sometimes, in the course of one&#8217;s life, one is left with a one-off task. In this case, I needed to call a binary a whole lot of times, and do something with the output each time. The details aren&#8217;t important; I just needed to write a wrapper script for this binary, do a modest amount of processing on it, and then output the result to a file. This is a pretty common task, one to which the command line in general and *nix in particular is well-suited.</p>

<p>Now, for various reasons, I prefer not to do this via shell scripts. I don&#8217;t have a hard and fast rule for when or why. Unless you routinely write shell scripts (I don&#8217;t), you&#8217;ll inevitably spend a bunch of time looking stuff up that you looked up, oh, six months ago. Or at least I do. I don&#8217;t enjoy it, so if I can&#8217;t do it with a few commands, maybe one loop or so, then I prefer to use a scripting language. I have the advantage of better/clearer failure modes and simpler syntax and I feel like I&#8217;m learning something more powerful and more widely applicable in the process.</p>

<p><span id="more-978"></span></p>

<p>One of my &#8220;favorite&#8221; things to forget and look up again is the difference between <code>[[ ]]</code> and <code>[ ]</code> in tests. Another one is how you loop over a file or output from a command (<code>while read line; do bar --baz $line; done &lt;foo</code> and <code>foo | while read line; do bar $line; done</code>, respectively).</p>

<p>If you have no trouble remembering this stuff or you write scripts often enough, I suppose this is moot. There was a time when I was scripting enough that I didn&#8217;t have to look all this stuff up every time, but that time is past. YMMV and all that.</p>

<p>Anyway, the point is that I probably could&#8217;ve done this with something like this (which is simplified somewhat):</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">ARGS</span>=<span style="color: #ff0000;">&quot;--bar=baz&quot;</span>
<span style="color: #007800;">CMD</span>=<span style="color: #ff0000;">&quot;~/bin/foo&quot;</span>
<span style="color: #007800;">KEY</span>=<span style="color: #ff0000;">&quot;quux&quot;</span>
<span style="color: #007800;">MIN</span>=<span style="color: #000000;">0</span>
<span style="color: #007800;">MAX</span>=<span style="color: #000000;">10000</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">seq</span> <span style="color: #000000;">0</span> <span style="color: #000000;">1000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> num <span style="color: #000000; font-weight: bold;">do</span>;
  <span style="color: #007800;">$CMD</span> <span style="color: #007800;">$num</span> <span style="color: #007800;">$ARGS</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">read</span> line;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #660033;">-q</span> <span style="color: #007800;">$KEY</span> <span style="color: #000000; font-weight: bold;">&lt;&lt;&lt;</span><span style="color: #007800;">$line</span>; <span style="color: #000000; font-weight: bold;">then</span>
      <span style="color: #007800;">value</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #660033;">-o</span> <span style="color: #ff0000;">'[1-9]+$'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$num</span>,<span style="color: #007800;">$value</span>&quot;</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
  <span style="color: #000000; font-weight: bold;">done</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></div></div>


<p>I&#8217;m sure I have a bajillion bugs in there. For example, I am not at all confident about the outer <code>for</code> loop syntax. And while it was easy to write, I&#8217;m not doing anything with command line args, doing any error handling, or anything like that. (On the other other hand, I uh underestimated how easy this was. That&#8217;s partially why I&#8217;m convinced there are bugs in there.)</p>

<p>I still don&#8217;t know how to write idiomatic (typo: idiotmatic) Ruby, so this is going to be very rough. Still, it feels natural to me, minus one or two hitches:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">ARGS = <span style="color:#996600;">&quot;--bar=baz&quot;</span>
CMD = <span style="color:#996600;">&quot;~/bin/foo&quot;</span>
KEY = <span style="color:#006600; font-weight:bold;">/</span>quux<span style="color:#006600; font-weight:bold;">/</span>
MIN = <span style="color:#006666;">0</span>
MAX = <span style="color:#006666;">10000</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> main
  <span style="color:#006600; font-weight:bold;">&#40;</span>MIN..<span style="color:#9900CC;">MAX</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span>
    out = <span style="color:#006600; font-weight:bold;">%</span>x<span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#008000; font-style:italic;">#{CMD} #{i} #{ARGS} ]</span>
    out.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'<span style="color:#000099;">\n</span>'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>line<span style="color:#006600; font-weight:bold;">|</span>
      KEY.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span>line<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>m<span style="color:#006600; font-weight:bold;">|</span>
        value = line.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">' '</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>
        <span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#996600;">&quot;#{i},#{value}<span style="color:#000099;">\n</span>&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>


<p>I&#8217;m not confident about the outer loop once again, and I&#8217;m not sure the call to match() will do what I want, let alone whether it&#8217;s elegant. Still, I feel pretty good about it. I love Ruby&#8217;s pattern of passing in blocks.</p>

<p>Fortunately or unfortunately, I had to use Python. And don&#8217;t get me wrong: I love Python. It is through Python that I learned to love scripting languages. Processing a file line by line was, I think, the real epiphany. And list comprehensions are wonderfully expressive.</p>

<p>But man alive is it <strong>awful</strong> for this sort of thing. I&#8217;m not even going to bother writing it out in Python. I&#8217;ll just excerpt, from the <a href="http://docs.python.org/library/subprocess.html#replacing-bin-sh-shell-backquote">2.7 subprocess docs</a>. Here&#8217;s what they say should replace backticks <code>output = \</code>mycmd myarg&#96;`:</p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">output = Popen<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;mycmd&quot;</span>, <span style="color: #483d8b;">&quot;myarg&quot;</span><span style="color: black;">&#93;</span>, stdout=PIPE<span style="color: black;">&#41;</span>.<span style="color: black;">communicate</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span></pre></div></div>


<p>Mind you, this is if you did <code>from subprocess import *</code>. Generally I don&#8217;t, which means it ends up looking like this:</p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">output = \
  <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">Popen</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;mycmd&quot;</span>, <span style="color: #483d8b;">&quot;myarg&quot;</span><span style="color: black;">&#93;</span>, <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">stdout</span>=PIPE<span style="color: black;">&#41;</span>.\
  communicate<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span></pre></div></div>


<p>Yes, once you have it written, sequestered in its own function that you never touch again, it&#8217;s not so bad. However, this is firmly in the category of something I will not be able to (and have not been able to) remember months later.</p>

<p>It is also not discoverable in the sense that it&#8217;s highly particular&#8212; <code>stdout=PIPE</code>? Really? Compare and contrast opening a file (<code>for line in open('somefile'): print foo(line)</code> or <code>[foo(line) for line in open('somefile')</code>) with this monster. Even my Ruby example could have used backticks if I hadn&#8217;t remembered the <code>%x</code> syntax. The best I can say about <code>subprocess</code> is that it&#8217;s a) better than <code>Popen</code> in Python 2.4 (or whatever), and b) it&#8217;s easier to search the web for than <code>%x</code>.</p>

<p>The kicker of course is that rest of the Python script was very easy to write! However, since calling the binary was changing some external state, though, I had to make sure it was doing the right thing. In the end building and testing the call to <code>subprocess.Popen()</code> took longer than the rest of the script. In an otherwise elegant, no-bullshit, batteries included language, the <code>subprocess</code> module is a terrible blemish. It doesn&#8217;t look any better in Python 3.0, either, unfortunately.</p>

<p>Even more unfortunately, this is ultimately why it would be my preference to use either Ruby or shell. Ruby seems to work well for a variety of tasks, from writing a full-fledged webapp to some grungy text manipulation. You don&#8217;t have to compromise because it&#8217;s quite easy to use it for Python-y things as well as Perl-y things. It&#8217;s just a shame that Python seems to treat this case with a bizarre kind of fussiness incongruent with the rest of the language and standard library.</p>
]]></content:encoded>
			<wfw:commentRss>http://incrediblevehicle.com/2011/08/31/python-python-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Markin&#8217; it down</title>
		<link>http://incrediblevehicle.com/2009/07/19/markin-it-down/</link>
		<comments>http://incrediblevehicle.com/2009/07/19/markin-it-down/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 04:36:35 +0000</pubDate>
		<dc:creator>Matthew</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://incrediblevehicle.com/?p=613</guid>
		<description><![CDATA[I&#8217;m giving Markdown a shot. I got sick of HTML. It&#8217;s cumbersome and not pleasant to read, and wyswig editors that operate with HTML under the hood have their own set of problems. Given the choice, I prefer a Wiki-like syntax. Markdown&#8217;s a lot like this; it&#8217;s human-readable but powerful in terms of the formatting [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m giving <a href="http://wordpress.org/extend/plugins/markdown-for-wordpress-and-bbpress/" title="John Gruber's Markdown">Markdown</a> a shot. I got sick of HTML. It&#8217;s cumbersome and not pleasant to read, and wyswig editors that operate with HTML under the hood have their own set of problems. Given the choice, I prefer a Wiki-like syntax. Markdown&#8217;s a lot like this; it&#8217;s human-readable but powerful in terms of the formatting it allows you.</p>

<p>Once I found a <a href="http://wordpress.org/extend/plugins/markdown-for-wordpress-and-bbpress/" title="John Gruber's Markdown">WordPress plugin</a> and a <a href="http://plasticboy.com/markdown-vim-mode/">Vim syntax file</a>, I was all set.</p>

<p>What&#8217;s more, Ubuntu has a package called <a href="http://www.freewisdom.org/projects/python-markdown/">python-markdown</a>. I&#8217;m not sure whether or not I&#8217;ll use Markdown for anything other than blog posting, but on the other hand, it&#8217;s awfully tempting to take advantage of this kind of intuitive, powerful formatting.</p>

<p>Yes, I&#8217;ve taken a step down a dark path: I&#8217;m writing blog posts in Vim, in Linux. Although I use it for coding as often as possible, I&#8217;ve thus far avoided it for anything like actual writing.</p>
]]></content:encoded>
			<wfw:commentRss>http://incrediblevehicle.com/2009/07/19/markin-it-down/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>readline, Python, IPython, and Mac OS X</title>
		<link>http://incrediblevehicle.com/2009/06/11/readline-python-ipython-and-mac-os-x/</link>
		<comments>http://incrediblevehicle.com/2009/06/11/readline-python-ipython-and-mac-os-x/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 03:18:43 +0000</pubDate>
		<dc:creator>Matthew</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[ipython]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[problems]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://incrediblevehicle.com/?p=132</guid>
		<description><![CDATA[All right, so maybe I&#8217;ll finally use this space. If you&#8217;ve tried to get IPython working on Mac OS X Leopard (that&#8217;d be 10.5, and 10.5.7 in particular) with an Intel based CPU, you&#8217;ve probably had some problems. I know I did! Let me cut to the chase, with a more technical explanation to follow. Oh, [...]]]></description>
			<content:encoded><![CDATA[<p>All right, so maybe I&#8217;ll finally use this space.</p>

<p>If you&#8217;ve tried to get IPython working on Mac OS X Leopard (that&#8217;d be 10.5, and 10.5.7 in particular) with an Intel based CPU, you&#8217;ve probably had some problems. I know I did! Let me cut to the chase, with a more technical explanation to follow. Oh, and some mild bitching, too.</p>

<p>I&#8217;m following the instructions I originally saw <a href="https://bugs.launchpad.net/ipython/+bug/254023">here</a>, on IPython&#8217;s Launchpad site.  Yes, my instructions are merely a reprint of someone else&#8217;s bug report. More on that after I explain.</p>

<h3>Quick instructions</h3>

<p>I&#8217;m going to assume you haven&#8217;t downloaded anything, although if, like me, you wrestled with this for a while, you can still do these steps and it&#8217;ll work. It worked for me, anyway. :/</p>

<ol>
    <li>Download the <code>.egg</code> for readline, presumably the latest version. Get IPython if you haven&#8217;t already. As of this writing, you can get it here at the <a href="http://ipython.scipy.org/moin/Download">IPython download site</a>. You can s/i386/fat/ if you like.</li>
    <li>Copy it to a directory, such as <code>~/python</code>.</li>
    <li>You can try to install readline and IPython: sudo easy_install readline ipython. One or both will fail.</li>
    <li>Open /usr/local/bin/ipython in your favorite text editor, and replace the contents of the file as described at the IPython Launchpad bug linked above. The key is to hardcode readline into the Python system path immediately after sys, and then to launch IPython explicitly.</li>
    <li>Launch ipython.</li>
</ol>

<p>You <em>should</em> be OK now, tabbing and all. If you haven&#8217;t checked this shit out, <a href="http://ipython.scipy.org/doc/stable/html/interactive/shell.html">using IPython for your system shell</a>, I highly recommend that you do. It&#8217;s none too shabby. <a href="http://ipython.scipy.org/doc/stable/html/interactive/shell.html">There&#8217;s a book on this, too.</a></p>

<p>Technical stuff after the jump.</p>

<p><span id="more-132"></span></p>

<h3>wtf, man</h3>

<p>So, here&#8217;s the skinny. It took me an hour or two to figure this out, which is way too long. I&#8217;m writing this post so that hopefully I can nail some of the search terms that weren&#8217;t around when I looked for it on the Googles. I&#8217;m mentioning a whole bunch of keywords and specific stuff in the hopes that the next person who has this problem gets here or to the Launchpad site and manages to solve their problem.</p>

<p>Anyway, the technical background: IPython wants GNU&#8217;s readline library (tabbing!), but Mac OS X ships with libedit. If you somehow manage to get IPython to work, tabbing won&#8217;t because IPython is using libedit. It sucks and practically defeats the purpose of IPython. I had this working briefly, and then I broke it somehow. Whee!</p>

<p>You can download the readline egg and try to install it with easy_install. It will try to compile, then error out. In particular, it errors out on <code>Modules/readline.c:681</code> a bunch of times. I&#8217;m not 100% sure why it does this, and I&#8217;m too lazy to remember/figure it out again. I think it&#8217;s trying to compile the readline lib specifically for Python and failing because of some mismatch. It sucks and it makes me sad inside.</p>

<p>When I tried to run IPython anyway, I got a bunch of errors about unable to find an entry point. I suspect this is some failure related to  easy_install, egg, or pkg_resources and readline being missing, although it&#8217;s vastly unintuitive if so. You&#8217;ll get an ImportError related to console_scripts and ipython. It&#8217;ll say entry point not found.</p>

<p>The fix appears to be skipping the egg, easy_install, pkg_resource shenanigans by explicitly adding readline to your path and then explicitly invoking IPython instead of giving something else a chance to error out. Sounds good to me!</p>

<h3>readline + IPython + Fink?</h3>

<p>I didn&#8217;t try doing anything with <a href="http://www.finkproject.org/">Fink</a>. Fink does have both a version of IPython and readline, so it&#8217;s possible that Fink would be a better choice overall. Honestly, I&#8217;d try that before anything else. I was too stubborn to give up halfway through.</p>

<p>Of course, not having tried it, I don&#8217;t know that it&#8217;ll work. It&#8217;s gotta be less painful than the hackery above, though, right? Right? I mean, what could possibly go wrong?</p>
]]></content:encoded>
			<wfw:commentRss>http://incrediblevehicle.com/2009/06/11/readline-python-ipython-and-mac-os-x/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
