User Tools

Site Tools


python:index

This is an old revision of the document!


Python

def open_dash(filename,mode='r',bufsize=-1):
	if filename == '-':
		if 'w' in mode  or  'a' in mode: return sys.stdout
		elif 'r' in mode:                return sys.stdin
		else:                            raise Exception("Bad mode")
	else:
		return open(filename,mode,bufsize)
 
# allows you to put color codes in backticks and have them turned into escape codes
# example: colorize("`31`red `44`on blue`` reset")
def colorize(s): return re.sub('`(.*?)`','\x1b[\\1m',s)
 
# a decorator that puts the given function into a dictionary, changing its name:
heuristics = {}
def heuristic(func):
	name = func.__name__.replace('gadget_is_','').replace('gadget_','').replace('_','-')
	heuristics[name]=func
	return func
 
def uniq(seq, idfun=None): 
    # order preserving
    if idfun is None: idfun = lambda x: x
    seen = {}
    result = []
    for item in seq:
        marker = idfun(item)
        if marker not in seen: 
			seen[marker] = 1
			result.append(item)
    return result
 
def product(L): return reduce(lambda x,y: x*y, L, 1)
python/index.1278426662.txt.gz · Last modified: 2010/07/06 07:31 by tkbletsc

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki