<?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>PyRuby blog &#187; Python</title>
	<atom:link href="http://www.pyruby.com/category/geek/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pyruby.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Mon, 12 Dec 2011 11:03:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Stubserver 0.1 released</title>
		<link>http://www.pyruby.com/2010/02/23/stubserver-0-1-released/</link>
		<comments>http://www.pyruby.com/2010/02/23/stubserver-0-1-released/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 04:03:41 +0000</pubDate>
		<dc:creator>PyRuby</dc:creator>
				<category><![CDATA[geek]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[mock]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.pyruby.com/?p=63</guid>
		<description><![CDATA[<p>I&#8217;ve released a python stub webserver, described in an earlier blog post, on pypi.  The features include support for any method (i.e. PUT, POST, DELETE, GET, RHUBARB &#8230;), handles chunked encoding, and supports capturing data passed for later assertions.  </p>
<p>Because it&#8217;s on pypi, you can install using:-</p>
<p>pip install stubserver</p>
<p>or</p>
<p>easy_install stubserver</p>
<p>The verification model is [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve released a python stub webserver, described in an earlier <a href="http://www.pyruby.com/2009/11/22/testing-against-a-stub-web-server/">blog post</a>, on <a href="http://pypi.python.org/pypi/stubserver">pypi</a>.  The features include support for any method (i.e. PUT, POST, DELETE, GET, RHUBARB &#8230;), handles chunked encoding, and supports capturing data passed for later assertions.  </p>
<p>Because it&#8217;s on pypi, you can install using:-</p>
<p><code>pip install stubserver</code></p>
<p>or</p>
<p><code>easy_install stubserver</code></p>
<p>The verification model is very simplistic, each registered expectation is only expected to occur once.  To get more details, please refer to the <a href="http://www.pyruby.com/pythonstubserver/">documentation</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pyruby.com/2010/02/23/stubserver-0-1-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Testing against a Stub Web Server</title>
		<link>http://www.pyruby.com/2009/11/22/testing-against-a-stub-web-server/</link>
		<comments>http://www.pyruby.com/2009/11/22/testing-against-a-stub-web-server/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 04:57:48 +0000</pubDate>
		<dc:creator>PyRuby</dc:creator>
				<category><![CDATA[geek]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.pyruby.com/?p=49</guid>
		<description><![CDATA[<p>I wrote recently about improving the way I usually create a stub web server for testing my applications that call out to external web services.  This is a particularly common occurrence where I work (Point2) because we have separated out our core services which are interacted with through REST apis.  As I&#8217;ve mentioned [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote recently about improving the way I usually create a stub web server for testing my applications that call out to external web services.  This is a particularly common occurrence where I work (<a href="http://www.point2.com">Point2</a>) because we have separated out our core services which are interacted with through REST apis.  As I&#8217;ve mentioned before, this was what started the <a href="http://djangorestmodel.sourceforge.net">Django REST models</a> open source project.</p>
<p>Well, I&#8217;ve written the code to effectively support the previously described testing api.  It is currently part of the above project, and can be found <a href="https://djangorestmodel.svn.sourceforge.net/svnroot/djangorestmodel/test_web.py">here</a>.  It has already proved handy in adding a much needed functional test capability for Django REST models, which has already exposed a bug in the awkward to test rest_client code.  As an example, here is the test:-</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">class</span> FunctionalTest<span class="br0">&#40;</span><span class="kw3">unittest</span>.<span class="me1">TestCase</span><span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">def</span> setUp<span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">server</span> = StubServer<span class="br0">&#40;</span><span class="nu0">8998</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">server</span>.<span class="me1">run</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">def</span> tearDown<span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">server</span>.<span class="me1">stop</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">server</span>.<span class="me1">verify</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">def</span> test_get_with_file_call<span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">server</span>.<span class="me1">expect</span><span class="br0">&#40;</span>method=<span class="st0">&quot;GET&quot;</span>, url=<span class="st0">&quot;/address/<span class="es0">\w</span>+$&quot;</span><span class="br0">&#41;</span>.<span class="me1">and_return</span><span class="br0">&#40;</span>mime_type=<span class="st0">&quot;text/xml&quot;</span>, content=<span class="st0">&quot;&lt;address&gt;&lt;number&gt;12&lt;/number&gt;&lt;street&gt;Early Drive&lt;/street&gt;&lt;city&gt;Calgary&lt;/city&gt;&lt;/address&gt;&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; address = Address.<span class="me1">objects</span>.<span class="me1">get</span><span class="br0">&#40;</span>city=<span class="st0">&quot;Calgary&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">assertEquals</span><span class="br0">&#40;</span><span class="st0">&quot;Early Drive&quot;</span>, address.<span class="me1">street</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>With the expanding usage of REST services, and a growing number of Python applications written against those services, this project is starting to gather pace again.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pyruby.com/2009/11/22/testing-against-a-stub-web-server/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Why I write Python</title>
		<link>http://www.pyruby.com/2009/11/05/why-i-write-python/</link>
		<comments>http://www.pyruby.com/2009/11/05/why-i-write-python/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 05:12:56 +0000</pubDate>
		<dc:creator>PyRuby</dc:creator>
				<category><![CDATA[geek]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.pyruby.com/?p=44</guid>
		<description><![CDATA[Why do you write Python?  Because it's [...]]]></description>
			<content:encoded><![CDATA[<p>I read an interesting thread asking <a href="http://groups.google.com/group/comp.lang.python/browse_thread/thread/66ccb10d2da5b740">why do you use Python</a>.  I&#8217;ve been using several languages recently, including Python, and this has been a recurring question.  There is much commonality in the responses of others</p>
<ul>
<li>It makes me more productive by removing boiler-plate code.</li>
<li>There are many good libraries for Python which are very consistent, making it easy to produce complex applications with few lines of code.</li>
<li>Meta programming techniques such as missing method and metaclasses make producing sophisticated frameworks easy.</li>
</ul>
<p>These are all interesting points, but none of these things are the real reason I write Python code in preference to Java, Scala, Groovy or Ruby.  The primary reason for me, is that I really enjoy writing Python applications.  I think in a Pythonic way, so writing Python applications is intuitive and enjoyable.  I rarely need to battle with the language to acheive what I want to do.  A common question that gets asked is &#8220;What can Python do that you can&#8217;t do in Language X?&#8221;.  The answer is usually &#8220;Nothing, provided you are prepared to write enough code, you can solve all the same problems.  It&#8217;s just I enjoy writing Python, which means I am more engaged in solving the business problem, and therefore able to consistently get the job done faster, better, and with a greater level of satisfaction in the solution&#8221;.</p>
<p>I know this isn&#8217;t a scientific answer to the question, but writing software is a job done by people who aren&#8217;t mechanised robots.  People need to be motivated in order to produce the best results and that includes being able to choose the tools they use based on what makes them happy.  Recently it&#8217;s been my pleasure to spread a little Python love around my team mates.  It&#8217;s been great to see the enthusiasm with which they have taken to using it, and the stunned awe when the see just how powerful this &#8216;Scripting Language&#8217; really is.  This has increased the motivation of the team, and as a result we are completing more work, to a higher standard, in a shorter time than we were previously while working in C# .NET  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.pyruby.com/2009/11/05/why-i-write-python/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Stubbing a REST api in Python</title>
		<link>http://www.pyruby.com/2009/10/06/stubbing-a-rest-api-in-python/</link>
		<comments>http://www.pyruby.com/2009/10/06/stubbing-a-rest-api-in-python/#comments</comments>
		<pubDate>Wed, 07 Oct 2009 04:58:19 +0000</pubDate>
		<dc:creator>PyRuby</dc:creator>
				<category><![CDATA[geek]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.pyruby.com/?p=15</guid>
		<description><![CDATA[<p>For the last few jobs I&#8217;ve had, Service Oriented Architecture has been a strong theme.  Having services presenting REST apis that consume and produce XML has been the regular approach.  When it comes to testing consumers of those services, I have found Python to be an extremely useful and convenient tool.  It&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>For the last few jobs I&#8217;ve had, <a href="http://en.wikipedia.org/wiki/Service-oriented_architecture">Service Oriented Architecture</a> has been a strong theme.  Having services presenting REST apis that consume and produce XML has been the regular approach.  When it comes to testing consumers of those services, I have found Python to be an extremely useful and convenient tool.  It&#8217;s quick and easy to <a href="http://docs.python.org/library/basehttpserver.html">throw an HTTP server up</a> in Python, and hard code the values that it returns.  The problem is though, that I have ended up just adding code to the do_GET / do_POST methods as I add tests that handle different expectations.  This ends up separating the expectations and return values from the test, and making maintaining the stub code harder than it should be.  </p>
<p>To resolve this, I&#8217;d like to add expectations of the stub http server in the same way as with defining mocks.  The syntax would look something like this</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"> &nbsp; &nbsp;server.<span class="me1">expect</span><span class="br0">&#40;</span>method=<span class="st0">&quot;GET&quot;</span>, url=<span class="st0">&quot;/address/<span class="es0">\d</span>+$&quot;</span><span class="br0">&#41;</span>.</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="me1">and_return</span><span class="br0">&#40;</span>mime=<span class="st0">&quot;text/xml&quot;</span>, file_content=<span class="st0">&quot;path/to/response.xml&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; capture = <span class="br0">&#123;</span><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; server.<span class="me1">expect</span><span class="br0">&#40;</span>method=<span class="st0">&quot;PUT&quot;</span>, url=<span class="st0">&quot;/address/<span class="es0">\d</span>+$&quot;</span>, data_capture=capture<span class="br0">&#41;</span>.</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="me1">and_return</span><span class="br0">&#40;</span>reply_code=<span class="nu0">201</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; server.<span class="me1">expect</span><span class="br0">&#40;</span>method=<span class="st0">&quot;POST&quot;</span>, url=<span class="st0">&quot;address/<span class="es0">\d</span>+/inhabitant&quot;</span>, data=<span class="st0">&#8216;&lt;inhabitant name=&quot;Chris&quot;/&gt;&#8217;</span><span class="br0">&#41;</span>.</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="me1">and_return</span><span class="br0">&#40;</span>reply_code=<span class="nu0">204</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; server.<span class="me1">expect</span><span class="br0">&#40;</span>method=<span class="st0">&quot;GET&quot;</span>, url=<span class="st0">&quot;/monitor/server_status$&quot;</span><span class="br0">&#41;</span>.</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="me1">and_return</span><span class="br0">&#40;</span>content=<span class="st0">&quot;&lt;html&gt;&lt;body&gt;Server is up&lt;/body&gt;&lt;/html&gt;&quot;</span>, mime_type=<span class="st0">&quot;text/html&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="co1"># execute system under test</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">assertEquals</span><span class="br0">&#40;</span><span class="st0">&quot;&lt;expected&gt;&lt;xml/&gt;&lt;/expected&gt;&quot;</span>, capture<span class="br0">&#91;</span><span class="st0">&quot;body&quot;</span><span class="br0">&#93;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>So, over the next couple of weeks I&#8217;ll be putting this together, which should among other things allow me to improve the end to end testing story for <a href="http://djangorestmodel.sourceforge.net">djangoRESTModel</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pyruby.com/2009/10/06/stubbing-a-rest-api-in-python/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Django REST models</title>
		<link>http://www.pyruby.com/2009/09/28/django-rest-models/</link>
		<comments>http://www.pyruby.com/2009/09/28/django-rest-models/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 01:05:07 +0000</pubDate>
		<dc:creator>PyRuby</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.pyruby.com/?p=4</guid>
		<description><![CDATA[We wrote an xml_models library, which allowed us to map our models to the XML data, using a declarative style similar to the Django DB models.  The library has been open sourced and can be found on <a [...]]]></description>
			<content:encoded><![CDATA[<h2>Making external XML REST apis more pythonic</h2>
<p>At <a href="http://www.point2.com">work</a> we have a <a href="http://www.djangoproject.com">Django</a> web application where the models are backed by a Java XML <a href="http://en.wikipedia.org/wiki/Representational_State_Transfer">REST api</a>.  The application is read only, so there is no requirement to write back to the REST api.</p>
<p>We started with copying the data into a local DB, and using regular Django DB models, and we wanted something that would feel very similar, but use an XML REST backend. We wrote an xml_models library, which allowed us to map our models to the XML data, using a declarative style similar to the Django DB models.  The library has been open sourced and can be found on <a href="http://djangorestmodel.sf.net">sourceforge</a>.</p>
<p>Here is a simple example:-</p>
<p>Suppose there was an API http://myaddressbook.com/addresses/{addressID} that returns the following XML</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;address</span> <span class="re0">id</span>=<span class="st0">&quot;123&quot;</span> <span class="re0">num</span>=<span class="st0">&quot;3301&quot;</span><span class="re2">&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="sc3"><span class="re1">&lt;street<span class="re2">&gt;</span></span></span>8th Street<span class="sc3"><span class="re1">&lt;/street<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="sc3"><span class="re1">&lt;city<span class="re2">&gt;</span></span></span>Saskatoon<span class="sc3"><span class="re1">&lt;/city<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="sc3"><span class="re1">&lt;province<span class="re2">&gt;</span></span></span>SK<span class="sc3"><span class="re1">&lt;/province<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="sc3"><span class="re1">&lt;country<span class="re2">&gt;</span></span></span>Canada<span class="sc3"><span class="re1">&lt;/country<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;/address<span class="re2">&gt;</span></span></span></div>
</li>
</ol>
</div>
<p>The model might be defined as</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">class</span> Address<span class="br0">&#40;</span>xml_models.<span class="me1">Model</span><span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">id</span> = xml_models.<span class="me1">IntField</span><span class="br0">&#40;</span>xpath=<span class="st0">&quot;/address/@id&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; num = xml_models.<span class="me1">IntField</span><span class="br0">&#40;</span>xpath=<span class="st0">&quot;/address/@num&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; street = xml_models.<span class="me1">CharField</span><span class="br0">&#40;</span>xpath=<span class="st0">&quot;/address/street&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; city = xml_models.<span class="me1">CharField</span><span class="br0">&#40;</span>xpath=<span class="st0">&quot;/address/city&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; province = xml_models.<span class="me1">CharField</span><span class="br0">&#40;</span>xpath=<span class="st0">&quot;/address/province&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; country = xml_models.<span class="me1">CharField</span><span class="br0">&#40;</span>xpath=<span class="st0">&quot;/address/country&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; finders = <span class="br0">&#123;</span> <span class="br0">&#40;</span><span class="kw2">id</span>,<span class="br0">&#41;</span>: <span class="st0">&quot;http://myaddressbook.com/addresses/%s&quot;</span> ,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span>num,street<span class="br0">&#41;</span>: <span class="st0">&quot;http://myaddressbook.com/addresses/num/%s/street/%s&quot;</span><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>And using the finder method would be like this</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">address = Address.<span class="me1">objects</span>.<span class="me1">get</span><span class="br0">&#40;</span><span class="kw2">id</span>=<span class="nu0">123</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>That&#8217;s the basics.  We are mapping much more complex relationships with collection fields, date fields and more.  Take a look and let me know what you think.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pyruby.com/2009/09/28/django-rest-models/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

