<?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>Terminally Incoherent</title>
	<atom:link href="http://www.terminally-incoherent.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.terminally-incoherent.com/blog</link>
	<description>I will not fix your computer.</description>
	<lastBuildDate>Mon, 20 May 2013 14:05:58 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Status Line in Vim</title>
		<link>http://www.terminally-incoherent.com/blog/2013/05/20/status-line-in-vim/</link>
		<comments>http://www.terminally-incoherent.com/blog/2013/05/20/status-line-in-vim/#comments</comments>
		<pubDate>Mon, 20 May 2013 14:05:58 +0000</pubDate>
		<dc:creator>Luke Maciak</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://www.terminally-incoherent.com/blog/?p=14436</guid>
		<description><![CDATA[If you are following the recent trends in the Vim community you have probably noted the ever growing popularity of Powerline. As the name suggests, it is a very powerful status line generation plugin, but I don&#8217;t think that&#8217;s why &#8230; <a href="http://www.terminally-incoherent.com/blog/2013/05/20/status-line-in-vim/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>If you are following the recent trends in the Vim community you have probably noted the ever growing popularity of <a href="https://github.com/Lokaltog/powerline" class="liexternal">Powerline</a>. As the name suggests, it is a very powerful status line generation plugin, but I don&#8217;t think that&#8217;s why most people use it. I believe its popularity has a lot to do with how sexy it looks on the bottom of your screen.</p>
<p>To be honest, I was never that fond of that particular extension for a variety of reasons. One of my chief gripes with it is directly related to the way it looks. Powerline uses non-standard patched fonts in order to achieve that segmented look which is not something Vim supports natively. Without them the extension looses a lot if it&#8217;s sex-appeal and starts looking a bit plain. To configure it, you don&#8217;t just add entries to your <var>.vimrc</var> but instead you need to create a separate dot-folder where it&#8217;s settings files are kept. While this neatly separates the Powerline setup from general Vim settings it also adds maintenance headaches. I like to keep all my Vim related config files in a single directory under source control so you can imagine this is less than optimal.</p>
<p>Finally, I have never really felt like I would use any of it&#8217;s advanced features. Most of the power-line setups I see on screenshots and in screen-casts are very basic: mode indcation, file path, some file and cursor position info. All of that can be easily set up in just a few lines of VimL. Powerline on the other hand is a monstrous semi-framework that can let you do really frivolous and complex things. For example, it has support for hooking into your power management module by the way of python and displaying battery status in Vim. It can also pull weather notifications from the web and display them in your status line&#8230; Though why would you ever want that is beyond me.</p>
<p>It is really a nice an powerful plugin, but it doesn&#8217;t seem like something I would necessarily need. For years I have been pretty happy using a very simple status line with just the name of the file and some positioning info along side the excellent <a href="https://github.com/vim-scripts/Obvious-Mode" class="liexternal">Obvious-Mode</a> plugin. It covered most of my needs &#8211; my status line would be red in insert mode, gray/cyan in normal mode, depending on whether or not the file had any unsaved changes.</p>
<p>Lately however I decided that I could use more informative status line. I briefly flirted with the concept of using Powerline but since I&#8217;m a system nomad who drags his <var>.vim</var> folder wherever he goes that idea died pretty quickly. Instead I decided to create my own status line which displays the information that I care about in a Powerline-like way but without any fancy fonts.</p>
<p>The killer feature for me is that I wrote it. By that I mean that if something breaks or doesn&#8217;t work correctly can easily figure out why, and fix it. Power line is pretty big and has a lot of moving parts, whereas my status line is just a dozen lines of very basic VimL so if it breaks, I can easily handle it without loosing too much time.</p>
<p>The result looks like this:</p>
<div id="attachment_14439" class="wp-caption aligncenter" style="width: 650px"><a href="http://www.terminally-incoherent.com/blog/wp-content/uploads/2013/05/neatstatus2.png" class="liimagelink"><img src="http://www.terminally-incoherent.com/blog/wp-content/uploads/2013/05/neatstatus2-640x119.png" alt="vim-neatstatus" width="640" height="119" class="size-large wp-image-14439" /></a><p class="wp-caption-text">Vim-NeatStatus Plugin</p></div>
<p>The mode indicator on the left changes color depending on the mode: green in normal, red in insert and blue in replace mode. I also had it turn purple in visual mode but it was kinda buggy (always lagged one keystroke behind) so at the moment it doesn&#8217;t do that. I still use Obvious-Mode so this functionality isn&#8217;t strictly necessary, but I included it so that it could be used as a stand-alone plugin.</p>
<p>The black box next to mode indicator shows you the server name (which in my case is treated as session name). This may not be useful to everyone, but since I set up my Vim to <a href="http://www.terminally-incoherent.com/blog/2013/04/29/vim-sessions/" class="liinternal">save sessions on exit</a> I actually like to see that information somewhere.</p>
<p>To the left of the file path I have file type (ie. what language vim thinks this file is written in), file format (important if you jump between systems as much as me) and encoding (as in utf8 or not). The last may not seem fairly relevant to you, but sometimes it does matter: especially if you are trying to parse files in a certain way, or if you create a script that doesn&#8217;t run properly because the non-vim text editor decided to insert a <a href="http://en.wikipedia.org/wiki/Byte_order_mark" rel="nofollow" class="liwikipedia">BOM</a> in it for shits and giggles.</p>
<p>Next I have the buffer number (which is useful when you have a lot of buffers open) and cursor positioning info with the current line highlighted in pink to stand out and be easily readable. There is also one more bit that is not visible in the picture above: whenever you edit a file, a little pink notification appears in the right corner of your status bar to let you know that the file has unsaved changes. Once again this is a duplication of Obvious-Mode functionality but it can be useful if you decide to install it as a stand-alone.</p>
<p>The colors work both in the graphical Vim as well as in the console (though you may need one that supports 256 colors). I tested it in in Gvim, MacVim and using Gnome-Console as well as Cygwin MinTTY on Windows.</p>
<p>I made the entire thing available as <a href="https://github.com/maciakl/vim-neatstatus" class="liexternal">a plugin</a> that can be easily installed via Pathogen like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> ~<span style="color: #000000; font-weight: bold;">/</span>.vim<span style="color: #000000; font-weight: bold;">/</span>bundle
<span style="color: #c20cb9; font-weight: bold;">git clone</span> git:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>maciakl<span style="color: #000000; font-weight: bold;">/</span>vim-neatstatus.git</pre></td></tr></table></div>

<p>If you have your <var>.vim</var> under source control like me then do:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> ~<span style="color: #000000; font-weight: bold;">/</span>.vim
<span style="color: #c20cb9; font-weight: bold;">git submodule</span> add git:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>maciakl<span style="color: #000000; font-weight: bold;">/</span>vim-neatstatus.git bundle<span style="color: #000000; font-weight: bold;">/</span>vim-neatstatus
<span style="color: #c20cb9; font-weight: bold;">git submodule</span> init
<span style="color: #c20cb9; font-weight: bold;">git submodule</span> update</pre></td></tr></table></div>

<p>At the moment the colors are hard coded and the plugin is not very configurable as a whole, but that will likely change in the future. If something is broken or the colors look atrocious under some setups <a href="https://github.com/maciakl/vim-neatstatus/issues" class="liexternal">let me know</a> and I will try fixing it.</p>
<p>Are you able to change the status line in your favorite text editor? If yes, what kind of information do you put there? Have you written your own status line or are you using a plugin written by someone else?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.terminally-incoherent.com/blog/2013/05/20/status-line-in-vim/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Iron Man 3</title>
		<link>http://www.terminally-incoherent.com/blog/2013/05/15/iron-man-3/</link>
		<comments>http://www.terminally-incoherent.com/blog/2013/05/15/iron-man-3/#comments</comments>
		<pubDate>Wed, 15 May 2013 14:07:35 +0000</pubDate>
		<dc:creator>Luke Maciak</dc:creator>
				<category><![CDATA[movies]]></category>
		<category><![CDATA[avengers]]></category>
		<category><![CDATA[review]]></category>

		<guid isPermaLink="false">http://www.terminally-incoherent.com/blog/?p=14407</guid>
		<description><![CDATA[Iron Man 3 is the first of the post-Avenger era Marvel movie. Joss Whedon&#8217;s epic super hero romp is a tough act to follow, both in terms of quality and box office success (which are not the same thing mind &#8230; <a href="http://www.terminally-incoherent.com/blog/2013/05/15/iron-man-3/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><em>Iron Man 3</em> is the first of the post-Avenger era Marvel movie. Joss Whedon&#8217;s epic super hero romp is a tough act to follow, both in terms of quality and box office success (which are not the same thing mind you). Before he helmed the big Marvel Universe cross-over the singular hero movies were all we got and we liked them. However, once the audiences got a taste of having Iron Man, Captain America, Thor and The Hulk in the same movie, one must wonder if they will still enjoy watching them adventure on their own. So the first post-Avengers film has to prove that a single hero story-line can still carry a movie.. And that it can do it without Joss Whedon&#8217;s writing and direction. </p>
<p>That&#8217;s not the only reason why Iron Man was a tough gig though. It also had a difficult task of reconciling the Iron Man continuity with the events that happened during the Avengers. Whedon didn&#8217;t really need to worry as much about reconciling all the plot threads from all the different movies because he was building something new and something bold and he could just pluck the heroes from their element and put them on a flying aircraft carrier. However, Iron Man 3 returns Tony Stark to his own established setting, with a cast of established characters (Pepper Pots, Happy Hogan, Col. Rhodes, etc..). It needs to tie the old with the new somehow, and this is especially difficult in terms of Iron Man mythos.</p>
<div id="attachment_14429" class="wp-caption aligncenter" style="width: 442px"><a href="http://www.terminally-incoherent.com/blog/wp-content/uploads/2013/05/im3.jpg" class="liimagelink"><img src="http://www.terminally-incoherent.com/blog/wp-content/uploads/2013/05/im3-432x640.jpg" alt="Iron Man 3" width="432" height="640" class="size-large wp-image-14429" /></a><p class="wp-caption-text">Iron Man 3</p></div>
<p>Thor is an alien space-god who fights frost giants and sorcerers with Clarktech magic so it is easy to hand-wave anything on his continuity. Captain is a displaced fish out of water character so anything that happens to him is essentially a blank slate &#8211; you can go anywhere with this hero right now and no one will probably make fuss about it. But Iron Man had two movies to establish his own setting. There is a thematic continuity and certain kind of feel to the Iron Man movies and you sort of must at least try to maintain that in order to make a successful sequel because that&#8217;s what people want to see. The problem is that in the last two movies Tony Stark fought relatively low powered (at least compared to Loki) rivals in power armors, and magic alien space gods were not even on his map. Going back to that place would be counter-productive.</p>
<p>I must say that Drew Pierce and Shane Black did an admirable job tying the two continuities into one by addressing the dissonance between them directly. Instead of ignoring the world changing events that happened in New York, they become a central point of Tony Stark&#8217;s character arc (yes, Tony has an arc in this film &#8211; this is new and definitely a welcome addition to the Iron Man franchise). After fighting alongside gods and monsters, the hero feels somewhat inadequate. Thor, Cap and Hulk all have super powers and can fight alien space monster with their bare hands if need be. Iron Man on the other hand is powerless without his armor, and Stark is all too keenly aware of that. Events of Avengers have shook him to the core, and he is still suffering night terrors and panic attacks triggered by memories of the giant wormhole in the sky. As if to compensate, he fills his house with ever more powerful variants of his armor and finding new ways to get the armor deployed and combat ready as fast as possible. Of course it is all for nothing, and he is soon stripped from his high-tech weapons and forced to overcome his insecurities fighting super-powered villains with nothing but the power of his intellect.</p>
<p>This is a solid character arc, and a solid foundation for a plot: something the previous two Iron Man movies didn&#8217;t really have. The second installment especially was rather aimless, directionless mess and the third movie is definitely an improvement over that.</p>
<p>A lot of people seem to be upset about the Mandarin bit, but lets face it: was anyone really expecting him to show up looking like Lo Pan from <em>Big Trouble in Little China</em> in this day and age? As much as comic book readers might be fond of the character, the truth is that the original character concept was somewhat racist. The updated movie version (a middle-eastern terrorist with oriental flair trappings) seems not only much more timely but also quite subversive. When Ben Kingsley showed up on the screen I couldn&#8217;t believe how calculated his character design was: this Mandarin was the sum of all post 9/11 fears made flesh. Part Bin-Laden, part Kim Jung Il, part super-villain mastermind he was the picture-perfect boogeyman that proponents of the <em>war on terror</em> have nightmares about.</p>
<p>Speaking of Mandarin, let&#8217;s talk about the big twist. If you haven&#8217;t seen the movie and don&#8217;t want to get spoiled I suggest you skip over the stuff in the gray box below.</p>
<div style="background: #F0F0F0; padding: 15px; border-left: 10px solid #E0E0E0">Did I like the twist? Did you like it?</p>
<p>At first I didn&#8217;t really know how to feel about the non-Mandarin. The more I think about it, the more I like it. Perhaps it is because I was never that attached to Iron Man comics and I don&#8217;t really care about Mandarin as a villain. So tweaking his back-story and his powers in an amusing way didn&#8217;t really rub me the wrong way. At the same time as I was watching the movie, I felt that Kingsley&#8217;s version of Mandarin was just a little bit too on the nose. It was too calculated and cartoonish in all the wrong ways so it was a relief to find out he was a decoy &#8211; and a brilliant one at that. Not to mention the fact that Kingsley is wickedly funny as the washed up actor.</p>
<p>You also have to keep in mind that the last two Iron Man movies completely bastardized and ruined other prominent Iron Man villains so expecting this installment to do any different seemed silly. It looks like Joss Whedon is the only super-hero writer in Hollywood who can currently write fun, compelling and enduring villains and I did not expect that to change overnight.</p></div>
<p><br/></p>
<p>The acting was pretty decent in this one: Kingsley was great, Downey was doing his thing as he always does, Gweneth Paltrow actually got to punch things&#8230; Though only after prolonged session of damsel in distress bondage. The kid was quite terrible, as all children in movies are but at least he was able to set up one of the funniest Downey lines in the movie.</p>
<p>Guy Pierce however was kinda annoying. I can&#8217;t really put my finger on it, but something about his performance bothered me. I didn&#8217;t necessarily hate him &#8211; when an actor can actually make you hate his villain then he is actually doing his job well (see Jack Gleeson, aka Joffrey Lanister &#8211; the most hated kid in America and Westeros right now). Pierce&#8217;s character just felt a bit boring. Granted it might just be that he was completely eclipsed and overshadowed by Kingsley and Downey in terms of the on-screen presence.</p>
<p>All in all, I think the movie was pretty solid. It might be one of the best installments in the Iron Man franchise so far &#8211; or at the very least, better than the second movie. It had a good character arc, some fun plot twists and pretty decent dialogue. Granted, it was not without flaws (movie children usually ruin shit pretty fiercely) and plot holes&#8230; For example: can someone explain to me why didn&#8217;t Tony open up his indestructible bunker that contained an army of remote operated Iron Man suits earlier? Like right away after crashing in the snow? Or when he got his suit powered up again? Because I can&#8217;t fucking figure it out.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.terminally-incoherent.com/blog/2013/05/15/iron-man-3/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Python: Increase Your Zen, Maximize Your Hapiness</title>
		<link>http://www.terminally-incoherent.com/blog/2013/05/13/python-increase-your-zen-maximize-your-hapiness/</link>
		<comments>http://www.terminally-incoherent.com/blog/2013/05/13/python-increase-your-zen-maximize-your-hapiness/#comments</comments>
		<pubDate>Mon, 13 May 2013 14:01:05 +0000</pubDate>
		<dc:creator>Luke Maciak</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.terminally-incoherent.com/blog/?p=14346</guid>
		<description><![CDATA[The philosophy of Python can be summed up in a single line: python -m this When I first discovered Python it still had that ugly, pixelated green snake logo all over their website, and the documentation was all like &#8220;Monty &#8230; <a href="http://www.terminally-incoherent.com/blog/2013/05/13/python-increase-your-zen-maximize-your-hapiness/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>The philosophy of Python can be summed up in a single line:</p>
<p><samp>python -m this</samp></p>
<p>When I first discovered Python it still had that ugly, pixelated green snake logo all over their website, and the documentation was all like <em>&#8220;Monty Python guise, LOL, amirite?&#8221;</em>. Over the years it has grown up a lot and became so mature, solid and reliable that people no longer even care about the white space thing as much. For years now Python has been ordinary and almost boring &#8211; all the rock stars, novelty chasers and cool kids left it for Ruby long time ago (and then recently left Ruby for things like Haskell and Node) and so serious shit can get done. </p>
<p>But despite being stable and mature environment, Python code is still a joy to work with. The beauty of the language is it&#8217;s enduring quality, baked right into the syntax and grammar of the language, and deeply rooted in the community. But as with all languages that have been around for a while, Python is not impervious to cruft, baggage and just plain poor coding. Despite the design of the language it is actually not that difficult to write ugly Python code. It is also not that hard to write pretty good code but in a way that is less than optimal.</p>
<p>So I wanted to share some tips, tricks and best practices for writing Python code that I have picked up from various sources over the years. This is definitely not an exhaustive or all-inclusive list. Rather it is a random assortment of suggestions and ideas that can help you structure your code better, and tools that will help you to do more with less.</p>
<h3>Use Virtualenv</h3>
<p>Installing packages globally is not always good idea. Especially if you use <em>easy_install</em> which can do funky things such as partial installs caused by network connectivity issues. It is better to keep your global install relatively clean and only install random packages on a project per project basis. Then, even if you mess up your installation you can just wipe the slate clean and start over again without much hassle.</p>
<p>Python doesn&#8217;t have native support of installing packages and modules this way, but you can force it to do so using the <a href="https://pypi.python.org/pypi/virtualenv" class="liexternal">virtualenv</a> package. The idea behind it is rather simple: you run a command line script and it copies your python environment into the current working directory, and then modifies your path for the current session so that all calls to python search the local folders first.</p>
<p>Here is an example how you would create a virtual environment for your project:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Copy a clean install of python with no packages to ./Foo</span>
<span style="color: #666666; font-style: italic;"># It will create ./Foo if it doesn't exist</span>
virtualenv <span style="color: #660033;">--no-site-packages</span> Foo
<span style="color: #7a0874; font-weight: bold;">cd</span> Foo
&nbsp;
<span style="color: #666666; font-style: italic;"># Update path for current session</span>
<span style="color: #7a0874; font-weight: bold;">source</span> bin<span style="color: #000000; font-weight: bold;">/</span>activate</pre></td></tr></table></div>

<p>This gives you a clean slate instance of Python with no baggage, which you can now build up with only the packages that are required for your project. Granted, this method litters your project folder with bunch of standard library files which can be annoying.</p>
<p>I usually recommend using <a href="https://bitbucket.org/dhellmann/virtualenvwrapper" class="liexternal">virtualenvwrapper</a> alongside with the virtualenv package which provides a set of wrapper scripts that aim to simplify your life. Once you install it, you can define <var>$WORKON_HOME</var> environment variable (usually <samp>~/.virtualenvs</samp>) which is going to be the default directory for installing virtual environments. It also simplifies the syntax a little bit, and modifies your prompt to reflect the virtualenv you are working in:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Create virtualenv in $WORKON_HOME/Foo</span>
mkvirtualenv Foo
&nbsp;
<span style="color: #666666; font-style: italic;"># Activate virtualenv</span>
workon Foo</pre></td></tr></table></div>

<p>The great thing about this approach is that you can have two separate environments (say ENV1 and ENV2) for the same project. For example if you want to investigate a bug that only happens when your code is running on a legacy version of Django, you can easily set it up.</p>
<h3>Use pip instead of easy_install</h3>
<p>One could argue this is a matter of personal preference, but I personally think that using pip should be considered one of the fundamental best practices for Python. As package managers go, it is simply superior in almost every conceivable way. It has meaningful error messages, it doesn&#8217;t leave your system in a weird state if an install fails, it can talk to source control repositories and it provides an <samp>uninstall</samp> command which lets you easily remove old or deprecated packages you no longer use.</p>
<p>It also allows you to create and use <a href="http://www.pip-installer.org/en/latest/requirements.html" class="liexternal">simple text files to track dependencies</a> for your project. You simply create a file (let&#8217;s call it <var>requirements.txt</var> like this, simply listing names and versions of packages that need to be installed on the system in order for your code to work:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">MyProject
<span style="color: #007800;">Markdown</span>==2.3.1</pre></td></tr></table></div>

<p>Save it with your code, commit it to the repository and forget about it. Later, when you need to deploy it somewhere else you can simply tell pip to fetch all the dependencies like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">pip <span style="color: #c20cb9; font-weight: bold;">install</span> <span style="color: #660033;">-r</span> requirements.txt</pre></td></tr></table></div>

<p>If you are using virtualenv, you can actually easily test that this works by creating an environment with the <var>&#8211;no-site-packages</var> switch. Or, if you already have a virtualenv custom-tailored to your project you can run the following command to auto-generate requirements file for yourself:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">pip freeze <span style="color: #000000; font-weight: bold;">&gt;</span> requirements.txt</pre></td></tr></table></div>

<p>Note that this typically dumps all the installed packages into the file, so if you run it globally instead of in a virtualenv you can end up with a really long list of packages that may or may not be related to your project.</p>
<h3>Standard Libraries are not always best</h3>
<p>Python code is beautiful, except when it isn&#8217;t. Most of the standard libraries follow the simple philosophy of Python Zen: they facilitate writing beautiful, simple, flat and sparse code. That said, the standard library has a few ugly warts: like urllib2 for example.</p>
<p>Take the following code snippet for example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib2</span>
&nbsp;
url <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'https://api.github.com'</span>
&nbsp;
req <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">Request</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>
&nbsp;
password_manager <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">HTTPPasswordMgrWithDefaultRealm</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
password_manager.<span style="color: black;">add_password</span><span style="color: black;">&#40;</span><span style="color: #008000;">None</span><span style="color: #66cc66;">,</span> url<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'user'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'pass'</span><span style="color: black;">&#41;</span>
&nbsp;
auth_manager <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">HTTPBasicAuthHandler</span><span style="color: black;">&#40;</span>password_manager<span style="color: black;">&#41;</span>
opener <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">build_opener</span><span style="color: black;">&#40;</span>auth_manager<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #dc143c;">urllib2</span>.<span style="color: black;">install_opener</span><span style="color: black;">&#40;</span>opener<span style="color: black;">&#41;</span>
handler <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span>req<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> handler.<span style="color: black;">getcode</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> handler.<span style="color: black;">headers</span>.<span style="color: black;">getheader</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'content-type'</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Is this beautiful? Is it clean? To me, the entire library has a very heavy handed Java feel to it. Compare it to the same functionality implemented using the third party <a href="https://github.com/kennethreitz/requests" class="liexternal">requests library</a> by Kenneth Reitz:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> requests
&nbsp;
r <span style="color: #66cc66;">=</span> requests.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'https://api.github.com'</span><span style="color: #66cc66;">,</span> auth<span style="color: #66cc66;">=</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'user'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'pass'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> r.<span style="color: black;">status_code</span>
<span style="color: #ff7700;font-weight:bold;">print</span> r.<span style="color: black;">headers</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'content-type'</span><span style="color: black;">&#93;</span></pre></td></tr></table></div>

<p>I think that most of us would agree that the latter is cleaner, clearer, more readable and arguably more <em>Pythonic</em> than the former. While it may seem counter-intuitive at first, sometimes picking a few good external packages can make a difference between unattainable mess and a clean and beautiful code. </p>
<p>If at any point you find yourself writing Python code that seems ugly, dirty and cumbersome, chances are there is a better way. You just have to look around a bit.</p>
<h3>Use Context Managers</h3>
<p>When you are writing code, chances are that sooner or later you will run into a situation where you need to open, lock or reserve some resource and then release it afterwards. The most trivial example would probably be writing to a file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">f <span style="color: #66cc66;">=</span> <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'myfile'</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">'w'</span><span style="color: black;">&#41;</span>
f.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Hello world!<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span>
f.<span style="color: black;">close</span></pre></td></tr></table></div>

<p>The unfortunate truth about code like that is that most of us habitually forget about like #3. And even if we don&#8217;t forget it, an exception or an error might prevent it from being ran. This may not happen so often when doing simple I/O but the same pattern is used to connect to databases or manipulating sockets where errors and exceptions are to be expected as a fact of life, and closing and releasing resources is doubly important. You could wrap your code in a try-finally blocks, but as of Python 2.5 and higher the cleaner and simpler way of writing the above is:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">with</span> <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'myfile'</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">'w'</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> f:
	f.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Hello world!<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>The cleanup is implicit, automatic and triggered whenever you leave the context of the block. This works great with objects, classes and functions that support it, but how do you bake it into your own custom code? It&#8217;s relatively simple: you put your set-up code in an <var>__enter__</var> method and your tear-down code in an <var>__exit__</var> method and they will be auto-magically called whenever your class is initialized using the <samp>with</samp> context manager:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># set it up</span>
<span style="color: #ff7700;font-weight:bold;">class</span> DBConnect:
    <span style="color: #ff7700;font-weight:bold;">def</span> __enter__<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #808080; font-style: italic;"># establish db connection</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">dbconnection</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> __exit__<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">dbconnection</span>.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># use it like this</span>
<span style="color: #ff7700;font-weight:bold;">with</span> DBConnect<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> db:
    <span style="color: #808080; font-style: italic;"># do stuff</span></pre></td></tr></table></div>

<p>Defining these methods for your custom classes not only makes the code more robust, but also more beautiful, allowing you to avoid writing the unseemly try-finally blocks all over the place.</p>
<h3>Create Objects that are Transparent</h3>
<p>The Python <acronym title="Read, Eval, Print, Loop">REPL</acronym> can be extremely useful tool. I use it all the time to quickly test ideas or poke my code around. Unfortunately it is not always easy to glimpse inside your custom classes and objects this way unless you take steps to make them transparent. For example, let&#8217;s say you have a class like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> Foo:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: #66cc66;">,</span> x<span style="color: #66cc66;">=</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> y<span style="color: #66cc66;">=</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">x</span><span style="color: #66cc66;">=</span>x
        <span style="color: #008000;">self</span>.<span style="color: black;">y</span><span style="color: #66cc66;">=</span>y</pre></td></tr></table></div>

<p>The problem you may run into is that objects initialized from this class do not display any useful information when invoked directly or printed in the REPL:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> t <span style="color: #66cc66;">=</span> Foo<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> f
<span style="color: #66cc66;">&lt;</span><span style="color: #dc143c;">__main__</span>.<span style="color: black;">Foo</span> instance at <span style="color: #ff4500;">0x02C82940</span><span style="color: #66cc66;">&gt;</span>
&nbsp;
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span>f<span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&lt;</span><span style="color: #dc143c;">__main__</span>.<span style="color: black;">Foo</span> instance at <span style="color: #ff4500;">0x02C82940</span><span style="color: #66cc66;">&gt;</span></pre></td></tr></table></div>

<p>A lot of people know that you can simply override the <var>__str__</var> method to get a nice string representation of your object that can be printed directly, or concatenated to some other text. It is however far less common to see the <var>__repr__</var> method implemented in a class. It is a pity, because it controls what is displayed in REPL when the object is invoked directly and as such can be tailored to display developer/debug friendly information about the internal state. For example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> Foo:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: #66cc66;">,</span> x<span style="color: #66cc66;">=</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> y<span style="color: #66cc66;">=</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">x</span><span style="color: #66cc66;">=</span>x
        <span style="color: #008000;">self</span>.<span style="color: black;">y</span><span style="color: #66cc66;">=</span>y
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__str__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">'({},{})'</span>.<span style="color: black;">format</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">x</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">self</span>.<span style="color: black;">y</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__repr__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">'{}(x={},y={})'</span>.<span style="color: black;">format</span><span style="color: black;">&#40;</span>
            <span style="color: #008000;">self</span>.__class__.__name__<span style="color: #66cc66;">,</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">x</span><span style="color: #66cc66;">,</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">y</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Will behave like this in the REPL:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> foo <span style="color: #66cc66;">=</span> Foo<span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">7</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> f
Foo<span style="color: black;">&#40;</span>x<span style="color: #66cc66;">=</span><span style="color: #ff4500;">5</span><span style="color: #66cc66;">,</span>y<span style="color: #66cc66;">=</span><span style="color: #ff4500;">7</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span>f<span style="color: black;">&#41;</span>
<span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">7</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Implementing the <var>__repr__</var> method may seem unimportant and insignificant, but it saves lives.. Or at least man hours of debugging.</p>
<h3>Lint Your Code</h3>
<p>I&#8217;m a firm believer in linting your code. It is a good practice that tends to yield saner, more beautiful and readable code most of the time. A lot of people don&#8217;t like very aggressive linters because they tend to complain about trivial things such as tabs vs. spaces, lines longer than 80 characters and etc. You should keep in mind however that every dumb linter rule is there for a reason, and generally adjusting your coding style a bit to conform to said rules is never really a bad idea. Granted, if you are working with an existing code base that has never been linted, you might be shocked how much refactoring it would take to get it in shape. But when you start a new project, a rigorous linter is a great thing. It will not only help you structure the code, but it will also catch minor bugs that could otherwise be overlooked.</p>
<p>Personally I&#8217;m a fan of <a href="http://www.pylint.org/" class="liexternal">PyLint</a> which tends to be super anal and very, very harsh on you. It actually scores your code, runs statistical analysis on it, and keeps track of last few runs so that you can see how much your changes have improved the overall quality of code.</p>
<p>Obviously PyLint is an external tool, but you can also get in-editor, as-you-type linting tools. Sublime has the excellent <a href="https://github.com/SublimeLinter/SublimeLinter" class="liexternal">Sublime Linter</a> which does more than just Python. If you run Vim there is a plethora of plugins that do just that, with the phenomenal <a href="https://github.com/scrooloose/syntastic" class="liexternal">Syntastic</a> leading the pack. If you use Emacs then&#8230; Then <a href="http://nullprogram.com/" class="liexternal">ask Chriss</a> &#8211; he will tell you what to use.</p>
<p>I highly recommend using PyLint in addition to any in-editor tool for the aggressive harshness and neat statistics it provides.</p>
<h3>Always Be Testing</h3>
<p>I don&#8217;t think I have to stress the importance of testing your code. At the very least you should be writing unit tests for all your classes. Python fortunately has a built in unit testing framework aptly named <a href="http://pyunit.sourceforge.net/" class="liexternal">PyUnit</a>. It is pretty much the industry standard and it follows the same conventions as JUnit and PHPUnit. If you have used these unit testing frameworks, PyUnit code should immediately look familiar and understandable:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">unittest</span>
<span style="color: #ff7700;font-weight:bold;">import</span> Foo
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> SimpleTestCase<span style="color: black;">&#40;</span><span style="color: #dc143c;">unittest</span>.<span style="color: black;">TestCase</span><span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> setUp<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot;Call before every test case.&quot;&quot;&quot;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">foo</span> <span style="color: #66cc66;">=</span> Foo<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: #008000;">file</span> <span style="color: #66cc66;">=</span> <span style="color: #008000;">open</span><span style="color: black;">&#40;</span> <span style="color: #483d8b;">&quot;blah&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;r&quot;</span> <span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> tearDown<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot;Call after every test case.&quot;&quot;&quot;</span>
        <span style="color: #008000;">self</span>.<span style="color: #008000;">file</span>.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> testA<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot;Test case A. &quot;&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">assert</span> foo.<span style="color: black;">bar</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #ff4500;">543</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;bar() not calculating values correctly&quot;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> testB<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot;Test case B&quot;&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">assert</span> foo+foo <span style="color: #66cc66;">==</span> <span style="color: #ff4500;">34</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;can't add Foo instances&quot;</span></pre></td></tr></table></div>

<p>If you want to do something along the lines of acceptance testing, then <a href="http://splinter.cobrateam.info/" class="liexternal">Splinter</a> is probably the way to go. It has really concise and clean API and lets you create complex tests for web applications fairly easily. The only downside is that it is not really a full fledged test framework like Codeception for example. There is no way to assert or fail tests in splinter &#8211; it is meant to be used as an abstraction layer or a component. So you basically just write PyUnit test cases which use Splinter, which works quite well as you end up with all your tests running on the same framework in the same place.</p>
<h3>Use a build tool</h3>
<p>As you may or may not know, I am a <a href="http://www.terminally-incoherent.com/blog/2013/02/18/what-is-your-favorite-build-tool/" class="liinternal">big fan of Rake</a> as a simple build tool. That said, it doesn&#8217;t really seem all that kosher (or practical) to use a Ruby build tool for Python projects. Fortunately there is <a href="http://paver.github.io/paver/" class="liexternal">Paver</a> which is essentially Rake of Python world, though perhaps not nearly as popular.</p>
<p>I believe that originally it was created to streamline the creation of distribution bundles and packages, but you can confidently use it to run any number of arbitrary tasks. Like with Rake, your build file is an executable script &#8211; you don&#8217;t configure your build, you code it. This makes it endlessly extensible and perfectly suited for our puposes.</p>
<p>To start using Paver you just <samp>pip install paver</samp> and create a <var>pavement.py</var> file somewhere in your project directory. An example file could look something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> paver.<span style="color: black;">easy</span> <span style="color: #ff7700;font-weight:bold;">import</span> *
&nbsp;
<span style="color: #66cc66;">@</span>task
<span style="color: #ff7700;font-weight:bold;">def</span> lint<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Linting...&quot;</span>
    <span style="color: #808080; font-style: italic;"># run linter </span>
&nbsp;
<span style="color: #66cc66;">@</span>task
<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #dc143c;">test</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Testing...&quot;</span>
    <span style="color: #808080; font-style: italic;"># run your unit tests</span>
&nbsp;
<span style="color: #66cc66;">@</span>task
<span style="color: #66cc66;">@</span>needs<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'lint'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'test'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">def</span> default<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">pass</span></pre></td></tr></table></div>

<p>You can run it like rake from the command line:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># run lint task</span>
paver lint
&nbsp;
<span style="color: #666666; font-style: italic;"># run the default task</span>
paver</pre></td></tr></table></div>

<p>It is usually a good idea to automate running of your linter and your unit tests with Paver. It allows you to make some changes, then immediately have them analyzed for errors and bad code smells, and ran through all your test cases.</p>
<h3>Suggestions?</h3>
<p>I hope this was helpful to at least few people out there. I am by no means an authority on Python but I feel like some of these tips can be useful for both novices as well as seasoned programmers. I generally enjoy reading this sort of write-ups because even if I already most of the stuff in the article is always a chance that I will learn something new. What are your favorite Python tips? Have you ever stumbled upon a Python related article or a video which made you go <em>&#8220;Wow, this changes everything!&#8221;</em>. If so, please share it in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.terminally-incoherent.com/blog/2013/05/13/python-increase-your-zen-maximize-your-hapiness/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Unity is not Great</title>
		<link>http://www.terminally-incoherent.com/blog/2013/05/08/unity-is-not-great/</link>
		<comments>http://www.terminally-incoherent.com/blog/2013/05/08/unity-is-not-great/#comments</comments>
		<pubDate>Wed, 08 May 2013 14:09:01 +0000</pubDate>
		<dc:creator>Luke Maciak</dc:creator>
				<category><![CDATA[technology]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.terminally-incoherent.com/blog/?p=14329</guid>
		<description><![CDATA[About two weeks ago my work laptop died. The motherboard just bricked itself to pieces and there was no rescuing it. As my old machine was old and decrepit, and I was going to be replacing it with something with &#8230; <a href="http://www.terminally-incoherent.com/blog/2013/05/08/unity-is-not-great/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>About two weeks ago my work laptop died. The motherboard just bricked itself to pieces and there was no rescuing it. As my old machine was old and decrepit, and I was going to be replacing it with something with an entirely different hard drive profile I opted to do a clean install of Ubuntu 12.4. Previously I&#8217;ve been running Kubuntu 10.4 and it&#8217;s been high time to move on. </p>
<p>Why not 13.4 you ask? Well, I&#8217;m in my 30&#8242;s now which means I&#8217;m living on an accelerated time-space curve now. Time simply flows faster than it did when I was in my teens or twenties. For example, I recently refereed to an event that happened back in 2010 as &#8220;the other day&#8221; because that&#8217;s honestly how it felt to me. From that you can probably extrapolate that I am not a huge fan of upgrading my OS every other day (which is roughly how 6 months seems to me). I can hardly give half of a fuck what quirky adjective-animal combination is the most up-to-date one. I just want something semi-stable that is regularly patched, and that has a decent sized package library. I&#8217;ve ran non-LTS versions of the OS before, and I was always unpleasantly surprised when Canonical end-of-lifed them and &#8220;vanished&#8221; the repositories leaving me stranded every 6 months or so. To put it plainly, anything that is not an LTS is not worth my time.</p>
<p>12.4 ships with Unity &#8211; Cannonical&#8217;s new window manager and desktop environment. There has been a lot of discussion about it when it was first released, and it seems that Ubuntu community has been torn apart between those who like it and those who hate it. Previously I didn&#8217;t really have an opinion in the discussion, because I haven&#8217;t really used it for anything substantial. I of course checked it out back when it was new, and it looked kinda sleek and a tad bit showy for my tastes but I didn&#8217;t outright hate it.</p>
<p>With my switch to 12.4 I decided to give it a fair shake and see how it performs in regular day-to-day usage. Unfortunately, it turns out that all the haters were right: Unity is not that great. It is style over substance. It seems to try so hard to be OSX like that it makes things that used to be simple needlessly difficult.</p>
<p>Here are my biggest complaints about Unity in order of severity:</p>
<ol>
<li>Poor performance</li>
<li>No functional pager</li>
<li>Terrible application awareness</li>
</ol>
<p>Let me tackle each of these in turn.</p>
<h3>Performance</h3>
<p>The machine I replaced my old laptop with wasn&#8217;t top of the line. It was one of the rank and file laptops we had in stock, and I might be able to swap it to a beefier dev machine at some point soon. Still, the hardware was nothing to scoff at &#8211; a respectable Intel i5 CPU and 4GB of RAM. Nothing to write home about, but we usually run Win 7 with full-blown Areo effects on this hardware, and it handles it without breaking a sweat. Unity was making it work for its money: the fan was whining full speed most of the time, and launching applications would actually freeze up the desktop for a few seconds. Switching desktops was literally painful.</p>
<p>You could argue that it&#8217;s not Unity but Ubuntu itself being an unoptimized resource hog. I was concerned by this too, but then I decided to do an experiment and ran the following command:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">aptitude</span> <span style="color: #c20cb9; font-weight: bold;">install</span> gnome-panel</pre></td></tr></table></div>

<p>I then logged out, logged back in and all my performance issues went away. Applications were now launching normally, and switching desktops wen from 1 second lag, followed by jerky animation to an instantaneous flick. </p>
<p>It&#8217;s probably also worth mentioning that after trying Gnome Panel, I went back and booted up in the Unity 2D mode to see how it stacks up. It was a big improvement on the default setup and the machine was actually usable. So if you do want to give Unity a whirl, I highly recommend using the 2D mode unless you have a top of the line rig. Of course half of Unity&#8217;s charm is how pretty it looks so you will be getting a diluted experience. Along with my other issues, I decided it just wasn&#8217;t worth it.</p>
<h3>Lack of Pager</h3>
<p>Both KDE and Gnome has always had excellent pagers. You know &#8211; those little widgets that sit in your task bar and let you switch between your virtual desktops. I always loved the fact that I could just glance onto said pager in the corner of my screen and see the rough layout of my windows on each desktop. Not only that, but I could just click on any arbitrary desktop to switch to it, or grab any of said windows and drag it to a different desktop letting me organize my shit without any hassle. In Unity that functionality is replaced with OSX inspired &#8220;workspace switcher&#8221; which does the pan-and-zoom-out kind of animation every time you activate it. It&#8217;s icon in the dock is static and doesn&#8217;t give you that at-a-glance preview which seems like a downgrade.</p>
<div id="attachment_14360" class="wp-caption aligncenter" style="width: 650px"><a href="http://www.terminally-incoherent.com/blog/wp-content/uploads/2013/05/gnome-panel1.png" class="liimagelink"><img src="http://www.terminally-incoherent.com/blog/wp-content/uploads/2013/05/gnome-panel1-640x359.png" alt="Gnome Panel" width="640" height="359" class="size-large wp-image-14360" /></a><p class="wp-caption-text">Note the pager in bottom right which shows you what windows are open on which desktop/display and allows you to drag and drop them.</p></div>
<p>I liked seeing window outlines in my KDE/Gnome pager. I liked being able to drag windows between desktops without actually switching to those desktops via the pager. The pager was always one of the best features of a Linux desktop. I always miss it whenever I&#8217;m working on Windows of OSX. In the past I have tried third party solutions that would add virtual desktops to Windows and pager like functionality of OSX but none of these have ever worked as well as the native KDE/Gnome task-bar widgets.</p>
<p>One could argue that removing the pager in not unorthodox because it mimics OSX behavior that users might be more familiar with. The fact that it was aped from Apple however doesn&#8217;t make it good. Personally I am not a huge fan of OSX spaces. I&#8217;m glad the functionality exists, but I often find myself wishing it worked more like traditional Gnome/KDE style virtual desktops. It is admirable that Cannonical is trying to learn from the best, but I think in this case they got it wrong. They took something that was working well, and replaced it with something similar but less functional.</p>
<h3>Application Awareness</h3>
<p>Unity is also missing a task bar. I don&#8217;t know about you but I like task bars. I&#8217;m very fond of the traditional, one entry per window task management. One of the first things I do when I install Windows is to disable collapsing in the task bar. I like to be able to see how many windows I have open, and be able to switch between them easily with a mouse. Collapsing applications into a single icon on the task bar hides vital information that I need to work efficiently.</p>
<p>OSX doesn&#8217;t have a traditional task bar, but it provides some alternatives. When you minimize windows in OSX, you essentially iconify them into custom doc entries. So as long as you command-M your windows instead of letting them go out of focus, you have yourself a functional task bar with one entry per minimized window.</p>
<p>Unity implementation combines worst features from both worlds and ends up with Launcher which is barely workable. Instead of showing you what windows are open on current desktop, Unity adds dots to the left of the icons of apps. One dot per open window on the current desktop&#8230; But only up to three dots in total for some reason. Instead of collapsing minimized windows into their own entries it simply hides them. So the only way to find out you have minimized windows on current desktop is to count the dots, then count open windows and subtract.</p>
<div id="attachment_14359" class="wp-caption aligncenter" style="width: 650px"><a href="http://www.terminally-incoherent.com/blog/wp-content/uploads/2013/05/unity1.png" class="liimagelink"><img src="http://www.terminally-incoherent.com/blog/wp-content/uploads/2013/05/unity1-640x358.png" alt="Unity Desktop" width="640" height="358" class="size-large wp-image-14359" /></a><p class="wp-caption-text">Pop Quiz: How many instances of Terminal are running here? The answer is five.</p></div>
<p>You can of course trigger Apple-expose like windows splay by clicking on said dots, but that shows you a preview of all the windows, without differentiating which ones were open, which were in focus and which were minimized. It seems a very haphazard, unfocused take on window management. With a traditional task-bar I can glance on the bottom of my screen and I instantly know how many windowed applications I&#8217;m running and on which desktops I have open windows. With Unity I always felt like I was flying blind, never having full awareness of my work environment.</p>
<h3>Conclusion</h3>
<p>In my honest opinion, Unity is mostly broken by design. I don&#8217;t blame Cannonical for trying to design a desktop environment that is easy to use and intuitive. I don&#8217;t blame them for abstracting and hiding away a lot of configuration details in lieu of a streamlined and unified look and feel. I don&#8217;t mind that they came up with an opinionated desktop environment that makes bold design choices. This is actually a good thing. Linux desktop needs this sort of focus on usability and user friendly environments. I&#8217;m glad that Unity exists because it allows us to have discussions about usability and user centric design on Linux desktop &#8211; something that used to be an almost foreign concept few years ago.</p>
<p>That said, when you come up with an opinionated framework that makes bold decisions, you risk using established power users. I am unfortunately one of them. I&#8217;ve been an Ubuntu user for many years now, but Unity is just not for me. I like traditional task-bars and pagers and I want them to be part of my desktop experience. If I didn&#8217;t like them I would probably be using a tiled window manager like real men are supposed to (or so I&#8217;m told).</p>
<p>That said, I can see how Unity could provide better user experience for novice users who have not yet developed habits such as juggling many virtual desktops and displays. If you are the type of person who usually runs one or two applications at a time, Unity 2D might actually be a viable option. The large icons on the launcher and search based application finder make it very easy for a Linux novice to find a program they might need for a particular task.</p>
<p>It&#8217;s a pity that Unity offers next to nothing to us power users.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.terminally-incoherent.com/blog/2013/05/08/unity-is-not-great/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Writing Vim Plugins in Python</title>
		<link>http://www.terminally-incoherent.com/blog/2013/05/06/vriting-vim-plugins-in-python/</link>
		<comments>http://www.terminally-incoherent.com/blog/2013/05/06/vriting-vim-plugins-in-python/#comments</comments>
		<pubDate>Mon, 06 May 2013 14:05:21 +0000</pubDate>
		<dc:creator>Luke Maciak</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://www.terminally-incoherent.com/blog/?p=14299</guid>
		<description><![CDATA[There are few things Vim and Emacs users have in common. One of these things is the fact they can talk together about programatically extending their work environment. You can&#8217;t really expect to talk to Eclipse of Visual studio about &#8230; <a href="http://www.terminally-incoherent.com/blog/2013/05/06/vriting-vim-plugins-in-python/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>There are few things Vim and Emacs users have in common. One of these things is the fact they can talk together about programatically extending their work environment. You can&#8217;t really expect to talk to Eclipse of Visual studio about such things, because only a small fraction of them have ever even attempted to write a plugin for their IDE. Many probably installed plugins, but scant few even entertain the notion of writing one? Why? Because it is usually a pain in the ass.</p>
<p>Vim and Emacs users are different &#8211; take one at random from each camp, put them in a room and sooner or later you will find them talking about that plugin/extension they wrote for their editor. Why is it that majority of people who use these two tools always eventually end up writing custom plugins for their editor, whereas most other programers never do? Perhaps Vim and Emacs simply attract certain kind of minds to them &#8211; inquisitive tinkerers and hackers who simply like to tweak and customize things. Or perhaps it is that both these editors are just so easy to tweak and modify that there is simply no barrier to entry. If you have written your <var>.vimrc</var> from scratch (as every Vim user should &#8211; don&#8217;t let anyone tell you otherwise) then you already know how to write plugins. In fact, you have probably already written a few without realizing it &#8211; all you would need to do now would be to dis-entangle them from your config file and generalize them a wee bit so they can be installed as a self contained module. Same goes for Emacs.</p>
<p>Of course, whenever Emacs user and Vim user start talking about plugin developments, the former is bound by law to smugly condescend about the quality of the respective domain specific languages employed by each editor. Emacs uses Elisp which is a Lisp flavor and therefore superior.</p>
<p>You see, the ancient legend says that before the universe was compiled, the creator deity pondered for a spell, and then on the first day said <em>Let there be parentheses!</em> and he saw they were good. Then on the second day he said <em>Let code be data and data be code</em> and there was much rejoicing. Then on the third day he hacked up a meta-programming macro, and fucked off for the rest of the week as the universe generated itself. Then, millions of years later man invented haskell and was like <em>&#8220;What now god?&#8221;</em> and god was like <em>&#8220;Shiiiiieeet, son!&#8221;</em> because in his omnipotence he saw it was also pretty fucking awesome, though mostly in an incomparable to lisp in an apples to barnacles kind of way.</p>
<p>The point is, Emacs users have a pretty cool language they can play around with. Us Vim users, on the other hand, have VimL which is&#8230; Neither pretty nor elegant. It has no OOP support, weird scoping, too many overloaded syntax level operators that behave differently based on context, no JIT, and it allows (or even encourages) you to write terrible, unreadable spaghetti code that is worse in terms of readability than Perl. You could probably argue about Elisp&#8217;s aesthetic flavor with all the parentheses and what not, but at the end of the day it is rather elegant and consistent environment. VimL is quite the opposite: it is quirky, inconsistent mess of a language that grew along with the editor developing strange warts and tentacles along the way. It is much like a giraffe developed by a committee: while it is supposed to be a giraffe, it is actually an aardvark wearing a pineapple fruit hat and a monocle. It is neither glamorous, nor particularly fun to work with &#8211; but it will get the job done if you clench your teeth and try to resist the overwhelming urge to slap it in the face for being so goofy looking.</p>
<p>Fortunately for us, we have options. Modern versions of Vim tend to come pre-baked with support for alternate languages such as Python or Ruby for example. So if we wish, we can get away from VimL and write code that looks and reads nicely. Of course such a thing comes at a trade-off. The general rule of plugin development is that the further you go from the native language the editor &#8220;speaks&#8221; the worse off you are. In case of Emacs, this doesn&#8217;t matter because it was largely written in Elisp (except the few small parts that were coded in C). Vim is mostly implemented in C with VimL already being second class <acronym title="Domain Specific Language">DSL</acronym> so putting another layer of abstraction between us and the editor can become problematic. So the rule of thumb is that for simple stuff you should stick to VimL and fall back on external language only when you have to do some heavy and/or convoluted processing. VimL is closely tied to Vim but not as expressive as it could be when it comes to representing complex program logic.</p>
<p>Python, on the other hand is. While, perhaps not as elegant as Lisp, Python is nice, strong, opinionated and beautiful language. Opinionated, because among other things, it doesn&#8217;t give a shit about your preconceived notions regarding the use of white space. Beautiful, because it is the kind of language that will force you to write code that doesn&#8217;t look like shit or die trying. It is the language you need, but perhaps not one you deserve.</p>
<p>Also, because it is older and more mature than Ruby and therefore you have a better chance of it being compiled in your copy of Vim. Mature only in software terms of course &#8211; after all making Monty Python references in your code is the accepted development pattern and recommended best practice after all.</p>
<p>How do you know if your Vim can handle Python code? Well, it is easy:</p>
<ul>
<li>
If you are outside of Vim you just run <samp>vim &#8211;version</samp> and look for <var>+python</var> which indicates you&#8217;ve got it or <var>-python</var> which indicates that whomever compiled your vim package is a major douchenozzle.</p>
</li>
<li>
If you are inside of Vim you can just issue the <samp>:py 1</samp> command. If nothing happens, you are good. If you get an error message complaining that <em>py</em> is not an editor command, then&#8230; Douchenozzle.</p>
</li>
</ul>
<p>Once you have confirmed that your vim will consume python code, you go about writing a plugin as you would with a VimL one. First, you create a file: say <var>helloworld.vim</var> or something like that. Then, at the top of that file you place some contingency management code:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #804040;">if</span> <span style="color: #000000;">!</span><span style="color: #25BB4D;">has</span><span style="color: #000000;">&#40;</span><span style="color: #C5A22D;">'python'</span><span style="color: #000000;">&#41;</span>
	<span style="color: #804040;">finish</span>
<span style="color: #804040;">endif</span></pre></td></tr></table></div>

<p>This checks if the user&#8217;s editor has the Python support baked in, and silently quits if it does not. This is a nice thing to do, since it prevents someone&#8217;s editor from blowing up. Chances are the person who will be installing your plugin doe not read your README file and thus does not realize he needs python support, and therefore his Vim will blow up with a barrage of error messages next time he launches it.</p>
<p>Of course we could try being more explicit, and display an error message along the lines of <em>&#8220;Y U has no +python!??&#8221;</em> but I personally prefer the silent failure. Don&#8217;t get me wrong, explicit is almost always good, but I hate when plugins spam me with error messages on start-up. I keep my <var>.vim/</var> directory under source control and I clone it onto every machine I work on. Sometimes I don&#8217;t have the luxury of using the latest and greatest Vim with all the fancy options. I don&#8217;t like to build specialized configs for those special cases &#8211; I&#8217;d rather use my usual setup, and just have the fancier stuff fail silently and simply be unavailable, rather than vomit stream of warning messages at me every time I do something. But that&#8217;s just me.</p>
<p>Next we want to expose some interface to the user in VimL so they can, for example bind it to a command or a key. That part has to be done in VimL. Think of it as bridging the divide between the native language and the external one. Usually, the best way to expose some functionality to the user in an unobtrusive way is to create a publicly accessible function:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #804040;">function</span><span style="color: #000000;">!</span> HeloWorld<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
endfunc</pre></td></tr></table></div>

<p>In general, you can access the Python environment in Vim by issuing the <samp>:python</samp> command (short version <samp>:py</samp>). You could technically write an entire plugin by simply prefacing every line with that command, but that would be extremely ugly and kinda crazy. Instead we can do something like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #804040;">function</span><span style="color: #000000;">!</span> HeloWorld<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
python <span style="color: #000000;">&lt;&lt;&lt;</span> EOF
&nbsp;
print <span style="color: #C5A22D;">&quot;Hello World!&quot;</span>
&nbsp;
EOF
endfunc</pre></td></tr></table></div>

<p>Now you can do <samp>:source %</samp> to load it up, and <samp>call HelloWorld()</samp> to test it. If all went well, you should see the text <em>Hello World</em> in the status line.</p>
<p>Granted, this is still kinda ugly. Mixing VimL and Python in a single file is definitely non-kosher, and it completely defeats the purpose of this entire exercise: to write plugins in a more elegant language, and make them more readable. Fortunately, there is another command we can use to load up an external python file, and execute it in Vim context: <samp>:pyfile</samp> (or <samp>:pyf</samp>). So our ugly function can be reduced to something like:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #804040;">function</span><span style="color: #000000;">!</span> HeloWorld<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    pyfile helloworld<span style="color: #000000;">.</span>py
endfunc</pre></td></tr></table></div>

<p><em>&#8220;That is all well and good,&#8221;</em> I can hear you say <em>&#8220;but what does this do for us?&#8221;</em> To actually do anything interesting as a plugin, your Python code needs access to internal Vim stuff &#8211; like the buffer list, or the contents of said buffers. How do we get that?</p>
<p>Well, we get it via the magical <samp>vim</samp> module. It is magical because it isn&#8217;t really a module that you can go and download or install in the usual way. It doesn&#8217;t exist on the outside, and can only be imported when the script is called from within Vim. This is both good and bad. It is good, because it makes it completely plug-and-play. If you have Vim with <var>+python</var> there is nothing else you need to do: it just works. On the other hand, it is bad because it requires Vim to work. This means you can&#8217;t run your code outside the context of your editor. For example, you may not be able use your favorite Python unit-testing suite to run test on your plugin, unless you mock up the Vim module functionality first. Which in retrospect is what you probably ought to do anyway so I guess it works out.</p>
<p>The <samp>vim</samp> module exposes bunch of useful objects such as <samp>vim.buffers</samp> or <samp>vim.current.buffer</samp> that you can use to manipulate your buffers, files and settings. The current buffer for example is actually a zero indexed array, each element representing a line as a string. Any changes to the elements in said array are immediately reflected on the screen. I will let that sit with you, so you can just imagine the possibilities for fun and abuse here.</p>
<p>If you want to know all the available objects and their details, I suggest you <a href="http://vimdoc.sourceforge.net/htmldoc/if_pyth.html#python-vim" class="liexternal">RTFM</a>. Just as an exercise however, lets write a quick and dirty python plugin together.</p>
<p>I work with Markdown files a lot, so I always run them through something like <a href="http://johnmacfarlane.net/pandoc/" class="liexternal">Pandoc</a> to check how will they look in HTML format. Let&#8217;s say I wanted to create a plugin that takes current buffer and dumps it into HTML format without the need for Pandoc or any other converter to be installed on the system. Because I&#8217;m lazy and I don&#8217;t feel like writing actual Markdown converter myself I will just use <a href="https://pypi.python.org/pypi/Markdown" class="liexternal">this Markdown package</a>.</p>
<p>So, first let&#8217;s create a VimL plugin file that will load our Python code and call it, say <var>markdown2html.vim</var>:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #804040;">if</span> <span style="color: #000000;">!</span><span style="color: #25BB4D;">has</span><span style="color: #000000;">&#40;</span><span style="color: #C5A22D;">'python'</span><span style="color: #000000;">&#41;</span>
	<span style="color: #804040;">finish</span>
<span style="color: #804040;">endif</span>
&nbsp;
<span style="color: #804040;">function</span><span style="color: #000000;">!</span> Markdown2HTML<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	pyfile markdown2html<span style="color: #000000;">.</span>py
endfunc
&nbsp;
<span style="color: #668080;">command</span><span style="color: #000000;">!</span> MKD2HTML <span style="color: #804040;">call</span> Markdown2HTML<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>The last line actually binds my function call to a Vim command providing an easy way for the user to run it. Next let&#8217;s write our python code:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">try</span>:
    <span style="color: #ff7700;font-weight:bold;">import</span> markdown
    <span style="color: #ff7700;font-weight:bold;">import</span> vim
    <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">string</span>
&nbsp;
    html <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">string</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>vim.<span style="color: black;">current</span>.<span style="color: black;">buffer</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
    html <span style="color: #66cc66;">=</span> markdown.<span style="color: black;">markdown</span><span style="color: black;">&#40;</span>html<span style="color: #66cc66;">,</span> output_format<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'html5'</span><span style="color: black;">&#41;</span>
&nbsp;
    filename <span style="color: #66cc66;">=</span> vim.<span style="color: black;">current</span>.<span style="color: black;">buffer</span>.<span style="color: black;">name</span>+<span style="color: #483d8b;">&quot;.html&quot;</span>
&nbsp;
    f <span style="color: #66cc66;">=</span> <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>filename<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'w'</span><span style="color: black;">&#41;</span>
    f.<span style="color: black;">write</span><span style="color: black;">&#40;</span>html<span style="color: black;">&#41;</span>
    f.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;HTML output written to &quot;</span>+filename
&nbsp;
<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">ImportError</span><span style="color: #66cc66;">,</span> e:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Markdown package not installed, please run: pip install markdown&quot;</span></pre></td></tr></table></div>

<p>Bam! Instant plugin. How awesome is that?</p>
<p>Of course the downside of using Python to write your plugins is that the users will need to have their editor compiled with <var>+python</var> in order to use them. Also, every time you use a third party Python package (like I just did above) your users will need to actually install that package on the system. So if you actually try to implement the above, don&#8217;t forget to <samp>pip install markdown</samp> at some point beforehand.</p>
<p>Still, the ability to bring the power of Python and all of it&#8217;s packages and modules into your Vim plugins is not something one should scoff at. If you would like to see a real life example of an excellent and popular plugin written in Python, go check out <a href="http://sjl.bitbucket.org/gundo.vim/" class="liexternal">Gundo</a>. It&#8217;s actually a plugin I use all the time as I work.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.terminally-incoherent.com/blog/2013/05/06/vriting-vim-plugins-in-python/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Lord of Light by Roger Zelazny</title>
		<link>http://www.terminally-incoherent.com/blog/2013/05/03/lord-of-light-by-roger-zelazny/</link>
		<comments>http://www.terminally-incoherent.com/blog/2013/05/03/lord-of-light-by-roger-zelazny/#comments</comments>
		<pubDate>Fri, 03 May 2013 14:07:39 +0000</pubDate>
		<dc:creator>Luke Maciak</dc:creator>
				<category><![CDATA[literature]]></category>
		<category><![CDATA[review]]></category>

		<guid isPermaLink="false">http://www.terminally-incoherent.com/blog/?p=14282</guid>
		<description><![CDATA[When I reviewed Illium and Olympos by Dan Simmons I was quite impressed by how he managed to seamlessly blend the legendary ancient epic with post-singularity science fiction while staying true to both. I gave Simmons a lot of credit &#8230; <a href="http://www.terminally-incoherent.com/blog/2013/05/03/lord-of-light-by-roger-zelazny/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>When I reviewed <a href="http://www.terminally-incoherent.com/blog/2011/01/05/illium-and-olympos-by-dan-simmons/" class="liinternal">Illium and Olympos by Dan Simmons</a> I was quite impressed by how he managed to seamlessly blend the legendary ancient epic with post-singularity science fiction while staying true to both. I gave Simmons a lot of credit for the idea of exploiting <em>Clarke&#8217;s Third Law</em> to it&#8217;s fullest logical extent. Little did I know that such a thing has been done before, and much better by Roger Zelazny in late 70&#8242;s. </p>
<p>Zelazny is a household name due to the unyielding popularity of his anachronistic, heroic power fantasy series <em>Chronicles of Amber</em>. I only read 1 and a half books from that series, and never really thought much of them mostly because they were just that: fantasy. And you know <a href="http://www.terminally-incoherent.com/blog/2010/06/25/science-fiction-vs-fantasy/" class="liinternal">how I feel about fantasy</a>. Don&#8217;t get me wrong: Zelazny writes very well, and the series departs quite a bit from the standard Tolkienesque template all fantasy writers have to work of (as this is essentially the litmus test of the genre). It just didn&#8217;t do much for me.</p>
<p>I did however, very much enjoy his Lord of Light which is most definitely a work of Science Fiction, even if it reads much like Fantasy. Zelazny was able to seamlessly blend the somewhat exotic (to us Westerners) myths of the Hindu pantheon with a dash of speculative science fiction creating something very unique and awesome. So awesome in fact that many years later Dan Simmons successfully re-used that formula, but using the more familiar Homeric epic as the backdrop. The premise, and for the most part the overall plot arc are rather similar.</p>
<div id="attachment_14293" class="wp-caption aligncenter" style="width: 430px"><a href="http://www.terminally-incoherent.com/blog/wp-content/uploads/2013/04/Lord-of-Light-2.jpg" class="liimagelink"><img src="http://www.terminally-incoherent.com/blog/wp-content/uploads/2013/04/Lord-of-Light-2.jpg" alt="Lord of Light" width="420" height="633" class="size-full wp-image-14293" /></a><p class="wp-caption-text">Lord of Light</p></div>
<p>The big difference between the two however is that Zelazny has more style. His omniscient, third person narrator never really breaks character and recants the story as if it was an old legend. He is deliciously ignorant of the SF elements of the story. Only the characters themselves are allowed to break the facade and utter anachronisms or explain technological gadgets. This, in my opinion gives the book an air of mystery as initially the reader is not privy to the inner workings of this world. It is plain that the magic of the setting is not at all magical, that the individuals who call themselves gods are not at all divine, and that the two moons in the sky, and planetary rings suggest visible at night suggest the story is not taking place on Earth at all.</p>
<p>Part of the fun with this book is slowly unraveling the mysteries of the setting, so I wouldn&#8217;t want to spoil too much of the plot. It will suffice to say it is worth reading. The characters are rather vibrant and colorful, and you come to enjoy them. Zelazny is a great storyteller an he weaves the plot threads well in a non-linear fashion creating a tapestry of legends and allegories about an enigmatic hero who is sometimes a prince, sometimes an enlightened teacher, sometimes a traveler possessed by demons and sometimes counted as one of the mighty gods who forged the world. Each story is another piece of the puzzle that helps you understand the enigmatic opening chapter in which someone or some thing is yanked out of Nirvana and incarnated into a human body, to aid a conspiracy in plot to topple Heaven.</p>
<p>I should probably also add that the protagonist is actually someone I could easily relate to and identify with: Sam is the last of the Accelerationist. He is the Prometheus of his world: one who aims to advance the technological progress and bring about change at any cost. His ideals mirror those of mine and I would like to think that if I was in his place I&#8217;d embark on a similar quest.</p>
<p>If you liked Simmon&#8217;s Illium novels, I highly recommend this book. If you didn&#8217;t like them, it is still worth picking up, considering it is shorter, more succinct and strikingly different not only with respect to the setting but also to the writing style. It is just a joy to read.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.terminally-incoherent.com/blog/2013/05/03/lord-of-light-by-roger-zelazny/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Vim: Tabs and Buffers</title>
		<link>http://www.terminally-incoherent.com/blog/2013/05/01/vim-tabs-and-buffers/</link>
		<comments>http://www.terminally-incoherent.com/blog/2013/05/01/vim-tabs-and-buffers/#comments</comments>
		<pubDate>Wed, 01 May 2013 14:05:21 +0000</pubDate>
		<dc:creator>Luke Maciak</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://www.terminally-incoherent.com/blog/?p=14280</guid>
		<description><![CDATA[Lets talk about Vim tabs today, because they seem to be a source of perpetual confusion for new users. Most conventional text editors use tabbed interfaces the same way as web browsers do. You take a file, and load it &#8230; <a href="http://www.terminally-incoherent.com/blog/2013/05/01/vim-tabs-and-buffers/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Lets talk about Vim tabs today, because they seem to be a source of perpetual confusion for new users. Most conventional text editors use tabbed interfaces the same way as web browsers do. You take a file, and load it into a tab and that&#8217;s all there is to it. There is a direct, unbreakable, strict, one-to-one relationship between a tab and an edited file and it cannot be broken at run-time. You can&#8217;t for example load another file into an existing tab without either overwriting its contents. This is sort of the behavior we came to expect from tabbed interfaces.</p>
<p>Vim novices usually come with these preconceived notion of how the tab metaphor ought to work in a text editing context, and become confused when Vim refuses to play by these rules. To put it simply, tabs in Vim are different. They won&#8217;t work the way you expect them to, because they were not designed for that. The one file per tab convention is not the Vim way.</p>
<p>Let me preempt the obligatory self-professed &#8220;usability expert&#8221; snark comments about vim breaking established UI conventions: please kindly go fuck a hedgehog. Bill Joy was establishing his own conventions when he wrote vi while doing your mom long before she spawned you and then became so fat she developed her own event horizon. Complaining that Vim doesn&#8217;t implement tabs the same exact way Sublime Text does makes very little sense when you consider the huge architectural differences between these two editors.</p>
<p>The original vi editor, and earlier versions of Vim did not really support tabbed interface. They did however support editing multiple files with a single instance of the program using buffers. If you have ever progamatically manipulated text in some way, you should be familiar with the concept of a buffer. To put it plainly a buffer is the copy of your document that is held in memory as you manipulate it. When you open a file, you load it into a memory buffer, and that&#8217;s where your changes are applied as you work on it. When you start a new document, your text is held in a memory buffer until you decide to write it to disk. Vim maintains a list of buffers that it has currently open, and you can view it by issuing the <samp>:buffers</samp> command (or alternatively the shorter <samp>:ls</samp> command alias).</p>
<p>You can typically only edit a single buffer at a time, so the current (or active) buffer is the one displayed on your screen, while all the other buffers are hidden away. When you open a new file, a new buffer is created and immediately displayed. The buffer that was being shown before it is not gone, but simply hidden away. Buffers are <em>&#8220;like&#8221;</em> tabs in modern editors, only they don&#8217;t have a graphical UI representation. They work the same though &#8211; you activate one of them, and it&#8217;s contents become visible while all the other ones are hidden away.</p>
<div id="attachment_14286" class="wp-caption aligncenter" style="width: 650px"><a href="http://www.terminally-incoherent.com/blog/wp-content/uploads/2013/04/buffers1.jpg" class="liimagelink"><img src="http://www.terminally-incoherent.com/blog/wp-content/uploads/2013/04/buffers1.jpg" alt="Vim Buffers" width="640" height="480" class="size-full wp-image-14286" /></a><p class="wp-caption-text">Vim Buffers</p></div>
<p>You can load any open buffer into your <em>view-port</em> by issuing the <samp>:b</samp> command followed by a number or partial file name which can be tab completed. The term &#8220;view-port&#8221; is a key concept here because it helps you understand what is really going on. It is best to think of the display area as a virtual port-hole through which you cane interact with your buffers one at a time. </p>
<p>When you create a split (for example using the <samp>:vsplit</samp> command) you effectively create two (or more) independent view-ports. Each one has access to Vim&#8217;s internal buffer list. So now you can have multiple buffers on the screen at the same time. Or you can load the same buffer in two splits, and watch changes you make on one side be automatically reflected on the other if that&#8217;s what floats your boat.</p>
<p>In essence, buffers do everything the tabs in say Sublime Text or whichever other Notepad replacement editor you happen to fancy. They just don&#8217;t have the clickable UI warts you might be tempted to fondle with your mouse. Lets face it &#8211; using a mouse to operate Vim is kinda like trying to eat tomato soup with a crossbow: it can probably be done, but it doesn&#8217;t seem at all practical. In fact, Vim&#8217;s buffer list is more flexible than most tabbed interfaces. For example, have you ever tried recursively loading your entire project tree into your editor? Let&#8217;s say you have 600 odd source files: what happens if you try to load all of them into your editor of choice? Firstly, chances are your app will crash and burn trying to prerender all these files or hitting some internal memory limits. It will also likely slow down to a crawl trying to create and display tabs in rapid succession (likely refreshing the screen for each one in the sequence). Assuming none of that will happen, your tab bar will likely become completely unusable: tabs will likely be about 1 pixel wide, or will force you to scroll around to find what you need.</p>
<p>Vim on the other hand will slurp up the entire project into buffers without even breaking a sweat because it doesn&#8217;t really have to render them unless you actually bring them into focus. Navigating a huge buffer list is actually easier than messing around with hundreds of tabs because you can use tab completion to match partial file names and get to where you need to go very quickly. Vim will also allow you to do bulk-edits using the <samp>:buffdo</samp> command which lets you run arbitrary vim commands (be it normal mode actions, build-in commands or function calls) on all the open buffers.</p>
<p>So the question is: could we simply force Vim to treat each buffer as a tab for the sake of following the UI convention followed by most of the relatively immature and untested (as compared to vim) editors on the market? The answer is no, we couldn&#8217;t. Why? Not because snobbery or entrenched traditionalism. It wouldn&#8217;t work because it would break the split screen functionality which is an established feature that predates the tabbed interface by a few decades.</p>
<p>Right now Vim lets you keep splitting your screen into independent view-ports, each of which can operate on a different buffer. How do you reconcile that with tabbed interface? Let&#8217;s say you have 7 files open, and you load 4 of them into 2&#215;2 split (split horizontally, then split each half again vertically): what happens to the tabs that were loaded into splits of another tab? Do they disappear from the tab bar? What if you want to swap the contents of the splits? Do you re-shuffle the tab bar every time you split? If no, how do you deal with the tabs that are loaded in other tabs? If yes, how do you prevent tabs from moving around or getting lost? It seems like a very, very difficult problem to solve without altering existing functionality, with little to no benefit for existing users. </p>
<p>Tabs as implemented by Sublime Text and others simply make no sense in Vim world. Which brings us back to the original point of this post: what the fuck does Vim use the tabs for then? I can answer that with an example:</p>
<p>Imagine you are working on some code and you somehow ended up with a 2&#215;2 split as above with 4 buffers on the screen at the same time. Suddenly you realize you need to open another file to tweak a thing or two related to your current task. You don&#8217;t want to split your screens any further, but at the same time you don&#8217;t want to blow that layout away since you are not done with it. To make matters worse said file is big, complex and ugly, so it ought to be viewed in full screen. In the olden days you would probably suspend (or minimize) your current session, and open another Vim instance to do so. But with tabs you don&#8217;t have to. You just open a new tab, which gives you a fresh view-port. One with access to the same buffer list, the same registers and recorded macros. One which you can split into multiple panes if you wish.</p>
<div id="attachment_14290" class="wp-caption aligncenter" style="width: 650px"><a href="http://www.terminally-incoherent.com/blog/wp-content/uploads/2013/05/buffers2.jpg" class="liimagelink"><img src="http://www.terminally-incoherent.com/blog/wp-content/uploads/2013/05/buffers2.jpg" alt="Vim Tabs" width="640" height="480" class="size-full wp-image-14290" /></a><p class="wp-caption-text">Vim Tabs</p></div>
<p>Vim tabs are essentially work-spaces or view-ports. They are more or less the virtual desktops for your text editor. They are not meant to be bound to buffers, but instead provide you with additional work surface where you can &#8220;spread out&#8221; the buffers you have opened. </p>
<p>You can of course try to force Vim to follow the &#8220;one-buffer-per-tab&#8221; convention, but it will always be an uphill battle. I&#8217;ve seen people write elaborate VimL scripts to accomplish this. I have seen countless plugins that claim they offer this functionality. But most of them break into thousand pieces as soon as you try to do something fancy with splits, hidden buffers or the <samp>:buffdo</samp> and <samp>:argdo</samp> commands. And if you are never going to try anything fancy, complex or weird then what&#8217;s the point of using Vim anyway?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.terminally-incoherent.com/blog/2013/05/01/vim-tabs-and-buffers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Vim Sessions</title>
		<link>http://www.terminally-incoherent.com/blog/2013/04/29/vim-sessions/</link>
		<comments>http://www.terminally-incoherent.com/blog/2013/04/29/vim-sessions/#comments</comments>
		<pubDate>Mon, 29 Apr 2013 14:04:14 +0000</pubDate>
		<dc:creator>Luke Maciak</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://www.terminally-incoherent.com/blog/?p=14269</guid>
		<description><![CDATA[Vim, like Emacs and most other decent editors (as if there were any other decent editors out there) is an application that ought to only run in a single session on your machine. When using the console version of vim, &#8230; <a href="http://www.terminally-incoherent.com/blog/2013/04/29/vim-sessions/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Vim, like Emacs and most other decent editors (as if there were any other decent editors out there) is an application that ought to only run in a single session on your machine. When using the console version of vim, this is usually not a problem since it actually kinda forces this kind of workflow. But graphical vim (Gvim and MacVim respectively) actually have a lot to offer. After all, most of us run graphical window managers anyway in order to browse the web, so might as well use them to our benefit and run the slicker and prettier version of the editor. </p>
<p>From there It is quite easy to fall into a trap of running multiple instances of gvim side by side. While being able to look at two files at once is great, the best way to accomplish this with Vim are splits not multiple windows. Why? Well, when you open a new Gvim instance (ether via GUI or by running <samp>gvim</samp> from the command line it opens up as a separate process. It doesn&#8217;t share the environment of your existing vim instance. What does that mean? It means it can&#8217;t access the buffers you have open in the other window, which might actually be what you want. It also isn&#8217;t affected by any run-time customizations. It&#8217;s argument list, search path and work directory are initialized separately and do not interfere with each other. There are times when this is precisely what you want.</p>
<p>Chances are however that you are working on a few files that are part of the same project and thus you would probably want your Gvim instances to be able to interact. For example you might define a macro in one file, and wish to run it on the other &#8211; but you can&#8217;t. Hell, you can&#8217;t even directly yank and paste between the two windows because they have separate sets of registers. The only way to copy text from one window to the other is by way of the system clipboard, which only works if your version was compiled with support for it.</p>
<p>But what to do if you are a shell fiend, and basically just want to open your files from Bash? Well, you just need to learn one command line switch then:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">gvim <span style="color: #660033;">--remote-silent</span> filename</pre></td></tr></table></div>

<p>This will find a running instance of gvim and attach a new buffer to it. From there you can put them side by side using <samp>:vsplit</samp> (to create a vertical split) and <kbd>Ctrl</kbd>-<kbd>^</kbd> to toggle current split pane to the previous buffer and hide the split with <samp>:only</samp> when you no longer need it. The only thing you have to remember is that you don&#8217;t want to use the <samp>:q</samp> command because it will close the entire application. When you are done with a buffer you save it and then do <samp>:bd</samp> which in my mind stands for <em>&#8220;Be Done!&#8221;</em>. There is also the <em>&#8220;Be Well!&#8221;</em> command <samp>:bw</samp> which saves the buffer before closing.</p>
<p>Splits and buffers are really the way to go. Throw in tabs for a good measure and you can set up multiple sets of different split panes and flip between them without ever having to take your hands off the home row. But alas, even with cute mnemonics for the buffer closing commands accidents happen. I can&#8217;t tell you how many times I had a dozen files open in a single vim session, and in the middle of coding, out of pure habit I typed <samp>:wq</samp>. Suddenly, all the buffers are gone. All the splits forgotten. All the tabs purged. No data was lost in the process of course, since the files themselves were saved. The undo history was preserved as well because I use Vim 7.3 and enable the persistent undo feature. But the contents of the registers and the buffer list was blown away.</p>
<p>This used to happen to me at least once per day, until I set up automatic session saving for myself. Vim, as any good editor as a session feature. When you issue <samp>:mksession</samp> command it will save the list of open buffer and tabs as well as your register contents into a file called <var>Session.vim</var> in the current working directory. You can then run <samp>gvim -S</samp> and it will look for a session file in the current directory and restore from it. Alternatively you can <samp>:source</samp> that file.</p>
<p>Unfortunately this does not help you with accidental closing of vim, as you need to remember to save the session yourself. Of course you could always add this to your <var>.vimrc</var>:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #804040;">autocmd</span> <span style="color: #25BB4D;">VimLeave</span> <span style="color: #000000;">*</span> <span style="color: #000000;">:</span>mksession<span style="color: #000000;">!</span></pre></td></tr></table></div>

<p>This will automatically save a session file each time you close your editor, which is much better. Unfortunately this will quickly litter your filesystem with session files. It is about as annoying as those damn .swp files vim loves to produce by default. I have disabled those long time ago because I have never actually had to use one and decided they were not worth the hassle. Session files in every directory bring back these bad memories. Fortunately you can redirect your session file to somewhere else, like:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #804040;">autocmd</span> <span style="color: #25BB4D;">VimLeave</span> <span style="color: #000000;">*</span> <span style="color: #000000;">:</span>mksession<span style="color: #000000;">!</span> <span style="color: #000000;">$</span>HOME<span style="color: #000000;">.</span><span style="color: #C5A22D;">&quot;/.vimsessions/session.vim&quot;</span></pre></td></tr></table></div>

<p>Now however you have only one session file, which gets overwritten every time the editor closes. This works when you accidentally blow away your buffer history like in the example above. But let&#8217;s say you are done for the night, and you close vim confident your session is saved. Then you run <samp>git commit</samp> to commit your changes into the repository. Git of course helpfully launches Vim to edit your commit log, and then happily overwrites your session. Next morning you want to get back to work, and your editor opens with a stale commit log message rather than a screen-full of buffers. Less than optimal, don&#8217;t you think?</p>
<p>I&#8217;ve been trying to figure out a good way to manage my sessions and eventually got an idea of naming my sessions. You can name your vim instance using the <samp>&#8211;servername</samp> command line switch like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">gvim <span style="color: #660033;">--servername</span> dev</pre></td></tr></table></div>

<p>You can grab that name inside Vim using the <var>v:servername</var> variable, and use it as a unique name given to your session. Whenever you work on a specific project you launch it with a relevant servername and that session will be preserved. If you later run Gvim without that switch, it defaults to <em>GVIM</em> or an empty string if you ran the console version. This means that editing a commit log won&#8217;t accidentally blow away your session.</p>
<p>Here is how I&#8217;ve done it:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #adadad; font-style: italic;">&quot; where do you want to save sessions?</span>
<span style="color: #804040;">let</span> g<span style="color: #000000;">:</span>session_dir = <span style="color: #000000;">$</span>HOME<span style="color: #000000;">.</span><span style="color: #C5A22D;">&quot;/.vimsessions&quot;</span><span style="color: #adadad; font-style: italic;">
&nbsp;
&quot; Save sessions whenever vim closes</span>
<span style="color: #804040;">autocmd</span> <span style="color: #25BB4D;">VimLeave</span> <span style="color: #000000;">*</span> <span style="color: #804040;">call</span> SaveSession<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #adadad; font-style: italic;">
&nbsp;
&quot; Saves the session to session dir. Creates session dir if it doesn't</span><span style="color: #adadad; font-style: italic;">
&quot; yet exist. Sessions are named after servername paameter</span>
<span style="color: #804040;">function</span><span style="color: #000000;">!</span> SaveSession<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #adadad; font-style: italic;">
&nbsp;
    &quot; get the server (session) name</span>
    <span style="color: #804040;">let</span> s = v<span style="color: #000000;">:</span>servername<span style="color: #adadad; font-style: italic;">
&nbsp;
    &quot; create session dir if needed</span>
    <span style="color: #804040;">if</span> <span style="color: #000000;">!</span><span style="color: #25BB4D;">isdirectory</span><span style="color: #000000;">&#40;</span>g<span style="color: #000000;">:</span>session_dir<span style="color: #000000;">&#41;</span>
    	<span style="color: #804040;">call</span> <span style="color: #25BB4D;">mkdir</span><span style="color: #000000;">&#40;</span>g<span style="color: #000000;">:</span>session_dir, <span style="color: #C5A22D;">&quot;p&quot;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #804040;">endif</span><span style="color: #adadad; font-style: italic;">
&nbsp;
    &quot; save session using the server name</span>
    <span style="color: #804040;">execute</span> <span style="color: #C5A22D;">&quot;mksession! &quot;</span><span style="color: #000000;">.</span>g<span style="color: #000000;">:</span>session_dir<span style="color: #000000;">.</span><span style="color: #C5A22D;">&quot;/&quot;</span><span style="color: #000000;">.</span>s<span style="color: #000000;">.</span><span style="color: #C5A22D;">&quot;.session.vim&quot;</span>
endfunc</pre></td></tr></table></div>

<p>That&#8217;s only part of the equation. Once you have sessions that save automatically, you also want an easy way to restore them. So I made another function that runs when you open vim:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #adadad; font-style: italic;">&quot; Load session when vim is opened</span>
<span style="color: #804040;">autocmd</span> <span style="color: #25BB4D;">VimEnter</span> <span style="color: #000000;">*</span> nested <span style="color: #804040;">call</span> OpenSession<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #adadad; font-style: italic;">
&nbsp;
&quot; Open a saved session if there were no file-names passed as arguments</span><span style="color: #adadad; font-style: italic;">
&quot; The session opened is based on servername (session name). If there</span><span style="color: #adadad; font-style: italic;">
&quot; is no session for this server, none will be opened</span>
<span style="color: #804040;">function</span><span style="color: #000000;">!</span> OpenSession<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #adadad; font-style: italic;">
&nbsp;
    &quot; check if file names were passed as arguments</span>
    <span style="color: #804040;">if</span> <span style="color: #25BB4D;">argc</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> == <span style="color: #000000; font-weight:bold;">0</span>
&nbsp;
    	<span style="color: #804040;">let</span> <span style="color: #668080;">sn</span> = v<span style="color: #000000;">:</span>servername
    	<span style="color: #804040;">let</span> file = g<span style="color: #000000;">:</span>session_dir<span style="color: #000000;">.</span><span style="color: #C5A22D;">&quot;/&quot;</span><span style="color: #000000;">.</span><span style="color: #668080;">sn</span><span style="color: #000000;">.</span><span style="color: #C5A22D;">&quot;.session.vim&quot;</span><span style="color: #adadad; font-style: italic;">
&nbsp;
        &quot; if session file exists, load it</span>
    	<span style="color: #804040;">if</span> <span style="color: #25BB4D;">filereadable</span><span style="color: #000000;">&#40;</span>file<span style="color: #000000;">&#41;</span>
            <span style="color: #804040;">execute</span> <span style="color: #C5A22D;">&quot;source &quot;</span><span style="color: #000000;">.</span>file
        <span style="color: #804040;">endif</span>
    <span style="color: #804040;">endif</span>
endfunc</pre></td></tr></table></div>

<p>One thing I decided here was that if I was opening a file in Vim, chances are I wasn&#8217;t interested in restoring the session. Most of the time when I run Gvim from the command line with a file argument I just want to quickly edit said file and be done with it. So I opted to never bother restoring sessions if a file argument was present.</p>
<p>If vim is opened without specifying a file, then I try to find a session file based on the servername variable. To make this even more seamless I actually added a few aliases to my <var>.bashrc</var>:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gv</span>=<span style="color: #ff0000;">&quot;gvim --servername DEV&quot;</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">g</span>=<span style="color: #ff0000;">&quot;gvim --servername DEV --remote-silent&quot;</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gv</span>=<span style="color: #ff0000;">&quot;gvim --remote-silent&quot;</span></pre></td></tr></table></div>

<p>So when I sit down to work I run <samp>gd</samp> on its own and it creates a named <em>DEV</em> session for me. Then I use <samp>g</samp> followed by filename to attach new buffers to it as needed needed. If I ever have to open an unrelated file, I use plain old gvim command which opens up a completely independent instance which doesn&#8217;t touch my session data. If I want to attach buffers to that session I can do that with <samp>gv</samp>. It works quite well, and it also reduces the amount of typing that I need to do at the console &#8211; so it&#8217;s a win-win in my book.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.terminally-incoherent.com/blog/2013/04/29/vim-sessions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The Dusk of Bloggosphere</title>
		<link>http://www.terminally-incoherent.com/blog/2013/04/24/the-dusk-of-bloggosphere/</link>
		<comments>http://www.terminally-incoherent.com/blog/2013/04/24/the-dusk-of-bloggosphere/#comments</comments>
		<pubDate>Wed, 24 Apr 2013 14:02:49 +0000</pubDate>
		<dc:creator>Luke Maciak</dc:creator>
				<category><![CDATA[technology]]></category>

		<guid isPermaLink="false">http://www.terminally-incoherent.com/blog/?p=14155</guid>
		<description><![CDATA[Remember when blogs were the hottest thing on the internet? No of course you don&#8217;t. That was well before your time, wasn&#8217;t it? Back in 2003 when I first got the idea of creating Terminally Incoherent, blogging swiftly becoming one &#8230; <a href="http://www.terminally-incoherent.com/blog/2013/04/24/the-dusk-of-bloggosphere/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Remember when blogs were the hottest thing on the internet? No of course you don&#8217;t. That was well before your time, wasn&#8217;t it?</p>
<p>Back in 2003 when I first got the idea of creating Terminally Incoherent, blogging swiftly becoming one of the hottest buzzwords on the internet. I was of course aware of blogging almost since it&#8217;s inception in the late 90&#8242;s but I resisted the siren&#8217;s call of the new publishing platform because I didn&#8217;t think I had anything interesting to say. I remember proudly bashing the idea on forums saying things like &#8220;what on earth would I write a blog about&#8221; as I was cranking out 6 thousand word analysis posts for new episodes of Farscape, or twice as long posts bashing SciFi Channel for sucking and killing all the good series. I guess most of us back then didn&#8217;t fully comprehend what exactly blogging was because of LiveJournal.</p>
<p>LiveJournal was one of the first successful (well, as far as you can measure success in therms of usage and popularity &#8211; not necessarily profitability) platforms and it used the &#8220;online diary&#8221; metaphor. It was it&#8217;s primary use case, and how it advertised itself to new users and so this is how we viewed blogging in general: as public vanity diaries. By the time I got into this thing I still didn&#8217;t fully understand what to do with a blog, but at least I tried. It actually took me a few years to actually get into something of a rhythm and find the voice for this site. And as soon as I did, something unthinkable happened &#8211; I have gained some audience.</p>
<p>I caught the wave and kept riding it. I was never the leader of the pack but I&#8217;m actually pretty happy with the way things turned out. I got inbound links from Digg, Reddit and few other influential places (and my server crashed and burned each time that happened) and got quite a few interesting discussion threads in the comments. For the past few years I have always had enough readers for the few adds I display here to pay for my hosting entirely and leaving me with some left-over extra change I can spend on like one coffee at Starbucks. I&#8217;d say this is pretty good for a site with unintelligible name and no clear focus.</p>
<p>I mean, how do you explain what Terminally Incoherent is about to someone who never visited it in a few words? It&#8217;s a blog about programming, and science fiction, and futurism and movies, and video games and anime and&#8230; You basically have to list bunch of unrelated topics which have a single unifying theme: they are things I&#8217;m interested in writing about. In short, this is a personal blog written by one guy about his nerdy interests. This is very much a vanity diary &#8211; something I never thought I would do, and never set out to create. But that&#8217;s essentially what I&#8217;ve been writing since 2003.</p>
<p>These days it almost feels like I&#8217;m the last guy on the internet doing this sort of thing. This kind of deeply personal, ego driven blogging has went out of vogue. It is something that mostly doesn&#8217;t make sense anymore, unless you happen to lead a very interesting life as a celebrity or other type of person of interest whose opinions count. Most of the smaller blogs you see these days have a very narrow, topic oriented focus. Most of the successful ones also have more than one contributor.</p>
<p>I have used to subscribe to a lot of blogs with a profile similar to mine, but most of them have been abandoned. It is actually quite sad, but as I was going through my subscription list tying to slim it down in preparation for <a href="http://www.terminally-incoherent.com/blog/2013/03/18/goodbye-google-reader/" class="liinternal">Google Reader closing</a> I realized just how many sites I used to read went silent over the years without anyone noticing. I guess this is a natural thing &#8211; sites go dead all the time and new ones get created to pick up the slack. Blogosphere seems to have shrunk and consolidated itself around big brand names and personalities.</p>
<p>Today it is easier to create a personal blog than it ever was, and yet few people choose to do so. I think part of the problem is that blogs are a commitment. They take effort. Blog readers expect to read &#8211; they expect at least few hundred words per post, and it is apparently more than most people are willing to write in a single sitting.</p>
<p>The explosion of social media has changed the personal publishing landscape, and blogs are no longer the preferred medium. They take time to set up and maintain. They don&#8217;t come with built in audience whereas platforms such as Facebook, Twitter or Tumblr do. Why run a link blog if you can just tweet interesting links to your followers? Why bother typing anything if you can just share, tweet and re-blog funny pictures using a bookmarklet. Why bother with RSS if you can have your content filtered and cherry-picked via social media or aggregation sites like reddit. The mainstream has moved onto the next shiny thing, and with them went all those who got into blogging to cash in on the popularity of the medium. The kids who thought it was hip and cool to run a pic blog, travelogue or tech news echo chamber (or whatever else was in vogue at the time) packed their bags and moved over to the social pastures.</p>
<p>Who is left? Well there are only two groups of people who blog today: people who get paid to blog, an those who blog for fun. The first group is the larger one &#8211; corporate blogs are still pretty fashionable, and allow faceless profit machines pretend like they have a connection with their <strike>victims</strike> customers. Journalism is moving online wholesale and the boundary between news stream and a blog becomes blurrier every day. Once upon a time we asked ourselves a question: &#8220;can bloggers be considered journalists?&#8221;. Today that question seems silly and infantile. Of course they can be: sometimes. Not every person with a blog is a journalist, just like not every person who publishes for a print publication is one. It is about what you write and how you write it. </p>
<p>Unfortunately most online &#8220;publications&#8221; are not really doing anything even remotely journalistic. Journalism doesn&#8217;t really get you enough traffic to make your site profitable. Top ten lists <a href="http://www.penny-arcade.com/report/article/ad-blocker-the-games-press-and-why-cosplay-galleries-lead-to-better-reporti" class="liexternal">and sexy cossplay galleries do</a>. So 90% of the &#8220;professionally&#8221; produced blog-like content is essentially garbage: click traps, circle-jerks or worse: paid advertising (aka. unbiased video game reviews).</p>
<p>The other group &#8211; one which I&#8217;m part of &#8211; blogs in their free time. Since we do not seek to gain any profit from our sites, we are free to make our content as shitty, or wonderfully deep. Usually we do a little bit of both (I myself tend to err on the side of shitty, but that&#8217;s usually just lack of inspiration). The problem with blogging for fun is that while it is a labor of love, it is still time consuming and resource intensive. Writing a blog post that is actually coherent is not trivial (fortunately I picked the right title for my blog so I am exempt from the coherence clause). It does require effort and commitment. It turns out that most people only have a few years of content in them.</p>
<p>I&#8217;ve been producing random content on this blog for close to a decade now. People ask me sometimes why I stuck with it for so long, but I don&#8217;t really have an answer. It is a creativity outlet. It is a way for me to organize my thoughts. It is a way for me to create a link to the past. Browsing my own archives is like a stroll down the memory late for me sometimes. I might be talking about some video game or programming project and that&#8217;s all you see when you read it. But when I read it, I also recall where I was when I was writing it. It is something very public and yet very personal. And after 10 years of doing it, I only now begin to feel like I&#8217;m kinda catching the drift of it.</p>
<p>I probably can&#8217;t promise you another decade of Terminally Incoherent (cause you know &#8211; we might have a singularity event in the meantime), but I can definitely assure you I am in no danger of quitting anytime soon. If it means I&#8217;m the last guy with randomly weird and slightly boring blog on the internet then so be it.</p>
<p>That said, when I was going through my RSS subscriptions I was astonished how many good blogs just died in the last few years or turned to absolute shit. Some ended up link farms, others started chasing profits with pointless garbage entries and some simply switched focus (worst thing that can happen to a good programming blog: the author gets promoted to management, and starts blogging about snooze-worthy managerial stuff instead talking about code). Someone asked me to publish my &#8220;must read&#8221; RSS list, but when I went to compile I was shocked to realized almost all the blogs in my important folder haven&#8217;t had any updates in about a year. I was even more shocked that I didn&#8217;t notice it until now.</p>
<p>I was quite upset when I found out Google is shutting down their RSS service, but now that I sat down and analyzed my subscriptions with a cool head I must admit that there are shockingly few live sites on there. My OPML is tumbleweed central &#8211; a ghost city full of empty husks that were once interesting and thoughtful publications. I guess it puts Google&#8217;s decision in perspective.</p>
<p>So out of curiosity, what blogs do you guys currently read? Who would you recommend to subscribe to and follow and why? I want to re-build my RSS collection where should I start?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.terminally-incoherent.com/blog/2013/04/24/the-dusk-of-bloggosphere/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Spoilers in the age of Social Media and Ubiquitous Time Shifting</title>
		<link>http://www.terminally-incoherent.com/blog/2013/04/22/spoilers-in-the-age-of-social-media-and-ubiquitous-time-shifting/</link>
		<comments>http://www.terminally-incoherent.com/blog/2013/04/22/spoilers-in-the-age-of-social-media-and-ubiquitous-time-shifting/#comments</comments>
		<pubDate>Mon, 22 Apr 2013 14:08:17 +0000</pubDate>
		<dc:creator>Luke Maciak</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.terminally-incoherent.com/blog/?p=14241</guid>
		<description><![CDATA[You know what really grinds my gears? Folks who complain about TV spoilers in social media. Quite often I will tweet something about an episode of The Walking Dead or Game of Thrones several hours, or even days after the &#8230; <a href="http://www.terminally-incoherent.com/blog/2013/04/22/spoilers-in-the-age-of-social-media-and-ubiquitous-time-shifting/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>You know what really grinds my gears? Folks who complain about TV spoilers in social media. Quite often I will tweet something about an episode of The Walking Dead or Game of Thrones several hours, or even days after the original air date of the episode, and there is always someone who gives me shit about it because they haven&#8217;t watched it yet. We live in an age where DVR&#8217;s are more or less ubiquitous and most people &#8220;tape&#8221; their favorite shows and watch them later. Granted, DVR is <a href="http://www.terminally-incoherent.com/blog/2011/12/14/the-big-dvr-swindle/" class="liinternal">far inferior to actual tape</a> (as in VCR tape) in when it comes to actual portability and usability of the time shifted material. Of course this is mostly a concern only in principle since bit torrent sites provide even better time and format shifting services to the masses for free. Needless to say, there are many ways to watch your favorite show on your own schedule whether you own a DVR or not.</p>
<p>The problem with this is that TV watching has always been a social experience. With the mainstreaming of social media, many viewers such as me want to engage with friends and community at large via those channels and discuss the on-screen events as they unfold. Many TV networks such as AMC have noticed this trend and actually encourage it by posting Twitter hash tag suggestions during commercial breaks. It is actually a lot of fun to live tweet your favorite show, and bounce around jokes and/or speculations with other fans. But there is always someone in your social circle who is not watching the show right now and wont be able to watch the show for days and thus becomes very upset at all the spoilers in their feed.</p>
<p>You would think that if you happen to be a fan of some show, and friends with many other fans via social media would know better than to wade into the update stream as a new episode is on the air and expect not to be spoiled. But lets say that live tweeting is just a tad bit inconsiderate. Perhaps we ought to leave a small courtesy window between the original air date, and actual posting of the plot revealing spoilers? What should that window be though? Is the morning after an acceptable compromise? Two days later? Next weekend? A month?</p>
<p>The truth is that spoiler-complainers don&#8217;t actually want a compromise. They want their social media to be pristine clean and devoid of spoilers forever. They want to watch their shows on their own schedule (as is their right) and want everyone to hold off any and all discussion of said shows until that an undetermined and arbitrary date when they choose to watch them (which I would say is inconsiderate on their part). In fact, if you happen to be followed by a lot of people, almost invariably you will piss off someone who doesn&#8217;t even get the show you are discussing in their region. Yes, I have actually been yelled at by someone who never seen Game of Thrones but was considering &#8220;<em>maybe</em> downloading it one day&#8221;.</p>
<p>So realistically, the only way to avoid the ire of the spoiler-complainers is to keep your mouth shut, and never mention any of the series you like in your social media feeds, ever.</p>
<p>I got fed up by this and decided to do something about it. The simplest and most direct solution to this problem would of course be to gather all these whining spoiler haters, put them in a rocket and launch it into the center of the sun. But since I don&#8217;t have a rocket, I decided to code up a solution instead. Something that would allow me to post spoilers in my tweets without pissing anyone off in the process.</p>
<p>The big thing about posting plot twists and revelations in Tweets is that there is really no way to hide or filter them. Forums and blogs have a long standing tradition of using vertical white space (often known as &#8220;spoiler space&#8221;) to set the spoilers apart from regular post, or change the font color to obscure the text. Twitter&#8217;s limited nature does not allow you to do any of that. There is really no way to hide part of your tweet in any meaningful way&#8230; Except of the <a href="https://dev.twitter.com/docs/cards" class="liexternal">Twitter Cards</a> functionality.</p>
<p>When you tweet a link to a site which implements Twitter Cards, the users can click on it and it expands to reveal an excerpt and a picture from said link. I figured I could exploit this functionality as a crude way to hide spoilers. So the idea was to design a service that would allow one to create a spoiler text, bind it to a unique URL and then automatically tweet that URL along with a non-spoiler teaser message. Let me show you an example &#8211; I could tweet something like this:</p>
<div id="attachment_14250" class="wp-caption aligncenter" style="width: 525px"><a href="http://www.terminally-incoherent.com/blog/wp-content/uploads/2013/04/spoiltweet.png" class="liimagelink"><img src="http://www.terminally-incoherent.com/blog/wp-content/uploads/2013/04/spoiltweet.png" alt="Spoiler Tweet" width="515" height="84" class="size-full wp-image-14250" /></a><p class="wp-caption-text">Spoiler Tweet</p></div>
<p>The link in the post would then link to a web page like this:</p>
<div id="attachment_14252" class="wp-caption aligncenter" style="width: 650px"><a href="http://www.terminally-incoherent.com/blog/wp-content/uploads/2013/04/example.png" class="liimagelink"><img src="http://www.terminally-incoherent.com/blog/wp-content/uploads/2013/04/example-640x209.png" alt="Spoiler" width="640" height="209" class="size-large wp-image-14252" /></a><p class="wp-caption-text">Spoiler</p></div>
<p>Note that I&#8217;m not showing Game of Thrones spoiler as an example for reasons that should probably be obvious by now. The Twitter Cards integration doesn&#8217;t work as of yet, because apparently it takes months for that functionality to get approved. The service however does integrate with Twitter so you don&#8217;t actually need to sign up for an account to start posting spoilers. You simply sign in with Twitter and you are ready to go.</p>
<p>I&#8217;m actually pretty happy with the URL I was able to nab for this service: <a href="http://dontspoil.us" class="liexternal">dontspoil.us</a>. What I really wish I was able to get was <em>spoil.er</em> but unfortunately the .er TLD is not open to the public yet.</p>
<div id="attachment_14260" class="wp-caption aligncenter" style="width: 650px"><a href="http://www.terminally-incoherent.com/blog/wp-content/uploads/2013/04/site.jpg" class="liimagelink"><img src="http://www.terminally-incoherent.com/blog/wp-content/uploads/2013/04/site-640x527.jpg" alt="This is how it looks" width="640" height="527" class="size-large wp-image-14260" /></a><p class="wp-caption-text">This is how it looks</p></div>
<p>As with most of my projects, this one is Open Source. You can <a href="https://github.com/maciakl/TweetSpoiler" class="liexternal">peruse the code</a> at your convenience over at GitHub.</p>
<p>The front end is my usual: clean HTML5 with a little bit of Twitter Boostrap which in this case I&#8217;m using primarily for the responsive grid and pretty buttons. I&#8217;m also using jQuery which drives some of the Bootstrap effects, and also lets me do very simple validation on user input.</p>
<p>The back end was written entirely in Ruby, using the excellently simple <a href="http://www.sinatrarb.com/" class="liexternal">Sinatra</a> as the web framework and <a href="http://datamapper.org/" class="liexternal">Data Mapper</a> as the ORM. I resisted the urge to host it on my old and busted Dreamhost account like I did with <a href="http://markdownjournal.com/" class="liexternal">MarkdownJournal.com</a>. As you probably seen that host has major stability problems, so I instead opted for something different: <a href="https://www.heroku.com/" class="liexternal">Heroku</a> cloud hosting platform.</p>
<p>If you are not familiar with Heroku, it is basically <a href="https://cloud.google.com/products/" class="liexternal">Google App-Engine</a> style service but with slightly different focus and a lot more flexibility. Whereas Google sort of binds you to its own proprietary API and it&#8217;s own proprietary NoSQL data storage, Heroku gives you a wide array of options. For example, in this project I&#8217;m using PostgreSQL database hosted and maintained entirely on the service, and running in fully distributed mode.</p>
<p>Heroku&#8217;s main strength as a hosting platform is the promise of almost infinite scalability, but that&#8217;s not really why I set my service up there. I don&#8217;t imagine it becoming popular enough to warrant more than a single Heroku dyno and a dev-mode database plan. I mostly made it to solve my own problem and picked the platform because I wanted to learn more about it. I created my Heroku account couple of years ago, when the service was still young but never really did anything with it until now.</p>
<p>I must say it is interesting environment. Working with it is a little weird at first because it doesn&#8217;t always behave as you would expect a regular platform to behave. Because if it&#8217;s decentralized nature and its limitations there are little quirks you need to keep in mind &#8211; like the differences between the way your app runs locally when you test it, and the way it works when deployed to the Heroku cloud.</p>
<p>I fully expected it to be the most annoying part of the project, but it wasn&#8217;t. That distinction goes to Twitter. When I was writing the application I kept running into Twitter API rate limits. I didn&#8217;t know this when I set out to create this service but each Twitter app is limited to 15 API requests per minute for each unique Twitter user. This of course includes authentication and any look-ups you may want to do.</p>
<p>For example, in the early version of the app I would authenticate the user using the Twitter OAUth, then look up some information about the account that just signed in such as Twitter username, display name and the profile picture URL. This actually counted as five requests, so I could only refresh the page 3 times per minute. If I accidentally made one request too many, I would get locked out for 15 minutes during which all my requests would be rejected. I quickly learned to cache everything and use as few requests as possible but it was extremely annoying at first.</p>
<p>Anyway, the service is now up and running. The Twitter Cards integration isn&#8217;t there yet so for now you have to click on the link to see the spoilers. Eventually I&#8217;m hoping you will just be able to expand them. This will have double benefit &#8211; it will make it easier for users to consume the spoilers, and it will lessen the traffic on my end allowing me to keep the service on a cheep (and preferably free) Heroku plan.</p>
<p>As usual, comments and suggestions are appreciated. If you manage to break the service, please submit <a href="https://github.com/maciakl/TweetSpoiler/issues" class="liexternal">a bug report</a> and I&#8217;ll get right on fixing it. Or, if you can code in Ruby feel free to fix the bug yourself and just issue a pull request for faster turnaround time.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.terminally-incoherent.com/blog/2013/04/22/spoilers-in-the-age-of-social-media-and-ubiquitous-time-shifting/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
