User Tools

Site Tools


python:index

Python

# use this like a dictonary (d['thing']) OR an object (d.thing)
class DictObject(dict):
    def __getattribute__(self,k): return self[k]
    def __setattribute__(self,k,v): self[k]=v
 
 
from datetime import datetime
def timestamp(): return datetime.now().strftime("%Y%m%d-%H%M%S")
 
def avg(v): return sum(v)/len(v)
def stdev(v): return math.sqrt(sum(x*x for x in v) / len(v) - (avg(v) ** 2))
def pct_diff(a,b): return (a/b)-1
def product(L): return reduce(lambda x,y: x*y, L, 1)
 
def ping(host,timeout=1):
	ret = subprocess.call(['ping','-c1','-W',str(timeout),host],stdout=open('/dev/null', 'w'))
	return ret==0
 
 
from timeit import Timer
t = Timer("test()", "from __main__ import test")
print t.timeit()
 
def shred_unicode(s):
	return unicodedata.normalize('NFKD', s.decode("utf-8")).encode('ascii','ignore')
 
# divide a list into equal pieces.  
# chunk ranges from 0 to num_chunks-1
def divide_list(lst,chunk,num_chunks):
	i = int( float(chunk)/num_chunks*len(lst) )
	j = int( float(chunk+1)/num_chunks*len(lst) )-1
	return lst[i:j]
 
import itertools
def sliding_window(seq, n=2, pad_value=None, pad_left=0, pad_right=0):
	"Returns a sliding window (of width n) over data from the iterable."
	"Can optionally pad the window left and right."
	it = itertools.chain(
		itertools.repeat(pad_value,pad_left), 
		seq, 
		itertools.repeat(pad_value,pad_right),
	)
	#it = iter(seq)
	result = tuple(itertools.islice(it, n))
	if len(result) == n:
		yield result    
	for elem in it:
		result = result[1:] + (elem,)
		yield result
 
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")
# if enable is set to False, then backticked numbers are merely removed instead of being colorized
def colorize(s,enable=True): return re.sub('`(.*?)`','\x1b[\\1m',s) if enable else re.sub('`(.*?)`','',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
 
# unlike set(), this is order preserving
def uniq(seq, idfunc=None): 
	seen = set()
	for item in seq:
		marker = idfunc(item) if idfunc else item
		if marker not in seen: 
			seen.add(marker)
			yield item

Drop into python shell:

import code
code.interact(local=locals())
class ConsoleThrobber(object):
	def __init__(self,chars='/\\'):
		self.chars=chars
		self.i=0
	def __call__(self):
		sys.stdout.write("\r" + self.chars[self.i])
		sys.stdout.flush()
		self.i = (self.i + 1) % len(self.chars)
 
def workload():
	throb = ConsoleThrobber("\\|/-")
	while True:
		# ... do stuff ...
		throb()
 
 
 
def recognize_type(v):
    if re.match(r'\d+$',v):
        return int(v)
    if re.match(r'\d+\.\d*$',v):
        return float(v)
    return v

Parse tab-delimited tables, especially pasted from excel:

def recognize_type(v):
    try: return float(v)
    except ValueError: pass
 
    try: return int(v)
    except ValueError: pass
 
    return v
 
def parse_table(table_tsv, xform=recognize_type):
    table_rows = table_tsv.strip().split("\n")
 
    table = {}
 
    col_headers = table_rows[0].split("\t")[1:]
    row_headers = [x.split("\t")[0] for x in table_rows[1:]]
    for row_header,row in zip(row_headers,table_rows[1:]):
        values = row.split("\t")[1:]
        table[row_header] = { 'name' : row_header }
        for (col_header,value) in zip(col_headers,values):
            if xform: value = xform(value)
            table[row_header][col_header] = value
    return (row_headers,col_headers,table)
 
 
engines_tsv = """name	cost	mass	thrust	TWR	Isp_atm	Isp_vac
LV-1R Liquid Fuel Engine	650	0.03	4	13.6	220	290
O-10 MonoPropellant Engine	800	0.09	20	22.7	220	290
Rockomax 24-77	480	0.09	20	22.7	250	300
Rockomax Mark 55 Radial Mount Liquid Engine	850	0.9	120	13.6	290	320
LV-1 Liquid Fuel Engine	350	0.03	4	13.6	220	290
Rockomax 48-7S	300	0.1	30	30.6	300	350
LV-T30 Liquid Fuel Engine	850	1.25	215	17.5	320	370
LV-T45 Liquid Fuel Engine	950	1.5	200	13.6	320	370
LV-909 Liquid Fuel Engine	750	0.5	50	10.2	300	390
R.A.P.I.E.R. Engine	3600	1.75	175	10.2	320	360
Toroidal Aerospike Rocket	3850	1.5	175	11.9	388	390
Rockomax "Poodle" Liquid Engine	1600	2	220	11.2	270	390
Rockomax "Mainsail" Liquid Engine	5650	6	1500	25.5	320	360
Rockomax "Skipper" Liquid Engine	2850	3	650	22.1	320	370
LV-N Atomic Rocket Motor	8700	2.25	60	2.7	220	800
LFB KR-1x2	16400	10	2000	20.4	320	360
Kerbodyne KR-2L Advanced Engine	20850	6.5	2500	39.2	320	380
S3 KS-25x4 Engine Cluster	32400	9.75	3200	33.5	320	360
"""
 
(engine_names,fields,engines) = parse_table(engines_tsv, recognize_type)
python/index.txt · Last modified: 2014/07/20 17:48 by tkbletsc