python:index
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
python:index [2012/08/30 17:24] – tkbletsc | python:index [2014/07/20 17:48] (current) – tkbletsc | ||
---|---|---|---|
Line 11: | Line 11: | ||
< | < | ||
+ | # use this like a dictonary (d[' | ||
+ | class DictObject(dict): | ||
+ | def __getattribute__(self, | ||
+ | def __setattribute__(self, | ||
+ | |||
+ | |||
+ | from datetime import datetime | ||
+ | def timestamp(): | ||
+ | |||
def avg(v): return sum(v)/ | def avg(v): return sum(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, | def pct_diff(a, | ||
+ | def product(L): return reduce(lambda x,y: x*y, L, 1) | ||
+ | |||
+ | def ping(host, | ||
+ | ret = subprocess.call([' | ||
+ | return ret==0 | ||
+ | |||
from timeit import Timer | from timeit import Timer | ||
Line 21: | Line 36: | ||
def shred_unicode(s): | def shred_unicode(s): | ||
return unicodedata.normalize(' | return unicodedata.normalize(' | ||
+ | |||
+ | # divide a list into equal pieces. | ||
+ | # chunk ranges from 0 to num_chunks-1 | ||
+ | def divide_list(lst, | ||
+ | i = int( float(chunk)/ | ||
+ | j = int( float(chunk+1)/ | ||
+ | return lst[i:j] | ||
import itertools | import itertools | ||
Line 59: | Line 81: | ||
return func | return func | ||
- | def uniq(seq, | + | # unlike set(), this is order preserving |
- | # order preserving | + | def uniq(seq, |
- | if idfun is None: idfun = lambda x: x | + | seen = set() |
- | | + | for item in seq: |
- | result = [] | + | marker = idfunc(item) |
- | for item in seq: | + | if marker not in seen: |
- | marker = idfun(item) | + | seen.add(marker) |
- | if marker not in seen: | + | yield item |
- | seen[marker] = 1 | + | |
- | result.append(item) | + | |
- | return result | + | |
- | def product(L): return reduce(lambda x,y: x*y, L, 1) | ||
</ | </ | ||
+ | |||
+ | Drop into python shell: | ||
+ | < | ||
+ | import code | ||
+ | code.interact(local=locals()) | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | class ConsoleThrobber(object): | ||
+ | def __init__(self, | ||
+ | self.chars=chars | ||
+ | self.i=0 | ||
+ | def __call__(self): | ||
+ | sys.stdout.write(" | ||
+ | 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' | ||
+ | return int(v) | ||
+ | if re.match(r' | ||
+ | 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, | ||
+ | table_rows = table_tsv.strip().split(" | ||
+ | |||
+ | table = {} | ||
+ | |||
+ | col_headers = table_rows[0].split(" | ||
+ | row_headers = [x.split(" | ||
+ | for row_header, | ||
+ | values = row.split(" | ||
+ | table[row_header] = { ' | ||
+ | for (col_header, | ||
+ | if xform: value = xform(value) | ||
+ | table[row_header][col_header] = value | ||
+ | return (row_headers, | ||
+ | |||
+ | |||
+ | engines_tsv = """ | ||
+ | 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 " | ||
+ | Rockomax " | ||
+ | Rockomax " | ||
+ | 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, | ||
+ | |||
+ | |||
+ | |||
+ | </ | ||
+ |
python/index.1346372686.txt.gz · Last modified: 2012/08/30 17:24 by tkbletsc