User Tools

Site Tools


python:index

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
python:index [2012/11/21 07:41] tkbletscpython:index [2014/07/19 22:04] tkbletsc
Line 11: Line 11:
  
 <code=python> <code=python>
 +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 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 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 pct_diff(a,b): return (a/b)-1
 def product(L): return reduce(lambda x,y: x*y, L, 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 from timeit import Timer
Line 99: Line 107:
  # ... do stuff ...  # ... do stuff ...
  throb()  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
 </code> </code>
 +
 +Parse tab-delimited tables, especially pasted from excel:
 +
 +<code=python>
 +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)
 +
 +
 +
 +</code>
 +
python/index.txt · Last modified: 2014/07/20 17:48 by tkbletsc

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki