<?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 little madness &#187; Python</title>
	<atom:link href="http://www.alittlemadness.com/category/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alittlemadness.com</link>
	<description>A man needs a little madness, or else he never dares cut the rope and be free. -Nikos Kazantzakis</description>
	<lastBuildDate>Wed, 26 Oct 2011 04:14:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>The Key Thing In Python&#8217;s Favour</title>
		<link>http://www.alittlemadness.com/2007/09/05/the-key-thing-in-pythons-favour/</link>
		<comments>http://www.alittlemadness.com/2007/09/05/the-key-thing-in-pythons-favour/#comments</comments>
		<pubDate>Tue, 04 Sep 2007 15:00:22 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://www.alittlemadness.com/2007/09/05/the-key-thing-in-pythons-favour/</guid>
		<description><![CDATA[I recently ran across this post advocating Python.  This made me think about why I prefer Python over the rest of the scripting crowd.  And I realised that there is just one key reason:
Python is easier to read
That&#8217;s it.  For all the joys of programming in dynamically-typed languages, I think there is [...]]]></description>
			<content:encoded><![CDATA[<p>I recently ran across <a href="http://adam.gomaa.us/blog/why-i-am-a-python-advocate/">this post</a> advocating Python.  This made me think about why I prefer Python over the rest of the scripting crowd.  And I realised that there is just one key reason:</p>
<blockquote><p>Python is easier to read</p></blockquote>
<p>That&#8217;s it.  For all the joys of programming in dynamically-typed languages, I think there is one major problem: in many ways these languages favour writers over readers.  I covered one example of this problem back in my post on <a href="http://www.alittlemadness.com/2006/06/21/duck-typing-vs-readability/">Duck Typing vs Readability</a>.  This is why it is so important that a dynamic language is designed with readability in mind.</p>
<p>In my opinion, Python really shines readability-wise.  And this is no accident &#8212; if you follow design discussions about Python you will see that Guido is always concerned with code clarity.  A feature is not worth adding just because it makes code more compact; it must make the code more <em>concise</em>.  Features have even been removed from Python because they were seen to encourage compact but difficult to comprehend code (<a href="http://www.artima.com/weblogs/viewpost.jsp?thread=98196">reduce</a> being a classic example).  Even controversial design choices like significant whitespace and the explicit use of &#8220;self&#8221; are actually good for readability.</p>
<p>The philosophy behind Python also extends beyond the language design.  Clever tricks and one-liners are not encouraged by the community.  Rather, the community aims for <a href="http://faassen.n--tree.net/blog/view/weblog/2005/08/06/0">Pythonic</a> code:</p>
<blockquote><p>To be Pythonic is to use the Python constructs and datastructures with clean, readable idioms.</p></blockquote>
<p>This is surely refreshing compared to the misguided boasting about how much can be crammed in to a few lines of [insert other scripting language].  Certainly, if I ever have to pick up and maintain another person&#8217;s dynamically-typed code, I hope it is Python.  And since I know I will have to maintain my own code, when I go dynamic I always pick Python.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alittlemadness.com/2007/09/05/the-key-thing-in-pythons-favour/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Automate Your Acceptance Tests With Python</title>
		<link>http://www.alittlemadness.com/2006/09/28/automate-your-acceptance-tests-with-python/</link>
		<comments>http://www.alittlemadness.com/2006/09/28/automate-your-acceptance-tests-with-python/#comments</comments>
		<pubDate>Thu, 28 Sep 2006 12:42:43 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Project Automation]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://www.alittlemadness.com/?p=47</guid>
		<description><![CDATA[Recently I&#8217;ve been cooking up some new acceptance-level tests for Pulse.  The sort of tests that need to take a Pulse release package, unpack it, start the server and poke it with a stick.  This is the sort of automation that I would typically use bash for: since a lot of the work [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I&#8217;ve been cooking up some new acceptance-level tests for <a href="http://zutubi.com/products/pulse/">Pulse</a>.  The sort of tests that need to take a Pulse release package, unpack it, start the server and poke it with a stick.  This is the sort of automation that I would typically use bash for: since a lot of the work involves executing and managing subprocess.  However, I had learned from earlier testing that process control from bash is not very portable.  More specifically, the behaviour of Cygwin bash on Windows differs considerably from *nix bash.  At the time, I worked around the problem by doing some process control using Ant, but there had to be a better way.</p>
<p>Enter Python.  Or, more specifically, the new(ish) subprocess module (introduced in Python 2.4).  With this module Python finally has a concise but powerful way to launch and control subprocesses.  It also includes convenient ways to handle subprocess I/O, including setting up pipelines.  What&#8217;s more, I have so far found it to be quite portable between Linux and Windows.</p>
<p>Starting with something simple, let&#8217;s say you just want to launch a command synchronously (much as you would in a shell script):</p>
<div class="codesnip-container" >
<div class="python codesnip" style="font-family:monospace;">exitcode = <span class="kw3">subprocess</span>.<span class="me1">call</span><span class="br0">&#40;</span><span class="br0">&#91;</span><span class="st0">&quot;cat&quot;</span>, <span class="st0">&quot;foo.txt&quot;</span><span class="br0">&#93;</span><span class="br0">&#41;</span></div>
</div>
<p>Simple.  Want to set up a pipeline like `dmesg | grep hda` (capturing the final output)?  Here&#8217;s an example straight from the doco:</p>
<div class="codesnip-container" >
<div class="python codesnip" style="font-family:monospace;"><span class="kw1">from</span> <span class="kw3">subprocess</span> <span class="kw1">import</span> Popen</p>
<p>p1 = Popen<span class="br0">&#40;</span><span class="br0">&#91;</span><span class="st0">&quot;dmesg&quot;</span><span class="br0">&#93;</span>, stdout=PIPE<span class="br0">&#41;</span><br />
p2 = Popen<span class="br0">&#40;</span><span class="br0">&#91;</span><span class="st0">&quot;grep&quot;</span>, <span class="st0">&quot;hda&quot;</span><span class="br0">&#93;</span>, stdin=p1.<span class="me1">stdout</span>, stdout=PIPE<span class="br0">&#41;</span><br />
output = p2.<span class="me1">communicate</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span></div>
</div>
<p>For my specific case, I used the ability to launch a Pulse server processes in the background and capture the output extensively.  When done, I shut down the server using the standard shutdown script (testing of said script being a nice side-effect).  An asynchronous child is started simply by creating a Popen object:</p>
<div class="codesnip-container" >
<div class="python codesnip" style="font-family:monospace;"><span class="kw1">from</span> <span class="kw3">subprocess</span> <span class="kw1">import</span> <span class="sy0">*</span></p>
<p><span class="co1"># Launch process, capturing output</span><br />
out = <span class="kw2">open</span><span class="br0">&#40;</span><span class="st0">&quot;stdout.txt&quot;</span>, <span class="st0">&quot;w&quot;</span><span class="br0">&#41;</span><br />
err = <span class="kw2">open</span><span class="br0">&#40;</span><span class="st0">&quot;stderr.txt&quot;</span>, <span class="st0">&quot;w&quot;</span><span class="br0">&#41;</span><br />
server = Popen<span class="br0">&#40;</span><span class="br0">&#91;</span><span class="st0">&quot;startup.sh&quot;</span><span class="br0">&#93;</span>, stdout=out, stderr=err<span class="br0">&#41;</span></p>
<p><span class="co1"># Do some stuff with the process&#8230;</span></p>
<p><span class="co1"># Trigger a shutdown and wait for the process to exit</span><br />
call<span class="br0">&#40;</span><span class="br0">&#91;</span><span class="st0">&quot;shutdown.sh&quot;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><br />
exitcode = server.<span class="me1">wait</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</div>
<p>I have simplified this example a bit, but the real work is all there, and it&#8217;s pleasantly straightforward.  This combined well with Python&#8217;s simple XML-RPC API, which I used to manipulate the running server.</p>
<p>So far the switch to from bash/Ant to Python has been a great success.  If you need a cross-platform way to automate your acceptance testing, give it a go.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alittlemadness.com/2006/09/28/automate-your-acceptance-tests-with-python/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

