# This is startup file for interactive python.
# It is not automatically loaded by python interpreter.
# To instruct the interpreter to load it insert the following commands
# into your .profile (use whatever syntax and initialization file
# is appropriate for your shell):
#
# PYTHONSTARTUP=$HOME/init.py # or where you have really put it
# export PYTHONSTARTUP
#
# Due to nested_scopes and pydoc.help(*args, **kwds) this file only works with
# Python 2.1 or higher.
#
# Text version here
#
# Generated by gvim :runtime syntax/2html.vim
#
# This is startup file for interactive python.
# It is not automatically loaded by python interpreter.
# To instruct the interpreter to load it insert the following commands
# into your .profile (use whatever syntax and initialization file
# is appropriate for your shell):
#
# PYTHONSTARTUP=$HOME/init.py # or where you have really put it
# export PYTHONSTARTUP
#
# Due to nested_scopes and pydoc.help(*args, **kwds) this file only works with
# Python 2.1 or higher.
from __future__ import nested_scopes
def init():
import sys, os
import __builtin__
if not hasattr(__builtin__, "help"): # Python 2.2 has already got this
# From Guido van Rossum,
# http://www.onlamp.com/pub/a/python/2001/06/14/pythonnews.html
import pydoc
class _Helper:
def __repr__(self):
return "Please type help() for interactive help."
def __call__(self, *args, **kwds):
return pydoc.help(*args, **kwds)
__builtin__.help = _Helper()
pyreadlinewin32_startup = os.path.join(sys.prefix,
"lib", "site-packages", "pyreadline", "configuration", "startup.py")
if os.path.exists(pyreadlinewin32_startup):
execfile(pyreadlinewin32_startup)
else:
# From Bruce Edge
try:
import rlcompleter, readline
initfile = os.path.expanduser("~/.inputrc")
readline.read_init_file(initfile)
histfile = os.path.expanduser("~/.python-history")
try:
readline.read_history_file(histfile)
except IOError:
pass # No such file
def savehist():
histfilesize = os.environ.get('HISTFILESIZE') or \
os.environ.get('HISTSIZE')
if histfilesize:
try:
histfilesize = int(histfilesize)
except ValueError:
pass
else:
readline.set_history_length(histfilesize)
readline.write_history_file(histfile)
import atexit
atexit.register(savehist)
except (ImportError, AttributeError):
pass # no readline or atexit, or readline doesn't have
# {read,write}_history_file - ignore the error
# From Peter Norvig
from datetime import datetime
now = datetime.now
home = os.path.expanduser('~')
user = os.environ.get('LOGNAME', 'UNKNOWN')
from socket import gethostname
host = gethostname().split('.')[0]
class Prompt(object):
def __init__(self, str='%(cwd)s\n%(user)s@%(host)s [%(time)s] %(n)d >>> '):
self.str = str
self.n = 0
def __str__(self):
cwd = os.getcwd()
if cwd.startswith(home):
cwd = cwd.replace(home, '~')
self.n += 1
return self.str % {
'cwd': cwd,
'user': user,
'host': host,
'time': now().strftime('%H:%M'),
'n': self.n,
}
#def __radd__(self, other):
# return str(other) + str(self)
term = os.environ.get("TERM")
if 'linux' in term:
background = 'dark'
else:
background = os.environ.get('BACKGROUND', 'light').lower()
# From Randall Hopper
for _term in ["linux", "term", "rxvt", "vt100", "screen"]:
if _term in term:
if background == 'dark':
ps1_color = '3' # yellow
stdout_color = '7' # bold white
else:
ps1_color = '4' # blue
stdout_color = '0' # bold black
sys.ps1 = Prompt("%%(cwd)s\n%%(user)s@%%(host)s [%%(time)s] %%(n)d \001\033[3%sm\002>>>\001\033[0m\002 " % ps1_color)
#sys.ps1 = "%(cwd)s\n%(user)s@%(host)s \001\033[3%sm\002>>>\001\033[0m\002 " % ps1_color
sys.ps2 = "\001\033[1;32m\002...\001\033[0m\002 " # bold green
# From Denis Otkidach
class ColoredFile:
def __init__(self, fp, begin, end='\033[0m'): # reset all attributes
self.__fp = fp
self.__begin = begin
self.__end = end
def write(self, s):
self.__fp.write(self.__begin+s+self.__end)
def writelines(self, lines):
map(self.write, lines)
def __getattr__(self, attr):
return getattr(self.__fp, attr)
sys.stdout = ColoredFile(sys.stdout, '\033[1;3%sm' % stdout_color)
sys.stderr = ColoredFile(sys.stderr, '\033[31m') # red
break
else:
sys.ps1 = Prompt()
try:
import locale
except ImportError:
pass # locale was not compiled
else:
try:
locale.setlocale(locale.LC_ALL, '')
from pprint import pprint
def displayhook(value):
if value is not None:
__builtin__._ = value
pprint(value)
sys.displayhook = displayhook
except (ImportError, locale.Error):
pass # no locale support or unsupported locale
# From: Paul Magwene
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(path)
def __call__(self, path=os.getcwd()):
path = os.path.expanduser(os.path.expandvars(path))
os.chdir(path)
def __repr__(self):
return os.getcwd()
global ls, cd
ls = DirLister()
cd = DirChanger()
# From Thomas Heller
#
#import pdb
#
#def info(*args):
# pdb.pm()
#sys.excepthook = info
class Pwd:
def __call__(self):
return repr(self)
def __repr__(self):
return os.getcwd()
global pwd
pwd = Pwd()
class _Exit:
def __repr__(self):
sys.exit()
def __call__(self, msg=None):
sys.exit(msg)
global x
x = _Exit()
if isinstance(__builtin__.exit, str): # In Python 2.5+ exit and quit are objects
global exit, quit
exit = quit = x
init()
del init
del nested_scopes
This is the page http://phd.pp.ru/Software/dotfiles/init.py.html.
It was generated on Thu, 10 Dec 2009 15:52:30 GMT from CheetahTemplate init.py.tmpl.
Some rights are reserved.
Read more about technical aspects of the site.