views:

84

answers:

3

Are there any common timesavers that people put in Python Interactive Startup scripts? I made a dopey one to help me know where I am when I try to do relative file operations or imports, using a win32 module to change the name of the console window.

import sys
import os
import win32api
__title_prefix = 'Python %i.%i.%i %s %s' % (sys.version_info[0:4] +
                                            (sys.version_info[4] or "",))

def __my_chdir(path):
    __os_chdir(path)
    win32api.SetConsoleTitle(__title_prefix + " - " + os.getcwd())

# replace chdir func
__os_chdir = os.chdir
os.chdir = __my_chdir

os.chdir(r'C:\Scripts')
+2  A: 
# Add auto-completion and a stored history file of commands to your Python
# interactive interpreter. Requires Python 2.0+, readline. Autocomplete is
# bound to the Esc key by default (you can change it - see readline docs).
#
# Store the file in ~/.pystartup, and set an environment variable to point
# to it:  "export PYTHONSTARTUP=/home/user/.pystartup" in bash.
#
# Note that PYTHONSTARTUP does *not* expand "~", so you have to put in the
# full path to your home directory.

import atexit
import os
import readline
import rlcompleter

readline.parse_and_bind("tab: complete")
historyPath = os.path.expanduser("~/.history.py")

def save_history(historyPath=historyPath):
    import readline
    readline.write_history_file(historyPath)

if os.path.exists(historyPath):
    readline.read_history_file(historyPath)

atexit.register(save_history)
del os, atexit, readline, rlcompleter, save_history, historyPath
aaa
Unix only from the looks of http://docs.python.org/library/readline.html
Nick T
@Nic what?! There are non-UNIX systems there? :-)
aaa
Psh, I do embedded development most of the time, so using *nix is practically impossible for me, as the vast majority of compilers, programmers, and debuggers are all Windows targeted.
Nick T
@Nic check this out http://stackoverflow.com/questions/1081405/python-tab-completion-in-windows.you may also want to check out Ipython which me will make your life a lot easier if you use Windows prompt
aaa
There is a readline replacement for windows : pyreadlinehttps://launchpad.net/pyreadline
pyfunc
+2  A: 

I also use see, an easier on the eye replacement for Python's dir.

from see import see

An example of use of see as opposed to dir follows:

>>> k = {}
>>> dir(k)
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__','__doc__',
'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__',
'__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy',
'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues',
'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
>>> see(k)
    []              in              <               <=              ==
    !=              >               >=              hash()          help()
    iter()          len()           repr()          str()           .clear()
    .copy()         .fromkeys()     .get()          .has_key()      .items()
    .iteritems()    .iterkeys()     .itervalues()   .keys()         .pop()
    .popitem()      .setdefault()   .update()       .values()
Muhammad Alkarouri