<?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>The Number 42 &#187; Programming</title>
	<atom:link href="http://www.thenumber42.net/category/computer-stuff/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thenumber42.net</link>
	<description>Mostly about software testing, but a little bit about life, the universe, and everything</description>
	<lastBuildDate>Wed, 31 Mar 2010 01:52:14 +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>Cool Python Tricks</title>
		<link>http://www.thenumber42.net/2009/12/03/cool-python-tricks/</link>
		<comments>http://www.thenumber42.net/2009/12/03/cool-python-tricks/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 14:21:30 +0000</pubDate>
		<dc:creator>Isaac</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.thenumber42.net/?p=203</guid>
		<description><![CDATA[This is a very good article with lots of cools things that Python can do.  Python Tips, Tricks, and Hacks
Link Summary

http://www.siafoo.net/article/52
]]></description>
			<content:encoded><![CDATA[<p>This is a very good article with lots of cools things that Python can do.  <a href="http://www.siafoo.net/article/52">Python Tips, Tricks, and Hacks</a></p>
<div class="link-summarizer">Link Summary
<ul>
<li><a href='http://www.siafoo.net/article/52'>http://www.siafoo.net/article/52</a></li>
</ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.thenumber42.net/2009/12/03/cool-python-tricks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sending Email with Python</title>
		<link>http://www.thenumber42.net/2009/07/28/sending-email-with-python/</link>
		<comments>http://www.thenumber42.net/2009/07/28/sending-email-with-python/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 15:04:25 +0000</pubDate>
		<dc:creator>Isaac</dc:creator>
				<category><![CDATA[Computer Stuff]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[email]]></category>

		<guid isPermaLink="false">http://www.thenumber42.net/?p=180</guid>
		<description><![CDATA[I feel like a proud father.  Yesterday I posted my first public Python recipe.  Go check it out: http://code.activestate.com/recipes/576858/.
Link Summary

http://code.activestate.com/recipes/576858/
]]></description>
			<content:encoded><![CDATA[<p>I feel like a proud father.  Yesterday I posted my first public Python recipe.  Go check it out: <a href="http://code.activestate.com/recipes/576858/">http://code.activestate.com/recipes/576858/</a>.</p>
<div class="link-summarizer">Link Summary
<ul>
<li><a href='http://code.activestate.com/recipes/576858/'>http://code.activestate.com/recipes/576858/</a></li>
</ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.thenumber42.net/2009/07/28/sending-email-with-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Long Sleeps In Python</title>
		<link>http://www.thenumber42.net/2009/07/15/long-sleeps-in-python/</link>
		<comments>http://www.thenumber42.net/2009/07/15/long-sleeps-in-python/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 19:45:03 +0000</pubDate>
		<dc:creator>Isaac</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[sleep]]></category>

		<guid isPermaLink="false">http://www.thenumber42.net/?p=169</guid>
		<description><![CDATA[There is a portion of my test infrastructure that requires long sleep periods for stability soak testing purposes.  So far the longest stability test run was 9 days.  Originally, I simply used the time.sleep() method from the Python library.  However, and I attribute this to VMware, I found that extremely inaccurate for [...]]]></description>
			<content:encoded><![CDATA[<p>There is a portion of my test infrastructure that requires long sleep periods for stability soak testing purposes.  So far the longest stability test run was 9 days.  Originally, I simply used the time.sleep() method from the Python library.  However, and I attribute this to VMware, I found that extremely inaccurate for long sleep durations.  My test controller is running in a VM and although I have nothing to confirm this I suspect that VMware is doing funny stuff under the covers with the OS timer and that is throwing off the accuracy of time.sleep().  Anyway, here is my solution to the problem.</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">datetime</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">datetime</span>, timedelta
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> longsleep<span style="color: black;">&#40;</span>minutes<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
    The normal time.sleep() method is not very accurate from inside
    VMware when sleeping for long periods of time.  This method is
    accurate to within +/- 5 seconds of the desired sleep time period.
    For long periods on the order of hours or days this small inaccuracy
    is acceptable.
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    sleepTimeSec = <span style="color: #ff4500;">60</span> * minutes
    now = <span style="color: #dc143c;">datetime</span>.<span style="color: black;">utcnow</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    td = timedelta<span style="color: black;">&#40;</span>seconds=sleepTimeSec<span style="color: black;">&#41;</span>
    endTime = now + td
    <span style="color: #ff7700;font-weight:bold;">while</span> now &lt;= endTime:
        <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        now = <span style="color: #dc143c;">datetime</span>.<span style="color: black;">utcnow</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>The method is accurate to around +/- 5 seconds, but for really long sleep durations 5 seconds is no big deal.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thenumber42.net/2009/07/15/long-sleeps-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Packet Creation With Python</title>
		<link>http://www.thenumber42.net/2009/05/26/packet-creation-with-python/</link>
		<comments>http://www.thenumber42.net/2009/05/26/packet-creation-with-python/#comments</comments>
		<pubDate>Tue, 26 May 2009 20:50:01 +0000</pubDate>
		<dc:creator>Isaac</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.thenumber42.net/?p=151</guid>
		<description><![CDATA[I work at a company that designs and builds network routers, so you can imagine that on occasion I need to create custom Ethernet packets for testing.  For hardcore traffic testing we've got some really expensive packet generator hardware that can send packets at full line rate.  For my tests I don't need raw data [...]]]></description>
			<content:encoded><![CDATA[<p>I work at a company that designs and builds network routers, so you can imagine that on occasion I need to create custom Ethernet packets for testing.  For hardcore traffic testing we've got some really expensive packet generator hardware that can send packets at full line rate.  For my tests I don't need raw data throughput, but I do need the ability to customize packets.</p>
<p>Just today I found a really great package for Python called <a href="https://www.secdev.org/projects/scapy/">Scapy</a>.  With Scapy I can create arbitrary packets with arbitrary values and fire away.  It also supports packet capturing and has all kinds of features.  As I get more comfortable with Scapy I'll post some examples.  In the mean time, those of you who need to inject custom packets for testing into equipment go check it out.</p>
<div class="link-summarizer">Link Summary
<ul>
<li><a href='https://www.secdev.org/projects/scapy/'>https://www.secdev.org/projects/scapy/</a></li>
</ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.thenumber42.net/2009/05/26/packet-creation-with-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Online Tool for Testing Regular Expressions</title>
		<link>http://www.thenumber42.net/2008/12/29/online-tool-for-testing-regular-expressions/</link>
		<comments>http://www.thenumber42.net/2008/12/29/online-tool-for-testing-regular-expressions/#comments</comments>
		<pubDate>Mon, 29 Dec 2008 14:21:27 +0000</pubDate>
		<dc:creator>Isaac</dc:creator>
				<category><![CDATA[Administration]]></category>
		<category><![CDATA[Computer Stuff]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[regular expression]]></category>

		<guid isPermaLink="false">http://www.thenumber42.net/?p=126</guid>
		<description><![CDATA[Today, while trying to get a particular regular expression to work I found an online tool to help me out.  What is particularly nice about this tool is that you can try multiple strings at a time against your regular expression.  Go check it out: http://www.fileformat.info/tool/regex.htm.
Link Summary

http://www.fileformat.info/tool/regex.htm
]]></description>
			<content:encoded><![CDATA[<p>Today, while trying to get a particular regular expression to work I found an online tool to help me out.  What is particularly nice about this tool is that you can try multiple strings at a time against your regular expression.  Go check it out: <a href="http://www.fileformat.info/tool/regex.htm">http://www.fileformat.info/tool/regex.htm</a>.</p>
<div class="link-summarizer">Link Summary
<ul>
<li><a href='http://www.fileformat.info/tool/regex.htm'>http://www.fileformat.info/tool/regex.htm</a></li>
</ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.thenumber42.net/2008/12/29/online-tool-for-testing-regular-expressions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Mercurial on SourceForge</title>
		<link>http://www.thenumber42.net/2008/05/20/using-mercurial-on-sourceforge/</link>
		<comments>http://www.thenumber42.net/2008/05/20/using-mercurial-on-sourceforge/#comments</comments>
		<pubDate>Tue, 20 May 2008 12:42:53 +0000</pubDate>
		<dc:creator>Isaac</dc:creator>
				<category><![CDATA[Computer Stuff]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[CVS]]></category>
		<category><![CDATA[Mercurial]]></category>
		<category><![CDATA[SourceForge]]></category>
		<category><![CDATA[Subversion]]></category>

		<guid isPermaLink="false">http://www.thenumber42.net/?p=102</guid>
		<description><![CDATA[Lately I've been working on resurrecting a project, called Kollektor, that I have on SourceForge.  Kollektor is a personal book collection manager that can fetch information from Amazon.  It's been a few years since I worked on it and in the mean time the Amazon interface has changed significantly.  A couple days [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I've been working on resurrecting a project, called <a href="http://sourceforge.net/projects/kollektor/">Kollektor</a>, that I have on <a href="http://www.sourceforge.net">SourceForge</a>.  Kollektor is a personal book collection manager that can fetch information from Amazon.  It's been a few years since I worked on it and in the mean time the Amazon interface has changed significantly.  A couple days ago I also started a new project called <a href="http://sourceforge.net/projects/mrmorse/">Mr. Morse</a>.</p>
<p>With both of these programs I want to try a <a href="http://en.wikipedia.org/wiki/Distributed_revision_control">distributed version control</a> system.  I've used CVS, Subversion, and Perforce for years, but <a href="http://video.google.com/videoplay?docid=-2199332044603874737">this talk by Linus Torvalds</a> convinced me to try something different.</p>
<p>Since SourceForge natively supports CVS and Subversion it is necessary to install a distributed version control system, such as Mercurial.  <a href="http://www.selenic.com/mercurial/wiki/index.cgi/MercurialOnSourceforge">This article</a> explains the steps necessary to perform the installation of Mercurial on SourceForge.</p>
<p>Keep an eye on my projects and good luck setting up your own distributed source control system.</p>
<div class="link-summarizer">Link Summary
<ul>
<li><a href='http://sourceforge.net/projects/kollektor/'>http://sourceforge.net/projects/kollektor/</a></li><li><a href='http://www.sourceforge.net'>http://www.sourceforge.net</a></li><li><a href='http://sourceforge.net/projects/mrmorse/'>http://sourceforge.net/projects/mrmorse/</a></li><li><a href='http://en.wikipedia.org/wiki/Distributed_revision_control'>http://en.wikipedia.o...uted_revision_control</a></li><li><a href='http://video.google.com/videoplay?docid=-2199332044603874737'>http://video.google.c...=-2199332044603874737</a></li><li><a href='http://www.selenic.com/mercurial/wiki/index.cgi/MercurialOnSourceforge'>http://www.selenic.co...ercurialOnSourceforge</a></li>
</ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.thenumber42.net/2008/05/20/using-mercurial-on-sourceforge/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cloning a List in Python</title>
		<link>http://www.thenumber42.net/2008/04/14/cloning-a-list-in-python/</link>
		<comments>http://www.thenumber42.net/2008/04/14/cloning-a-list-in-python/#comments</comments>
		<pubDate>Mon, 14 Apr 2008 21:42:32 +0000</pubDate>
		<dc:creator>Isaac</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[clone]]></category>
		<category><![CDATA[list]]></category>

		<guid isPermaLink="false">http://www.thenumber42.net/?p=97</guid>
		<description><![CDATA[As far as I know there is no official way to clone a list in Python.  This is desirable at times because normal variable assignment of lists is just a reference copy.  For example:
&#160;
&#62;&#62;&#62; listA = &#91;'A', 'B', 'C', 'D'&#93;
&#62;&#62;&#62; listB = listA
&#62;&#62;&#62; listA
&#91;'A', 'B', 'C', 'D'&#93;
&#62;&#62;&#62; listB
&#91;'A', 'B', 'C', 'D'&#93;
&#62;&#62;&#62; listA.remove&#40;'C'&#41;
&#62;&#62;&#62; listA
&#91;'A', [...]]]></description>
			<content:encoded><![CDATA[<p>As far as I know there is no official way to clone a list in Python.  This is desirable at times because normal variable assignment of lists is just a reference copy.  For example:</p>
<pre class="python">&nbsp;
&gt;&gt;&gt; listA = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'A'</span>, <span style="color: #483d8b;">'B'</span>, <span style="color: #483d8b;">'C'</span>, <span style="color: #483d8b;">'D'</span><span style="color: black;">&#93;</span>
&gt;&gt;&gt; listB = listA
&gt;&gt;&gt; listA
<span style="color: black;">&#91;</span><span style="color: #483d8b;">'A'</span>, <span style="color: #483d8b;">'B'</span>, <span style="color: #483d8b;">'C'</span>, <span style="color: #483d8b;">'D'</span><span style="color: black;">&#93;</span>
&gt;&gt;&gt; listB
<span style="color: black;">&#91;</span><span style="color: #483d8b;">'A'</span>, <span style="color: #483d8b;">'B'</span>, <span style="color: #483d8b;">'C'</span>, <span style="color: #483d8b;">'D'</span><span style="color: black;">&#93;</span>
&gt;&gt;&gt; listA.<span style="color: black;">remove</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'C'</span><span style="color: black;">&#41;</span>
&gt;&gt;&gt; listA
<span style="color: black;">&#91;</span><span style="color: #483d8b;">'A'</span>, <span style="color: #483d8b;">'B'</span>, <span style="color: #483d8b;">'D'</span><span style="color: black;">&#93;</span>
&gt;&gt;&gt; listB
<span style="color: black;">&#91;</span><span style="color: #483d8b;">'A'</span>, <span style="color: #483d8b;">'B'</span>, <span style="color: #483d8b;">'D'</span><span style="color: black;">&#93;</span>
&nbsp;</pre>
<p>As you can see, B was assigned to reference the same list as A and removing something from A also removed it from B.</p>
<p>At times that is fine, however, sometimes you need a clone of the list so that changes in A do not reflect in B.  A very simple way to accomplish that is by using slicing.  Our example now becomes:</p>
<pre class="python">&nbsp;
&gt;&gt;&gt; listA = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'A'</span>, <span style="color: #483d8b;">'B'</span>, <span style="color: #483d8b;">'C'</span>, <span style="color: #483d8b;">'D'</span><span style="color: black;">&#93;</span>
&gt;&gt;&gt; listB = listA<span style="color: black;">&#91;</span>:<span style="color: black;">&#93;</span>
&gt;&gt;&gt; listA
<span style="color: black;">&#91;</span><span style="color: #483d8b;">'A'</span>, <span style="color: #483d8b;">'B'</span>, <span style="color: #483d8b;">'C'</span>, <span style="color: #483d8b;">'D'</span><span style="color: black;">&#93;</span>
&gt;&gt;&gt; listB
<span style="color: black;">&#91;</span><span style="color: #483d8b;">'A'</span>, <span style="color: #483d8b;">'B'</span>, <span style="color: #483d8b;">'C'</span>, <span style="color: #483d8b;">'D'</span><span style="color: black;">&#93;</span>
&gt;&gt;&gt; listA.<span style="color: black;">remove</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'C'</span><span style="color: black;">&#41;</span>
&gt;&gt;&gt; listA
<span style="color: black;">&#91;</span><span style="color: #483d8b;">'A'</span>, <span style="color: #483d8b;">'B'</span>, <span style="color: #483d8b;">'D'</span><span style="color: black;">&#93;</span>
&gt;&gt;&gt; listB
<span style="color: black;">&#91;</span><span style="color: #483d8b;">'A'</span>, <span style="color: #483d8b;">'B'</span>, <span style="color: #483d8b;">'C'</span>, <span style="color: #483d8b;">'D'</span><span style="color: black;">&#93;</span>
&nbsp;</pre>
<p>There we go, problem solved.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thenumber42.net/2008/04/14/cloning-a-list-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Turning a Multi-Line File Into a CSV File</title>
		<link>http://www.thenumber42.net/2008/04/10/turning-a-multi-line-file-into-a-csv-file/</link>
		<comments>http://www.thenumber42.net/2008/04/10/turning-a-multi-line-file-into-a-csv-file/#comments</comments>
		<pubDate>Thu, 10 Apr 2008 23:07:33 +0000</pubDate>
		<dc:creator>Isaac</dc:creator>
				<category><![CDATA[Bash/Shell]]></category>
		<category><![CDATA[linux utilities csv]]></category>

		<guid isPermaLink="false">http://www.thenumber42.net/?p=95</guid>
		<description><![CDATA[Linux has many amazing programs that can be chained together to produce amazing results.  Today I needed to turn a multi-line file into a CSV (comma separated) file.  In Linux this is super easy:
cat file &#124; tr '\n' ','
Basically, this will turn a file like this:
Line1
Line2
Line3
Line4
Into this:
Line1,Line2,Line3,Line4

]]></description>
			<content:encoded><![CDATA[<p>Linux has many amazing programs that can be chained together to produce amazing results.  Today I needed to turn a multi-line file into a CSV (comma separated) file.  In Linux this is super easy:</p>
<pre class="bash"><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #c20cb9; font-weight: bold;">file</span> | <span style="color: #c20cb9; font-weight: bold;">tr</span> <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span> <span style="color: #ff0000;">','</span></pre>
<p>Basically, this will turn a file like this:</p>
<pre>Line1
Line2
Line3
Line4</pre>
<p>Into this:</p>
<pre>Line1,Line2,Line3,Line4
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.thenumber42.net/2008/04/10/turning-a-multi-line-file-into-a-csv-file/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Making the TCL Shell More Useful (tclreadline)</title>
		<link>http://www.thenumber42.net/2008/04/09/making-the-tcl-shell-more-useful-tclreadline/</link>
		<comments>http://www.thenumber42.net/2008/04/09/making-the-tcl-shell-more-useful-tclreadline/#comments</comments>
		<pubDate>Wed, 09 Apr 2008 19:59:23 +0000</pubDate>
		<dc:creator>Isaac</dc:creator>
				<category><![CDATA[TCL]]></category>
		<category><![CDATA[completion]]></category>
		<category><![CDATA[ipython]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[tab]]></category>
		<category><![CDATA[tclreadline]]></category>

		<guid isPermaLink="false">http://www.thenumber42.net/?p=94</guid>
		<description><![CDATA[One of the greatest things about being a test engineer is that I get to delve into a little bit of everything.  At work I use a handful of different languages to accomplish different tasks.  Up until recently TCL programming has been a thorn in my side.  I love the interactive Python [...]]]></description>
			<content:encoded><![CDATA[<p>One of the greatest things about being a test engineer is that I get to delve into a little bit of everything.  At work I use a handful of different languages to accomplish different tasks.  Up until recently <a href="http://www.tcl.tk/">TCL</a> programming has been a thorn in my side.  I love the interactive Python shell <a href="http://ipython.scipy.org">ipython</a> and sorely miss having the same thing in TCL.  Just today I found <a href="http://tclreadline.sourceforge.net/">tclreadline</a>, which adds command history and tab completion to the TCL shell.  This is definitely something to add to your superhero tool belt.</p>
<div class="link-summarizer">Link Summary
<ul>
<li><a href='http://www.tcl.tk/'>http://www.tcl.tk/</a></li><li><a href='http://ipython.scipy.org'>http://ipython.scipy.org</a></li><li><a href='http://tclreadline.sourceforge.net/'>http://tclreadline.sourceforge.net/</a></li>
</ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.thenumber42.net/2008/04/09/making-the-tcl-shell-more-useful-tclreadline/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to Use JPype</title>
		<link>http://www.thenumber42.net/2008/03/31/how-to-use-jpype/</link>
		<comments>http://www.thenumber42.net/2008/03/31/how-to-use-jpype/#comments</comments>
		<pubDate>Mon, 31 Mar 2008 18:41:11 +0000</pubDate>
		<dc:creator>Isaac</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[jpype]]></category>

		<guid isPermaLink="false">http://www.thenumber42.net/2008/03/31/how-to-use-jpype/</guid>
		<description><![CDATA[Background
I love Python and my personal experience has shown that testers are about 4x more productive when using Python than when using Java.  However, at work I am a tester on a Java application.  I've been looking for a way to bridge that gap and allow me to write my tests in Python [...]]]></description>
			<content:encoded><![CDATA[<h2>Background</h2>
<p>I love Python and my personal experience has shown that testers are about 4x more productive when using Python than when using Java.  However, at work I am a tester on a Java application.  I've been looking for a way to bridge that gap and allow me to write my tests in Python but test a Java application.</p>
<p>I've looked into <a href="http://www.jython.org/Project/index.html">Jython</a> and while it had many advantages it has one major drawback that I can't live with: Jython only implements Python 2.2.  Most of the time this is fine, but there are times where I use Python 2.4 or 2.5 features and don't want to create a workaround just so I can use an older Python standard.  If I had the money I would pay someone to bring Jython up to date.  But, that's not a possibility.</p>
<p>I did find another alternative that so far seems to work great.  It is still in development, but <a href="http://jpype.sourceforge.net/">JPype</a> seems to be working fairly well.  JPype differs in philosophy from Jython in a fundamental way: Jython is a complete reimplementation of Python in Java, whereas JPype is a bridge between CPython and native Java.</p>
<h2>Kicking The Tires</h2>
<p>This article is a "how to," so let's get down to business.  I'm going to create a Java class that I would like to access from Python.  Here is Java class:</p>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">package</span> org.<span style="color: #006600;">wg3i</span>.<span style="color: #006600;">test</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Test <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> msg;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Test<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> speak<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> msg<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span>msg<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setString<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> s<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	msg = s;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> getString<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">return</span> msg;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
<p>The class is simple enough, but it will illustrate the basic JPype usage.  For my testing I compiled this and placed it into a jar file.  Feel free to leave it as a class file if you wish.  I'll show you how to deal with both situations below.</p>
<p>For reference, here is my ant build file:<br />
<font face="courier"><br />
&lt;?xml&nbsp;version=&quot;1.0&quot;?&gt;<br />
&lt;project&nbsp;name=&quot;TestProject&quot;<br />
	&nbsp;basedir=&quot;.&quot;<br />
	&nbsp;default=&quot;jar&quot;&gt;</p>
<p>&nbsp;&nbsp;&lt;property&nbsp;name=&quot;src.dir&quot;&nbsp;value=&quot;src&quot;/&gt;<br />
&nbsp;&nbsp;&lt;property&nbsp;name=&quot;build.dir&quot;&nbsp;value=&quot;build&quot;/&gt;<br />
&nbsp;&nbsp;&lt;property&nbsp;name=&quot;classes.dir&quot;&nbsp;value=&quot;${build.dir}/classes&quot;/&gt;<br />
&nbsp;&nbsp;&lt;property&nbsp;name=&quot;jar.dir&quot;&nbsp;value=&quot;${build.dir}/jar&quot;/&gt;<br />
&nbsp;<br />
&nbsp;&nbsp;&lt;target&nbsp;name=&quot;clean&quot;&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;delete&nbsp;dir=&quot;build&quot;/&gt;<br />
&nbsp;&nbsp;&lt;/target&gt;<br />
&nbsp;<br />
&nbsp;&nbsp;&lt;target&nbsp;name=&quot;compile&quot;&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;mkdir&nbsp;dir=&quot;${classes.dir}&quot;/&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;javac&nbsp;srcdir=&quot;${src.dir}&quot;&nbsp;destdir=&quot;${classes.dir}&quot;/&gt;<br />
&nbsp;&nbsp;&lt;/target&gt;<br />
&nbsp;<br />
&nbsp;&nbsp;&lt;target&nbsp;name=&quot;jar&quot;&nbsp;depends=&quot;compile&quot;&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;mkdir&nbsp;dir=&quot;${jar.dir}&quot;/&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;jar&nbsp;destfile=&quot;${jar.dir}/${ant.project.name}.jar&quot;<br />
	&nbsp;basedir=&quot;${classes.dir}&quot;/&gt;<br />
&nbsp;&nbsp;&lt;/target&gt;<br />
&nbsp;<br />
&nbsp;&nbsp;&lt;target&nbsp;name=&quot;clean-build&quot;&nbsp;depends=&quot;clean,jar&quot;&gt;<br />
&nbsp;&nbsp;&lt;/target&gt;<br />
&lt;/project&gt;</font><br />
After creating a jar file you are ready to go.  Here is the Python code to utilize a jar file and create an instance of the Java class.</p>
<pre class="python">&nbsp;
<span style="color: #808080; font-style: italic;"># Simple little JPype test.  Note: This test assumes</span>
<span style="color: #808080; font-style: italic;"># that the jar file is in the build/jar directory</span>
<span style="color: #808080; font-style: italic;"># relative to the current directory</span>
<span style="color: #ff7700;font-weight:bold;">import</span> jpype
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>
&nbsp;
jarpath = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">abspath</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.'</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">'build/jar'</span><span style="color: black;">&#41;</span>
jpype.<span style="color: black;">startJVM</span><span style="color: black;">&#40;</span>jpype.<span style="color: black;">getDefaultJVMPath</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">&quot;-Djava.ext.dirs=%s&quot;</span> % jarpath<span style="color: black;">&#41;</span>
Test = jpype.<span style="color: black;">JClass</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'org.wg3i.test.Test'</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># get the class</span>
t = Test<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># create an instance of the class</span>
t.<span style="color: black;">speak</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;This is a test message&quot;</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># try to call one of the class methods</span>
t.<span style="color: black;">setString</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Hello, World&quot;</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># set a string</span>
s = t.<span style="color: black;">getString</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># get the string back</span>
<span style="color: #ff7700;font-weight:bold;">print</span> s
jpype.<span style="color: black;">shutdownJVM</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>In a nutshell that's it.  I promised to show you how to initialize JPype when you have class files and not jar files.  Here's a Python example that shows that  This example also shows the use of the JPackage method, which gets a <em>package</em>, not a class.</p>
<pre class="python">&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> jpype
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>
&nbsp;
classpath = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">abspath</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.'</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">'build/classes'</span><span style="color: black;">&#41;</span>
jpype.<span style="color: black;">startJVM</span><span style="color: black;">&#40;</span>jpype.<span style="color: black;">getDefaultJVMPath</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">&quot;-Djava.class.path=%s&quot;</span> % classpath<span style="color: black;">&#41;</span>
testPkg = jpype.<span style="color: black;">JPackage</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'org'</span><span style="color: black;">&#41;</span>.<span style="color: black;">wg3i</span>.<span style="color: #dc143c;">test</span>  <span style="color: #808080; font-style: italic;"># get the package</span>
Test = testPkg.<span style="color: black;">Test</span> <span style="color: #808080; font-style: italic;"># get the class</span>
t = Test<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># create an instance of the class</span>
t.<span style="color: black;">speak</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;This is a test message&quot;</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># try to call one of the class methods</span>
t.<span style="color: black;">setString</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Hello, World&quot;</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># set a string</span>
s = t.<span style="color: black;">getString</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># get the string back</span>
<span style="color: #ff7700;font-weight:bold;">print</span> s
jpype.<span style="color: black;">shutdownJVM</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<h2>Conclusion</h2>
<p>There are many things I have not covered, such as passing arrays back and forth, types other than strings, exceptions, etc.  However, this should be enough to get you started using JPype.  I'd be interested in hearing your own experiences or suggestions.</p>
<div class="link-summarizer">Link Summary
<ul>
<li><a href='http://www.jython.org/Project/index.html'>http://www.jython.org/Project/index.html</a></li><li><a href='http://jpype.sourceforge.net/'>http://jpype.sourceforge.net/</a></li><li><a href='http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky'>http://www.google.com...27m%20Feeling%20Lucky</a></li><li><a href='http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky'>http://www.google.com...27m%20Feeling%20Lucky</a></li>
</ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.thenumber42.net/2008/03/31/how-to-use-jpype/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
