<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Andrea Corbellini</title>
	<atom:link href="http://valueerror.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://valueerror.wordpress.com</link>
	<description>raise ValueError(&#34;Just another WordPress.com weblog&#34;)</description>
	<lastBuildDate>Tue, 03 Nov 2009 14:12:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='valueerror.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Andrea Corbellini</title>
		<link>http://valueerror.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://valueerror.wordpress.com/osd.xml" title="Andrea Corbellini" />
	<atom:link rel='hub' href='http://valueerror.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Python shell history, autocompletion and RC file</title>
		<link>http://valueerror.wordpress.com/2009/11/03/python-shell-history-autocompletion-and-rc-file/</link>
		<comments>http://valueerror.wordpress.com/2009/11/03/python-shell-history-autocompletion-and-rc-file/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 14:12:13 +0000</pubDate>
		<dc:creator>Andrea Corbellini</dc:creator>
				<category><![CDATA[Planet BeeSeek]]></category>
		<category><![CDATA[Planet Ubuntu]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://valueerror.wordpress.com/?p=53</guid>
		<description><![CDATA[Every Python developer knows that the Python shell can speed up the work, but not anybody know how fast it can be. In Bash you can browse the history of previously typed commands and have autocompletion with the tab key. Why not having the same features in the Python shell too? History On UNIX-like systems, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=valueerror.wordpress.com&amp;blog=6096202&amp;post=53&amp;subd=valueerror&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Every Python developer knows that the Python shell can speed up the work, but not anybody know how fast it can be.<br />
In Bash you can browse the history of previously typed commands and have autocompletion with the tab key. Why not having the same features in the Python shell too?</p>
<h4>History</h4>
<p>On UNIX-like systems, Python is shipped with a really useful module in the standard library: <a href="http://docs.python.org/library/readline.html">readline</a> that, among other things, can be used to enable the history. Its usage is very simple: it just needs a file to read and write the history, just like Bash. Here&#8217;s how it should be used:</p>
<pre>
&gt;&gt;&gt; import os
&gt;&gt;&gt; import readline
&gt;&gt;&gt; history_file = os.path.expanduser('~/.python_history')
&gt;&gt;&gt; readline.read_history_file(history_file)
</pre>
<p>Note that the history file is not written automatically at exit: you ought to call <code>write_history_file()</code> when you finish to use the interpreter or, better, register a function with <a href="http://docs.python.org/library/atexit.html">atexit</a>:</p>
<pre>
&gt;&gt;&gt; import atexit
&gt;&gt;&gt; atexit.register(readline.write_history_file, history_file)
</pre>
<h4>Autocompletion</h4>
<p>The <i>readline</i> module can be used for autocompletion too, but needs some help from a second module. In fact <i>readline</i> just binds the <b>tab</b> key, but the word completer itself is in <a href="http://docs.python.org/library/rlcompleter.html">rlcompleter</a>.</p>
<pre>
&gt;&gt;&gt; import rlcompleter
&gt;&gt;&gt; readline.parse_and_bind('tab: complete')
</pre>
<h4>RC file</h4>
<p>We have understood how to enable both history and autocompletion, but does this mean that we should type the above commands every time? No, of course. When used in interactive mode, Python always looks into the <code>PYTHONSTARTUP</code> environment variable and executes the specified script, if any. So, you can put the code to enable history and autocompletion in a file (for example: <code>~/.pythonrc</code>) and specify it in your <code>PYTHONSTARTUP</code>.</p>
<p>Using the RC file you also have the advantage that you can import the modules that you use frequently, so that you won&#8217;t have to import the manually in the interpreter. For example, here&#8217;s my own Python RC file:</p>
<pre>
import atexit
import os
import re
import readline
import rlcompleter
import socket
import _socket
import sys
import time
import timeit

history = os.path.expanduser('~/.python_history')
readline.read_history_file(history)
readline.parse_and_bind('tab: complete')
atexit.register(readline.write_history_file, history)

def t(*args):
    return timeit.Timer(*args).timeit()
</pre>
<br /> Tagged: Python <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/valueerror.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/valueerror.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/valueerror.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/valueerror.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/valueerror.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/valueerror.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/valueerror.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/valueerror.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/valueerror.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/valueerror.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/valueerror.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/valueerror.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/valueerror.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/valueerror.wordpress.com/53/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=valueerror.wordpress.com&amp;blog=6096202&amp;post=53&amp;subd=valueerror&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://valueerror.wordpress.com/2009/11/03/python-shell-history-autocompletion-and-rc-file/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">andrea-bs</media:title>
		</media:content>
	</item>
		<item>
		<title>Hello world!</title>
		<link>http://valueerror.wordpress.com/2009/11/02/hello-world/</link>
		<comments>http://valueerror.wordpress.com/2009/11/02/hello-world/#comments</comments>
		<pubDate>Mon, 02 Nov 2009 13:36:27 +0000</pubDate>
		<dc:creator>Andrea Corbellini</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Myself]]></category>

		<guid isPermaLink="false">http://valueerror.wordpress.com/?p=50</guid>
		<description><![CDATA[#!/usr/bin/python if __name__ == '__main__': print "Hello world!" Tagged: Myself<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=valueerror.wordpress.com&amp;blog=6096202&amp;post=50&amp;subd=valueerror&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<pre>
#!/usr/bin/python

if __name__ == '__main__':
    print "Hello world!"
</pre>
<br /> Tagged: Myself <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/valueerror.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/valueerror.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/valueerror.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/valueerror.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/valueerror.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/valueerror.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/valueerror.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/valueerror.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/valueerror.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/valueerror.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/valueerror.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/valueerror.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/valueerror.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/valueerror.wordpress.com/50/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=valueerror.wordpress.com&amp;blog=6096202&amp;post=50&amp;subd=valueerror&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://valueerror.wordpress.com/2009/11/02/hello-world/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">andrea-bs</media:title>
		</media:content>
	</item>
	</channel>
</rss>
