Whats in your $PYTHONSTARTUP$ ?

Paul Magwene p.magwene at snet.net
Tue Mar 27 10:55:41 EST 2001


I've been programming in Python for several years now.  Recently I realized
that I've never really taken advantage of being able to specify a default
startup file.

Other than import statements, what little programming gems do you folks have
in your startup files?  I thought it would be useful to draw on the
collective knowledge of the Python community.  If we get enough good ideas
it might be worth sticking up on a web page, or even distributing (in the
demos or Tools section) with a later release.

Cheers,

Paul Magwene
paul.magwene at yale.edu


My contribution is the following little snippet that I found useful.  It
makes it easier to navigate and explore directories from the interactive
interpretter, using the familiar 'cd' and 'ls' (change it to 'dir' if you're
a long time DOS user).


#-------------------------------------------------------------------
import sys, os, os.path
class DirLister:

    def __getitem__(self, key):
        s =  os.listdir(os.getcwd())
        return s[key]

    def __getslice__(self,i,j):
        s =  os.listdir(os.getcwd())
        return s[i:j]

    def __call__(self, path=os.getcwd()):
        path = os.path.expanduser(os.path.expandvars(path))
        return os.listdir(path)

    def __repr__(self):
        return str(os.listdir(os.getcwd()))


class DirChanger:
    def __init__(self, path=os.getcwd()):
        self.__call__(path)

    def __call__(self, path=os.getcwd()):
        path = os.path.expanduser(os.path.expandvars(path))
        os.chdir(path)

    def __repr__(self):
        return os.getcwd()


ls = DirLister()
cd = DirChanger()
#-------------------------------------------------------------------




More information about the Python-list mailing list