<?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>tail -f findings.out &#187; Python</title>
	<atom:link href="http://dancingpenguinsoflight.com/category/programming/python-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://dancingpenguinsoflight.com</link>
	<description></description>
	<lastBuildDate>Fri, 20 Apr 2012 03:24:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Spruce up your desktop with the National Geographic Photo of the Day</title>
		<link>http://dancingpenguinsoflight.com/2011/02/spruce-up-your-desktop-with-the-national-geographic-photo-of-the-day/</link>
		<comments>http://dancingpenguinsoflight.com/2011/02/spruce-up-your-desktop-with-the-national-geographic-photo-of-the-day/#comments</comments>
		<pubDate>Sat, 19 Feb 2011 21:54:45 +0000</pubDate>
		<dc:creator>Samuel Huckins</dc:creator>
				<category><![CDATA[Desktop]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://dancingpenguinsoflight.com/?p=1877</guid>
		<description><![CDATA[[Update, 2011-02-24] Reader Jason Coombs kindly made the necessary changes to get this working on Windows 7! Now I might be shamed into getting it working for Mac OS X as well His changes are here. I&#8217;ve updated my script &#8230; <a href="http://dancingpenguinsoflight.com/2011/02/spruce-up-your-desktop-with-the-national-geographic-photo-of-the-day/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><em>[Update, 2011-02-24] Reader Jason Coombs kindly made the necessary changes to get this working on Windows 7! Now I might be shamed into getting it working for Mac OS X as well <img src='http://dancingpenguinsoflight.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  His <a target="_blank" href="https://bitbucket.org/jaraco/jaraco.util/changeset/7cfc6de94873">changes are here</a>. I&#8217;ve updated my script linked below as well. I&#8217;ll add some nicer instructions for Windows users soon .[/Update]</em></p>
<p>I love checking the <a title="NatGeo Photo of the day site" target="_blank" href="http://photography.nationalgeographic.com/photography/photo-of-the-day">National Geographic Photo of the Day page</a> to see what wonderful slice of nature or mankind some intrepid soul has captured and shared. Often a wallpaper-sized image download is provided. I wanted to automate the process of checking the page for a wallpaper link, downloading the file, putting it somewhere appropriate, and setting it as my background. The result is <a target="_blank" title="Background script on GitHub" href="https://github.com/shuckins/sph_code/blob/master/misc-scripts/nat-geo_background-setter.py">this script</a>, which does all that and even:</p>
<ul>
<li>Verifies you have at least 25% free disk space before continuing</li>
<li>Renames the image to the title shown on the page</li>
<li>Creates a dir to hold these images if it doesn&#8217;t exist</li>
</ul>
<p><span id="more-1877"></span></p>
<h2>Windows users</h2>
<p>Download that script and double click assuming Python is installed. </p>
<h2>Linux users</h2>
<p>Download it, set it to be executable (chmod u+x nat-geo_background-setter.py), put it somewhere like /opt. You could add an alias to run it whenever you fancy:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;"># Update path to where you placed the script:</span><br />
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">newbg</span>=<span style="color: #ff0000;">&quot;/opt/USER/nat-geo_background-setter.py&quot;</span></div></td></tr></tbody></table></div>
<p>If you want it to run every day on Linux you could add a crontab entry:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">crontab <span style="color: #660033;">-e</span><br />
<span style="color: #666666; font-style: italic;"># Inside your crontab:</span><br />
<span style="color: #666666; font-style: italic;"># Set BG to picture of the day</span><br />
<span style="color: #000000;">0</span> <span style="color: #000000;">6</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>USER<span style="color: #000000; font-weight: bold;">/</span>nat-geo_background-setter.py</div></td></tr></tbody></table></div>
<p>Enjoy a bit of nature on your desktop!</p>
    ]]></content:encoded>
			<wfw:commentRss>http://dancingpenguinsoflight.com/2011/02/spruce-up-your-desktop-with-the-national-geographic-photo-of-the-day/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Review of Hacking Vim 7.2</title>
		<link>http://dancingpenguinsoflight.com/2010/06/review-of-hacking-vim-7-2/</link>
		<comments>http://dancingpenguinsoflight.com/2010/06/review-of-hacking-vim-7-2/#comments</comments>
		<pubDate>Sun, 20 Jun 2010 01:24:43 +0000</pubDate>
		<dc:creator>Samuel Huckins</dc:creator>
				<category><![CDATA[Book Review]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://dancingpenguinsoflight.com/?p=1480</guid>
		<description><![CDATA[Hacking Vim 7.2 by Kim Schulz is filled with a variety of useful explanations and helpful tips for Vim users wanting to improve their efficiency and learn more about their editor. While I&#8217;ve enjoyed finding new and better ways to &#8230; <a href="http://dancingpenguinsoflight.com/2010/06/review-of-hacking-vim-7-2/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><img style="float: left; margin-right: 10px;" title="vim_logo" src="http://dancingpenguinsoflight.com/wp-content/uploads/2009/07/vim_logo.png" alt="vim_logo" width="125" height="125" /><a title="Hacking Vim on Packt" href="https://www.packtpub.com/hacking-vim-7-2/book" target="_blank"><em>Hacking Vim 7.2</em></a> by Kim Schulz is filled with a variety of useful explanations and helpful tips for <a title="vim homepage" href="http://www.vim.org/" target="_blank">Vim</a> users wanting to improve their efficiency and learn more about their editor. While I&#8217;ve enjoyed finding <a title="DPOL posts on Vim" href="http://dancingpenguinsoflight.com/?s=vim" target="_blank">new and better ways to use Vim</a> for some time, I still learned quite a lot from this book.</p>
<p>If you use Vim only to edit text files when you happen to be  logged  into a CLI: mosey on along, this isn&#8217;t the book for you. If you   currently use Vim regularly and want to improve your knowledge and effectiveness with it: your time reading this book will be well-spent. If you&#8217;ve somehow never heard of   Vim but are enraptured by the idea of a text editor that can be an effective IDE in   addition to allowing you to play Tetris and Rubik&#8217;s Cube: you might like this book too.<br />
<span id="more-1480"></span><br />
At first this book seemed rather short for such a  potentially expansive topic. In the  end I found this to be mostly a virtue  rather than a problem, as the amount  of information presented is, in fact,  voluminous. A more comprehensive  approach likely would have been too  hefty and less  effective. It doesn&#8217;t pretend to be a  manual for Vim. Instead it  illustrates the best approaches for various issues and situations, suggests improvements  on more common operations, and provides lots of  references to external  resources (primarily Vim internal help and vim.org) to  learn more. Examples and illustrations provide insight into some of Vim&#8217;s lesser known abilities, while commonly used ones are explained and presented in new and useful ways. Groups of features resolve into <em>solutions</em>.</p>
<h2>Chapter by Chapter</h2>
<h3><strong>Chapter 1, Getting Started with Vim</strong></h3>
<p>Primarily a history of the Vim editor. Useful to expand the knowledge of Vim fans, but not really pertinent to <em>hacking</em> your beloved editor. Would probably have been better as an appendix or excised entirely.</p>
<h3><strong>Chapter 2</strong>, <strong>Personalizing Vim</strong></h3>
<p>Editing your Vim config, customizing fonts, colors, menu buttons and other UI elements. Lots of great material for tuning the visual appearance. More about vim.org and other resources for colorschemes (such as <a title="Vim colorscheme test site" href="http://vimcolorschemetest.googlecode.com/svn/html/index-c.html" target="_blank">this gem</a>) would have been helpful. The ability to add tooltip balloons was new to me and quite exciting. Schulz provides a number of great examples for their use. The discussion of abbreviations was a bit confusing, especially considering the recommendation of the more intuitive snippets approach in Chapter 4.</p>
<h3><strong>Chapter 3, Better Navigation</strong></h3>
<p>By now you might already be feeling overwhelmed by the number of key commands to remember. I recommend starting a cheat sheet of those you find especially interesting. Speaking of that, perhaps it&#8217;s just my predilections and lack of a perfect memory, but the vast number of code navigation keys was just a bit too much. Sure, &#8220;extra keys pressed equals extra time wasted&#8221;, but what about extra time sorting through key commands in your thoughts? Still, plenty of tips should be mind-blowing if new to you. Jumping to files named in the text of the current file, for example, is really handy. The information about search improvements, both in user files and in the help system, was quite good. I&#8217;ve yet to really use signs or marks, but found their explanation here illuminating.</p>
<h3><strong>Chapter 4, Production (Productivity?) Boosters</strong></h3>
<p>Templates! Templates are awesome, and this was a welcome introduction to a topic <a title="DPOL post on Bash scripting, template" href="http://dancingpenguinsoflight.com/2009/03/vim-for-bash-scripting-a-happier-union/" target="_blank">I&#8217;ve barely scratched</a> in my Vim journey. I&#8217;ve also not used placeholders in templates before, which look to be quite useful. Tag lists and essential plugins for their use are explained, providing an effective presentation of this feature. I was getting a little worried around pg. 86, as the number of Ctrl + x, Ctrl + SOME_KEY commands started to get cumbersome. Then came the explanation of triggering all completion types using a single key, which was quite refreshing. Complete using dictionaries, words in the current document, language files, and more, all using a single key. The SuperCleverTab function will be a very welcome addition to my configuration. Until seeing the example about macros, I never really felt how useful they might be. This definitely convinced me.</p>
<p>But this meaty chapter isn&#8217;t over yet! Sessions felt rather opaque to me before reading this section, and I think they will now definitely serve a role for me. No more ritual of opening the right files, folding them just how I want, etc every morning. Undo branching&#8230; I still find this rather too complicated and marginal of a feature to care about. But the examples were at least explanatory. Folding is a great eye-saver, and the coverage was adequate. I&#8217;m still not really a fan of vimdiff, as there are other diff utilities out there that make the process faster and more intuitive to me. One exception is finding the diff between the current buffer and the saved file, which is very handy and not something available in most editors for sure! I hadn&#8217;t opened remote files in Vim directly before, so the section on this at the end of this chapter was quite useful.</p>
<h3><strong>Chapter 5, Advanced Formatting</strong></h3>
<p>I felt mostly ambivalent about the beginning of this chapter. To me, if you want text aligned a certain way, or want to create proper lists and use other more advanced document formatting features, you should use a word processor. Adding whitespace to my text files for alignment&#8217;s sake doesn&#8217;t strike me as a good idea.</p>
<p>The second part covering formatting pertinent to coding was much more useful. I&#8217;ve used things like autoindent and formatting of pasted code for a while now, and they are quite essential to sanity. Great coverage for those new to these features. Integration of external formatting tools such as Tidy is also described. <strong></strong></p>
<h3><strong>Chapter 6, Basic Vim Scripting</strong></h3>
<p>&#8220;<em>Now</em> we get to it&#8221; was my feeling starting this chapter. There&#8217;s some very helpful stuff in here, but it&#8217;s strangely organized and should have been moved or at least mentioned earlier in the book to provide the most benefit. Some critically handy items: explanation of Vim configuration file directories, how to properly override layers of configuration, Vimball installation, syntax highlighting and colorscheme creation. Then&#8230; comes an introductory language tutorial. Honestly, it&#8217;s pretty dull and mostly elementary. I would have rather this been moved to an appendix mentioned early in the book. It does provide broad coverage of the basics one would need to start writing Vim scripts.</p>
<h3><strong>Chapter 7, Extended Vim Scripting</strong></h3>
<p><strong></strong>Everything you need to know about actually writing Vim scripts: suggested code format, function structure, adding key mappings, detecting OS and Vim version, debugging and distributing scripts, documentation, even integrating programming languages like Python and Ruby! This is certainly a great reference to get started with Vim scripting. Schulz also gives an example script wrapping up a number of these points, providing something like a Vim scripter&#8217;s &#8220;Hello World&#8221;.</p>
<h3><strong>Appendix A, Vim Can Do Everything</strong></h3>
<p>Examples of the many and varied things you can do with Vim, such as Tweet and play Tetris. Provides amusement, but not a lot of value. The section on Vim as an IDE should really have been expanded to an entire chapter or appendix considering the likely audience for this book.</p>
<h3><strong>Appendix B, Vim Configuration Alternatives</strong></h3>
<p>A number of tips on how to keep your Vim configuration organized. Spends time explaining a system of scripts created by Jos van RisWick that allows the user to make changes to their configuration from GUI menus and make them permanent, instead of editing config files. I had never heard of this and found it quite interesting.</p>
<p>The last few pages deal with storing your Vim configuration online. While I whole-heartedly agree with the intent, the particular manifestation presented is rather terrible. Using this setup, the user&#8217;s .vimrc would be downloaded <em>every time</em> Vim is opened. Then it still has to be parsed, which for heavily customized setups can already take a moment. Not the best when you&#8217;re looking for a lean editor. And what about the legion of scripts and other supporting files outside .vimrc? I think not. My approach to the problem is to keep all of my Vim configuration files inside an SCM system, checking it out on each system the first time I want to use Vim.</p>
<h2>Issues</h2>
<ul>
<li><strong>Confused structure</strong>: Chapters 6 and 7 felt rather strangely divided  in  themselves, and I felt most of the content should have been provided   earlier. Having a better knowledge of the syntax and nature of ex   commands/Vim scripting would have made the content throughout the book   more understandable. I felt I was mostly taking the syntax in the code provided for   granted, without feeling comfortable enough about all the components that I could have written something of my own design.</li>
<li><strong>Typos</strong>: The text would have greatly benefited from another round of   copy-editing. Most of the errors weren&#8217;t very distracting. There were, however, a number of typos in the code provided that   were more problematic, especially for a work on an editor that can be   rather picky about commands. For a first edition, these aren&#8217;t overly   numerous.</li>
<li><strong>Lack of color diagrams</strong>: I&#8217;m all for using black and  white images  when they will suffice, but when providing illustrations of  *color*  highlighting files, images without color are downright useless!  I was  rather surprised at this oversight, first appearing in chapter 2,  then  in multiple places in chapter 6. They could have at least linked  to  color images online.</li>
</ul>
<h2>Conclusion</h2>
<p>All in all this was a very useful  treasure trove of tips. Some I&#8217;ve  already added to my repertoire, and  more I&#8217;m trying to understand  better. In fact, I learned so much from it  that I decided I will likely  need to drop most of my current Vim config  and remake it cleanly. I  know I&#8217;m not alone in collecting bits and  pieces of configuration for  this wonderful editor, in a fashion  occasionally reminiscent of a  deranged hermit crab. Now that I have a  more solid understanding I can  rebuild it quickly and effectively.</p>
<p>In summary, I agree with <a title="Brian Carper's Hacking Vim 7.2 review" href="http://briancarper.net/blog/review-hacking-vim-72" target="_blank">Brian Carper</a>:</p>
<blockquote><p>The best you can hope for in a book is a broad outline to point the way  toward features that you didn&#8217;t know much about. Hacking Vim 7.2  achieves this goal.</p></blockquote>
    ]]></content:encoded>
			<wfw:commentRss>http://dancingpenguinsoflight.com/2010/06/review-of-hacking-vim-7-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>A better way to search for methods of Python objects</title>
		<link>http://dancingpenguinsoflight.com/2009/11/a-better-way-to-search-for-methods-of-python-objects/</link>
		<comments>http://dancingpenguinsoflight.com/2009/11/a-better-way-to-search-for-methods-of-python-objects/#comments</comments>
		<pubDate>Sun, 08 Nov 2009 22:08:10 +0000</pubDate>
		<dc:creator>Samuel Huckins</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[efficiency]]></category>

		<guid isPermaLink="false">http://dancingpenguinsoflight.com/?p=1316</guid>
		<description><![CDATA[Python&#8217;s introspection abilities are quite extensive and useful. They are also well-documented, so I won&#8217;t go into the basics here. Check out this article if you need a good overview. N.B.: discussion and code below applies to both methods and &#8230; <a href="http://dancingpenguinsoflight.com/2009/11/a-better-way-to-search-for-methods-of-python-objects/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Python&#8217;s introspection abilities are quite extensive and useful. They are also well-documented, so I won&#8217;t go into the basics here. <a title="Guide to Python Introspection" target="_blank" href="http://www.ibm.com/developerworks/library/l-pyint.html">Check out this article</a> if you need a good overview. N.B.: discussion and code below applies to both methods and attributes. I will simply refer to &#8220;methods&#8221; for simplicity.</p>
<p>Beyond simply listing the methods of an object, however, I often find that I want to search through them for something in particular. And eyeballing the output of dir(obj) is only efficient in the simplest of cases. hasattr(obj, &#8220;method&#8221;) won&#8217;t get you far either, as you need to match the &#8220;method&#8221; name exactly. What if you just have a good guess about the name of a method based on what you need to do? What if you want to know everything you can do with directories in the os module or ISO related methods in datetime.date? I haven&#8217;t found anything to help with this sort of problem yet. Approaches like writing a loop to do the search every time or perusing the pertinent API docs are too circuitous for such quick questions. So let&#8217;s make a tool to do this more easily!</p>
<p>We start with knowing some object and some string containing all or part of the method(s) we&#8217;re interested in:</p>
<div class="codecolorer-container python blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">def</span> mf<span style="color: black;">&#40;</span>obj<span style="color: #66cc66;">,</span> term<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #483d8b;">&quot;&quot;&quot;<br />
&nbsp; &nbsp; Searches through the methods and attributes defined for obj, <br />
&nbsp; &nbsp; looks for those containing the term passed.<br />
&nbsp; &nbsp; Returns all matches or a warning if none found.<br />
&nbsp; &nbsp; &quot;&quot;&quot;</span><br />
&nbsp; &nbsp; meths <span style="color: #66cc66;">=</span> <span style="color: #008000;">dir</span><span style="color: black;">&#40;</span>obj<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; match_meths <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> meth <span style="color: #ff7700;font-weight:bold;">in</span> meths:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> meth.<span style="color: black;">rfind</span><span style="color: black;">&#40;</span>term<span style="color: black;">&#41;</span> <span style="color: #66cc66;">!=</span> -<span style="color: #ff4500;">1</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; match_meths.<span style="color: black;">append</span><span style="color: black;">&#40;</span>meth<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> match_meths:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> match_meths<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;No matches!&quot;</span></div></td></tr></tbody></table></div>
<p>This simply iterates over the methods and attributes defined for the object passed and looks for the term passed within the name of each. rfind returns the highest index of the substring passed in the string it&#8217;s called on and returns -1 if no match is found. Once the matches are collected they are printed out. An example:</p>
<div class="codecolorer-container python blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&gt;&gt;&gt;</span><span style="color: #ff7700;font-weight:bold;">from</span> method_finder <span style="color: #ff7700;font-weight:bold;">import</span> mf<br />
<span style="color: #66cc66;">&gt;&gt;&gt;</span><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span><br />
<span style="color: #66cc66;">&gt;&gt;&gt;</span>mf<span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;dir&quot;</span><span style="color: black;">&#41;</span><br />
<span style="color: black;">&#91;</span><span style="color: #483d8b;">'chdir'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'curdir'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'fchdir'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'listdir'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'makedirs'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'mkdir'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'pardir'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'removedirs'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'rmdir'</span><span style="color: black;">&#93;</span></div></td></tr></tbody></table></div>
<p>For convenience let&#8217;s make it available on the Python interactive prompt at every load without any extra effort. This is easy enough using a .pythonstartup file. This file can be used to load various useful items like tab completion for interactive Python sessions. If you haven&#8217;t used it before, you&#8217;ll need to add the following to your .bashrc:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">PYTHONSTARTUP</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$HOME</span>/.pythonstartup&quot;</span></div></td></tr></tbody></table></div>
<p>Then in your home directory create a .pythonstartup file containing something like the following:</p>
<div class="codecolorer-container python blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span><br />
<span style="color: #808080; font-style: italic;"># Adds mf(obj, &quot;str&quot;) allowing search for methods matching 'str' on obj</span><br />
<span style="color: #808080; font-style: italic;"># as well as obinfo(obj) returning lots of info on obj</span><br />
util_loc <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">&quot;/home/shuckins/code/code_homerepo/python-programming/utilities&quot;</span><br />
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">isdir</span><span style="color: black;">&#40;</span>util_loc<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span><br />
&nbsp; &nbsp; <span style="color: #dc143c;">sys</span>.<span style="color: black;">path</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span>util_loc<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">from</span> utils <span style="color: #ff7700;font-weight:bold;">import</span> mf<span style="color: #66cc66;">,</span> obinfo<br />
&nbsp; &nbsp; <span style="color: #dc143c;">sys</span>.<span style="color: black;">path</span>.<span style="color: black;">remove</span><span style="color: black;">&#40;</span>util_loc<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">del</span> <span style="color: #dc143c;">sys</span><br />
<span style="color: #ff7700;font-weight:bold;">del</span> <span style="color: #dc143c;">os</span></div></td></tr></tbody></table></div>
<p>You&#8217;ll need to change the path specified to wherever you placed utils.py containing the mf() function of course. You can <a target="_blank" title="utils.py on github" href="http://github.com/shuckins/sph_code/blob/master/python-programming/utilities/utils.py">download mine here</a> if you&#8217;d like. The del statements are to clean up any trace of this operation once you get to the interactive interpreter. This way you get the added functions without mucking up your namespace. </p>
<p>This also loads in an obinfo(obj) function that I included in the same utils.py file. This is based on the interrogate() function written by Patrick O&#8217;Brien in the <a title="Guide to Python Introspection" target="_blank" href="http://www.ibm.com/developerworks/library/l-pyint.html">introspection article mentioned above</a>. My version just adds a check for objects without docstrings and prints more of the docstring:</p>
<div class="codecolorer-container python blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">def</span> obinfo<span style="color: black;">&#40;</span>obj<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #483d8b;">&quot;&quot;&quot;<br />
&nbsp; &nbsp; Print useful information about object.<br />
<br />
&nbsp; &nbsp; From http://www.ibm.com/developerworks/library/l-pyint.html<br />
&nbsp; &nbsp; &quot;&quot;&quot;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">hasattr</span><span style="color: black;">&#40;</span>obj<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'__name__'</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;NAME: &nbsp; &nbsp;&quot;</span><span style="color: #66cc66;">,</span> obj.__name__<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">hasattr</span><span style="color: black;">&#40;</span>obj<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'__class__'</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;CLASS: &nbsp; &quot;</span><span style="color: #66cc66;">,</span> obj.__class__.__name__<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;ID: &nbsp; &nbsp; &nbsp;&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">id</span><span style="color: black;">&#40;</span>obj<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;TYPE: &nbsp; &nbsp;&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">type</span><span style="color: black;">&#40;</span>obj<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;VALUE: &nbsp; &quot;</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">repr</span><span style="color: black;">&#40;</span>obj<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;CALLABLE:&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">callable</span><span style="color: black;">&#40;</span>obj<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Yes&quot;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;No&quot;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">hasattr</span><span style="color: black;">&#40;</span>obj<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'__doc__'</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; doc <span style="color: #66cc66;">=</span> <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span>obj<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'__doc__'</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; doc <span style="color: #66cc66;">=</span> doc.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; topfive <span style="color: #66cc66;">=</span> doc.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span>:<span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;DOC: &nbsp; &nbsp; &quot;</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>topfive<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;No docstring. Yell at the author.&quot;</span></div></td></tr></tbody></table></div>
<p>It&#8217;s fairly useful:</p>
<div class="codecolorer-container python blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&gt;&gt;&gt;</span> obinfo<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;some&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;list&quot;</span><span style="color: black;">&#93;</span>.<span style="color: black;">count</span><span style="color: black;">&#41;</span><br />
NAME: &nbsp; &nbsp; count<br />
CLASS: &nbsp; &nbsp;builtin_function_or_method<br />
ID: &nbsp; &nbsp; &nbsp; <span style="color: #ff4500;">139922087930696</span><br />
TYPE: &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span><span style="color: #008000;">type</span> <span style="color: #483d8b;">'builtin_function_or_method'</span><span style="color: #66cc66;">&gt;</span><br />
VALUE: &nbsp; &nbsp;<span style="color: #66cc66;">&lt;</span>built-<span style="color: #ff7700;font-weight:bold;">in</span> method count of <span style="color: #008000;">list</span> <span style="color: #008000;">object</span> at <span style="color: #ff4500;">0x7f422658ef80</span><span style="color: #66cc66;">&gt;</span><br />
CALLABLE: Yes<br />
DOC: &nbsp; &nbsp; &nbsp;L.<span style="color: black;">count</span><span style="color: black;">&#40;</span>value<span style="color: black;">&#41;</span> -<span style="color: #66cc66;">&gt;</span> integer -- <span style="color: #ff7700;font-weight:bold;">return</span> number of occurrences of value</div></td></tr></tbody></table></div>
    ]]></content:encoded>
			<wfw:commentRss>http://dancingpenguinsoflight.com/2009/11/a-better-way-to-search-for-methods-of-python-objects/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Catching warnings from the MySQLdb module</title>
		<link>http://dancingpenguinsoflight.com/2009/10/catching-warnings-from-the-mysqldb-module/</link>
		<comments>http://dancingpenguinsoflight.com/2009/10/catching-warnings-from-the-mysqldb-module/#comments</comments>
		<pubDate>Mon, 26 Oct 2009 03:53:50 +0000</pubDate>
		<dc:creator>Samuel Huckins</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://dancingpenguinsoflight.com/?p=1300</guid>
		<description><![CDATA[The MySQLdb Python module implements the Python DB API for MySQL. I&#8217;ve written about its use before. MySQL issues warning messages in a number of circumstances and PEP 249 (which specifies the Python DB API) describes a Warning error message &#8230; <a href="http://dancingpenguinsoflight.com/2009/10/catching-warnings-from-the-mysqldb-module/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The <a target="_blank" title="MySQLdb home" href="http://sourceforge.net/projects/mysql-python/">MySQLdb Python module</a> implements the Python DB API for MySQL. I&#8217;ve written about its use <a target="_blank" title="MySQL queries in Python" href="http://dancingpenguinsoflight.com/2009/03/running-mysql-queries-in-python/">before</a>. MySQL issues <a target="_blank" title="MySQL show warnings" href="http://dev.mysql.com/doc/refman/5.0/en/show-warnings.html">warning messages</a> in a number of circumstances and <a target="_blank" title="PEP 249" href="http://www.python.org/dev/peps/pep-0249/">PEP 249</a> (which specifies the Python DB API) describes a Warning error message to be included.</p>
<p>One issue I ran into recently was how to catch warnings thrown by this module when running queries. Oftentimes tutorials or forum discussions that cover warnings in the context of MySQLdb describe how to filter them (they can clog up script output). But in a recent case I wanted to grab and check the warning, logging a dependent result. I had hoped this clean implementation would work in a method used for all calls to the MySQL DB:</p>
<div class="codecolorer-container python blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">try</span>:<br />
&nbsp; &nbsp; cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span>query<span style="color: black;">&#41;</span><br />
<span style="color: #ff7700;font-weight:bold;">except</span> MySQLdb.<span style="color: black;">Error</span><span style="color: #66cc66;">,</span> e:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">raise</span> e<br />
<span style="color: #ff7700;font-weight:bold;">except</span> MySQLdb.<span style="color: #008000;">Warning</span><span style="color: #66cc66;">,</span> e:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">raise</span> e<br />
<span style="color: #ff7700;font-weight:bold;">finally</span>:<br />
&nbsp; &nbsp; data <span style="color: #66cc66;">=</span> cursor.<span style="color: black;">fetchall</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; rows_returned <span style="color: #66cc66;">=</span> cursor.<span style="color: black;">rowcount</span><br />
&nbsp; &nbsp; cursor.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; db.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></td></tr></tbody></table></div>
<p>But the warnings just went right through. Instead I needed the warnings module&#8217;s assistance:</p>
<div class="codecolorer-container python blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">warnings</span><br />
<span style="color: #ff7700;font-weight:bold;">with</span> <span style="color: #dc143c;">warnings</span>.<span style="color: black;">catch_warnings</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #dc143c;">warnings</span>.<span style="color: black;">simplefilter</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'error'</span><span style="color: #66cc66;">,</span> MySQLdb.<span style="color: #008000;">Warning</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">try</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span>query<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">except</span> MySQLdb.<span style="color: black;">Error</span><span style="color: #66cc66;">,</span> e:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">raise</span> e<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">finally</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; data <span style="color: #66cc66;">=</span> cursor.<span style="color: black;">fetchall</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; rows_returned <span style="color: #66cc66;">=</span> cursor.<span style="color: black;">rowcount</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; cursor.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; db.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></td></tr></tbody></table></div>
<p>This catches the warnings and raises them as errors, although their class is still correct, allowing a clean implementation to call the above code (wrapped into a method called do_query):</p>
<div class="codecolorer-container python blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">try</span>:<br />
&nbsp; &nbsp; <span style="color: #008000;">self</span>.<span style="color: black;">do_query</span><span style="color: black;">&#40;</span>make_cool_table<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; logger.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Created cool_table table.&quot;</span><span style="color: black;">&#41;</span><br />
<span style="color: #ff7700;font-weight:bold;">except</span> MySQLdb.<span style="color: #008000;">Warning</span>:<br />
&nbsp; &nbsp; logger.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;cool_table already exists.&quot;</span><span style="color: black;">&#41;</span></div></td></tr></tbody></table></div>
    ]]></content:encoded>
			<wfw:commentRss>http://dancingpenguinsoflight.com/2009/10/catching-warnings-from-the-mysqldb-module/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Checking options with optparse callbacks</title>
		<link>http://dancingpenguinsoflight.com/2009/08/checking-options-with-optparse-callbacks/</link>
		<comments>http://dancingpenguinsoflight.com/2009/08/checking-options-with-optparse-callbacks/#comments</comments>
		<pubDate>Sun, 16 Aug 2009 05:44:52 +0000</pubDate>
		<dc:creator>Samuel Huckins</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[CLI]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://dancingpenguinsoflight.com/?p=1150</guid>
		<description><![CDATA[optparse is a flexible and powerful module for processing command line options in Python programs. I&#8217;ve used it for a while, but I didn&#8217;t have much occasion to get into callbacks until recently. A callback here is a user-defined function &#8230; <a href="http://dancingpenguinsoflight.com/2009/08/checking-options-with-optparse-callbacks/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a target="_blank" title="optparse docs" href="http://docs.python.org/library/optparse.html">optparse</a> is a flexible and powerful module for processing command line options in Python programs. I&#8217;ve used it for a while, but I didn&#8217;t have much occasion to get into <a target="_blank" title="callbacks in optparse" href="http://docs.python.org/library/optparse.html#optparse-option-callbacks">callbacks</a> until recently. A callback here is a user-defined function optparse calls when its associated option is encountered. They can help you keep your script&#8217;s logic clear within a forest of options by providing arbitrary checks when particular options are passed. </p>
<p>When I first began using them, I ran into a critical point of confusion that wasn&#8217;t clearly illuminated in the assorted guides I found for optparse. In response, here is a short overview of how to use callbacks to perform checks and validations against passed options.</p>
<p>First, a very basic script using optparse for option parsing:</p>
<div class="codecolorer-container python blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">optparse</span><br />
<span style="color: #dc143c;">parser</span> <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">optparse</span>.<span style="color: black;">OptionParser</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-f&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;--input-file&quot;</span><span style="color: #66cc66;">,</span> dest<span style="color: #66cc66;">=</span><span style="color: #483d8b;">&quot;inputfile&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">help</span><span style="color: #66cc66;">=</span><span style="color: #483d8b;">&quot;The file with input&quot;</span><span style="color: black;">&#41;</span><br />
<span style="color: black;">&#40;</span>options<span style="color: #66cc66;">,</span> arguments<span style="color: black;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">parser</span>.<span style="color: black;">parse_args</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">print</span> options</div></td></tr></tbody></table></div>
<p>Which outputs:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">python test.py <span style="color: #660033;">-f</span> test.txt<br />
<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">'inputfile'</span>: <span style="color: #ff0000;">'test.txt'</span><span style="color: #7a0874; font-weight: bold;">&#125;</span></div></td></tr></tbody></table></div>
<p>For a more detailed coverage of the basics, review Doug Hellman&#8217;s <a target="_blank" title="Doug Hellman's PyMOTW on optparse" href="http://blog.doughellmann.com/2007/08/pymotw-optparse.html">PyMOTW article on optparse</a>. It would be best to verify that this input file exists so that we might inform the user and exit early. Adding that functionality to our example:</p>
<div class="codecolorer-container python blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">optparse</span><br />
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> check_file<span style="color: black;">&#40;</span>option<span style="color: #66cc66;">,</span> opt<span style="color: #66cc66;">,</span> value<span style="color: #66cc66;">,</span> <span style="color: #dc143c;">parser</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">exists</span><span style="color: black;">&#40;</span>value<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #dc143c;">optparse</span>.<span style="color: black;">OptionValueError</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%s doesn't seem to exist.&quot;</span> % value<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #dc143c;">parser</span>.<span style="color: black;">values</span>.<span style="color: black;">inputfile</span> <span style="color: #66cc66;">=</span> value<br />
<br />
<span style="color: #dc143c;">parser</span> <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">optparse</span>.<span style="color: black;">OptionParser</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-f&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;--input-file&quot;</span><span style="color: #66cc66;">,</span> dest<span style="color: #66cc66;">=</span><span style="color: #483d8b;">&quot;inputfile&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; action<span style="color: #66cc66;">=</span><span style="color: #483d8b;">&quot;callback&quot;</span><span style="color: #66cc66;">,</span> callback<span style="color: #66cc66;">=</span>check_file<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">type</span><span style="color: #66cc66;">=</span><span style="color: #483d8b;">&quot;string&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">help</span><span style="color: #66cc66;">=</span><span style="color: #483d8b;">&quot;The file with input&quot;</span><span style="color: black;">&#41;</span><br />
<span style="color: black;">&#40;</span>options<span style="color: #66cc66;">,</span> arguments<span style="color: black;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">parser</span>.<span style="color: black;">parse_args</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">print</span> options</div></td></tr></tbody></table></div>
<p>We&#8217;ve added a check_file() function to perform our check and also added the callback specification to the associated option. The arguments a callback function is passed are described in the <a target="_blank" title="callbacks in optparse" href="http://docs.python.org/library/optparse.html#optparse-option-callbacks">module documentation</a>. optparse.OptionValueError provides an elegant way to send an error message about the problem and quit the program in one step.</p>
<p>So what&#8217;s the problem? When I first wrote a callback, I didn&#8217;t catch the line in the tutorial examples that set the value of the option. I still don&#8217;t understand why this is necessary, but without the &#8220;parser.values.inputfile = value&#8221; line above, you&#8217;d find the inputfile variable to have None for its value. Strange, but true. I could understand the callback function having to <em>return</em> the option value if all was well, but why would the option&#8217;s value get unset in the first place?</p>
<p>Additionally, it turns out you have to set the option&#8217;s type even if the desired value is supposed to be default. In the above example, without &#8216;type=&#8221;string&#8221;&#8216;, the callback function would have thrown an error, complaining that we were claiming to use a string and weren&#8217;t.</p>
<p>So watch out for these details, and clean up your scripts with callbacks.</p>
    ]]></content:encoded>
			<wfw:commentRss>http://dancingpenguinsoflight.com/2009/08/checking-options-with-optparse-callbacks/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>What are all the wxPython Events?</title>
		<link>http://dancingpenguinsoflight.com/2007/10/what-are-all-wxpython-events/</link>
		<comments>http://dancingpenguinsoflight.com/2007/10/what-are-all-wxpython-events/#comments</comments>
		<pubDate>Tue, 02 Oct 2007 03:44:00 +0000</pubDate>
		<dc:creator>Samuel Huckins</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[wxPython]]></category>

		<guid isPermaLink="false">http://dancingpenguinsoflight.com/2007/10/01/what-are-all-the-wxpython-events/</guid>
		<description><![CDATA[On several occasions recently I found did not know what event to specify to trigger something in my wxPython apps. I had a hard time finding anything close to a comprehensive list of all events available, so I was limited &#8230; <a href="http://dancingpenguinsoflight.com/2007/10/what-are-all-wxpython-events/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>On several occasions recently I found did not know what event to specify to trigger something in my wxPython apps. I had a hard time finding anything close to a comprehensive list of all events available, so I was limited to posting questions on the <a href="http://www.wxpython.org/maillist.php">wxPython users mailing list</a>. And, while this is a wonderful and active list, it would be nice to have a guide even closer at hand.</p>
<p>Cody Precord on the list provided me with a handy way to get this:</p>
<div class="codecolorer-container python blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">import</span> wx<br />
<br />
<span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">dir</span><span style="color: black;">&#40;</span>wx<span style="color: black;">&#41;</span>:<br />
&nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> x.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'EVT_'</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> x</div></td></tr></tbody></table></div>
<p>Run that, and out comes a list of all the EVT types. Of course, in some cases you may not know what the event is just from the name, but they are mostly self-explanatory. See for yourself:</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">EVT_ACTIVATE<br />
EVT_ACTIVATE_APP<br />
EVT_BUTTON<br />
EVT_CALCULATE_LAYOUT<br />
EVT_CHAR<br />
EVT_CHAR_HOOK<br />
EVT_CHECKBOX</div></td></tr></tbody></table></div>
    ]]></content:encoded>
			<wfw:commentRss>http://dancingpenguinsoflight.com/2007/10/what-are-all-wxpython-events/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

