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/29 08:39] – tkbletsc | python:index [2024/07/09 07:28] (current) – tkbletsc | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Python ====== | ====== Python ====== | ||
| - | * [[Colorization and tab completion for the python shell]] | + | * [[:Python virtual environment recipe|]] |
| - | * [[Defer to another program in the PATH, if available]] | + | * [[: |
| - | * [[SOCKSv4 proxy]] - Simple proxy in pure python. | + | * [[: |
| - | * [[virtualenv]] - A recipe to download and deploy Python 2.7 in your home directory without dependencies (except gcc) | + | * [[: |
| - | * [[optparse]] | + | * [[:python:virtualenv|]] - A recipe to download and deploy Python 2.7 in your home directory without dependencies (except gcc) |
| - | * [[IntVectorRange]] | + | * [[:python:optparse|]] |
| - | * [[Progress bar]] | + | * [[: |
| - | * [[Simple DynDNS updater]] | + | * [[: |
| + | * [[: | ||
| + | |||
| + | <code python> | ||
| + | # 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 stdev(v): return math.sqrt(sum(x*x for x in v) / len(v) - (avg(v) ** 2)) | ||
| + | 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 | ||
| t = Timer(" | t = Timer(" | ||
| Line 16: | Line 34: | ||
| def shred_unicode(s): | def shred_unicode(s): | ||
| - | 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 | ||
| def sliding_window(seq, | def sliding_window(seq, | ||
| - | " | + | |
| - | "Can optionally pad the window left and right." | + | "Can optionally pad the window left and right." |
| - | it = itertools.chain( | + | it = itertools.chain( |
| - | itertools.repeat(pad_value, | + | itertools.repeat(pad_value, |
| - | seq, | + | seq, |
| - | itertools.repeat(pad_value, | + | itertools.repeat(pad_value, |
| - | ) | + | ) |
| - | #it = iter(seq) | + | #it = iter(seq) |
| - | result = tuple(itertools.islice(it, | + | result = tuple(itertools.islice(it, |
| - | if len(result) == n: | + | if len(result) == n: |
| - | yield result | + | yield result |
| - | for elem in it: | + | for elem in it: |
| - | result = result[1:] + (elem,) | + | result = result[1:] + (elem,) |
| - | yield result | + | yield result |
| def open_dash(filename, | def open_dash(filename, | ||
| - | if filename == ' | + | |
| - | if ' | + | if ' |
| - | elif ' | + | elif ' |
| - | else: raise Exception(" | + | else: raise Exception(" |
| - | else: | + | else: |
| - | return open(filename, | + | return open(filename, |
| # allows you to put color codes in backticks and have them turned into escape codes | # allows you to put color codes in backticks and have them turned into escape codes | ||
| Line 51: | Line 76: | ||
| heuristics = {} | heuristics = {} | ||
| def heuristic(func): | def heuristic(func): | ||
| - | name = func.__name__.replace(' | + | |
| - | heuristics[name]=func | + | heuristics[name]=func |
| - | 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() |
| - | seen = {} | + | |
| - | result = [] | + | |
| for item in seq: | for item in seq: | ||
| - | marker = idfun(item) | + | marker = idfunc(item) |
| - | if marker not in seen: | + | if marker not in seen: |
| - | seen[marker] = 1 | + | seen.add(marker) |
| - | result.append(item) | + | yield item |
| - | | + | |
| - | def product(L): return reduce(lambda x,y: x*y, L, 1) | ||
| </ | </ | ||
| + | |||
| + | Drop into python shell: | ||
| + | |||
| + | <code python> | ||
| + | import code | ||
| + | code.interact(local=locals()) | ||
| + | |||
| + | </ | ||
| + | |||
| + | <code python> | ||
| + | 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: | ||
| + | |||
| + | <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, | ||
| + | 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 | ||
| + | O-10 MonoPropellant Engine | ||
| + | Rockomax 24-77 480 0.09 20 22.7 250 300 | ||
| + | Rockomax Mark 55 Radial Mount Liquid Engine | ||
| + | LV-1 Liquid Fuel Engine | ||
| + | Rockomax 48-7S 300 0.1 30 30.6 300 350 | ||
| + | LV-T30 Liquid Fuel Engine | ||
| + | LV-T45 Liquid Fuel Engine | ||
| + | LV-909 Liquid Fuel Engine | ||
| + | R.A.P.I.E.R. Engine | ||
| + | Toroidal Aerospike Rocket | ||
| + | Rockomax " | ||
| + | Rockomax " | ||
| + | Rockomax " | ||
| + | LV-N Atomic Rocket Motor 8700 2.25 60 2.7 220 800 | ||
| + | LFB KR-1x2 | ||
| + | Kerbodyne KR-2L Advanced Engine | ||
| + | S3 KS-25x4 Engine Cluster | ||
| + | """ | ||
| + | |||
| + | (engine_names, | ||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | |||
python/index.1346254744.txt.gz · Last modified: by tkbletsc
