<?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>A Party to the World &#187; computer science</title>
	<atom:link href="http://dan.lecocq.us/wordpress/category/cs/feed/" rel="self" type="application/rss+xml" />
	<link>http://dan.lecocq.us/wordpress</link>
	<description>Life, love, and computer science</description>
	<lastBuildDate>Fri, 06 Jan 2012 17:21:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Chef-Solo and God &#8212; A Divine Duo</title>
		<link>http://dan.lecocq.us/wordpress/2012/01/06/chef-solo-and-god-a-divine-duo/</link>
		<comments>http://dan.lecocq.us/wordpress/2012/01/06/chef-solo-and-god-a-divine-duo/#comments</comments>
		<pubDate>Fri, 06 Jan 2012 17:21:19 +0000</pubDate>
		<dc:creator>dan.lecocq</dc:creator>
				<category><![CDATA[computer science]]></category>
		<category><![CDATA[SEOmoz]]></category>
		<category><![CDATA[chef]]></category>
		<category><![CDATA[god]]></category>
		<category><![CDATA[seomoz]]></category>

		<guid isPermaLink="false">http://dan.lecocq.us/wordpress/?p=1050</guid>
		<description><![CDATA[I recently wrote a new post on the SEOmoz dev blog about our deployment with chef-solo and god on EC2.]]></description>
			<content:encoded><![CDATA[<p>I recently wrote a new post on the <a href="http://devblog.seomoz.org/2012/01/chef-solo-and-god-a-divine-duo/" title="SEOmoz Dev Blog Post">SEOmoz dev blog</a> about our deployment with chef-solo and god on EC2.</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.lecocq.us/wordpress/2012/01/06/chef-solo-and-god-a-divine-duo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Never Trust Callbacks</title>
		<link>http://dan.lecocq.us/wordpress/2011/10/16/never-trust-callbacks/</link>
		<comments>http://dan.lecocq.us/wordpress/2011/10/16/never-trust-callbacks/#comments</comments>
		<pubDate>Sun, 16 Oct 2011 16:27:23 +0000</pubDate>
		<dc:creator>dan.lecocq</dc:creator>
				<category><![CDATA[computer science]]></category>
		<category><![CDATA[callbacks]]></category>
		<category><![CDATA[exceptions]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[lessons learned]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://dan.lecocq.us/wordpress/?p=1042</guid>
		<description><![CDATA[It&#8217;s a lesson that has now been hammered home repeatedly in my head: never trust callbacks. Just don&#8217;t. Go ahead and execute them, but if you trust them to not throw exceptions or errors, you are in for a world of unhappiness. For me, I first learned this lesson when making use of twisted, writing [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s a lesson that has now been hammered home repeatedly in my head: never trust callbacks. Just don&#8217;t. Go ahead and execute them, but if you trust them to not throw exceptions or errors, you are in for a world of unhappiness.</p>
<p>For me, I first learned this lesson when making use of twisted, writing some convenience classes to help with some of the somewhat odd class structure they have. (Sidebar: twisted is an extremely powerful framework, but their naming schemes are not what they could be.) Twisted makes heavy use of a deferred model where callbacks are executed in separate threads, while mission-critical operations run in the main thread. My convenience classes exposed further callbacks that could be overridden in subclasses, but I made the critical mistake of not executing that code inside of a try/except block.</p>
<p>Twisted has learned this lesson. In fact, their deferred model makes it very hard to throw a real exception. If your callbacks fail, execution takes a different path &#8212; calling errback functions. In fact, twisted is so pessimistic about callbacks (rightly so) that you just can&#8217;t make enough exceptions to break out of errback functions. However, wrapped in my convenience classes were pieces of code that were mission critical, and my not catching exceptions in the callbacks I provided was causing me a world of hurt.</p>
<p>That whole experience was enough to make me learn my lesson. Then, a few days ago I encountered it again in a different library, in a different language, in a different project, where I was exposing callbacks for user interface code in JavaScript. The logical / functional chunk of code exposed events that the UI would be interested in, but was too trusting, leading to errors in callbacks skipping over critical parts of the code.</p>
<p>All in all, <strong>when exposing callbacks, never trust a callback to not throw an exception.</strong> Even if you wrote the callbacks it&#8217;s executing (as was the case with both of these instances, at least in the beginning). <strong>Callbacks are a courtesy &#8212; a chance for code to be notified of an event,</strong> but like many courtesies, they can be abused.</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.lecocq.us/wordpress/2011/10/16/never-trust-callbacks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python and Arbitrary Function Arguments &#8211; **kwargs</title>
		<link>http://dan.lecocq.us/wordpress/2011/09/14/python-and-arbitrary-function-arguments-kwargs/</link>
		<comments>http://dan.lecocq.us/wordpress/2011/09/14/python-and-arbitrary-function-arguments-kwargs/#comments</comments>
		<pubDate>Wed, 14 Sep 2011 12:18:56 +0000</pubDate>
		<dc:creator>dan.lecocq</dc:creator>
				<category><![CDATA[computer science]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[keyword arguments]]></category>
		<category><![CDATA[kwargs]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://dan.lecocq.us/wordpress/?p=1023</guid>
		<description><![CDATA[Python has a pretty useful policy: named arguments. When you call a function, you can explicitly say that such-and-such value is what you&#8217;re providing for a particular argument, and can even include them in any order: def hello&#40;first, last&#41;: print 'Hello %s %s' % &#40;first, last&#41; &#160; hello&#40;last='Lecocq', first='Dan'&#41; In fact, you can programmatically gain [...]]]></description>
			<content:encoded><![CDATA[<p>Python has a pretty useful policy: named arguments. When you call a function, you can explicitly say that such-and-such value is what you&#8217;re providing for a particular argument, and can even include them in any order:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> hello<span style="color: black;">&#40;</span>first, last<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Hello %s %s'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>first, last<span style="color: black;">&#41;</span>
&nbsp;
hello<span style="color: black;">&#40;</span>last=<span style="color: #483d8b;">'Lecocq'</span>, first=<span style="color: #483d8b;">'Dan'</span><span style="color: black;">&#41;</span></pre></div></div>

<p>In fact, you can programmatically gain insight into functions with the <a href="http://docs.python.org/library/inspect.html" title="inspect" target="_blank">inspect module</a>. But suppose you want to be able to accept an arbitrary number of parameters. For example, for a printf equivalent. Or where I encountered it in wanting to read a module name from a configuration file, as well as the arguments to instantiate it. In this case, you&#8217;d get the module and class as a string and then a dictionary of the arguments to make an instance of it. Of course, Python always has a way. In this case, **kwargs.</p>
<p>This is actually dictionary unpacking, taking all the keys in a dictionary and mapping them to argument names. For example, in the above example, I could say:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">hello<span style="color: black;">&#40;</span><span style="color: #66cc66;">**</span><span style="color: black;">&#123;</span><span style="color: #483d8b;">'last'</span>:<span style="color: #483d8b;">'Lecocq'</span>, <span style="color: #483d8b;">'first'</span>:<span style="color: #483d8b;">'Dan'</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Of course, in that case it&#8217;s a little verbose, but if you&#8217;re getting a dictionary of arguments programmatically, then it&#8217;s invaluable. But wait, there&#8217;s more! Not only can you use the **dict operator to map a dictionary into parameters, but you can accept arbitrary parameters with it, too!</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> kw<span style="color: black;">&#40;</span><span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">for</span> key, value <span style="color: #ff7700;font-weight:bold;">in</span> kwargs.<span style="color: black;">items</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;">'%s =&gt; %s'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>key, value<span style="color: black;">&#41;</span>
&nbsp;
kw<span style="color: black;">&#40;</span><span style="color: #66cc66;">**</span><span style="color: black;">&#123;</span><span style="color: #483d8b;">'hello'</span>:<span style="color: #483d8b;">'Howdy!'</span>, <span style="color: #483d8b;">'first'</span>:<span style="color: #483d8b;">'Dan'</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>
kw<span style="color: black;">&#40;</span>hello=<span style="color: #483d8b;">'Howdy!'</span>, first=<span style="color: #483d8b;">'Dan'</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Magic! <strong>No matter how you invoke the function, it has access to the parameters.</strong>  You can even split the difference, making some parameters named and some parameters variable. For example, if you wanted to create an instance of a class that you passed a name in for, initialized with the arguments you give it:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> factory<span style="color: black;">&#40;</span>module, cls, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
	<span style="color: #808080; font-style: italic;"># The built-in __import__ does just what it sounds like</span>
	m = <span style="color: #008000;">__import__</span><span style="color: black;">&#40;</span>module<span style="color: black;">&#41;</span>
	<span style="color: #808080; font-style: italic;"># Now get the class in that module</span>
	c = <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span>m, cls<span style="color: black;">&#41;</span>
	<span style="color: #808080; font-style: italic;"># Now make an instance of it, given the args</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> c<span style="color: black;">&#40;</span><span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>
&nbsp;
factory<span style="color: black;">&#40;</span><span style="color: #483d8b;">'datetime'</span>, <span style="color: #483d8b;">'datetime'</span>, year=<span style="color: #ff4500;">2011</span>, month=<span style="color: #ff4500;">11</span>, day=<span style="color: #ff4500;">8</span><span style="color: black;">&#41;</span>
factory<span style="color: black;">&#40;</span><span style="color: #483d8b;">'decimal'</span>, <span style="color: #483d8b;">'Decimal'</span>, value=<span style="color: #ff4500;">7</span><span style="color: black;">&#41;</span></pre></div></div>

<p>This is one place where Python&#8217;s flexibility is extremely useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.lecocq.us/wordpress/2011/09/14/python-and-arbitrary-function-arguments-kwargs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Named Pipes</title>
		<link>http://dan.lecocq.us/wordpress/2011/09/11/named-pipes/</link>
		<comments>http://dan.lecocq.us/wordpress/2011/09/11/named-pipes/#comments</comments>
		<pubDate>Sun, 11 Sep 2011 11:20:59 +0000</pubDate>
		<dc:creator>dan.lecocq</dc:creator>
				<category><![CDATA[computer science]]></category>
		<category><![CDATA[fifo]]></category>
		<category><![CDATA[named pipes]]></category>
		<category><![CDATA[operating system]]></category>
		<category><![CDATA[os]]></category>
		<category><![CDATA[pipes]]></category>

		<guid isPermaLink="false">http://dan.lecocq.us/wordpress/?p=1018</guid>
		<description><![CDATA[Yesterday I encountered a concept I hadn&#8217;t known about: named pipes. They&#8217;re essentially a path that acts as a pipe for reading from / writing to. In that sense, you work with them like with file redirection and traditional files. But that data doesn&#8217;t get stored anywhere really permanent. All data that goes through it [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday I encountered a concept I hadn&#8217;t known about: named pipes. They&#8217;re essentially a <strong>path that acts as a pipe</strong> for reading from / writing to. In that sense, you work with them like with file redirection and traditional files. But that data doesn&#8217;t get stored anywhere really permanent. All data that goes through it is meat to be written once, and read once, and it comes with a performance boost of not having to write large chunks to disk.</p>
<p> Pipes, for those who don&#8217;t know, are the bees knees. They&#8217;re the cat&#8217;s meow. They allow you to (as the name implies) make a pipeline between one or more programs, with the output of one feeding into the input of others. Suppose, for example, that we want to find out how many files that contain &#8216;.a&#8217; there are in a directory. There&#8217;s a tool you might know, &#8216;ls,&#8217; that lists all the files in a directory. And &#8216;grep&#8217; is a tool to search for lines of text that match a regular expression. And &#8216;wc&#8217; is a tool that can count the number of bytes, words, lines, etc. in a file.</p>
<p>Typically, each of these <strong>operates in isolation</strong>, reading from a file (in the case of grep and wc), or&#8230; standard input. And they all write to standard output. A pipe is away to hook up one&#8217;s process&#8217; standard output file descriptor to the standard input file descriptor of the another, making one the <strong>producer of information and the other the consumer</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-l</span> <span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>some<span style="color: #000000; font-weight: bold;">/</span>directory <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">'.a'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">wc</span> <span style="color: #660033;">-l</span></pre></div></div>

<p>This is typical of the design of many command line utilities. Most either come with an option to read from standard in (usually either the absence of a filename or a single &#8216;-&#8217;). <strong>And most do exactly one task well</strong>. Each has one very specific purpose, but is generally happy to play along with others.</p>
<p>File redirection is another handy tool that is related to named pipes. File redirection lets you either read the contents of a file as if it were standard input, or have a process write to it as if it were standard output. Going back to the earlier example, if we wanted to store a list of the all such files in our own file called &#8216;list&#8217;:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">'.a'</span> <span style="color: #000000; font-weight: bold;">&gt;</span> list</pre></div></div>

<p>Easy as pie. Now&#8230; for named pipes. They&#8217;re also called &#8216;<strong>FIFO</strong>&#8216;s for their first-in-first-out behavior. You can make one with &#8216;mkfifo &lt;filename&gt;&#8217;. And then, feel free to read from it and write to it. Perhaps in two different terminals:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># In one terminal:</span>
<span style="color: #c20cb9; font-weight: bold;">mkfifo</span> <span style="color: #7a0874; font-weight: bold;">test</span>
<span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #7a0874; font-weight: bold;">test</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># In another terminal:</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'hello'</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #7a0874; font-weight: bold;">test</span></pre></div></div>

<p>The first terminal, cat plugs along doing the one thing it knows how to do: display what it reads in out to standard out. Take a minute for what has just happened to sink in. You were able to have one process wait around until it had something read&#8230; from a pipe. And in a completely different terminal, you had a <em>different</em> process communicate with the first one through opening a file. This is a mechanism that&#8217;s commonly used for <strong>inter-process communication</strong> (IPC) for obvious reasons &#8212; everyone knows how to read from and write to a file, so it makes use of a known paradigm. But wait &#8212; it gets even better.</p>
<p>Suppose you want to aggregate some statistics about how many different types of requests your application serves, but you don&#8217;t want to have to write that in. Or maybe it&#8217;s an application that you know already just writes to a log file. Of course, you could trawl the log file, but there are conceivably cases where you don&#8217;t want the overhead of keeping around huge files, so you&#8217;d rather avoid it if possible. You have to be careful when doing this (not all applications play nicely with named pipes &#8212; mostly surrounding blocking described below), but chances are you might be able to dupe the application into just logging to a named pipe! If you remove the log file and in that same path you make a pipe, then your work is done &#8212; just read from that pipe to aggregate your statistics periodically. <strong>This works particularly well with the python logging module.</strong></p>
<p>Reading from and writing to a named pipe can be a little more nuanced, however. Some things to bear in mind:</p>
<ul>
<li><strong>Opening a named pipe can block</strong>, so consider opening them non-blocking. Of course, it depends on your access pattern, but if you&#8217;re not sure if another process has written to the pipe and you don&#8217;t want that to trip up your reading, non-blocking is the way to go.</li>
<li><strong>Named pipes have &#8216;no size.&#8217;</strong> If you write to a pipe, data gets queued up for the other end to read, but even before that gets read, stat(1) reports that the file has a size of 0 bytes. So, you can&#8217;t rely on a change in file size to know it&#8217;s ready for reading.</li>
<li><strong>Instead, use select, poll, epoll, etc. to detect read/write-ability on the pipe.</strong> If you&#8217;re only interested in one file descriptor, then you can go ahead and use select, but if you&#8217;re starting to listen to too many, perhaps one of the others is a better idea</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://dan.lecocq.us/wordpress/2011/09/11/named-pipes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>System Stats in Python</title>
		<link>http://dan.lecocq.us/wordpress/2011/09/07/system-stats-in-python/</link>
		<comments>http://dan.lecocq.us/wordpress/2011/09/07/system-stats-in-python/#comments</comments>
		<pubDate>Wed, 07 Sep 2011 16:24:48 +0000</pubDate>
		<dc:creator>dan.lecocq</dc:creator>
				<category><![CDATA[computer science]]></category>
		<category><![CDATA[psutils]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://dan.lecocq.us/wordpress/?p=1016</guid>
		<description><![CDATA[Turns out, there&#8217;s a pretty handy package called psutil that allows you to not only gain insight into the currently-running process, but other processes, physical and virtual memory usage, and CPU usage. For example: import psutil &#160; psutil.phymem_usage&#40;&#41;.percent # 31.2 psutil.virtmem_usage&#40;&#41;.percent # 0.0 Pretty handy tool if you&#8217;re doing any sort of monitoring!]]></description>
			<content:encoded><![CDATA[<p>Turns out, there&#8217;s a pretty handy package called <a href="http://code.google.com/p/psutil/" title="psutil" target="_blank">psutil</a> that allows you to not only gain insight into the currently-running process, but other processes, physical and virtual memory usage, and CPU usage. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> psutil
&nbsp;
psutil.<span style="color: black;">phymem_usage</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">percent</span>
<span style="color: #808080; font-style: italic;"># 31.2</span>
psutil.<span style="color: black;">virtmem_usage</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">percent</span>
<span style="color: #808080; font-style: italic;"># 0.0</span></pre></div></div>

<p>Pretty handy tool if you&#8217;re doing any sort of monitoring!</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.lecocq.us/wordpress/2011/09/07/system-stats-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SEOmoz and Dev Blog Post</title>
		<link>http://dan.lecocq.us/wordpress/2011/08/31/seomoz-and-dev-blog-post/</link>
		<comments>http://dan.lecocq.us/wordpress/2011/08/31/seomoz-and-dev-blog-post/#comments</comments>
		<pubDate>Wed, 31 Aug 2011 16:01:36 +0000</pubDate>
		<dc:creator>dan.lecocq</dc:creator>
				<category><![CDATA[computer science]]></category>
		<category><![CDATA[boto]]></category>
		<category><![CDATA[deployment]]></category>
		<category><![CDATA[fabric]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://dan.lecocq.us/wordpress/?p=1007</guid>
		<description><![CDATA[I began work almost a month ago at a Seattle company, SEOmoz. Interesting projects, talented people, and a good place to be. Today I posted my first contribution to their Dev Blog talking about scripting the launching and deployment of EC2 instances with boto and frabric.]]></description>
			<content:encoded><![CDATA[<p>I began work almost a month ago at a Seattle company, <a href="http://www.seomoz.org/" title="SEOmoz" target="_blank">SEOmoz</a>. Interesting projects, talented people, and a good place to be. Today I posted my first contribution to their <a href="http://devblog.seomoz.org/" title="Dev Blog" target="_blank">Dev Blog</a> talking about <a href="http://devblog.seomoz.org/2011/08/launching-and-deploying-instances-with-boto-and-fabric/" title="Launching and Deploying Instances with Boto and Fabric" target="_blank">scripting the launching  and deployment of EC2 instances with boto and frabric</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.lecocq.us/wordpress/2011/08/31/seomoz-and-dev-blog-post/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>yes(1)</title>
		<link>http://dan.lecocq.us/wordpress/2011/08/18/yes1/</link>
		<comments>http://dan.lecocq.us/wordpress/2011/08/18/yes1/#comments</comments>
		<pubDate>Thu, 18 Aug 2011 22:40:49 +0000</pubDate>
		<dc:creator>dan.lecocq</dc:creator>
				<category><![CDATA[command line ninja magic]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[yes]]></category>

		<guid isPermaLink="false">http://dan.lecocq.us/wordpress/?p=999</guid>
		<description><![CDATA[Yes, yes(1) is built-in to Mac and Linux (at least OS X Lion and Ubuntu 11.04). And, as you might guess, it repeatedly prints a string of your choice (&#8216;y&#8217; by default) followed by a newline to stdout. Its sole purpose in life is to automate agreeing to prompts. I encountered it recently in a [...]]]></description>
			<content:encoded><![CDATA[<p>Yes, yes(1) is built-in to Mac and Linux (at least OS X Lion and Ubuntu 11.04). And, as you might guess, it repeatedly prints a string of your choice (&#8216;y&#8217; by default) followed by a newline to stdout. Its sole purpose in life is to automate agreeing to prompts. I encountered it recently in a script that was automating RAID array deployment on EC2 ephemeral disks:<br />
<code><br />
# mdadm doesn't let you automate by default, so pipe in 'y'!<br />
yes | mdadm ...<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://dan.lecocq.us/wordpress/2011/08/18/yes1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Keeping Build Notes</title>
		<link>http://dan.lecocq.us/wordpress/2011/08/14/keeping-build-notes/</link>
		<comments>http://dan.lecocq.us/wordpress/2011/08/14/keeping-build-notes/#comments</comments>
		<pubDate>Sun, 14 Aug 2011 16:43:09 +0000</pubDate>
		<dc:creator>dan.lecocq</dc:creator>
				<category><![CDATA[computer science]]></category>
		<category><![CDATA[build notes]]></category>
		<category><![CDATA[good practices]]></category>

		<guid isPermaLink="false">http://dan.lecocq.us/wordpress/?p=989</guid>
		<description><![CDATA[I initial put off upgrading to Snow Leopard until almost a year after its release because I was worried about rebuilding my development environment. It&#8217;s amazing how many packages one accumulates over time without thinking about it, and when you have deadlines to meet it can be disastrous to risk your current working setup. But [...]]]></description>
			<content:encoded><![CDATA[<p>I initial put off upgrading to Snow Leopard until almost a year after its release because I was worried about rebuilding my development environment. It&#8217;s amazing how many packages one accumulates over time without thinking about it, and when you have deadlines to meet it can be disastrous to risk your current working setup.</p>
<p>But rebuilding your development environment comes up more than just upgrading your OS. If you need to migrate to that new computer you got, or that work gave you, or help someone else get up and running with a project you&#8217;re thinking about releasing. Admittedly, it took me a little while to learn this lesson, but finally it&#8217;s drilled into my head: <strong>keep build notes!</strong></p>
<p>A couple weeks ago I was trying to install an internal package whose docs hadn&#8217;t been updated in a very long time. After struggling and hitting countless snags, I finally got it up and running when I got an email that was along the lines of, &#8220;Oh, if you could write down what problems you ran into, that would be great.&#8221; Fortunately, I just made notes of what I had done in order to get it built, and I was able to whip off a reply with speed that surprised the recipient.</p>
<p>Even at a system-wide level, I try to make it a habit to record every package I install/build associated with development. It makes it extremely easy to get set up on the next system, even if the instructions have to be updated for a new environment. I call it a manifest and I manage it as a flat file, though I know there are package managers that can do a lot of heavy lifting for me. However, I find that no package manager is perfect and so even if I make use of one for certain libraries, it&#8217;s important to me to have everything documented in one place. At a minimum (and you probably don&#8217;t need more than this) keep the following:</p>
<ol>
<li><strong>Package name and version</strong> &#8211; Maybe you needed readline 6.1 to get your project running, or you know that such-and-such version is buggy for your purposes.</li>
<li><strong>Why you installed it</strong> &#8211; I find that many libraries I install are used for a particular project, and so it&#8217;s useful to have the motivation for getting it.</li>
<li><strong>How you installed it</strong> &#8211; Whether it was macports or a typical configure, make and install, how did you build it? Did you need special flags to make it go? You will absolutely forget these, so why not write them down? Even just copy and paste from your history!</li>
</ol>
<p>I can&#8217;t stress enough how much easier this has made my development life in a lot of ways, and how little a time investment it is.</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.lecocq.us/wordpress/2011/08/14/keeping-build-notes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Conditional Compilation</title>
		<link>http://dan.lecocq.us/wordpress/2011/08/08/conditional-compilation/</link>
		<comments>http://dan.lecocq.us/wordpress/2011/08/08/conditional-compilation/#comments</comments>
		<pubDate>Mon, 08 Aug 2011 20:38:32 +0000</pubDate>
		<dc:creator>dan.lecocq</dc:creator>
				<category><![CDATA[computer science]]></category>
		<category><![CDATA[autoconf]]></category>
		<category><![CDATA[make]]></category>

		<guid isPermaLink="false">http://dan.lecocq.us/wordpress/?p=983</guid>
		<description><![CDATA[Last week I had the (dis)pleasure of porting some code to Mac, and today it came time to merge with the original codebase. As helpful as it was to use macros for different code paths, we needed something in the makefile to optionally add flags when compiling on Mac. // This is all well and [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I had the (dis)pleasure of porting some code to Mac, and today it came time to merge with the original codebase. As helpful as it was to use macros for different code paths, we needed something in the makefile to optionally add flags when compiling on Mac.<br />
<code><br />
// This is all well and good<br />
#ifndef __APPLE__<br />
    // Do your Linux-y includes here<br />
#else<br />
    // Do your Apple-y includes here<br />
#endif<br />
</code></p>
<p>Apparently, there are a couple conventions for doing this. First, you can inject a configuration step (à la autoconf, for example) which would detect what platform you&#8217;re building on in a robust way and build a Makefile for you. Second, if you&#8217;re lazy or autoconf would be like hitting a fly with a hammer, you can use make&#8217;s conditionals:<br />
<code><br />
# Ensure that this gets declared in time,<br />
# and fill it with the result of `uname`<br />
UNAME := $(shell uname)</p>
<p># If the environment is Darwin...<br />
ifeq ($(UNAME), Darwin)<br />
    CXXFLAGS = # Something Apple-y<br />
else<br />
    CXXFLAGS = # Something Linux-y<br />
endif<br />
</code></p>
<p>Simple enough!</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.lecocq.us/wordpress/2011/08/08/conditional-compilation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Type-Conversion Operators&#8217; Unintuitive Behavior</title>
		<link>http://dan.lecocq.us/wordpress/2011/08/06/type-conversion-operators-unintuitive-behavior/</link>
		<comments>http://dan.lecocq.us/wordpress/2011/08/06/type-conversion-operators-unintuitive-behavior/#comments</comments>
		<pubDate>Sat, 06 Aug 2011 14:59:10 +0000</pubDate>
		<dc:creator>dan.lecocq</dc:creator>
				<category><![CDATA[computer science]]></category>
		<category><![CDATA[compiler]]></category>
		<category><![CDATA[mangling]]></category>
		<category><![CDATA[operator]]></category>
		<category><![CDATA[type conversion]]></category>
		<category><![CDATA[typedef]]></category>

		<guid isPermaLink="false">http://dan.lecocq.us/wordpress/?p=978</guid>
		<description><![CDATA[A feature I only recently learned about are type-conversion operators. For any class, if you want to support type conversion to any type, you can do so by merely declaring (and of course defining) operators of the form operator type(): class Widget { ... operator bool(); operator thing(); operator Foo(); ... } While this is [...]]]></description>
			<content:encoded><![CDATA[<p>A feature I only recently learned about are type-conversion operators. For any class, if you want to support type conversion to any type, you can do so by merely declaring (and of course defining) operators of the form <em>operator type()</em>:<br />
<code><br />
class Widget {<br />
...<br />
operator bool();<br />
operator thing();<br />
operator Foo();<br />
...<br />
}<br />
</code></p>
<p>While this is fine and dandy (and admittedly obviously attractive in ways), there is a big problem SEOmoz co-worker Brandon pointed out: <strong>There&#8217;s no way to determine which code path will be taken.</strong></p>
<p>For a little bit of context, I came across a set of type-conversion operators that seemed reasonable enough. They tried to cover the whole gamut of possible primitive types:<br />
<code><br />
operator unsigned long long() const;<br />
operator long long() const;<br />
operator unsigned long() const { return operator unsigned long long(); }<br />
operator long() const { return operator long long(); }<br />
...<br />
</code></p>
<p><strong>The compiler has absolutely no problem with the above declaration.</strong> The class you put that in will happily compile, but the problem arises when you try to use it:<br />
<code><br />
Widget w(...);<br />
// Suddenly, the compiler complains, not knowing which operator to use<br />
unsigned long int foo = w;<br />
</code></p>
<p>At this point, the compiler puts its foot down. What to me seems unintuitive is that even though there is an conversion operator to this exact type, the compiler won&#8217;t use it. What&#8217;s even more bizarre to me is that typedefs and in-header definitions can further muddle things up:<br />
<code><br />
operator long long() const;<br />
operator long() const;<br />
operator int() const;<br />
operator short() const;<br />
// For whatever reason, let's say you do this:<br />
operator int32_t() const {<br />
    return operator long long();<br />
}<br />
</code></p>
<p><strong>Even though int32_t will be the same as one of those other types, this will still compile.</strong> It makes a certain amount of sense when viewed in the context of the compiler because after all, it only does so much processing on headers because they&#8217;re going to be directly included wherever you use them. <strong>You actually don&#8217;t get duplicate symbols in this case, and thus no &#8220;previously-defined&#8221; error.</strong> In reality, their function definitions are the same, and they actually get mangled to the same name (on my system the operators for int32_t and int both mangle to &#8216;_ZNK6WidgetcviEv&#8217;):<br />
<code><br />
# See what mangled symbols actually appear<br />
nm -j widget.o<br />
# See what demangled symbols are actually there<br />
nm -j widget.o | sed s/__/_/ | grep -v .eh | c++filt -n<br />
</code></p>
<p>The above (with in-header definitions) is exactly what we encountered in the code. We (well, a co-worker) suspected that the reason that the sort of multiple definition was allowed was that the names were getting mangled based on their typedef name string (mangled on int32_t instead of the actual type it maps to), but this is not the case. If you move the in-header definition for the int32_t operator into the .cpp file, the compiler will complain to you earlier.</p>
<p>My first inclination when dealing with the &#8220;conversion to type long long is ambiguous&#8221; error was to ask for an explicit conversion: static_cast&lt;long long int&gt;(myWidget). However, this doesn&#8217;t work either. So even in this scenario, <strong>you can&#8217;t even ask for a specific type conversion operator.</strong> From what I can gather, type-conversion operators are a double-edged lightsaber: few things in C++ were added without a purpose, but it&#8217;s extremely important to understand that exact purpose and its risks. To require that type conversions are <strong>explicit</strong> you should generally use something like:<br />
<code><br />
template <class T><br />
const T convert() const {<br />
...<br />
}</p>
<p>template <><br />
const bool convert<bool>() const {<br />
// Your conversion to bool<br />
...<br />
}<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://dan.lecocq.us/wordpress/2011/08/06/type-conversion-operators-unintuitive-behavior/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>port(1): A Four-Letter Word?</title>
		<link>http://dan.lecocq.us/wordpress/2011/08/05/port1-a-four-letter-word/</link>
		<comments>http://dan.lecocq.us/wordpress/2011/08/05/port1-a-four-letter-word/#comments</comments>
		<pubDate>Sat, 06 Aug 2011 05:26:45 +0000</pubDate>
		<dc:creator>dan.lecocq</dc:creator>
				<category><![CDATA[computer science]]></category>
		<category><![CDATA[macports]]></category>
		<category><![CDATA[port]]></category>

		<guid isPermaLink="false">http://dan.lecocq.us/wordpress/?p=980</guid>
		<description><![CDATA[To me, port has always been kind of a dirty word. Sure, it&#8217;s nice to have a package manager for Mac, especially after getting used to apt-get. Still, things tend to show up in weird places, and paths get confused. For instance, I was extremely frustrated this week to find that on OS X Lion, [...]]]></description>
			<content:encoded><![CDATA[<p>To me, port has always been kind of a dirty word. Sure, it&#8217;s nice to have a package manager for Mac, especially after getting used to apt-get. Still, things tend to show up in weird places, and paths get confused.</p>
<p>For instance, I was extremely frustrated this week to find that on OS X Lion, gcc-4.4.5 just would&#8230; not&#8230; compile. Frustrating stuff. I was tasked with porting this enormous existing in-house code base (of about 60-100k lines) to Mac, but was dismayed to find that they required C++0x features, which are unsupported in gcc 4.2.</p>
<p>Giving up, I turned to MacPorts as a broken, empty shell of a man. MacPorts was able to compile it, though relegated to /opt/, and though I could add that to my path, this new version of gcc wasn&#8217;t ready to consider the libraries I had installed in /usr/local/ by hand. Of course, I could edit all the makefiles, or do some other magics, but it turns out MacPorts can be bent to your will.</p>
<p>Like most, I had installed the binary release of MacPorts, configured to live in /opt/, but if you instead <strong>build from source</strong>, you can:<br />
<code><br />
./configure --prefix=/usr/local --with-unsupported-prefix<br />
</code></p>
<p>This has not only MacPorts reside in /usr/local/, but then it will also in turn install its packages there as well! I don&#8217;t think I&#8217;m the only one who appreciates that kind of consistency &#8212; all my libraries in the right place. I still feel slightly dirty whenever I have to rely on port, but at least when I do, I can save a little face.</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.lecocq.us/wordpress/2011/08/05/port1-a-four-letter-word/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Error Macro Win</title>
		<link>http://dan.lecocq.us/wordpress/2011/08/05/error-macro-win/</link>
		<comments>http://dan.lecocq.us/wordpress/2011/08/05/error-macro-win/#comments</comments>
		<pubDate>Fri, 05 Aug 2011 16:31:54 +0000</pubDate>
		<dc:creator>dan.lecocq</dc:creator>
				<category><![CDATA[computer science]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[macro]]></category>

		<guid isPermaLink="false">http://dan.lecocq.us/wordpress/?p=972</guid>
		<description><![CDATA[Still porting Linux code to Mac, I&#8217;ve been trying to keep a useful habit: using the #warning and #error macros. This new code is riddled with #ifdef&#8217;s checking whether or not we&#8217;re trying to build on a Mac, and using alternatives to the Linux-only system calls, but in parsing these large chunks of code, sometimes [...]]]></description>
			<content:encoded><![CDATA[<p>Still porting Linux code to Mac, I&#8217;ve been trying to keep a useful habit: using the #warning and #error macros. This new code is riddled with #ifdef&#8217;s checking whether or not we&#8217;re trying to build on a Mac, and using alternatives to the Linux-only system calls, but in parsing these large chunks of code, sometimes I forget what I&#8217;m doing. How horrible would it be to accidentally leave empty the Mac-only code block when it&#8217;s meant to actually do something.</p>
<p>So, whenever I open up one such block, I add a little macro:<br />
<code><br />
#ifndef MAC<br />
// ... The Linux-only code<br />
// ... takes up<br />
// ... a lot<br />
// ... of space<br />
#else<br />
#error "Don't forget to implement it as Mac!"<br />
#endif<br />
</code></p>
<p>At least I&#8217;ll always catch it at compile time, and when I fix/add the Mac-only code, then I can go ahead and remove that macro. It&#8217;s not that I&#8217;m forgetful, but I&#8217;ve shot myself in the foot so much at this point.</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.lecocq.us/wordpress/2011/08/05/error-macro-win/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Boost, typedef, #define and GCC Pain</title>
		<link>http://dan.lecocq.us/wordpress/2011/08/04/boost-typedef-define-and-gcc-pain/</link>
		<comments>http://dan.lecocq.us/wordpress/2011/08/04/boost-typedef-define-and-gcc-pain/#comments</comments>
		<pubDate>Thu, 04 Aug 2011 15:40:34 +0000</pubDate>
		<dc:creator>dan.lecocq</dc:creator>
				<category><![CDATA[command line ninja magic]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[SEOmoz]]></category>
		<category><![CDATA[boost]]></category>
		<category><![CDATA[define]]></category>
		<category><![CDATA[g++]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[preprocessor]]></category>
		<category><![CDATA[typedef]]></category>

		<guid isPermaLink="false">http://dan.lecocq.us/wordpress/?p=969</guid>
		<description><![CDATA[Recently I&#8217;ve been working on porting some code to Mac, and yesterday I ran into a bug that stumped me for a little bit. Compiling against Boost was raising a bunch of errors, specifically in lines that seemed pretty innocuous (from cstdint.hpp): using ::int8_t; using ::int_least8_t; using ::int_fast8_t; using ::uint8_t; using ::uint_least8_t; using ::uint_fast8_t; G++ [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I&#8217;ve been working on porting some code to Mac, and yesterday I ran into a bug that stumped me for a little bit. Compiling against Boost was raising a bunch of errors, specifically in lines that seemed pretty innocuous (from cstdint.hpp):<br />
<code><br />
  using ::int8_t;<br />
  using ::int_least8_t;<br />
  using ::int_fast8_t;<br />
  using ::uint8_t;<br />
  using ::uint_least8_t;<br />
  using ::uint_fast8_t;<br />
</code></p>
<p>G++ kept giving me errors for each of those lines: &#8220;error: expected unqualified-id before ‘signed’,&#8221; referring to the line &#8220;using ::int8_t.&#8221; I&#8217;m a little embarrassed that I couldn&#8217;t figure it out right away, but eventually I figured out that it was caused by int8_t being #define&#8217;d somewhere else. For those of you that don&#8217;t know, #define really just takes one term and substitutes every subsequent occurrence of that term with one you provide.<br />
<code><br />
// If you define it as a macro<br />
#define int8_t signed char<br />
// Then this line will be interpreted very differently from how you expect<br />
using ::int8_t;<br />
// Gets interpreted as "using ::signed char"!!!<br />
</code></p>
<p>And this is what g++ had been complaining about. That is not legal C++ syntax; I&#8217;m sorry I doubted you, g++! But there still remained a larger question: where were these types getting defined? I didn&#8217;t want to be in the business of &#8220;patching&#8221; a library, and especially the largely impeccable Boost library. Typically these types (int8_t, uint8_t, etc.) are defined in cstdint or stdint.h, but looking at the system&#8217;s version, I found to my surprise that they were not macro-defined, but typedef&#8217;d, which is the right way to do it.</p>
<p><strong>Sidebar:</strong> In general, you should be using typedef instead of #define for this reason, and another very good reason. Because #define macros just go through code and blindly replace references, it can be difficult to trace the origin of a type, but typedefs are carried through, and so even after preprocessing, you can still see what the semantic meaning was (that you wanted int8_t specifically, not just something that happens to be the same type). And when debugging, this extra type information can be helpful. Similarly, you should generally also use const to define constants in your code, instead of #define macros, because while you might remember what a magic number is when you write it into your library, the meaning of that particular constant becomes unclear when you encounter it&#8217;s value when debugging. (If you haven&#8217;t, read <a href="http://www.aristeia.com/books.html">Scott Meyer&#8217;s Effective C++</a>.)</p>
<p>Getting back to the morality tale, the library I was porting wasn&#8217;t macro-defining int8_t, and stdint.h wasn&#8217;t, so where was the culprit? Clearly there are potentially hundreds of places it could be, and I was running out of good guesses. Luckily, SEOmoz C++ shaman Martin taught me a little <strong>ninja magic</strong>: use the -E flag with g++ to only run the preprocessor stage, and redirect the output into a file! When compiling with &#8216;make,&#8217; it typically spits out the offending g++ command, so rerun it to just pass that one through the preprocessor, which reaches out and fetches the header files and gloms them in order into one giant input file. Then, step through <em>this</em> file to see where &#8220;define&#8221; and &#8220;int8_t&#8221; occur on the same line! In two minutes we were able to find the header that was causing all this trouble, where I had spent two hours learning and reading about where the problem might be.</p>
<p>In the end we found it in a very small library that we happened to use, and on Mac we had just been using a slightly old version and this problem had been fixed in subsequent releases. Still, I&#8217;m glad to have added this preprocessor trick to my toolkit.</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.lecocq.us/wordpress/2011/08/04/boost-typedef-define-and-gcc-pain/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>A Sign That You&#8217;re Doing it Wrong</title>
		<link>http://dan.lecocq.us/wordpress/2011/06/29/a-sign-that-youre-doing-it-wrong/</link>
		<comments>http://dan.lecocq.us/wordpress/2011/06/29/a-sign-that-youre-doing-it-wrong/#comments</comments>
		<pubDate>Wed, 29 Jun 2011 17:10:51 +0000</pubDate>
		<dc:creator>dan.lecocq</dc:creator>
				<category><![CDATA[computer science]]></category>

		<guid isPermaLink="false">http://dan.lecocq.us/wordpress/?p=967</guid>
		<description><![CDATA[Lifehacker recently posted an article for Memory Restart, which restarts Firefox when its RAM consumption becomes too high. To me, it&#8217;s a pretty clear sign that you&#8217;ve done something wrong if people are writing plugins for you browser to restart it, because it consumes too much RAM with some regularity.]]></description>
			<content:encoded><![CDATA[<p>Lifehacker recently posted an article for <a href="http://lifehacker.com/5815636/memory-restart-tells-you-when-firefoxs-memory-usage-gets-too-high-and-restarts-it-for-you">Memory Restart</a>, which restarts Firefox when its RAM consumption becomes too high.</p>
<p>To me, it&#8217;s a pretty clear sign that you&#8217;ve done something wrong if people are writing plugins for you browser to restart it, because it consumes too much RAM with some regularity.</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.lecocq.us/wordpress/2011/06/29/a-sign-that-youre-doing-it-wrong/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Champagne Problem</title>
		<link>http://dan.lecocq.us/wordpress/2011/06/28/champagne-problem/</link>
		<comments>http://dan.lecocq.us/wordpress/2011/06/28/champagne-problem/#comments</comments>
		<pubDate>Tue, 28 Jun 2011 08:55:49 +0000</pubDate>
		<dc:creator>dan.lecocq</dc:creator>
				<category><![CDATA[computer science]]></category>
		<category><![CDATA[interview questions]]></category>
		<category><![CDATA[logic]]></category>
		<category><![CDATA[puzzles]]></category>

		<guid isPermaLink="false">http://dan.lecocq.us/wordpress/?p=953</guid>
		<description><![CDATA[I had an interview a few weeks ago, and I spent a little time preparing for it. I puzzled over some logic problems, and read some common interview questions, knowing that there would still likely be questions I hadn&#8217;t heard. Still, I did have one cache hit, so to speak. One of my favorite questions, [...]]]></description>
			<content:encoded><![CDATA[<p>I had an interview a few weeks ago, and I spent a little time preparing for it. I puzzled over some logic problems, and read some common interview questions, knowing that there would still likely be questions I hadn&#8217;t heard. Still, I did have one cache hit, so to speak.</p>
<p>One of my favorite questions, though, was one about pouring bottles of champagne: Suppose you are in charge of pouring the champagne for the midnight toast at a prestigious party with more VIPs than you can count. You have 10 waiters, who are wheeling in 1000 bottles of champagne and some bad news; exactly one of these bottles is poisoned, and if you drink even a single drop, you will become violently ill in one hour. If you serve tainted champagne to any guest, you and your employees will be fired on the spot.</p>
<p>Your waiters are sympathetic to this fact, and have thus volunteered to be taste-testers &#8212; a night home on the couch being miserable is better than being unemployed. Though, because of time constraints, you only have time for one testing before people get sick and you have to serve champagne.</p>
<p>The naive approach is to split the bottles up into groups of 100, and have each waiter try a drop from each. You would know from whichever waiter gets sick which group of 100 bottles has the bad bottle, but there would be 99 good bottles wasted, and of course, the wasted good bottles get deducted from your pay.</p>
<p>So, you want everyone to keep their jobs (and not serve tainted booze) while wasting as few good bottles as possible. How many bottles do you have to waste? Click below to reveal solution.</p>
<p><a href="javascript:void(null);" onclick="s_toggleDisplay(document.getElementById('SID519697704'), this, 'Show &#9660;', 'Hide &#9650;');">Show &#9660;</a></p>
<div id='SID519697704' style='display:none;'>
<p>You don&#8217;t have to waste a single bottle of good champagne, while ensuring that no VIP gets sick. Label your waiters 1, 2, 3, and so on up to 10. Label the bottles of champagne 1, 2, 3, and so on up to 1000. Then, for each bottle, convert that number to binary representations, and let each waiter be associated with a bit. For example, bottle 417 has representation 0110100001, meaning that waiters 1, 6, 8, and 9 would sample a small drop from it, but bottle 418 would have waiters 2, 6, 8, and 9. In this way, each bottle has a unique set of waiters who sampled it. Then, when waiters start becoming sick, say, waiters 3, 5, 6, and 10, you&#8217;d know that it&#8217;s bottle 1000110100 (564) that&#8217;s contaminated.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://dan.lecocq.us/wordpress/2011/06/28/champagne-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript Unit Testing with QUnit</title>
		<link>http://dan.lecocq.us/wordpress/2011/06/16/javascript-unit-testing-with-qunit/</link>
		<comments>http://dan.lecocq.us/wordpress/2011/06/16/javascript-unit-testing-with-qunit/#comments</comments>
		<pubDate>Thu, 16 Jun 2011 17:47:07 +0000</pubDate>
		<dc:creator>dan.lecocq</dc:creator>
				<category><![CDATA[computer science]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[qunit]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://dan.lecocq.us/wordpress/?p=947</guid>
		<description><![CDATA[In the vein of habits I wish I had picked up in Software Engineering, I&#8217;ve been increasingly using unit tests. Working on a project recently with a friend of mine, it fell to me to pick out a unit testing library for our development. My uninformed search led me to QUnit, from the same people [...]]]></description>
			<content:encoded><![CDATA[<p>In the vein of habits I wish I had picked up in Software Engineering, I&#8217;ve been increasingly using unit tests. Working on a project recently with a friend of mine, it fell to me to pick out a unit testing library for our development.</p>
<p>My uninformed search led me to <a href="http://docs.jquery.com/Qunit">QUnit</a>, from the same people that bring us <a href="http://jquery.com/">jQuery</a>, which we already use heavily. We found it immediately simple to use, expressive and powerful. Much of this particular site involves AJAX, and as such, we wanted to be able to test and make sure our interface to these resources is working, as well. Not to mention that most operations rely on information retrieved from remote resources.</p>
<p>To that end, the vast majority of our tests use their <code>asyncTest</code> function, which lets you perform any kind of asynchronous request, and then in your callback, you indicate to the system that your test has all its necessary information and can continue. For example,</p>
<p><code><br />
asyncTest("Our Site's API", function() {<br />
	$.ajax({...<br />
	success: function(response) {<br />
		ok('data' in response, "We expect data in the response.");<br />
		}<br />
	}<br />
}<br />
</code></p>
<p>One big &#8216;get&#8217; in my mind is that it&#8217;s from the same group that produces a library we already use heavily, so they tend to be thought-out in similar ways. Plus, it runs in the browser, and has nice styling for the interface that make your unit tests look extra classy!</p>
<p>The QUnit site has a lot more examples and demos, but this concludes my shameless plug for a unit testing quite I&#8217;ve come to appreciate very much.</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.lecocq.us/wordpress/2011/06/16/javascript-unit-testing-with-qunit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Git in Four Commands</title>
		<link>http://dan.lecocq.us/wordpress/2011/06/05/git-in-four-commands/</link>
		<comments>http://dan.lecocq.us/wordpress/2011/06/05/git-in-four-commands/#comments</comments>
		<pubDate>Sun, 05 Jun 2011 17:47:09 +0000</pubDate>
		<dc:creator>dan.lecocq</dc:creator>
				<category><![CDATA[computer science]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[version control]]></category>

		<guid isPermaLink="false">http://dan.lecocq.us/wordpress/?p=941</guid>
		<description><![CDATA[Git is not a complicated tool for most things. I still find it a little tricky to set up for multiple users, but even that&#8217;s pretty easy. It really caters to the use-case where you&#8217;re just starting a project, or are the sole developer, but just want to keep track of changes and versions, make [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://book.git-scm.com/">Git</a> is not a complicated tool for most things. I still find it a little tricky to set up for multiple users, but even that&#8217;s pretty easy. It really caters to the use-case where you&#8217;re just starting a project, or are the sole developer, but just want to keep track of changes and versions, make branches, etc.</p>
<p>The first major point of git is that everyone has their own copy of the repository. When you commit changes, you commit them to your local copy of the repository. If you are working on a group project, there is a shared resource that can be <strong>pushed to</strong> and <strong>pulled from</strong>, but I actually like that it takes an extra command to do that &#8212; it forces me to make sure that it&#8217;s what I actually want to do. Now, to the bare-bones commands:</p>
<ol>
<li><strong>git init</strong> &#8212; Wherever you&#8217;re writing your code, type &#8220;git init&#8221;. It creates an empty repository in that directory (there&#8217;s a magical hidden folder &#8220;.git&#8221; that gets created and knows things).</li>
<li><strong>git status</strong> &#8212; Git knows which files have changed since you last saved changes, and it will happily tell you which files are new and changed with this command.</li>
<li><strong>git add</strong> &#8212; When you change files and are at a point where it make sense to save changes to your code (a bug fix, a new feature, etc.), then tell git which files you want to put in this commit  with &#8220;git add&#8221;. If committing a set of changes with git is like shooting a gun, then adding files to be committed is like loading that gun. Git knows which files have changed, but it can make sense to group changed files into different logical commits. For example, if you fix two bugs between commits, you might want to add the changes for each bug fix separately.</li>
<li><strong>git commit</strong> &#8212; When you have added a bunch of changed files to be committed, now you&#8217;re ready to actually commit those changes. Type &#8220;git commit -m &#8216;A short, meaningful summary of the changes that happened.&#8217;&#8221;</li>
</ol>
<p>There is a lot more to git, and a <a href="http://www.kernel.org/pub/software/scm/git/docs/gittutorial.html">million tutorials</a>, that will explain things in more detail, but these are the commands I spend 95% of my time using, and enough to at least get you starting tracking changes for anything and everything. It doesn&#8217;t matter very much what version control you use, <strong>just use something.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://dan.lecocq.us/wordpress/2011/06/05/git-in-four-commands/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unfuddle</title>
		<link>http://dan.lecocq.us/wordpress/2011/06/02/unfuddle/</link>
		<comments>http://dan.lecocq.us/wordpress/2011/06/02/unfuddle/#comments</comments>
		<pubDate>Thu, 02 Jun 2011 17:46:54 +0000</pubDate>
		<dc:creator>dan.lecocq</dc:creator>
				<category><![CDATA[computer science]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[issue tracking]]></category>
		<category><![CDATA[unfuddle]]></category>

		<guid isPermaLink="false">http://dan.lecocq.us/wordpress/?p=939</guid>
		<description><![CDATA[For a recent project, we were looking for an issue tracker. Our biggest criteria was something that was free &#8212; we weren&#8217;t planning on doing anything complicated, and so available issue trackers were essentially equal in our eyes. We ended up going with Unfuddle, but it wasn&#8217;t until after signing up that we discovered its [...]]]></description>
			<content:encoded><![CDATA[<p>For a recent project, we were looking for an issue tracker. Our biggest criteria was something that was free &#8212; we weren&#8217;t planning on doing anything complicated, and so available issue trackers were essentially equal in our eyes. We ended up going with <a href="http://unfuddle.com/">Unfuddle</a>, but it wasn&#8217;t until after signing up that we discovered its biggest strength: an API.</p>
<p>There are other issue trackers with an API, but this one seemed reasonable enough so we used it. The big get for us in building a web-based app, is that it <strong>lowers the barrier for reporting bugs</strong>. We set up our development server to include a bug-reporting button that collected a little report that then sent it to our server, which uses the Unfuddle API to add a ticket. It also records and submits a little information about the user&#8217;s browser, current session, etc. that might help in debugging their problem.</p>
<p>Not the most complicated or profound system in the world, but it does put user-reported (and tester-reported) bugs in the same place we check our to-dos and other issues. No checking a support email account we remember once a month. Just puts bugs right were we&#8217;re looking anyway. I hope to share a little bit of the backend code soon!</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.lecocq.us/wordpress/2011/06/02/unfuddle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Things I Wish Software Engineering Pounded Into My Head</title>
		<link>http://dan.lecocq.us/wordpress/2011/05/31/things-i-wish-software-engineering-pounded-into-my-head/</link>
		<comments>http://dan.lecocq.us/wordpress/2011/05/31/things-i-wish-software-engineering-pounded-into-my-head/#comments</comments>
		<pubDate>Tue, 31 May 2011 17:50:53 +0000</pubDate>
		<dc:creator>dan.lecocq</dc:creator>
				<category><![CDATA[computer science]]></category>
		<category><![CDATA[school]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[issue tracking]]></category>
		<category><![CDATA[software engineering]]></category>
		<category><![CDATA[unit testing]]></category>
		<category><![CDATA[version control]]></category>

		<guid isPermaLink="false">http://dan.lecocq.us/wordpress/?p=938</guid>
		<description><![CDATA[Until recently, I bore the burden of a dirty little secret: I didn&#8217;t use unit testing. I don&#8217;t know if this has been other students&#8217; experience, but my software engineering course could have been a lot better. Perhaps it&#8217;s a topic that requires students to determine their own level of involvement, but particularly, I am [...]]]></description>
			<content:encoded><![CDATA[<p>Until recently, I bore the burden of a dirty little secret: I didn&#8217;t use unit testing. I don&#8217;t know if this has been other students&#8217; experience, but my software engineering course could have been a lot better. Perhaps it&#8217;s a topic that requires students to determine their own level of involvement, but particularly, I am irked that these issues were not as developed as they could have been:</p>
<ol>
<li>Version control &#8211; svn, git, something! Anything, really! Just use something!</li>
<li>Unit testing &#8211; sure we talked about it, but its utility isn&#8217;t clear until you use it in earnest</li>
<li>Documentation &#8211; always felt more like a chore than a tool</li>
<li>Issue tracking &#8211; <a href="http://www.joelonsoftware.com/articles/fog0000000029.html">Joel on Software</a> said it best: &#8220;I was born with only two bug-storing-slots in my brain.&#8221;</li>
<li>More best practices &#8211; <a href="http://www.aristeia.com/books.html">Effective C++</a> (it&#8217;s worth it &#8212; get it and read it) alone was worth more than the course was.
</ol>
<p>I concede that these are things whose utility might be hard to convey to college students who may or may not be working on projects they really believe in. Still, very important.</p>
<p>One of the things I like about documentation is that you can refer to the documentation when uncertain about side-effects, input, etc. of functions and you don&#8217;t really feel like reading through code and trying to keep it in your brain-RAM. Not only that, but I usually refer to my own documentation to get an idea of the guarantee that a function was offering, to try to make sure that it&#8217;s making those <strong>same guarantees after I make changes</strong>. Not only this, but <strong>articulating functionality crystalizes it&#8217;s implications</strong> in your mind. When describing how something works to a friend, it often reminds you of issues you hadn&#8217;t considered. Writing documentation is the same in this respect.</p>
<p>When working with other people, while it&#8217;s nice to be able to talk face-to-face, the issues we think of that need changing go in one ear and out the other for me. Not that it wasn&#8217;t a good idea, but my brain-RAM is not infinite. <strong>Bugs, features, ideas, issues all get paged out of my head. Write them down instead,</strong> which is where issue tracking comes in. Design questions and decisions come out of issues all the time, and the trail of comments and discussion make a nice reference for documentation.</p>
<p>I find that when I&#8217;m fixing a bug, the functionality of that chunk of code gets reduced to that particular feature in my mind. So, if when I try using it, if that bug is fixed, then my job is done. But we&#8217;ve all experienced unintended side effects, where in fixing one thing, we break another. Having a list of features to check is a step up from trying to remember the things that need to work, but it&#8217;s much less than the power of unit testing. I&#8217;ve recently been using <a href="http://unittest-cpp.sourceforge.net/UnitTest++.html">UnitTest++</a> for C++ applications, and <a href="http://docs.jquery.com/QUnit">QUnit</a> from the jQuery people, and have been pleased to thrilled. It allows you to <strong>check functionality exhaustively and quickly on a whim</strong>. Even the task of <strong>writing the tests forces you to define the functional requirements of your code</strong>, and I find it helps to clarify a lot.</p>
<p>Version control lets you try crazy new approaches and track changes, isn&#8217;t that great? I was helping a friend last semester with some of his code, and he was trying to make code changes with copy, paste, and the undo buffer. In the end, he ended up introducing more bugs into the code than he was fixing. <strong>It&#8217;s intractable to keep these kinds of code changes in your brain-RAM.</strong> Use something, use anything. I have a qualm with svn because it was something I used so rarely, I could never remember how to set it up. <strong>Even a barrier as small as that was enough to prevent me from using version control at all.</strong> I love git because it&#8217;s so easy to set up, I use it for almost anything: from a report (if using LaTeX), to a weekend project, to my thesis. There are tons of tools out there, but <strong>just use something.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://dan.lecocq.us/wordpress/2011/05/31/things-i-wish-software-engineering-pounded-into-my-head/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python Lesson Learned</title>
		<link>http://dan.lecocq.us/wordpress/2011/05/18/python-lesson-learned/</link>
		<comments>http://dan.lecocq.us/wordpress/2011/05/18/python-lesson-learned/#comments</comments>
		<pubDate>Wed, 18 May 2011 19:17:54 +0000</pubDate>
		<dc:creator>dan.lecocq</dc:creator>
				<category><![CDATA[computer science]]></category>
		<category><![CDATA[lessons]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://dan.lecocq.us/wordpress/?p=936</guid>
		<description><![CDATA[I have finally learned a lesson after running into this fact repeatedly: no matter what you are trying to do, python has a module for it. I&#8217;m reminded of two xkcd comics in particular.]]></description>
			<content:encoded><![CDATA[<p>I have finally learned a lesson after running into this fact repeatedly: no matter what you are trying to do, python has a module for it. I&#8217;m reminded of two <a href="http://xkcd.com/353/">xkcd comics</a> in <a href="http://xkcd.com/413/">particular</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.lecocq.us/wordpress/2011/05/18/python-lesson-learned/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

