Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
python [2006/05/08 22:47]
a created
python [2008/03/09 00:08]
a updated
Line 1: Line 1:
 ====== Python ====== ====== Python ======
  
-==== python interpreter (history + autocomplete) ====+see: **[[http://www.red-sweater.com/blog/140/learn-python|Good article about learning python]]**, [[http://nodebox.net/]]
  
-**''.pyrc''** 
-  try:  
-          import readline 
-  except ImportError: 
-          print "Module readline not available." 
-  else: 
-          import rlcompleter 
-          readline.parse_and_bind("tab: complete") 
-  import os 
-  histfile = os.path.join(os.environ["HOME"], ".pyhist") 
-  try: 
-          readline.read_history_file(histfile) 
-  except IOError: 
-          pass 
-  import atexit 
-  atexit.register(readline.write_history_file, histfile) 
  
 +==== python interpreter (history + autocomplete) ====
  
 +**''.pyrc''**
 +   # startup script for python to enable saving of interpreter history and
 +   # enabling name completion
 +   
 +   # import needed modules
 +   import atexit
 +   import os
 +   import readline
 +   import rlcompleter
 +   
 +   # where is history saved
 +   historyPath = os.path.expanduser("~/.pyhistory")
 +   
 +    # handler for saving history
 +    def save_history(historyPath=historyPath):
 +       import readline
 +       readline.write_history_file(historyPath)
 +   
 +   # read history, if it exists
 +   if os.path.exists(historyPath):
 +       readline.read_history_file(historyPath)
 +   
 +   # register saving handler
 +   atexit.register(save_history)
 +   
 +   # enable completion
 +   readline.parse_and_bind('tab: complete')
 +   
 +   # cleanup
 +   del os, atexit, readline, rlcompleter, save_history, historyPath
 +   
 **''.bashrc''** **''.bashrc''**
   export PYTHONSTARTUP=~/.pyrc   export PYTHONSTARTUP=~/.pyrc
python.txt ยท Last modified: 2009/05/25 00:35 (external edit)
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0 ipv6 ready