<?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>gefira blog</title>
	<atom:link href="http://www.gefira.pl/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gefira.pl/blog</link>
	<description>10,000,000,000 Years Ago</description>
	<lastBuildDate>Sun, 22 Aug 2010 20:56:56 +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>Spring Python 1.1 book review</title>
		<link>http://www.gefira.pl/blog/2010/08/22/spring-python-1-1-book-review/</link>
		<comments>http://www.gefira.pl/blog/2010/08/22/spring-python-1-1-book-review/#comments</comments>
		<pubDate>Sun, 22 Aug 2010 20:56:56 +0000</pubDate>
		<dc:creator>Dariusz Suchojad</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[books]]></category>
		<category><![CDATA[Spring Python]]></category>

		<guid isPermaLink="false">http://www.gefira.pl/blog/?p=668</guid>
		<description><![CDATA[Here&#8217;s a copy of my Amazon&#8217;s Spring Python 1.1 book review.
Spring Python 1.1 is a book for professional programmers who either wish to venture into a world beyond traditional OOP and tackle problems differently thanks to Spring Python&#8217;s IoC &#38; AOP features or simply have practical issues &#8211; such as spreading the application across multiple [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a copy of my Amazon&#8217;s Spring Python 1.1 book review.</p>
<p><a title="Spring Python 1.1 book - Amazon" href="http://www.amazon.com/Spring-Python-1-1-Greg-Turnquist/dp/1849510660/">Spring Python 1.1</a> is a book for professional programmers who either wish to venture into a world beyond traditional OOP and tackle problems differently thanks to <a title="Spring Python - Homepage" href="http://springpython.webfactional.com/">Spring Python&#8217;s</a> <a title="Spring Python - Inversion of Control" href="http://springpython.webfactional.com/1.2.x/sphinx/objects.html">IoC</a> &amp; <a title="Spring Python - Aspect Oriented Programming" href="http://springpython.webfactional.com/1.2.x/sphinx/aop.html">AOP</a> features or simply have practical issues &#8211; such as spreading the application across multiple nodes &#8211; to solve. The book does not merely rephrase <a title="Sprnig Python - ref docs" href="http://springpython.webfactional.com/1.2.x/sphinx/index.html">the comprehensive reference documentation</a> available at the project&#8217;s website and I was actually very curious on how the author would be laying out the material.</p>
<p>Greg took a very good approach of explaining the basic of IoC first and building upon it in subsequent chapters although they can all be read separately. All of the major Spring Python&#8217;s building blocks are covered and I can only applaud that despite its Java roots almost no XML is used &#8211; that&#8217;s a good news for all Python programmers fearing that &#8220;Spring&#8221; means a lot of unnecessary XML, the prevailing majority of examples is in pure Python. Java programmers wishing to leverage their existing Spring skills won&#8217;t be disappointed though as there are some chapters devoted to how one can easily migrate a Spring Java IoC container over to Spring Python one, there&#8217;s also a discussion of how to use Spring Python &amp; Jython which, along with CPython, is also a target Python implementation Spring Python can run on. But let that not confuse you, the book is mostly written for Python programmers.</p>
<p>I liked it that the book was focusing on getting things done without turning aside into discussing vaguely related concepts or nuances, for instance the chapter on SQL doesn&#8217;t do any &#8216;quick recaps&#8217; of what SQL and relational databases are as is unfortunately quite common in many other books. On the other hand, not everyone is familiar with IoC and AOP and I know that anyone unfamiliar with those concepts &#8211; regardless of their primary programming language of choice &#8211; can easily take the book and get acquainted with them. A superb idea was that most of the chapters teach you how to write unit tests for the given Spring Python component you&#8217;d want to use in your code, IoC, AOP, security, database access and so on, that&#8217;s an often neglected area and I was very happy to see the author having placed an importance on it. Another good idea was to create a case-study chapter which combines all the knowledge and discusses it thoroughly.</p>
<p>All in all, it&#8217;s a good book written by a practitioner for practitioners, full of diagrams and examples that by necessity are simple yet don&#8217;t feel contrived and it&#8217;s certainly not something you need to plough through, it does a very good job of introducing the reader to Spring Python and showing how to use its pluggable components to write interesting and useful applications solving real-world problems. If I were to pick a nit I&#8217;d only say that it would be nice if the second edition contained at least a couple of examples of using Spring Python&#8217;s IoC <a title="Spring Python - YamlConfig" href="http://springpython.webfactional.com/1.2.x/sphinx/objects-yamlconfig.html">YamlConfig</a>, but that&#8217;s only a matter of syntax so the issue is really minor.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gefira.pl/blog/2010/08/22/spring-python-1-1-book-review/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ToStringBuilder.reflectionToString in Python</title>
		<link>http://www.gefira.pl/blog/2010/08/20/tostringbuilder-reflectiontostring-in-python/</link>
		<comments>http://www.gefira.pl/blog/2010/08/20/tostringbuilder-reflectiontostring-in-python/#comments</comments>
		<pubDate>Fri, 20 Aug 2010 18:55:10 +0000</pubDate>
		<dc:creator>Dariusz Suchojad</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.gefira.pl/blog/?p=645</guid>
		<description><![CDATA[I use Apache Commons ToStringBuilder.reflectionToString whenever I code in Java and thought it would come handy to have a similar construct in Python. What I&#8217;ve finally come up with is called make_repr and may be downloaded here along with a simple usage example (it&#8217;s all Python license).
So how does it work? Say you have a [...]]]></description>
			<content:encoded><![CDATA[<p>I use Apache Commons <a title="Apache Commons - Class ToStringBuilder" href="http://commons.apache.org/lang/api-release/org/apache/commons/lang/builder/ToStringBuilder.html">ToStringBuilder.reflectionToString</a> whenever I code in Java and thought it would come handy to have a similar construct in Python. What I&#8217;ve finally come up with is called <a title="make_repr - ToStringBuilder.reflectionToString in Python" href="http://www.gefira.pl/blog/?attachment_id=664"><em>make_repr</em> and may be downloaded here</a> along with a simple usage example (it&#8217;s all Python license).</p>
<p>So how does it work? Say you have a class like the one below</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> Foo<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, x, y, z<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">x</span> = x
        <span style="color: #008000;">self</span>.<span style="color: black;">y</span> = y
        <span style="color: #008000;">self</span>.<span style="color: black;">z</span> = z</pre></div></div>

<p>and want to add a nice string representation of it. You could carefully pick the attributes manually and add them to the __repr__ method or you can use <em>make_repr</em> to achieve what follows (I&#8217;m assuming make_repr has been saved to a util.py module):</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> util <span style="color: #ff7700;font-weight:bold;">import</span> make_repr
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Foo<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, x, y, z<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">x</span> = x
        <span style="color: #008000;">self</span>.<span style="color: black;">y</span> = y
        <span style="color: #008000;">self</span>.<span style="color: black;">z</span> = z
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__repr__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> make_repr<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> Foo<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">2</span>,<span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Which will show &lt;Foo at 0&#215;7f2ed9873510 x=[1] y=[2] z=[3]&gt; in the terminal. Callable objects won&#8217;t be returned and by the default attributes shown will be sorted alphabetically while any attributes with names starting with a double underscore will be ignored. Another customization option is <em>to_avoid_list</em> &#8211; if there&#8217;s a class attribute which goes by the name it points to (by default it&#8217;s <em>repr_to_avoid</em>, it must be in iterable object, such as a tuple or a list whose elements are names of attributes that must not be returned on the output, like below:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> util <span style="color: #ff7700;font-weight:bold;">import</span> make_repr
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Foo<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
&nbsp;
    repr_to_avoid = <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;x&quot;</span>,<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, x, y, z<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">x</span> = x
        <span style="color: #008000;">self</span>.<span style="color: black;">y</span> = y
        <span style="color: #008000;">self</span>.<span style="color: black;">z</span> = z
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__repr__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> make_repr<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> Foo<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">2</span>,<span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Noticed that &#8220;x&#8221; wasn&#8217;t returned on the output?</p>
<p>Well, that&#8217;s about it, I hope you like the little tool <img src='http://www.gefira.pl/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.gefira.pl/blog/2010/08/20/tostringbuilder-reflectiontostring-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WebSphere MQ and Ubuntu HOWTO</title>
		<link>http://www.gefira.pl/blog/2010/07/03/websphere-mq-and-ubuntu-howto/</link>
		<comments>http://www.gefira.pl/blog/2010/07/03/websphere-mq-and-ubuntu-howto/#comments</comments>
		<pubDate>Sat, 03 Jul 2010 17:53:14 +0000</pubDate>
		<dc:creator>Dariusz Suchojad</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[WebSphere MQ]]></category>

		<guid isPermaLink="false">http://www.gefira.pl/blog/?p=589</guid>
		<description><![CDATA[Until IBM finally recognizes that Ubuntu is the most popular Linux distribution and starts offering WebSphere MQ DEBs to install, here&#8217;s a small HOWTO which will guide you through the installation process. I&#8217;m using Ubuntu 9.10 and MQ 7.0.1 (CZ4VEML), both 64-bits, but it&#8217;s something I&#8217;ve been using since at least Ubuntu 7.04  I think, [...]]]></description>
			<content:encoded><![CDATA[<p>Until IBM finally recognizes that Ubuntu is the <a title="Ubuntu popularity - Google Trends" href="http://www.google.com/trends?q=ubuntu%2C+redhat%2C+fedora">most</a> <a title="Ubuntu popularity - Distrowatch" href="http://distrowatch.com/stats.php?section=popularity">popular</a> Linux distribution and starts offering WebSphere MQ DEBs to install, here&#8217;s a small HOWTO which will guide you through the installation process. I&#8217;m using Ubuntu 9.10 and MQ 7.0.1 (CZ4VEML), both 64-bits, but it&#8217;s something I&#8217;ve been using since at least Ubuntu 7.04  I think, so it will work reliably with other versions of Ubuntu and MQ as well.<br />
<br/></p>
<h1>Installation</h1>
<p><br/><br />
First untar the CZ4VEML.tar.gz archive and then install <em>rpm</em> and <em>sharutils</em> packages:</p>
<pre>dsuch$ <span style="color: #008000;"><strong>sudo apt-get install rpm sharutils</strong></span></pre>
<p>Next try to execute the ./mqlicense.sh script which will most probably fail horribly but that&#8217;s OK, we&#8217;ll work around it</p>
<pre>dsuch$ <strong><span style="color: #008000;">sudo ./mqlicense.sh</span></strong></pre>
<p>Accept the license if it doesn&#8217;t end with an error such as the one below ..</p>
<pre>Exception in thread "main" java.lang.UnsatisfiedLinkError: fontmanager
(libstdc++.so.5: cannot open shared object file: No such file or directory)</pre>
<p>.. but if it does, just read the license carefully</p>
<pre>dsuch$ <strong><span style="color: #008000;">less ./licenses/LA_en</span></strong></pre>
<p>I&#8217;m assuming the script has failed but if you take a look at it closely you&#8217;ll notice that all it does in the end is to create a <em>/tmp/mq_license/license/status.dat</em> file, so we&#8217;ll do the same ourselves a couple of times.</p>
<p>Now comes the runtime, preceded by the manual creation of the file which ./mqlicense.sh failed to create.</p>
<pre>dsuch$ <span style="color: #008000;"><strong>sudo mkdir -p /tmp/mq_license/license/</strong></span>
dsuch$ <span style="color: #008000;"><strong>sudo touch /tmp/mq_license/license/status.dat</strong></span>
dsuch$ <strong><span style="color: #008000;">sudo rpm -iavh --nodeps --force-debian
./MQSeriesRuntime-7.0.1-0.x86_64.rpm</span></strong></pre>
<p>.. more RPMs to install ..</p>
<pre>dsuch$ <span style="color: #008000;"><strong>sudo mkdir -p /tmp/mq_license/license/</strong></span>
dsuch$ <span style="color: #008000;"><strong>sudo touch /tmp/mq_license/license/status.dat</strong></span>
dsuch$ <span style="color: #008000;"><strong>sudo rpm -iavh --nodeps --force-debian ./MQSeriesJava-7.0.1-0.x86_64.rpm</strong></span>
<span style="color: #008000;"><strong>./MQSeriesClient-7.0.1-0.x86_64.rpm ./MQSeriesServer-7.0.1-0.x86_64.rpm</strong></span>
<span style="color: #008000;"><strong>./MQSeriesSDK-7.0.1-0.x86_64.rpm ./MQSeriesSamples-7.0.1-0.x86_64.rpm</strong></span>
<span style="color: #008000;"><strong>./MQSeriesTXClient-7.0.1-0.x86_64.rpm
</strong></span><span style="color: #008000;"> </span></pre>
<p>.. and the last batch of RPMs ..</p>
<pre>dsuch$ <strong><span style="color: #008000;">sudo mkdir -p /tmp/mq_license/license/</span></strong>
dsuch$ <strong><span style="color: #008000;">sudo touch /tmp/mq_license/license/status.dat</span></strong>
dsuch$ <strong><span style="color: #008000;">sudo rpm -iavh --nodeps --force-debian  ./MQSeriesMan-7.0.1-0.x86_64.rpm
./MQSeriesKeyMan-7.0.1-0.x86_64.rpm</span> </strong></pre>
<p>The newly created <em>mqm</em> user would sure like to use Bash by default <img src='http://www.gefira.pl/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<pre>dsuch$ <span style="color: #008000;"><strong>sudo usermod -s /bin/bash mqm</strong></span></pre>
<p>That&#8217;s all there is to installing, let&#8217;s try to send some messages.<br />
<br/></p>
<h1>Verifying the installation</h1>
<p><br/><br />
First change the user to mqm ..</p>
<pre>dsuch$ <strong><span style="color: #008000;">sudo su - mqm</span></strong></pre>
<p>.. and create &amp; start a QM01 queue manager:</p>
<pre>mqm$ <span style="color: #008000;"><strong>crtmqm QM01</strong></span>
mqm$ <span style="color: #008000;"><strong>strmqm QM01</strong></span></pre>
<p>Let&#8217;s define a Q1 local queue for testing</p>
<pre>mqm$ <span style="color: #008000;"><strong>runmqsc QM01</strong></span></pre>
<p>Now cd to <em>/opt/mqm/samp/bin/</em> which holds various precompiled utilities among which are <em>amqsput</em> and <em>amqsget</em>. By the way, the source code is in <em>/opt/mqm/samp</em>.</p>
<p>Send two messages to the Q1 queue on the QM01 queue manager and quit amsqput with Ctrl-C:</p>
<pre>mqm$ <span style="color: #008000;"><strong>./amqsput Q1 QM01</strong></span>
Sample AMQSPUT0 start
target queue is Q1
<span style="color: #008000;"><strong>123</strong>
<strong>zxc
^C</strong></span></pre>
<p>Now let&#8217;s use amqsget to get the messages off Q1 queue and quit with Ctrl-C again, as you can see, there are 2 messages sent there in the previous step:</p>
<pre>mqm$ <span style="color: #008000;"><strong>./amqsget Q1 QM01</strong></span>
Sample AMQSGET0 start
message &lt;123&gt;
message &lt;zxc&gt;
<span style="color: #008000;"><strong>^C</strong></span></pre>
<p><br/></p>
<h1>Removing WebSphere MQ</h1>
<p><br/><br />
Use those three commands if for some reason you need to remove MQ from your system.</p>
<pre>dsuch$ <span style="color: #008000;"><strong>sudo rpm -qa | grep "MQSeries" | xargs sudo rpm -e --force-debian --noscripts</strong></span>
dsuch$ <span style="color: #008000;"><strong>sudo rm -rf /var/mqm</strong></span>
dsuch$ <span style="color: #008000;"><strong>sudo userdel mqm</strong></span></pre>
<p>That&#8217;s all folks, the rest is in the MQ documentation, using MQ on Ubuntu is in no way different to what RHEL has to offer, it&#8217;s simply Linux and everything works just fine. Except the installer <img src='http://www.gefira.pl/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.gefira.pl/blog/2010/07/03/websphere-mq-and-ubuntu-howto/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>LNK1112 and plain setup.py</title>
		<link>http://www.gefira.pl/blog/2010/06/26/lnk1112-and-plain-setup-py/</link>
		<comments>http://www.gefira.pl/blog/2010/06/26/lnk1112-and-plain-setup-py/#comments</comments>
		<pubDate>Sat, 26 Jun 2010 18:11:48 +0000</pubDate>
		<dc:creator>Dariusz Suchojad</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.gefira.pl/blog/?p=579</guid>
		<description><![CDATA[I was scratching my head over why I couldn&#8217;t create a Windows AMD64 installer for PyMQI and even though I had Visual Studio 2008 Proffesional Edition installed I was still greeted with &#8220;LNK1112: module machine type &#8216;x64&#8242; conflicts with target machine type &#8216;X86&#8242;&#8221;. All of the blog posts were mentioning various tweaks to nmake but [...]]]></description>
			<content:encoded><![CDATA[<p>I was scratching my head over why I couldn&#8217;t create a Windows AMD64 installer for <a title="PyMQI - Python interface to WebSphere MQ" href="http://packages.python.org/pymqi/">PyMQI</a> and even though I had Visual Studio 2008 Proffesional Edition installed I was still greeted with <em>&#8220;LNK1112: module machine type &#8216;x64&#8242; conflicts with target machine type &#8216;X86&#8242;&#8221;</em>. All of the blog posts were mentioning various tweaks to nmake but that wasn&#8217;t actually helpful because I just wanted plain <em>&#8220;python setup.py build&#8221;</em> to work first and besides, I&#8217;m not using any makefiles.</p>
<p>Once again, turned out that reading <a title="Python distutils - Cross-compiling on Windows" href="http://docs.python.org/distutils/builtdist.html#cross-compiling-on-windows">the fine documentation</a> should&#8217;ve been my first step. As you can read over there, for some unknown reason the compiler for 64-bit systems isn&#8217;t installed by default. Once I installed it, I found I could run a new &#8220;vcvarsx86_amd64.bat&#8221; command and then miraculously I could do<em> &#8220;python setup.py build &#8211;plat-name=win-amd64 bdist_wininst&#8221;</em> to make the installer <img src='http://www.gefira.pl/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.gefira.pl/blog/2010/06/26/lnk1112-and-plain-setup-py/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Make mc remember the last directory</title>
		<link>http://www.gefira.pl/blog/2010/06/16/make-mc-remember-the-last-directory/</link>
		<comments>http://www.gefira.pl/blog/2010/06/16/make-mc-remember-the-last-directory/#comments</comments>
		<pubDate>Wed, 16 Jun 2010 18:30:07 +0000</pubDate>
		<dc:creator>Dariusz Suchojad</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://www.gefira.pl/blog/?p=573</guid>
		<description><![CDATA[This is mostly so I don&#8217;t forget about it myself.. when installing Ubuntu, don&#8217;t forget to add this line to ~/.bashrc and have Midnight Commander remember the last directory you were in on exit instead of taking you back to the one you were in when you typed mc in the shell:
source /usr/share/mc/bin/mc.sh
]]></description>
			<content:encoded><![CDATA[<p>This is mostly so I don&#8217;t forget about it myself.. when installing Ubuntu, don&#8217;t forget to add this line to ~/.bashrc and have Midnight Commander remember the last directory you were in on exit instead of taking you back to the one you were in when you typed <em>mc</em> in the shell:</p>
<pre>source /usr/share/mc/bin/mc.sh</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.gefira.pl/blog/2010/06/16/make-mc-remember-the-last-directory/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Intro To Spring Python Slides</title>
		<link>http://www.gefira.pl/blog/2010/06/03/intro-to-spring-python-slides/</link>
		<comments>http://www.gefira.pl/blog/2010/06/03/intro-to-spring-python-slides/#comments</comments>
		<pubDate>Thu, 03 Jun 2010 19:29:38 +0000</pubDate>
		<dc:creator>Dariusz Suchojad</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[Spring Python]]></category>

		<guid isPermaLink="false">http://www.gefira.pl/blog/?p=563</guid>
		<description><![CDATA[For those of you who are still wavering over whether to use Spring Python in your next project, have a look at this slidecast describing the current Spring Python project&#8217;s status, as of release 1.1, and where it&#8217;s heading to.
]]></description>
			<content:encoded><![CDATA[<p>For those of you who are still wavering over whether to use <a title="Spring Python - homepage" href="http://springpython.webfactional.com/">Spring Python</a> in your next project, <a title="Intro to Spring Python - Slidecast" href="http://springpythonbook.com/slide-cast-about-spring-python-mentions-book">have a look at this slidecast</a> describing the current Spring Python project&#8217;s status, as of release 1.1, and where it&#8217;s heading to.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gefira.pl/blog/2010/06/03/intro-to-spring-python-slides/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to read PKG-INFO metadata off a Python .egg distribution</title>
		<link>http://www.gefira.pl/blog/2010/05/02/how-to-read-pkg-info-metadata-off-a-python-egg-distribution/</link>
		<comments>http://www.gefira.pl/blog/2010/05/02/how-to-read-pkg-info-metadata-off-a-python-egg-distribution/#comments</comments>
		<pubDate>Sun, 02 May 2010 10:40:47 +0000</pubDate>
		<dc:creator>Dariusz Suchojad</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.gefira.pl/blog/?p=553</guid>
		<description><![CDATA[It&#8217;s obvious when you see it but somehow I&#8217;ve just spent an hour on figuring out how to read a PKG-INFO metadata file off a Python .egg distribution, so here it is in case someone needs it some day  

# stdlib
import zipimport
&#160;
# Distribute
import pkg_resources
&#160;
egg_file = &#34;/path/to/a/distribution-1.0-py2.6.egg&#34;
dist = pkg_resources.Distribution.from_filename&#40;egg_file,
       [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s obvious when you see it but somehow I&#8217;ve just spent an hour on figuring out how to read a PKG-INFO metadata file off a Python .egg distribution, so here it is in case someone needs it some day <img src='http://www.gefira.pl/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># stdlib</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">zipimport</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Distribute</span>
<span style="color: #ff7700;font-weight:bold;">import</span> pkg_resources
&nbsp;
egg_file = <span style="color: #483d8b;">&quot;/path/to/a/distribution-1.0-py2.6.egg&quot;</span>
dist = pkg_resources.<span style="color: black;">Distribution</span>.<span style="color: black;">from_filename</span><span style="color: black;">&#40;</span>egg_file,
             metadata=pkg_resources.<span style="color: black;">EggMetadata</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">zipimport</span>.<span style="color: black;">zipimporter</span><span style="color: black;">&#40;</span>egg_file<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># pkg_info will now be equal to the contents of a PKG-INFO metadata file</span>
pkg_info = dist.<span style="color: black;">get_metadata</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PKG-INFO&quot;</span><span style="color: black;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.gefira.pl/blog/2010/05/02/how-to-read-pkg-info-metadata-off-a-python-egg-distribution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>An introduction to Spring Python and IoC/DI (Inversion of Control/Dependency Injection)</title>
		<link>http://www.gefira.pl/blog/2010/05/01/an-introduction-to-spring-python-and-iocdi-inversion-of-controldependency-injection/</link>
		<comments>http://www.gefira.pl/blog/2010/05/01/an-introduction-to-spring-python-and-iocdi-inversion-of-controldependency-injection/#comments</comments>
		<pubDate>Sat, 01 May 2010 09:32:04 +0000</pubDate>
		<dc:creator>Dariusz Suchojad</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Spring Python]]></category>

		<guid isPermaLink="false">http://www.gefira.pl/blog/?p=457</guid>
		<description><![CDATA[Continuing on the last post, we&#8217;ll now be extending the code with an IoC (Inversion of Control)-driven configuration. Spring Python offers an IoC container which may be used for injecting dependencies between the application&#8217;s objects. The concept is that objects are to focus on their job and usually should be blissfully unaware of where the [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Continuing on <a title="gefira blog - Spring Python, Pyro and remote services" href="http://www.gefira.pl/blog/2010/04/25/spring-python-pyro-and-remote-services/">the last post</a>, we&#8217;ll now be extending the code with an <a title="IoC - Wikipedia" href="http://en.wikipedia.org/wiki/Inversion_of_Control">IoC (Inversion of Control)</a>-driven configuration. <a title="Spring Python - Homepage" href="http://springpython.webfactional.com/">Spring Python</a> <a title="Spring Python - IoC ref docs" href="http://springpython.webfactional.com/1.1.0/reference/html/objects.html">offers an IoC container</a> which may be used for injecting dependencies between the application&#8217;s objects. The concept is that objects are to focus on their job and usually should be blissfully unaware of where the other objects they operate upon come from. The dependencies are dynamically injected so the control of who uses what is sort of inverted, it&#8217;s the container who knows what and when to distribute to others in order for everything to play nicely together and that&#8217;s why language lawyers can&#8217;t decide whether to call it IoC (Inversion of Control) or DI (Dependency Injection) but let that not stop us from applying it in practice <img src='http://www.gefira.pl/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  And of course it&#8217;s only a concept, it&#8217;s not something magical, it&#8217;s simply a decent addition to your toolbox which you may like to get familiar with and Spring Python with its gentle learning curve is just the right tool. Throughout the article, <a title="Spring Python - ref docs 1.1" href="http://springpython.webfactional.com/1.1.0/reference/html/index.html">Spring Python 1.1</a> will be used.</p>
<p style="text-align: justify;">Picking off from where we left <a title="gefira blog - Spring Python, Pyro and remote services" href="http://www.gefira.pl/blog/2010/04/25/spring-python-pyro-and-remote-services/">last time</a>, here&#8217;s a bare bones &#8216;uptime&#8217; server and its client.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># An 'uptime' server</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># stdlib</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">logging</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">commands</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Spring Python</span>
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">remoting</span>.<span style="color: black;">pyro</span> <span style="color: #ff7700;font-weight:bold;">import</span> PyroServiceExporter
&nbsp;
<span style="color: #808080; font-style: italic;"># Configure logging subsystem</span>
<span style="color: #dc143c;">logging</span>.<span style="color: black;">basicConfig</span><span style="color: black;">&#40;</span>level=<span style="color: #dc143c;">logging</span>.<span style="color: black;">DEBUG</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Service<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> get_uptime<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #dc143c;">logging</span>.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;get_uptime invoked&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">commands</span>.<span style="color: black;">getoutput</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;uptime&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Create a service ..</span>
service = Service<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># .. export it through Pyro ..</span>
service_exporter = PyroServiceExporter<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
service_exporter.<span style="color: black;">service</span> = service
service_exporter.<span style="color: black;">service_name</span> = <span style="color: #483d8b;">&quot;Service&quot;</span>
service_exporter.<span style="color: black;">service_host</span> = <span style="color: #483d8b;">&quot;127.0.0.1&quot;</span>
service_exporter.<span style="color: black;">service_port</span> = <span style="color: #483d8b;">&quot;16099&quot;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># .. and start the server.</span>
service_exporter.<span style="color: black;">after_properties_set</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Client</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># stdlib</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">logging</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Spring Python</span>
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">remoting</span>.<span style="color: black;">pyro</span> <span style="color: #ff7700;font-weight:bold;">import</span> PyroProxyFactory
&nbsp;
<span style="color: #808080; font-style: italic;"># Configure logging subsystem</span>
<span style="color: #dc143c;">logging</span>.<span style="color: black;">basicConfig</span><span style="color: black;">&#40;</span>level=<span style="color: #dc143c;">logging</span>.<span style="color: black;">DEBUG</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Point the client to a server ..</span>
service = PyroProxyFactory<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
service.<span style="color: black;">service_url</span> = <span style="color: #483d8b;">&quot;PYROLOC://127.0.0.1:16099/Service&quot;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># .. invoke the service ..</span>
uptime = service.<span style="color: black;">get_uptime</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># and print it on the console.</span>
<span style="color: #dc143c;">logging</span>.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Current uptime: [%s]&quot;</span> <span style="color: #66cc66;">%</span> uptime<span style="color: black;">&#41;</span></pre></div></div>

<p style="text-align: justify;">A word on Spring Python&#8217;s IoC syntax, there&#8217;s a plenty of choices to pick from, with three major ones being <a title="Spring Python - PythonConfig" href="http://springpython.webfactional.com/1.1.0/reference/html/objects.html#objects-config-object">PythonConfig,</a> <a title="Spring Python - YamlConfig" href="http://springpython.webfactional.com/1.1.0/reference/html/objects.html#objects-config-yamlconfig">YamlConfig</a> and <a title="Spring Python - XMLConfig" href="http://springpython.webfactional.com/1.1.0/reference/html/objects.html#objects-config-xmlconfig">XMLConfig</a>. Being agile Python folks as we are, we chose to use YamlConfig and PythonConfig for configuring things but it should be noted that all other config modes would&#8217;ve allowed us to achieve the same.</p>
<p style="text-align: justify;">As always, it&#8217;s all up to you to decide what makes more sense in given circumstances though my recommendation is to use YamlConfig or XMLConfig with a strong preference on the former if you work in a diverse Python/Java/.NET environment with people who already know Spring Framework and PythonConfig/YamlConfig if you&#8217;re free to choose whatever you like.</p>
<p style="text-align: justify;">So here&#8217;s the server&#8217;s basic configuration written in <a title="YAML: YAML Ain't Markup Language - Homepage" href="http://yaml.org/">YAML</a>..</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># server-config.yml</span>
&nbsp;
objects:
    - <span style="color: #008000;">object</span>: service_name
      <span style="color: #008000;">str</span>: Service
&nbsp;
    - <span style="color: #008000;">object</span>: service_host
      <span style="color: #008000;">str</span>: 127.0.0.1
&nbsp;
    - <span style="color: #008000;">object</span>: service_port
      <span style="color: #008000;">str</span>: <span style="color: #ff4500;">16099</span></pre></div></div>

<p>.. and here&#8217;s some code to read if off a file we stored it in:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># ioc-yaml-sample.py</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Spring Python</span>
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">config</span> <span style="color: #ff7700;font-weight:bold;">import</span> YamlConfig
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">context</span> <span style="color: #ff7700;font-weight:bold;">import</span> ApplicationContext
&nbsp;
config = YamlConfig<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;./server-config.yml&quot;</span><span style="color: black;">&#41;</span>
container = ApplicationContext<span style="color: black;">&#40;</span>config<span style="color: black;">&#41;</span>
&nbsp;
service_name = container.<span style="color: black;">get_object</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;service_name&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Hey, the name is '%s'&quot;</span> <span style="color: #66cc66;">%</span> service_name</pre></div></div>

<p style="text-align: justify;">YamlConfig&#8217;s instance reads the file, parses it, creates an internal cache of definitions found and then the config may be fed to ApplicationContext which is the actual class used for fetching objects from a container. Why is the class called ApplicationContext instead of Container, you wonder? Well in fact the actual container is called <a title="Spring Python - ObjectContainer vs. ApplicationContext" href="http://springpython.webfactional.com/1.1.0/reference/html/objects.html#objects-container-objectcontainer">springpython.container.ObjectContainer and ApplicationContext subclasses it</a> in order to add some additional goodies such as the possibility of hooking into an object&#8217;s lifecycle.</p>
<p style="text-align: justify;">Let&#8217;s now introduce server to the config and let&#8217;s create the main application&#8217;s module, boringly named &#8216;main.py&#8217;, so that in result we have following files ..</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">.
|-- client.<span style="color: black;">py</span>
|-- main.<span style="color: black;">py</span>
|-- server-config.<span style="color: black;">yml</span>
<span style="color: #66cc66;">`</span>-- server.<span style="color: black;">py</span></pre></div></div>

<p>.. with such a contents ..</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># client.py (no changes here so far)</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># stdlib</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">logging</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Spring Python</span>
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">remoting</span>.<span style="color: black;">pyro</span> <span style="color: #ff7700;font-weight:bold;">import</span> PyroProxyFactory
&nbsp;
<span style="color: #808080; font-style: italic;"># Configure logging subsystem</span>
<span style="color: #dc143c;">logging</span>.<span style="color: black;">basicConfig</span><span style="color: black;">&#40;</span>level=<span style="color: #dc143c;">logging</span>.<span style="color: black;">DEBUG</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Point the client to a server ..</span>
service = PyroProxyFactory<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
service.<span style="color: black;">service_url</span> = <span style="color: #483d8b;">&quot;PYROLOC://127.0.0.1:16099/Service&quot;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># .. invoke the service ..</span>
uptime = service.<span style="color: black;">get_uptime</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># and print it on the console.</span>
<span style="color: #dc143c;">logging</span>.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Current uptime: [%s]&quot;</span> <span style="color: #66cc66;">%</span> uptime<span style="color: black;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># main.py</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># stdlib</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">logging</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Spring Python</span>
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">config</span> <span style="color: #ff7700;font-weight:bold;">import</span> YamlConfig
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">context</span> <span style="color: #ff7700;font-weight:bold;">import</span> ApplicationContext
&nbsp;
config = YamlConfig<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;./server-config.yml&quot;</span><span style="color: black;">&#41;</span>
container = ApplicationContext<span style="color: black;">&#40;</span>config<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Fetch configuration from the container ..</span>
log_level = container.<span style="color: black;">get_object</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;log_level&quot;</span><span style="color: black;">&#41;</span>
log_format = container.<span style="color: black;">get_object</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;log_format&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># .. and configure logging.</span>
logger = <span style="color: #dc143c;">logging</span>.<span style="color: black;">getLogger</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: black;">&#41;</span>
logger.<span style="color: black;">setLevel</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">logging</span>.<span style="color: black;">getLevelName</span><span style="color: black;">&#40;</span>log_level<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
handler = <span style="color: #dc143c;">logging</span>.<span style="color: black;">StreamHandler</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #dc143c;">formatter</span> = <span style="color: #dc143c;">logging</span>.<span style="color: black;">Formatter</span><span style="color: black;">&#40;</span>log_format<span style="color: black;">&#41;</span>
handler.<span style="color: black;">setFormatter</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">formatter</span><span style="color: black;">&#41;</span>
&nbsp;
logger.<span style="color: black;">addHandler</span><span style="color: black;">&#40;</span>handler<span style="color: black;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># server-config.yml</span>
&nbsp;
objects:
    - <span style="color: #008000;">object</span>: service_name
      <span style="color: #008000;">str</span>: Service
&nbsp;
    - <span style="color: #008000;">object</span>: service_host
      <span style="color: #008000;">str</span>: 127.0.0.1
&nbsp;
    - <span style="color: #008000;">object</span>: service_port
      <span style="color: #008000;">str</span>: <span style="color: #ff4500;">16099</span>
&nbsp;
    - <span style="color: #008000;">object</span>: service
      <span style="color: #ff7700;font-weight:bold;">class</span>: server.<span style="color: black;">Service</span>
&nbsp;
    - <span style="color: #008000;">object</span>: exporter
      <span style="color: #ff7700;font-weight:bold;">class</span>: springpython.<span style="color: black;">remoting</span>.<span style="color: black;">pyro</span>.<span style="color: black;">PyroServiceExporter</span>
      properties:
        service: <span style="color: black;">&#123;</span>ref: service<span style="color: black;">&#125;</span>
        service_name: <span style="color: black;">&#123;</span>ref: service_name<span style="color: black;">&#125;</span>
        service_host: <span style="color: black;">&#123;</span>ref: service_host<span style="color: black;">&#125;</span>
        service_port: <span style="color: black;">&#123;</span>ref: service_port<span style="color: black;">&#125;</span>
&nbsp;
    - <span style="color: #008000;">object</span>: log_level
      <span style="color: #008000;">str</span>: DEBUG
&nbsp;
    - <span style="color: #008000;">object</span>: log_format
      <span style="color: #008000;">str</span>: <span style="color: #483d8b;">&quot;%(asctime)s - %(levelname)s - %(process)d:%(threadName)s - %(name)s:%(lineno)d - %(message)s&quot;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># server.py</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># stdlib</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">logging</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">commands</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Service<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">logger</span> = <span style="color: #dc143c;">logging</span>.<span style="color: black;">getLogger</span><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>__name__, <span style="color: #008000;">self</span>.__class__.__name__<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> get_uptime<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">logger</span>.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;get_uptime invoked&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">commands</span>.<span style="color: black;">getoutput</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;uptime&quot;</span><span style="color: black;">&#41;</span></pre></div></div>

<p style="text-align: justify;">So what have we achieved? The actual business logic has been decoupled from its configuration so that it doesn&#8217;t know anything about Pyro because, after all, should it care about it? Its business is to return the current uptime and that&#8217;s what it&#8217;s good at. That it&#8217;s going to be exposed through Pyro is also not that important for its core functionality, it&#8217;s Pyro today but could be Zope tomorrow, who knows it. PyroServiceExporter also happily lives in a state of not really being aware of what service it is publishing outside. Changing either of those should not mean touching the source code even if it meant merely adding a couple of lines of code. And the other stuff, it&#8217;s just a simple configuration we had to externalize anyway so why not put it into same place for consistency? To keep things simpler, the client code hasn&#8217;t been changed yet though it will be later on when we&#8217;ll be working with PythonConfig.</p>
<p style="text-align: justify;">When choosing what to place in the container there&#8217;s always a danger of going too far and letting it all look awkwardly and unpythonic or just plain ugly. As a rule of thumb, your application surely can be explained in terms of some kind of components which need to be wired up together and that&#8217;s where the division line could be initially drawn. Another point is standard configuration details such as host, port or the logging format above, it&#8217;s something that sysadmins love to customize so it should allow for an easy access (and that incidentally is another reason why XMLConfig is something you should think twice about before making use of as sysadmins usually have their, let&#8217;s call it, well-grounded opinion about working with XML). Remember how several years ago some applications were doing crazy things like ..</p>
<pre>&lt;query id="get_customer"&gt;
  &lt;table name="customer" pkey="cust_no"&gt;
    &lt;columns&gt;
      &lt;column name="cust_name" /&gt;
      &lt;column name="address" /&gt;
      &lt;column name="phone_no" /&gt;
    &lt;/columns&gt;
  &lt;/table&gt;
  &lt;operator name="EQ" /&gt;
&lt;/query&gt;</pre>
<p>.. instead of ..</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">get_customer <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;SELECT cust_name, address, phone_no FROM customer WHERE cust_no=?&quot;</span></pre></div></div>

<p style="text-align: justify;">.. and they used to call it being &#8220;XML-ready&#8221; or &#8220;XML-aware&#8221;? Well, if you catch yourself on doing similar things with IoC then you know you&#8217;ve gone too far <img src='http://www.gefira.pl/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p style="text-align: justify;">If you happened to jump directly into running the code above instead of reading the text then you probably were wondering why the server had been started even though nowhere in the code we explicitly told it to. That&#8217;s because PyroServiceExporter is an <em>InitializingObject</em> &#8211; it&#8217;s a subclass of springpython.context.InitializingObject which means its <em>after_properties_set</em> method gets called right after all of its properties are resolved and set in place and it just happens that PyroServiceExporter starts a new server thread in its after_properties_set hook. If you scroll up to the beginning of this post you&#8217;ll recall that odd-looking <em>service_exporter.after_properties_set()</em> line that was advertized to &#8217;start the server&#8217;, which it did -  but if you&#8217;re using IoC, the container will call it on your behalf so that you don&#8217;t have to remember about it.</p>
<p style="text-align: justify;">Other interesting classes which your objects may wish to subclass are <em>springpython.context.ObjectPostProcessor</em> and <em>springpython.context.DisposableObject</em>. Subclassing ObjectPostProcessor gives you two new methods to override &#8211; <em>post_process_before_initialization</em> which will be invoked before after_properties_set and <em>post_process_after_initialization</em> which will be called after after_properties_set will have been called. DisposableObject on the other hand lets you implement a method called <em>destroy</em> which will be called at the time when the container itself will be shutting down; to be precise, ApplicationContext registers its &#8217;shutdown_hook&#8217; method to the Python stdlib&#8217;s <a title="atexit - Python ref docs" href="http://docs.python.org/library/atexit.html">atexit</a> module and the shutdown hook is responsible for calling each of the disposable objects&#8217; &#8216;destroy&#8217; method; &#8216;destroy&#8217; is the right place for your components to clean up after themselves, e.g. it can be used to release any external resources held during an object&#8217;s lifetime.</p>
<p style="text-align: justify;">It&#8217;s worth noting that ApplicationContext may be passed in <em>a list</em> of configurations and although it won&#8217;t be used in examples, each of them may be of different type and that&#8217;s what I usually end up with, bits that I feel are more or less of a static nature are kept in YamlConfig and PythonConfig stores those which sometimes need to do something more before they&#8217;re ready to be used.</p>
<p style="text-align: justify;">Before focusing on adding security features there&#8217;s still one thing that badly needs be improved, it&#8217;s the client code which knows nothing about IoC and breaks the <a title="Wikipedia - DRY Don't Repeat Yourself" href="http://en.wikipedia.org/wiki/DRY">DRY</a> principle by duplicating configuration. In real world situation this could be in some degree expected, as both the server and a client could&#8217;ve been developed or operated by different teams or organizations but here it looks feebly, though at the same time it provides a good pretext for focusing a bit more on PythonConfig <img src='http://www.gefira.pl/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  Let&#8217;s rewrite it all into PythonConfig while keeping in mind the DRY rule then:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">.
|-- client-main.<span style="color: black;">py</span>
|-- client_config.<span style="color: black;">py</span>
|-- common_config.<span style="color: black;">py</span>
|-- server-main.<span style="color: black;">py</span>
|-- server.<span style="color: black;">py</span>
<span style="color: #66cc66;">`</span>-- server_config.<span style="color: black;">py</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># client-main.py</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># stdlib</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">logging</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Spring Python</span>
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">context</span> <span style="color: #ff7700;font-weight:bold;">import</span> ApplicationContext
&nbsp;
<span style="color: #808080; font-style: italic;"># Our application</span>
<span style="color: #ff7700;font-weight:bold;">from</span> client_config <span style="color: #ff7700;font-weight:bold;">import</span> ClientConfig
&nbsp;
client_config = ClientConfig<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
container = ApplicationContext<span style="color: black;">&#40;</span>client_config<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Fetch configuration from the container ..</span>
log_level = container.<span style="color: black;">get_object</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;log_level&quot;</span><span style="color: black;">&#41;</span>
log_format = container.<span style="color: black;">get_object</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;log_format&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># .. and configure logging.</span>
<span style="color: #dc143c;">logging</span>.<span style="color: black;">basicConfig</span><span style="color: black;">&#40;</span>level=<span style="color: #dc143c;">logging</span>.<span style="color: black;">getLevelName</span><span style="color: black;">&#40;</span>log_level<span style="color: black;">&#41;</span>, format=log_format<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># .. fetch the service ..</span>
service = container.<span style="color: black;">get_object</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;service&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># .. invoke it ..</span>
uptime = service.<span style="color: black;">get_uptime</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># and print the output.</span>
<span style="color: #dc143c;">logging</span>.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Current uptime: [%s]&quot;</span> <span style="color: #66cc66;">%</span> uptime<span style="color: black;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># client_config.py</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Spring Python</span>
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">config</span> <span style="color: #ff7700;font-weight:bold;">import</span> Object
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">remoting</span>.<span style="color: black;">pyro</span> <span style="color: #ff7700;font-weight:bold;">import</span> PyroProxyFactory
&nbsp;
<span style="color: #808080; font-style: italic;"># Our application</span>
<span style="color: #ff7700;font-weight:bold;">from</span> common_config <span style="color: #ff7700;font-weight:bold;">import</span> CommonConfig
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> ClientConfig<span style="color: black;">&#40;</span>CommonConfig<span style="color: black;">&#41;</span>:
&nbsp;
    @Object
    <span style="color: #ff7700;font-weight:bold;">def</span> service<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        service = PyroProxyFactory<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        service.<span style="color: black;">service_url</span> = <span style="color: #483d8b;">&quot;PYROLOC://%s:%s/%s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">service_host</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>,
                                <span style="color: #008000;">self</span>.<span style="color: black;">service_port</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, <span style="color: #008000;">self</span>.<span style="color: black;">service_name</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> service</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># common_config.py</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># stdlib</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">logging</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Spring Python</span>
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">config</span> <span style="color: #ff7700;font-weight:bold;">import</span> Object
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">config</span> <span style="color: #ff7700;font-weight:bold;">import</span> PythonConfig
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> CommonConfig<span style="color: black;">&#40;</span>PythonConfig<span style="color: black;">&#41;</span>:
&nbsp;
    @Object
    <span style="color: #ff7700;font-weight:bold;">def</span> service_host<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;127.0.0.1&quot;</span>
&nbsp;
    @Object
    <span style="color: #ff7700;font-weight:bold;">def</span> service_port<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;16099&quot;</span>
&nbsp;
    @Object
    <span style="color: #ff7700;font-weight:bold;">def</span> service_name<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;Service&quot;</span>
&nbsp;
    @Object
    <span style="color: #ff7700;font-weight:bold;">def</span> log_level<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;DEBUG&quot;</span>
&nbsp;
    @Object
    <span style="color: #ff7700;font-weight:bold;">def</span> log_format<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;%(asctime)s - %(levelname)s - %(process)d:%(threadName)s - %(name)s:%(lineno)d - %(message)s&quot;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># server-main.py</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># stdlib</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">logging</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Spring Python</span>
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">context</span> <span style="color: #ff7700;font-weight:bold;">import</span> ApplicationContext
&nbsp;
<span style="color: #808080; font-style: italic;"># Our application</span>
<span style="color: #ff7700;font-weight:bold;">from</span> server_config <span style="color: #ff7700;font-weight:bold;">import</span> ServerConfig
&nbsp;
server_config = ServerConfig<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
container = ApplicationContext<span style="color: black;">&#40;</span>server_config<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Fetch configuration from the container ..</span>
log_level = container.<span style="color: black;">get_object</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;log_level&quot;</span><span style="color: black;">&#41;</span>
log_format = container.<span style="color: black;">get_object</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;log_format&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># .. and configure logging.</span>
<span style="color: #dc143c;">logging</span>.<span style="color: black;">basicConfig</span><span style="color: black;">&#40;</span>level=<span style="color: #dc143c;">logging</span>.<span style="color: black;">getLevelName</span><span style="color: black;">&#40;</span>log_level<span style="color: black;">&#41;</span>, format=log_format<span style="color: black;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># server.py (no changes here)</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># stdlib</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">logging</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">commands</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Service<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">logger</span> = <span style="color: #dc143c;">logging</span>.<span style="color: black;">getLogger</span><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>__name__, <span style="color: #008000;">self</span>.__class__.__name__<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> get_uptime<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">logger</span>.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;get_uptime invoked&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">commands</span>.<span style="color: black;">getoutput</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;uptime&quot;</span><span style="color: black;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># server_config.py</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Spring Python</span>
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">config</span> <span style="color: #ff7700;font-weight:bold;">import</span> Object
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">remoting</span>.<span style="color: black;">pyro</span> <span style="color: #ff7700;font-weight:bold;">import</span> PyroServiceExporter
&nbsp;
<span style="color: #808080; font-style: italic;"># Our application</span>
<span style="color: #ff7700;font-weight:bold;">from</span> server <span style="color: #ff7700;font-weight:bold;">import</span> Service
<span style="color: #ff7700;font-weight:bold;">from</span> common_config <span style="color: #ff7700;font-weight:bold;">import</span> CommonConfig
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> ServerConfig<span style="color: black;">&#40;</span>CommonConfig<span style="color: black;">&#41;</span>:
&nbsp;
    @Object
    <span style="color: #ff7700;font-weight:bold;">def</span> service<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> Service<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    @Object
    <span style="color: #ff7700;font-weight:bold;">def</span> exporter<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        exporter = PyroServiceExporter<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        exporter.<span style="color: black;">service</span> = <span style="color: #008000;">self</span>.<span style="color: black;">service</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        exporter.<span style="color: black;">service_name</span> = <span style="color: #008000;">self</span>.<span style="color: black;">service_name</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        exporter.<span style="color: black;">service_host</span> = <span style="color: #008000;">self</span>.<span style="color: black;">service_host</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        exporter.<span style="color: black;">service_port</span> = <span style="color: #008000;">self</span>.<span style="color: black;">service_port</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">return</span> exporter</pre></div></div>

<p style="text-align: justify;">Methods decorated with an<em> @Object</em> decorator are turned into container-managed objects and by default they are stored in an internal cache of <em>singleton objects</em> as opposed to being fetched each time they are requested. Objects can also be <em>lazily-initialized</em> when they are first time needed by someone. Finally, they can be <em>abstract</em> and/or <em>have parents</em>. Let&#8217;s discuss the options through.</p>
<p style="text-align: justify;">@Object is a short-hand form of @Object(scope.SINGLETON) and these two are equivalent.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Spring Python</span>
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">config</span> <span style="color: #ff7700;font-weight:bold;">import</span> Object
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">config</span> <span style="color: #ff7700;font-weight:bold;">import</span> PythonConfig
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> foo <span style="color: #ff7700;font-weight:bold;">import</span> SomeClass
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> MyConfig<span style="color: black;">&#40;</span>PythonConfig<span style="color: black;">&#41;</span>:
&nbsp;
    @Object
    <span style="color: #ff7700;font-weight:bold;">def</span> my_singleton<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        singleton = SomeClass<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> singleton</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Spring Python</span>
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">context</span> <span style="color: #ff7700;font-weight:bold;">import</span> scope
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">config</span> <span style="color: #ff7700;font-weight:bold;">import</span> Object, PythonConfig
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> foo <span style="color: #ff7700;font-weight:bold;">import</span> SomeClass
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> MyConfig<span style="color: black;">&#40;</span>PythonConfig<span style="color: black;">&#41;</span>:
&nbsp;
    @Object<span style="color: black;">&#40;</span>scope.<span style="color: black;">SINGLETON</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> my_singleton<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        singleton = SomeClass<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> singleton</pre></div></div>

<p style="text-align: justify;">The code below defines a regular object which won&#8217;t be stored in any cache, its value, and if fact the object returned, will be different each time the &#8216;random_password&#8217; will be requested from a container.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># stdlib</span>
<span style="color: #ff7700;font-weight:bold;">from</span> uuid <span style="color: #ff7700;font-weight:bold;">import</span> uuid4
&nbsp;
<span style="color: #808080; font-style: italic;"># Spring Python</span>
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">context</span> <span style="color: #ff7700;font-weight:bold;">import</span> scope
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">config</span> <span style="color: #ff7700;font-weight:bold;">import</span> Object, PythonConfig
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> MyConfig<span style="color: black;">&#40;</span>PythonConfig<span style="color: black;">&#41;</span>:
&nbsp;
    @Object<span style="color: black;">&#40;</span>scope.<span style="color: black;">PROTOTYPE</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> random_password<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> uuid4<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: #008000;">hex</span></pre></div></div>

<p style="text-align: justify;">If an object is marked as being lazily-initialized (which isn&#8217;t the default behaviour) it won&#8217;t be created until someone will try to reference it for the first time. This is in contrast to the default container&#8217;s action which is to eagerly create an object upon first seeing its definition in the config. In Python programming terms, it means that the first method below will be invoked as soon as the container will come across it whereas the second one won&#8217;t be.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Spring Python</span>
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">config</span> <span style="color: #ff7700;font-weight:bold;">import</span> Object
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">config</span> <span style="color: #ff7700;font-weight:bold;">import</span> PythonConfig
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> foo <span style="color: #ff7700;font-weight:bold;">import</span> SomeClass
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> MyConfig<span style="color: black;">&#40;</span>PythonConfig<span style="color: black;">&#41;</span>:
&nbsp;
    @Object
    <span style="color: #ff7700;font-weight:bold;">def</span> my_singleton<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        singleton = SomeClass<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> singleton</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Spring Python</span>
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">config</span> <span style="color: #ff7700;font-weight:bold;">import</span> Object
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">config</span> <span style="color: #ff7700;font-weight:bold;">import</span> PythonConfig
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> foo <span style="color: #ff7700;font-weight:bold;">import</span> SomeClass
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> MyConfig<span style="color: black;">&#40;</span>PythonConfig<span style="color: black;">&#41;</span>:
&nbsp;
    @Object<span style="color: black;">&#40;</span>lazy_init=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> my_singleton<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        singleton = SomeClass<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> singleton</pre></div></div>

<p style="text-align: justify;">As in real life, parent-child relationships can be a bit more complex because an object can be both a parent and a child of yet another objects. Objects that shouldn&#8217;t be fetched directly from the container are quite obviously called <em>abstract ones</em>. If an object wishes to subclass another one, it needs to name the parent in its <em>parent</em> attribute. Note that <em>credentials</em> and <em>crm_credentials</em> objects below need to be made prototypes as they would be otherwise cached and <em>crm_dev_credentials</em>, <em>crm_uat_credentials</em> and <em>ivr_credentials </em>would be referencing the same <em>Python</em> objects.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Spring Python</span>
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">context</span> <span style="color: #ff7700;font-weight:bold;">import</span> scope
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">config</span> <span style="color: #ff7700;font-weight:bold;">import</span> Object
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">config</span> <span style="color: #ff7700;font-weight:bold;">import</span> PythonConfig
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Credentials<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, app=<span style="color: #008000;">None</span>, <span style="color: #dc143c;">user</span>=<span style="color: #008000;">None</span>, password=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">app</span> = app
        <span style="color: #008000;">self</span>.<span style="color: #dc143c;">user</span> = <span style="color: #dc143c;">user</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">password</span> = password
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__str__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;%s %s %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">app</span>, <span style="color: #008000;">self</span>.<span style="color: #dc143c;">user</span>, <span style="color: #008000;">self</span>.<span style="color: black;">password</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> ServerConfig<span style="color: black;">&#40;</span>PythonConfig<span style="color: black;">&#41;</span>:
&nbsp;
    @Object<span style="color: black;">&#40;</span>scope.<span style="color: black;">PROTOTYPE</span>, abstract=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> credentials<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> Credentials<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;MyApp&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    @Object<span style="color: black;">&#40;</span>scope.<span style="color: black;">PROTOTYPE</span>, parent=<span style="color: #483d8b;">&quot;credentials&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> crm_credentials<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, credentials=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
        credentials.<span style="color: #dc143c;">user</span> = <span style="color: #483d8b;">&quot;MYUSER&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> credentials
&nbsp;
    @Object<span style="color: black;">&#40;</span>parent=<span style="color: #483d8b;">&quot;crm_credentials&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> crm_dev_credentials<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, credentials=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
        credentials.<span style="color: black;">password</span> = <span style="color: #483d8b;">&quot;MYPASSWORD1&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> credentials
&nbsp;
    @Object<span style="color: black;">&#40;</span>parent=<span style="color: #483d8b;">&quot;crm_credentials&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> crm_uat_credentials<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, credentials=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
        credentials.<span style="color: black;">password</span> = <span style="color: #483d8b;">&quot;MYPASSWORD2&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> credentials
&nbsp;
    @Object<span style="color: black;">&#40;</span>parent=<span style="color: #483d8b;">&quot;credentials&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> ivr_credentials<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, credentials=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
        credentials.<span style="color: #dc143c;">user</span> = <span style="color: #483d8b;">&quot;FOO&quot;</span>
        credentials.<span style="color: black;">password</span> = <span style="color: #483d8b;">&quot;BAR&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> credentials</pre></div></div>

<p style="text-align: justify;">Armed with all that knowledge we can finally start configuring security stuff. The method returning an uptime shouldn&#8217;t of course be troubled with authenticating incoming requests, that should be a responsibility of some kind of a security provider and the server should have a dependency on it. We&#8217;ll add a new module and update a few.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">.
|-- client-main.<span style="color: black;">py</span>
|-- client.<span style="color: black;">py</span>
|-- client_config.<span style="color: black;">py</span>
|-- common_config.<span style="color: black;">py</span>
|-- security.<span style="color: black;">py</span>
|-- server-main.<span style="color: black;">py</span>
|-- server.<span style="color: black;">py</span>
<span style="color: #66cc66;">`</span>-- server_config.<span style="color: black;">py</span></pre></div></div>

<p>Only new or modified modules are shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># security.py</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> UsernamePasswordCredentials<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, username, password<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">username</span> = username
        <span style="color: #008000;">self</span>.<span style="color: black;">password</span> = password
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__str__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;&amp;lt;%s at %s, username=%s&amp;gt;&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.__class__.__name__, <span style="color: #008000;">hex</span><span style="color: black;">&#40;</span><span style="color: #008000;">id</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>,
                    <span style="color: #008000;">self</span>.<span style="color: black;">username</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> UsernamePasswordSecurityProvider<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, database=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">database</span> = database
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> authenticate<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, credentials<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> credentials.<span style="color: black;">username</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">database</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">database</span><span style="color: black;">&#91;</span>credentials.<span style="color: black;">username</span><span style="color: black;">&#93;</span> == credentials.<span style="color: black;">password</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># server.py</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># stdlib</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">logging</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">commands</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Forbidden<span style="color: black;">&#40;</span><span style="color: #008000;">Exception</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">pass</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Service<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, security_provider=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">security_provider</span> = security_provider
        <span style="color: #008000;">self</span>.<span style="color: black;">logger</span> = <span style="color: #dc143c;">logging</span>.<span style="color: black;">getLogger</span><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>__name__, <span style="color: #008000;">self</span>.__class__.__name__<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> get_uptime<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, credentials<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">logger</span>.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;get_uptime invoked&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">security_provider</span>.<span style="color: black;">authenticate</span><span style="color: black;">&#40;</span>credentials<span style="color: black;">&#41;</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">logger</span>.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Letting the client in&quot;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">commands</span>.<span style="color: black;">getoutput</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;uptime&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">logger</span>.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Unknown client %s&quot;</span> <span style="color: #66cc66;">%</span> credentials<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">raise</span> Forbidden<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;No chance mate!&quot;</span><span style="color: black;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># server_config.py</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Spring Python</span>
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">config</span> <span style="color: #ff7700;font-weight:bold;">import</span> Object
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">remoting</span>.<span style="color: black;">pyro</span> <span style="color: #ff7700;font-weight:bold;">import</span> PyroServiceExporter
&nbsp;
<span style="color: #808080; font-style: italic;"># Our application</span>
<span style="color: #ff7700;font-weight:bold;">from</span> server <span style="color: #ff7700;font-weight:bold;">import</span> Service
<span style="color: #ff7700;font-weight:bold;">from</span> common_config <span style="color: #ff7700;font-weight:bold;">import</span> CommonConfig
<span style="color: #ff7700;font-weight:bold;">from</span> security <span style="color: #ff7700;font-weight:bold;">import</span> UsernamePasswordSecurityProvider
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> ServerConfig<span style="color: black;">&#40;</span>CommonConfig<span style="color: black;">&#41;</span>:
&nbsp;
    @Object
    <span style="color: #ff7700;font-weight:bold;">def</span> service<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        service = Service<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        service.<span style="color: black;">security_provider</span> = <span style="color: #008000;">self</span>.<span style="color: black;">security_provider</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">return</span> service
&nbsp;
    @Object
    <span style="color: #ff7700;font-weight:bold;">def</span> exporter<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        exporter = PyroServiceExporter<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        exporter.<span style="color: black;">service</span> = <span style="color: #008000;">self</span>.<span style="color: black;">service</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        exporter.<span style="color: black;">service_name</span> = <span style="color: #008000;">self</span>.<span style="color: black;">service_name</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        exporter.<span style="color: black;">service_host</span> = <span style="color: #008000;">self</span>.<span style="color: black;">service_host</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        exporter.<span style="color: black;">service_port</span> = <span style="color: #008000;">self</span>.<span style="color: black;">service_port</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">return</span> exporter
&nbsp;
    @Object
    <span style="color: #ff7700;font-weight:bold;">def</span> security_provider<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        security_provider = UsernamePasswordSecurityProvider<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        security_provider.<span style="color: black;">database</span> = <span style="color: #008000;">self</span>.<span style="color: black;">security_database</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">return</span> security_provider
&nbsp;
    @Object
    <span style="color: #ff7700;font-weight:bold;">def</span> security_database<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#123;</span><span style="color: #483d8b;">&quot;FOO&quot;</span>: <span style="color: #483d8b;">&quot;BAR&quot;</span><span style="color: black;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># client-main.py</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># stdlib</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">logging</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Spring Python</span>
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">context</span> <span style="color: #ff7700;font-weight:bold;">import</span> ApplicationContext
&nbsp;
<span style="color: #808080; font-style: italic;"># Our application</span>
<span style="color: #ff7700;font-weight:bold;">from</span> client_config <span style="color: #ff7700;font-weight:bold;">import</span> ClientConfig
<span style="color: #ff7700;font-weight:bold;">from</span> security <span style="color: #ff7700;font-weight:bold;">import</span> UsernamePasswordCredentials
&nbsp;
client_config = ClientConfig<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
container = ApplicationContext<span style="color: black;">&#40;</span>client_config<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Fetch configuration from the container ..</span>
log_level = container.<span style="color: black;">get_object</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;log_level&quot;</span><span style="color: black;">&#41;</span>
log_format = container.<span style="color: black;">get_object</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;log_format&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># .. and configure logging.</span>
<span style="color: #dc143c;">logging</span>.<span style="color: black;">basicConfig</span><span style="color: black;">&#40;</span>level=<span style="color: #dc143c;">logging</span>.<span style="color: black;">getLevelName</span><span style="color: black;">&#40;</span>log_level<span style="color: black;">&#41;</span>, format=log_format<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># .. fetch the service ..</span>
service = container.<span style="color: black;">get_object</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;service&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># .. invoke it ..</span>
credentials = UsernamePasswordCredentials<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;FOO&quot;</span>, <span style="color: #483d8b;">&quot;BAR&quot;</span><span style="color: black;">&#41;</span>
uptime = service.<span style="color: black;">get_uptime</span><span style="color: black;">&#40;</span>credentials<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># and print the output.</span>
<span style="color: #dc143c;">logging</span>.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Current uptime: [%s]&quot;</span> <span style="color: #66cc66;">%</span> uptime<span style="color: black;">&#41;</span></pre></div></div>

<p style="text-align: justify;">It&#8217;s the security provider&#8217;s burden to authenticate clients and server only needs a simple yes/no decision before returning data or raising an exception. Setting it up in that fashion, the server won&#8217;t have to be touched at all should we wish to change providers and start authenticating clients in some other way, for instance, through SSL certificates. And I know you&#8217;ll start yawning when I tell you once again that I think it&#8217;s not a provider&#8217;s business how to get its hand on the usernames/passwords database either, it just needs to operate on it.</p>
<p style="text-align: justify;">A nice side-effect of not importing dependencies directly is that it&#8217;s easier to test the components in isolation without resorting to patching (which still comes handy at times) &#8211; replacing real objects with mocks and stubs becomes only a matter of a container&#8217;s returning different objects, though of course they still need to coded to given interfaces. Given that PythonConfig is pure Python code, it&#8217;s laughably easy to use  it in tests, you just need to subclass the real config and return mock objects instead of real ones. For instance, we don&#8217;t really need to start a new Pyro thread just to make sure server and security provider can play nicely together &#8211; Pyro is just the transport layer and has nothing to do with how other components co-operate.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">.
|-- client-main.<span style="color: black;">py</span>
|-- client.<span style="color: black;">py</span>
|-- client_config.<span style="color: black;">py</span>
|-- common_config.<span style="color: black;">py</span>
|-- security.<span style="color: black;">py</span>
|-- server-main.<span style="color: black;">py</span>
|-- server.<span style="color: black;">py</span>
|-- server_config.<span style="color: black;">py</span>
|-- server_test_config.<span style="color: black;">py</span>
<span style="color: #66cc66;">`</span>-- test_all.<span style="color: black;">py</span></pre></div></div>

<p>(Only new code below)</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># server_test_config.py</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Spring Python</span>
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">config</span> <span style="color: #ff7700;font-weight:bold;">import</span> Object
&nbsp;
<span style="color: #808080; font-style: italic;"># Our application</span>
<span style="color: #ff7700;font-weight:bold;">from</span> server_config <span style="color: #ff7700;font-weight:bold;">import</span> ServerConfig
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> _DummyExporter<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">pass</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> TestServerConfig<span style="color: black;">&#40;</span>ServerConfig<span style="color: black;">&#41;</span>:
&nbsp;
    @Object
    <span style="color: #ff7700;font-weight:bold;">def</span> exporter<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> _DummyExporter<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># test_all.py</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># stdlib</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">unittest</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Spring Python</span>
<span style="color: #ff7700;font-weight:bold;">from</span> springpython.<span style="color: black;">context</span> <span style="color: #ff7700;font-weight:bold;">import</span> ApplicationContext
&nbsp;
<span style="color: #808080; font-style: italic;"># Our application</span>
<span style="color: #ff7700;font-weight:bold;">from</span> server_test_config <span style="color: #ff7700;font-weight:bold;">import</span> TestServerConfig
<span style="color: #ff7700;font-weight:bold;">from</span> security <span style="color: #ff7700;font-weight:bold;">import</span> UsernamePasswordCredentials
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> ServerTestCase<span style="color: black;">&#40;</span><span style="color: #dc143c;">unittest</span>.<span style="color: black;">TestCase</span><span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> setUp<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">container</span> = ApplicationContext<span style="color: black;">&#40;</span>TestServerConfig<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">default_credentials</span> = UsernamePasswordCredentials<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;FOO&quot;</span>, <span style="color: #483d8b;">&quot;BAR&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> tearDown<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">container</span>.<span style="color: black;">shutdown_hook</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> test_uptime<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        service = <span style="color: #008000;">self</span>.<span style="color: black;">container</span>.<span style="color: black;">get_object</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;service&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertTrue</span><span style="color: black;">&#40;</span>service.<span style="color: black;">get_uptime</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">default_credentials</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    <span style="color: #dc143c;">unittest</span>.<span style="color: black;">main</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p style="text-align: justify;">Well, that&#8217;s about all for now. I&#8217;ve only touched the tip of the iceberg and showed you toy-size examples though it is almost always the case that IoC starts to shine when the code gets bigger and bigger -  it&#8217;s just that creating a full-fledged application seemed a bit too much for a blog post <img src='http://www.gefira.pl/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  Is IoC a panacea? Of course it isn&#8217;t, when not used judiciously you can quickly go from one extreme to another and instead of having dependencies intertwined right in the code you can lose the sight of the bigger picture of how control flows through an application&#8217;s building blocks simply because everything will seem too magical and declarative. As always, don&#8217;t overengineer things and don&#8217;t overdo with IoC, especially if you choose to use XML.</p>
<p style="text-align: justify;">There&#8217;s also a question of whether Python really needs IoC, isn&#8217;t that really useful only in less dynamic languages like Java. Well, I truly believe that IoC should not really, or maybe not only, be thought of as a way to circumvent their way of dealing with changes to the source code, namely, that it needs to be recompiled each and every time, which is something that sometimes takes ages. It&#8217;s rather a fine way of bringing in more order and letting the programmers focus on their job while at the same time letting them be less afraid of introducing changes which could otherwise have a negative impact on applications. And it really helps in testing!</p>
<p style="text-align: justify;">In case you ever need help with <a title="Spring Python - Homepage" href="http://springpython.webfactional.com/">Spring Python</a> or just want to chat, here&#8217;s <a title="Spring Python - mailing list" href="http://springpython.webfactional.com/node/12">the mailing list</a>, <a title="Spring Python - forum" href="http://forum.springsource.org/forumdisplay.php?f=45">a forum</a>, <a title="Spring Python - LinkedIn group" href="http://www.linkedin.com/groups?gid=1525237">a LinkedIn group</a>,  an <a title="Spring Python - IRC channel" href="irc://springpython@freenode.net">IRC #springpython channel</a> on Freenode network and there&#8217;s also a <a title="Spring Python 1.1 book - Homepage" href="http://springpythonbook.com/">Spring Python 1.1 book </a>upcoming.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gefira.pl/blog/2010/05/01/an-introduction-to-spring-python-and-iocdi-inversion-of-controldependency-injection/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Spring Python, Pyro and remote services</title>
		<link>http://www.gefira.pl/blog/2010/04/25/spring-python-pyro-and-remote-services/</link>
		<comments>http://www.gefira.pl/blog/2010/04/25/spring-python-pyro-and-remote-services/#comments</comments>
		<pubDate>Sun, 25 Apr 2010 16:41:29 +0000</pubDate>
		<dc:creator>Dariusz Suchojad</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Spring Python]]></category>

		<guid isPermaLink="false">http://www.gefira.pl/blog/?p=435</guid>
		<description><![CDATA[One of the Spring Python&#8217;s features is a wrapper around Pyro, a Python Remote
Objects library which makes it very easy to expose Python applications as network services
running across distributed systems. You write ordinary Python code that is unaware
of any networking and Spring Python lets you publish and invoke it through
its PyroServiceExporter and PyroProxyFactory objects. The [...]]]></description>
			<content:encoded><![CDATA[<p>One of the <a title="Spring Python - home page" href="http://springpython.webfactional.com/">Spring Python</a>&#8217;s features is <a title="Spring Python - Remoting" href="http://springpython.webfactional.com/1.1.0/reference/html/remoting.html">a wrapper</a> around <a title="Pyro - Python Remote Objects" href="http://pyro.sourceforge.net/">Pyro</a>, a Python Remote<br />
Objects library which makes it very easy to expose Python applications as network services<br />
running across distributed systems. You write ordinary Python code that is unaware<br />
of any networking and Spring Python lets you publish and invoke it through<br />
its <em>PyroServiceExporter</em> and <em>PyroProxyFactory</em> objects. The setup uses a binary<br />
<a title="pickle - Python object serialization" href="http://docs.python.org/library/pickle.html"> pickle protocol</a> underneath so it&#8217;s only available if both sides, both client and the<br />
server, are written in Python.</p>
<p>Here&#8217;s how the service&#8217;s code for returning current system&#8217;s uptime might look<br />
like. As you can see, there&#8217;s nothing Spring Python going on there and in fact<br />
it&#8217;s just a simple class with just one method which knows nothing about Spring<br />
Python nor is it aware that it&#8217;s going to be available through any network.<br />
Staying out of your way and letting you reuse an already existing code is one<br />
of the biggest advantages of Spring Python, sure, you need to learn its API<br />
and principles but you can still simply code in Python and not be afraid of<br />
getting caught in some artificial maze of interfaces and dependencies.</p>
<pre># stdlib
import logging
import commands

# Configure logging subsystem
logging.basicConfig(level=logging.DEBUG)

class Service(object):
    def get_uptime(self):
        logging.info("get_uptime invoked")
        return commands.getoutput("uptime")

# Create a service ..
service = Service()

# .. get the uptime ..
uptime = service.get_uptime()

# and print it on the console.
logging.info("Current uptime: [%s]" % uptime)
</pre>
<p>And here&#8217;s the Spring Python&#8217;s approach to exposing the service through Pyro<br />
protocol, new bits have been highlighted in green. PyroServiceExporter is an<br />
object which grabs a service, lets you set its name and let it run on a given<br />
host and port. Note that the service&#8217;s code hasn&#8217;t been changed at all.</p>
<pre># stdlib
import logging
import commands

<span style="color: #008000;"># Spring Python
from springpython.remoting.pyro import PyroServiceExporter</span>

# Configure logging subsystem
logging.basicConfig(level=logging.DEBUG)

class Service(object):
    def get_uptime(self):
        logging.info("get_uptime invoked")
        return commands.getoutput("uptime")

# Create a service ..
service = Service()

<span style="color: #008000;"># .. export it through Pyro ..
service_exporter = PyroServiceExporter()
service_exporter.service = service
service_exporter.service_name = "Service"
service_exporter.service_host = "127.0.0.1"
service_exporter.service_port = "16099"

# .. and start the server.
service_exporter.after_properties_set()</span>
</pre>
<p>Such a service needs a client now and here&#8217;s the code that invokes the server<br />
looks like, key parts are in green again. PyroProxyFactory needs a server&#8217;s<br />
location and then invoking a server is only a matter of invoking a method of<br />
an object, it can&#8217;t be easier than that <img src='http://www.gefira.pl/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<pre># stdlib
import logging

<span style="color: #008000;"># Spring Python
from springpython.remoting.pyro import PyroProxyFactory</span>

# Configure logging subsystem
logging.basicConfig(level=logging.DEBUG)

<span style="color: #008000;"># Point the client to a server ..
service = PyroProxyFactory()
service.service_url = "PYROLOC://127.0.0.1:16099/Service"</span>

<span style="color: #008000;"># .. invoke the service ..
uptime = service.get_uptime()</span>

# and print it on the console.
logging.info("Current uptime: [%s]" % uptime)
</pre>
<p>Of course there&#8217;s still room for improvement. Firstly, there&#8217;s no authorization<br />
of whatsoever, everyone can invoke the service. Secondly, the configuration of<br />
both parties is written directly in the source code. It would be much better if<br />
it all were externalized to some other place and that&#8217;s exactly what the next<br />
installment will be about, we&#8217;ll be using <a title="Spring Python - Inversion of Control container" href="http://springpython.webfactional.com/1.1.0/reference/html/objects.html">Spring Python&#8217;s Inversion of Control</a><br />
features to decouple the actual implementation from its configuration.</p>
<p><strong>Update</strong>: <a title="gefira blog - An introduction to Spring Python and IoC/DI (Inversion of Control/Dependency Injection)" href="http://www.gefira.pl/blog/2010/05/01/an-introduction-to-spring-python-and-iocdi-inversion-of-controldependency-injection/">here is the second part</a> which shows Spring Python&#8217;s IoC in action.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gefira.pl/blog/2010/04/25/spring-python-pyro-and-remote-services/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Pika &#8211; Python + AMQP</title>
		<link>http://www.gefira.pl/blog/2010/02/24/pika-python-amqp/</link>
		<comments>http://www.gefira.pl/blog/2010/02/24/pika-python-amqp/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 16:42:26 +0000</pubDate>
		<dc:creator>Dariusz Suchojad</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[AMQP]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.gefira.pl/blog/?p=429</guid>
		<description><![CDATA[Przeprowadziłem ostatnimi dniami szczegółowe porównanie bibliotek AMQP dla Pythona i Pika jest tym, czego spokojnie będę używał. Badałem ją na różne sposoby, w testach wielowątkowych, wieloprocesowych, pod kątem wydajności oraz wygody użycia i to jest właśnie to  
]]></description>
			<content:encoded><![CDATA[<p>Przeprowadziłem ostatnimi dniami szczegółowe porównanie bibliotek AMQP dla Pythona i <a title="Pika - Github" href="http://github.com/tonyg/pika">Pika</a> jest tym, czego spokojnie będę używał. Badałem ją na różne sposoby, w testach wielowątkowych, wieloprocesowych, pod kątem wydajności oraz wygody użycia i to jest właśnie to <img src='http://www.gefira.pl/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.gefira.pl/blog/2010/02/24/pika-python-amqp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
