<?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>Magoo &#187; Programming</title>
	<atom:link href="http://morgangoose.com/blog/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://morgangoose.com/blog</link>
	<description>affiliated with the society of blog bloggables</description>
	<lastBuildDate>Thu, 03 Jun 2010 12:58:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>How fabric gets it right</title>
		<link>http://morgangoose.com/blog/2010/02/how-fabric-gets-it-right/</link>
		<comments>http://morgangoose.com/blog/2010/02/how-fabric-gets-it-right/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 05:56:33 +0000</pubDate>
		<dc:creator>Morgan</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[fabric]]></category>
		<category><![CDATA[pyohio]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[rst]]></category>

		<guid isPermaLink="false">http://morgangoose.com/blog/?p=76</guid>
		<description><![CDATA[I like fabric. A lot. Its a easy to use tool that continually makes my life simpler, and my projects smarter and more automated. Not much out there can really say that. At least nothing I use daily, without noticing, and dependably. I used to use vellum, and that did what I needed. But fabric [...]]]></description>
			<content:encoded><![CDATA[<p>I like <a href="http://docs.fabfile.org/0.9.0/">fabric</a>. A lot. Its a easy to use tool that continually makes my life simpler, and my projects smarter and more automated. Not much out there can really say that. At least nothing I use daily, without noticing, and dependably.</p>
<p>I used to use vellum, and that did what I needed. But fabric being under active development, and getting new features each version it seems is a huge plus. That and it does the network stuff for you, along with the nitty gritty.</p>
<p>Recently I have been giving presentations to the <a href="http://opensource.osu.edu/">Ohio State University&#8217;s Open Source Club</a> about <a href="http://morgangoose.com/p/gnu_tools/">gnu tools</a>, <a href="http://morgangoose.com/p/tool_oriented_python/">python tools</a>, and soon some cli apps. Fabric really helped make this simple for me to get a whole system down for making and uploading these.</p>
<p>I made all of those presentations in restrctured text, and compiled them into their final formats. All of which was scripted in fabric. I became really attached to ReST after getting introduced to it watching <a href="http://catherinedevlin.pythoneers.com/">Catherine Devlin</a> give a <a href="http://catherinedevlin.pythoneers.com/presentations/rst/olf.html">talk about restructured text</a> at Ohio Linux Fest. I ended up finding a cool rst2s5 command that makes nice presentations and with a little tweaking it now also has syntax highlighted code blocks, and can make nice pdfs.</p>
<p>In starting to use fabric you&#8217;ll notice the basic idea is that you&#8217;d make a fabfile that works a lot like a Makefile or a SConstruct file would, with make and scons respectively. You&#8217;ll make calls with the fab command in the directory the fabfile is located and it will supply the targets.</p>
<p>Below in this example, two targets are made, pack and deploy. The pack target will just makes a tarball, using the local function fabric provides. The deploy target calls pack to make this tarball, then using the put function will place the tarball into the tmp directory, then change into the web dir provided, and extract the archive. It knows automaticly to do this to both hosts I provided, and since I am using an ssh key does all this trickery autonomously.</p>
<p><strong>fabfile.py</strong></p>
<div class="codecolorer-container python vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">from</span> fabric.<span style="color: black;">api</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span><br />
<br />
env.<span style="color: #dc143c;">user</span> = <span style="color: #483d8b;">'username'</span><br />
env.<span style="color: black;">hosts</span> = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'host1.com'</span>, <span style="color: #483d8b;">'host2.com'</span><span style="color: black;">&#93;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> pack<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; local<span style="color: black;">&#40;</span><span style="color: #483d8b;">'tar czf /tmp/project_foo.tgz project_foo/'</span>, capture=<span style="color: #008000;">False</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> deploy<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; pack<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; put<span style="color: black;">&#40;</span><span style="color: #483d8b;">'/tmp/project_foo.tgz'</span>, <span style="color: #483d8b;">'/tmp/'</span><span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">with</span> <span style="color: #dc143c;">cd</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/var/www/foo/'</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; run<span style="color: black;">&#40;</span><span style="color: #483d8b;">'tar xzf /tmp/project_foo.tgz'</span><span style="color: black;">&#41;</span></div></div>
<p>Fabric can do a lot more than just this, and its <a href="http://docs.fabfile.org/0.9.0/">docs</a> have a lot of detail, and explain most everything well. </p>
<p>A last example of some a cool fabric config would be the one I use to publish my presentations to this site.</p>
<p><strong>fabfile.py</strong></p>
<div class="codecolorer-container python vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">from</span> fabric.<span style="color: black;">api</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span><br />
<br />
env.<span style="color: black;">roledefs</span> = <span style="color: black;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #483d8b;">'production'</span>: <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;morgangoose.com&quot;</span><span style="color: black;">&#93;</span>,<br />
&nbsp; &nbsp; <span style="color: black;">&#125;</span><br />
<br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> setup_vars<span style="color: black;">&#40;</span>project<span style="color: black;">&#41;</span>: &nbsp; &nbsp;<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">global</span> presentation<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">global</span> presentation_archive<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">global</span> rst_source<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">global</span> pdf<br />
<br />
&nbsp; &nbsp; project = project.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;/&quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; presentation = project<br />
&nbsp; &nbsp; presentation_archive = <span style="color: #483d8b;">&quot;%s.tar.gz&quot;</span> <span style="color: #66cc66;">%</span> presentation<br />
&nbsp; &nbsp; rst_source = <span style="color: #483d8b;">&quot;%s.rst&quot;</span> <span style="color: #66cc66;">%</span> presentation<br />
&nbsp; &nbsp; pdf = <span style="color: #483d8b;">&quot;%s.pdf&quot;</span> <span style="color: #66cc66;">%</span> presentation<br />
<br />
@roles<span style="color: black;">&#40;</span><span style="color: #483d8b;">'production'</span><span style="color: black;">&#41;</span><br />
<span style="color: #ff7700;font-weight:bold;">def</span> upload<span style="color: black;">&#40;</span>project<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; env.<span style="color: #dc143c;">user</span> = <span style="color: #483d8b;">&quot;username&quot;</span><br />
&nbsp; &nbsp; p_dir = <span style="color: #483d8b;">&quot;/var/www/html/p/&quot;</span><br />
<br />
&nbsp; &nbsp; package<span style="color: black;">&#40;</span>project<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; put<span style="color: black;">&#40;</span>presentation_archive, p_dir<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; put<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%s/%s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>presentation, pdf<span style="color: black;">&#41;</span>, p_dir<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">with</span> <span style="color: #dc143c;">cd</span><span style="color: black;">&#40;</span>p_dir<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; run<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;rm -rf %s/&quot;</span> <span style="color: #66cc66;">%</span> presentation<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; run<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;tar zxvf %s&quot;</span> <span style="color: #66cc66;">%</span> presentation_archive<span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; local<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;rm -f %s&quot;</span> <span style="color: #66cc66;">%</span> presentation_archive<span style="color: black;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp;<br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> package<span style="color: black;">&#40;</span>project<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; setup_vars<span style="color: black;">&#40;</span>project<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; make_presentation<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; local<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;tar zcvf %s %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>presentation_archive, presentation<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> make_presentation<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#PDF first</span><br />
&nbsp; &nbsp; local<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;rst2pdf %s/%s -o %s/%s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; presentation, rst_source, presentation, pdf, <span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#Then s5 html presentation</span><br />
&nbsp; &nbsp; local<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;python rst-directive.py <span style="color: #000099; font-weight: bold;">\</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --stylesheet=pygments.css <span style="color: #000099; font-weight: bold;">\</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --theme=small-black <span style="color: #000099; font-weight: bold;">\</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --quiet <span style="color: #000099; font-weight: bold;">\</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; %s/%s &gt; %s/index.html&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; presentation, rst_source, presentation, <span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #dc143c;">new</span><span style="color: black;">&#40;</span>project<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; setup_vars<span style="color: black;">&#40;</span>project<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; local<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;mkdir -p %s/{,files}&quot;</span> <span style="color: #66cc66;">%</span> presentation<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; local<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;cp -R ui %s/&quot;</span> <span style="color: #66cc66;">%</span> presentation<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; local<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;touch %s/%s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>presentation, rst_source<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></div></div>
<p>This has some more complicated bits, where it uses the role decorator to specify only to use the hosts listed in the production role definitions. </p>
<p>It also takes advantage of an awesome feature I didn&#8217;t know fabric had where, one can send arguments to a fabric target. So the project parameter in the targets here can be, and is, supplied via the command line. </p>
<p>For example, I used this to deploy the updates to my most recent presentation:</p>
<div class="codecolorer-container bash vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ fab upload:tool_oriented_python</div></div>
<p>That&#8217;s telling fabric to run the upload target, and send the string &#8220;tool_oriented_python&#8221; as an argument to the function. </p>
<p>If you forget the targets you have just do:</p>
<div class="codecolorer-container bash vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ fab <span style="color: #660033;">-l</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://morgangoose.com/blog/2010/02/how-fabric-gets-it-right/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Decouple with kwargs</title>
		<link>http://morgangoose.com/blog/2009/07/decouple-with-kwargs/</link>
		<comments>http://morgangoose.com/blog/2009/07/decouple-with-kwargs/#comments</comments>
		<pubDate>Wed, 22 Jul 2009 22:26:42 +0000</pubDate>
		<dc:creator>Morgan</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[kwargs]]></category>
		<category><![CDATA[mutliprocessing]]></category>
		<category><![CDATA[optparse]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://morgangoose.com/blog/?p=45</guid>
		<description><![CDATA[So I&#8217;ve been attempting to make a suite of cli scripts for work. I recently discovered the multiprocessing module for python, and really liked its simplicity, and started using it, with great success. Everything was faster. This then spurred me to take the scripts that were for the most part, copy common-ish bits and then [...]]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;ve been attempting to make a suite of cli scripts for work. I recently discovered the <a href="http://docs.python.org/library/multiprocessing.html">multiprocessing</a> module for python, and really liked its simplicity, and started using it, with great success. Everything was faster.</p>
<p>This then spurred me to take the scripts that were for the most part, copy common-ish bits and then modify to suite, and turn them into a library of sorts. The neat part then arose when I wanted to import a argument parser, as well as pass off to a proc creation component.  </p>
<p>In doing this I had in mind that the &#8216;script&#8217; would need to only define a function to make a list of commands to run on a given server, and a __main__ section that would pass in a list of servers, the function to make a command list and some other info. This way the script itself would only be two definition sections, and only the parts that were going to be unique for the most part.</p>
<p>The problem that came up in doing this is when I wanted the function that makes the command list to have more arguments that normal. How would I pass them in, and how would I define them so that I don&#8217;t have to edit my libraries to accommodate this argument passing.  It was kwargs that saved me there, that and some <a href="http://docs.python.org/library/optparse.html">optparse</a> tweaking.</p>
<p>Here is a basic example:</p>
<p><strong>script.py</strong></p>
<div class="codecolorer-container python vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">def</span> get_commands<span style="color: black;">&#40;</span>host, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; command_list = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #dc143c;">user</span> = kwargs<span style="color: black;">&#91;</span><span style="color: #483d8b;">'user'</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; key_file = kwargs<span style="color: black;">&#91;</span><span style="color: #483d8b;">'key_file'</span><span style="color: black;">&#93;</span><br />
<br />
&nbsp; &nbsp; command_list.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;echo %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #dc143c;">user</span><span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> command_list &nbsp; &nbsp;<br />
<br />
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">import</span> automation<br />
<br />
&nbsp; &nbsp; hosts = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><br />
<br />
&nbsp; &nbsp; <span style="color: black;">&#40;</span>hosts, options<span style="color: black;">&#41;</span> = automation.<span style="color: black;">process_args</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; automation.<span style="color: black;">thread_hosts</span><span style="color: black;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hosts,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get_commands,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; options,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #dc143c;">user</span>=<span style="color: #483d8b;">&quot;test&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: black;">&#41;</span></div></div>
<p><strong>automation.py</strong> (library w/ functions)</p>
<div class="codecolorer-container python vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">def</span> thread_hosts<span style="color: black;">&#40;</span>hosts, get_commands, options=<span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">import</span> multiprocessing<br />
&nbsp;<br />
&nbsp; &nbsp; kwargs.<span style="color: black;">update</span><span style="color: black;">&#40;</span>options<span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; jobs = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> host <span style="color: #ff7700;font-weight:bold;">in</span> hosts:<br />
&nbsp; &nbsp; &nbsp; &nbsp; p = multiprocessing.<span style="color: black;">Process</span><span style="color: black;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; target=run_commands, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; args=<span style="color: black;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; host,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get_commands<span style="color: black;">&#40;</span>host, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: black;">&#41;</span>, <span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; jobs.<span style="color: black;">append</span><span style="color: black;">&#40;</span>p<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; p.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></div>
<p>So this example is a script that defines the function to return a command list, and provides an options var, and list of hosts. The thread hosts then loops over the hosts each time  passing the host and the get_commands function to another library function that connects to said host, and loops over the returned command list.</p>
<p>A part that might be confusing is that the parse_args function returns optparse&#8217;s options variable but the options.__dict__ representation specifically. This then allows me to be able to update kwargs with any options that I allow to be set at the command line. The example in the script being the key_file variable.</p>
<p>The neat part of all this is being able to take the kwargs for one function and pass it right along to the next. This is key, because it allows for the library function in this case to be able to be entirely decoupled from the script itself. </p>
<p>With this implementation I am able to write a script that defines extra args to use, and only the script need know what they are. In the examples the library will just dumbly pass them along in the kwargs dict, I never have to tell it that I want to pass a user variable to it, and it makes the script a nice self contained unit.</p>
]]></content:encoded>
			<wfw:commentRss>http://morgangoose.com/blog/2009/07/decouple-with-kwargs/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
