[Tutor] Python as a superior teaching language

Arthur Watts arthur.watts@gbst.com
Thu, 19 Apr 2001 09:31:13 +1000


Guys,

	One of our developers had me build the GNU readline library, then
'activate' the module for Python 2.0 on our Alpha. I initially thought that
being able to scroll thru one's command history in the interative
interpreter was pretty good, but that's nothing compared to the info he just
sent me :

Here's some of the cool stuff you can do with Readline in Python.

As well as providing a command history, the Readline module allows you to do
auto-completion.  This can be very handy when you are just learning the
language and/or its objects.  (NOTE Readline is installed on Taurus, but is
not yet on Jupiter). Run the following lines when you run the Python
Interactive Shell (or better still, put them in your $PYTHONSTARTUP program)

	>>> import rlcompleter
	>>> import readline
	>>> readline.parse_and_bind("tab: complete")

And you now have AutoCompleting on.  To use it, type part of a variable and
press "<TAB>".  If there is only one variable matching, it will be filled in
automatically, as in

	>>> readline.i<TAB>

Line will become

	>>> readline.insert_text

because the readline module on has one attribute beginning with the letter
'i'.

If there are more than one, nothing will happen after the first <TAB>.
Press <TAB> again to see a list of variables that match, as in

	>>> import sys
	>>> sys.<TAB><TAB>
	sys.__doc__               sys.exec_prefix           sys.ps1
	sys.__name__              sys.executable            sys.ps2
	sys.__stderr__            sys.exit
sys.setcheckinterval
	sys.__stdin__             sys.exitfunc              sys.setprofile
	sys.__stdout__            sys.getdefaultencoding
sys.setrecursionlimit
	sys.argv                  sys.getrecursionlimit     sys.settrace
	sys.builtin_module_names  sys.getrefcount           sys.stderr
	sys.byteorder             sys.hexversion            sys.stdin
	sys.copyright             sys.maxint                sys.stdout
	sys.exc_info              sys.modules               sys.version
	sys.exc_traceback         sys.path                  sys.version_info
	sys.exc_type              sys.platform              
	sys.exc_value             sys.prefix                
	gem> sys.


Very handy if you don't know what methods/attributes are available (or how
to spell them). Like I said earlier, the main purpose of Readline is to
provide a command-line history.  By default, it is emacs style.  Use your
arrow keys to go through your previous commands.  I however, prefer the 'vi'
style line editing.  To do this, put a '.inputrc' file in your home
directory with the following line
	set editing-mode vi

Now you can use all your 'vi' commands (<ESC-K>, search with '/", etc).
Also, if you want to save your command history between sessions, get Python
to save it to file when you exit and re-reading it when you go back in.
Here's my complete $PYTHONSTARTUP script (That is, I have written this to
'/u/stephenb/python.py' and pointed the PYTHONSTARTUP Shell variable to it)

	try:
	  import readline
	except ImportError:
	  pass
	else:
	  import rlcompleter
	  import os
	  import atexit

	  # Allow auto-complete stuff
	  readline.parse_and_bind("tab: complete")

	  # Save the history file for next session
	  histfile = os.path.join(os.environ["HOME"], ".pyhist")
	  try:
	      readline.read_history_file(histfile)
	  except IOError:
	      pass
	  atexit.register(readline.write_history_file, histfile)

	  # Cleanup
	  del os, histfile, readline, atexit, rlcompleter


	I know that many commercial IDE's contain handy online references,
but I find this particularly valuable when you want to work your way thru a
module's namespace to find what you need.

Enjoy,

Arthur
Arthur Watts
Software Engineer GBST Automation
Global Banking & Securities Transactions

Telephone + 61 7 3331 5555
mailto: arthur.watts@gbst.com
www.gbst.com