<?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>Code-itch &#187; Python</title>
	<atom:link href="http://www.code-itch.com/blog/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.code-itch.com/blog</link>
	<description>A non-coders attempts at writing useful code</description>
	<lastBuildDate>Sat, 20 Mar 2010 20:13:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Command line handling in Python with optparse</title>
		<link>http://www.code-itch.com/blog/2009/04/command-line-handling-in-python-with-optparse/</link>
		<comments>http://www.code-itch.com/blog/2009/04/command-line-handling-in-python-with-optparse/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 10:49:28 +0000</pubDate>
		<dc:creator>harijay</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[python-tips]]></category>

		<guid isPermaLink="false">http://www.code-itch.com/blog/?p=114</guid>
		<description><![CDATA[Command lines interfaces to programs are very empowering. I started using computers with the  Linux command line and  I have never strayed too far from programs that are predominantly command line based. Whether it was rasmol , povray , phenix or ffmpeg I always found that the command line gave the program a more  transparent [...]]]></description>
			<content:encoded><![CDATA[<p>Command lines interfaces to programs are very empowering. I started using computers with the  Linux command line and  I have never strayed too far from programs that are predominantly command line based. Whether it was rasmol ,<a href="http://www.povray.org/documentation/view/3.6.1/216/"> povray</a> , <a href="http://phenix-online.org/documentation/refinement.htm#anch46">phenix</a> or <a href="http://ffmpeg.mplayerhq.hu/ffmpeg-doc.html">ffmpeg</a> I always found that the command line gave the program a more  transparent interface . By that I mean it was easier ( at least for me ) to figure out how to do decipher a  manual page  than to go looking for a particular functionality in a GUI window.</p>
<p>No in the case of python ,  I have been writing scripts that take in user input from the command line for a while now. In these scripts , I would generally accept only one input and that would be the first in the sys.argv list. If I had more than one input I would iterate over the input list and try and figure out what the inputs were . Even worse in most cases I would  hard code the order  of inputs into my code ( terrible practise). Fortunately  for me , my discovery of the optparse module has changed all that.</p>
<p>The optparse module is  an object oriented ( dont let that scare you) and super-intuitive way to add command line options to any python scriptSo say you want to add an input file command line switch with the -i attribute , All you have to do is</p>
<p>from optparse import OptionParser</p>
<p>optparse_object =OptionParser()</p>
<p>optparser_object.add_option(&#8220;-i&#8221;,&#8221;&#8211;infile&#8221;, dest=&#8221;infile&#8221;,help=&#8221;input file for script&#8221; , metavar=&#8221;[infile.txt]&#8220;)</p>
<p>Once you do this you can easily have the module parse the sys.argv list and make sense of it .So you would add the following line</p>
<p>options_object, spillover_options = optparser_object.parse_args()</p>
<p>Then options_object.infile  will have the value of the input option . This is specified by the dest section in the add_option argument list) . The nice thing with the module is that all possibilities can be mapped to the same options_object.infile destination . So for eg I have mapped &#8220;-i&#8221; and &#8220;&#8211;infile&#8221; to the same destination .Even better is the option to add a help string with the help=&#8221;help text&#8221; argument . This help is then printed out if the user provides an option that the script cannot handle or if the code specifically calls  the optparser_object.print_help() function.</p>
<p>For a concrete example on how to use the opt_parse module consult the <a href="http://docs.python.org/library/optparse.html">docs</a> or <a href="http://github.com/harijay/xtaltools/blob/369a0d14710edecffd55b5707778b82690d50f26/mtztophs.py">my example code on github </a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.code-itch.com/blog/2009/04/command-line-handling-in-python-with-optparse/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Code-itch 2009 &#8211; Progess report</title>
		<link>http://www.code-itch.com/blog/2009/01/code-itch-2009-progess-report/</link>
		<comments>http://www.code-itch.com/blog/2009/01/code-itch-2009-progess-report/#comments</comments>
		<pubDate>Wed, 07 Jan 2009 17:34:54 +0000</pubDate>
		<dc:creator>harijay</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code-itch]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.code-itch.com/blog/?p=78</guid>
		<description><![CDATA[Almost two years ago I first started writing the codeitch weblog about my code ambitions on wordpress ( codeitch.wordpress.com) .This blog was driven by a strong desire to get better at writing code and importantly building solutions that enabled better science. To do that I had decided to pick up python , Java , javascript [...]]]></description>
			<content:encoded><![CDATA[<p>Almost t<a href="http://codeitch.wordpress.com/2007/04/11/codeitch-or-the-desire-to-codeify-and-systematize-everything-i-do/">wo years ago I first started writing the codeitch weblog about my code ambitions</a> on  wordpress  ( codeitch.wordpress.com) .This blog was driven by a strong desire to get better at writing code and importantly building solutions that enabled better science. To do that I had decided to pick up python , Java , javascript and also master Excel and Matlab. Since a new year is always a time for reflections , resolutions and such, I figured I will write down this progress report.</p>
<p>Python: Python has clearly becoming my first choice for tackling any problem. Whether it involved <a href="http://www.ncbi.nlm.nih.gov/pubmed/18678918?ordinalpos=1&amp;itool=EntrezSystem2.PEntrez.Pubmed.Pubmed_ResultsPanel.Pubmed_DefaultReportPanel.Pubmed_RVDocSum">analysing the many trajectories generated by  the channel finding program HOLE</a> , or writing an<a href="http://www.bioscreencast.com/api/user/harijay"> http based API for Bioscreencast</a> , or an <a href="http://www.bioscreencastwiki.com/Writing_Python_extensions_for_deki-wiki">HTTP based web service for Bioscreencastwik</a>i, python has ensured that I can get it done . There is no problem that I dont attempt to solve using python . Python has even ensured that  I jettison a desire to pick up Excel and <a href="http://www.code-itch.com/blog/2008/12/resolver-one-pythonic-spreadsheets/">instead adopt Resolver one </a>, the pythonic spreadsheet instead . My only problem with python is the learning curve to pick up a new API . The dynamic typing sure makes it hard to unravel code by just reading source , something that I was used to doing having leant to code in Java. Further , tools like netbeans and Eclipse with code completion also make it easier to familiarize yourself with a new API  in Java  . In python , I still go about it using the dir builtin function and reading the various forms of python documentation available on the web. In some ways I really cannot wait for something like nbpython to become fully useable.</p>
<p>Java : Since I am still developing code to solve problems for myself or web-centic problems for an apache based webserver . I have almost not written a single line of java code the last year.  Python  has ensured that java takes a backseat.</p>
<p>Javascript : To me javascript is  mainly a UI language , since I regard the browser as the ultimate UI . Since I have yet to get into the UI writing mode , my javascript useage over the last year was restricted to troubleshooting the few glitches we had at Bioscreencast . Again tool support ( like the amazing firebug utility or even netbeans) ensure that I dont abandon picking up javascript in my spare time and definitely plan to use javascript and a browser as my UI of choice  when the need arises.</p>
<p>Excel : Having got on to the python horse, I can hardly justify the headaches that mastering Excel syntaz gave me . And for most simple spreadhsheets I have gotten used to Google spreadsheets.</p>
<p>Matlab: My desire to master matlab was to understand the many algorihtms I encounter in X-ray crystallography. In may ways Python and <a href="http://matplotlib.sourceforge.net/">MatPlotlib </a>have ensured that I can get a lot of that done using python , without the need to learn the Matlab way of things . I have also attended a <a href="http://www.wolfram.com/services/education/seminar.cgi">few Mathematica Seminars </a>and am extremely impressed with the Mathemtica 6 ( and 7) platform . For <a href="http://demonstrations.wolfram.com/">understanding algotihms</a> and the behavior of functions etc I have been turning to Mathematica and python more than Matlab.</p>
<p>So in summary , python and the pythonic way are what I have  embraced in 2008 and really want to start writing python code that starts to build on this foundation in 2009.I am also really starting to use Mathematica and pick up processing as a java centric visualization platform and hope to write about these in this new year.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.code-itch.com/blog/2009/01/code-itch-2009-progess-report/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wanna learn python , join comp.lang.python : Regexp matching of fixed digit numbers</title>
		<link>http://www.code-itch.com/blog/2008/11/wanna-learn-python-join-complangpython-regexp-matching-of-fixed-digit-numbers/</link>
		<comments>http://www.code-itch.com/blog/2008/11/wanna-learn-python-join-complangpython-regexp-matching-of-fixed-digit-numbers/#comments</comments>
		<pubDate>Mon, 24 Nov 2008 16:55:01 +0000</pubDate>
		<dc:creator>harijay</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[comp.lang.python]]></category>
		<category><![CDATA[diveintopython]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[python regexp]]></category>

		<guid isPermaLink="false">http://www.code-itch.com/blog/?p=53</guid>
		<description><![CDATA[One of the fun parts of learning a programming language yourself is when you get stuck at solving a trivial problem . When this happens I generally try to do a few things generally involving searching . I have tabulated these activities and also the apparent value to me , both for my short-term solving [...]]]></description>
			<content:encoded><![CDATA[<p>One of the fun parts of learning a programming language yourself is when you get stuck at solving a trivial problem . When this happens I generally try to do a few things generally involving searching . I have tabulated these activities and also the apparent value to me , both for my short-term solving of the immediate problem and for my long-term understanding of the subject in addition to improving my tacit knowledge.</p>
<p><img src="http://s3.amazonaws.com/code-itch/images/whyJoinANewsGroup.png" alt="Table describing merits of newsgroup memberships" width="480" height="222" /></p>
<p>In most cases the above acitivities take almost an hour. When despite all this I dont get an answer I start to get frustrated. Most of my frustrations stem from being unable to define the problem because I dont have a good enough grasp of the vocabulary required to improve my searching and querying.</p>
<p>This is when I resort to what I call my ultimate resource , I get outside human help and  contact a newsgroup In almost 100% of these problems I get a very satisfying answer from these forums . Forums also have the social benefit of expanding my network and importantly creating a lasting record of the solution.</p>
<p>Let me qualify all of this with an actual example . I had to develop a regular expression to parse text from a file and only extract sentences that contained four digit numbers</p>
<p>for eg</p>
<p>I have 1234 dollars left in the bank ( result desired:Match)</p>
<p>I dont have 12345 issues to deal with ( result desired : SKIP )</p>
<p>My initial regular expression was</p>
<div class="igBar"><span id="lpython-2"><a href="#" onclick="javascript:showPlainTxt('python-2'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PYTHON:</span>
<div id="python-2">
<div class="python">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">p = <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\d</span>{4}'</span><span style="color: black;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Based on what I knew this would search for "exactly" four digit numbers . But I soon realized that this regexp matched both 1245 and 12345 i.e when you say r'\d{4}' what you are saying is match exactly a four digit number with no constraints on whats on either side . So all of  "1234" and "12345" and "a1234abcd" or "1234asdf" were matched.</p>
<p>Clearly when you say "exact" in the definition of the regular expression you mean something different from "exact" to a non-savant regexp writer .</p>
<p>I then assumed that this had something to do with greedy vs non-greedy matching ( hence approach  in row 2 of table above)  . This too quickly proved to be a non-starter. I was clearly stuck and had no idea how to  proceed with solving the problem or even go about finding more information .</p>
<p>Thats when I wrote in to the <a href="http://groups.google.com/group/comp.lang.python/topics">comp.lang.python</a> newsgroup . Within 15 minutes I had a reply . Although it wasnt the right answer , the promptness of the reply was well worth it. <a href="http://groups.google.com/group/comp.lang.python/browse_thread/thread/ca0663f80fa6cbe7/cea11975b386958a?lnk=gst&amp;q=harijay#cea11975b386958a">A few minutes later I had a few suggestions all of which listed multiple approaches to my problem</a> . I quickly learnt that I had not defined the regexp fully enough.  I had to also codeify the fact that the exact match had to be a separate "word" unto itself .</p>
<p>Three other posts helped me arrive at the perfect solution the regexps</p>
<p>r'\b\d{4}\b' " match a four digit  number with a word boundary on either side"</p>
<p>OR more intricate examples that allowed me to learn more about the regexp syntax for eg. expressions containing <a href="http://www.python.org/doc/2.5.2/lib/re-syntax.html">negative look-behinds , expression notations and non-grouping parenthesis</a></p>
<p><span class="fixed_width" style="font-family: Courier,Monospaced;">(r'(?&lt;!\d)\d{4}(?!\d)') " four digit number preceded by a non digit and  followed by a non digit)<br />
</span></p>
<p>Or</p>
<p><span class="fixed_width" style="font-family: Courier,Monospaced;">(?:\D|\b)\d{4}(?:\D|\b) " four digit number surounded by non digit or word boundaries "<br />
</span></p>
<p>Clearly almost 20 minutes after my post to the newsgroup , I had not only solved my particular problem but more importantly also considerably increased my knowledge of regular expression behavior and syntax. Most of the replies had not just applied their solutions to my test case , but also had tried to clear my misunderstandings of how regexps worked.  Some posts even provided whole code and novel test cases with results .</p>
<p>Joining an active newsgroup and posing your questions with some effort  always gives you an answer . In all of my experience on these newsgroups , the more effort you spend in definining your problem the more value you gain from the interaction.</p>
<p>I am quite convinced that joining a newsgroup is well worth it to start learning python or any other language or  platform</p>
]]></content:encoded>
			<wfw:commentRss>http://www.code-itch.com/blog/2008/11/wanna-learn-python-join-complangpython-regexp-matching-of-fixed-digit-numbers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
