Search the archives!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Whats in your $PYTHONSTARTUP$ ?
- From: p.magwene at snet.net (Paul Magwene)
- Subject: Whats in your $PYTHONSTARTUP$ ?
- Date: Tue, 27 Mar 2001 10:55:41 -0500
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()
#-------------------------------------------------------------------
- Follow-Ups:
- Whats in your $PYTHONSTARTUP$ ?
- From: Oleg Broytmann
- Whats in your $PYTHONSTARTUP$ ?
- From: Randall Hopper
- Whats in your $PYTHONSTARTUP$ ?
- Prev by Date: Combining Lists
- Next by Date: Curses...
- Previous by thread: Why not Tcl/Tk?
- Next by thread: Whats in your $PYTHONSTARTUP$ ?
- Index(es):