python:index
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| python:index [2014/07/20 17:48] – 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> | + | <code python> |
| # use this like a dictonary (d[' | # use this like a dictonary (d[' | ||
| class DictObject(dict): | class DictObject(dict): | ||
| def __getattribute__(self, | def __getattribute__(self, | ||
| def __setattribute__(self, | def __setattribute__(self, | ||
| - | |||
| from datetime import datetime | from datetime import datetime | ||
| Line 26: | Line 26: | ||
| def ping(host, | def ping(host, | ||
| - | ret = subprocess.call([' | + | |
| - | return ret==0 | + | return ret==0 |
| from timeit import Timer | from timeit import Timer | ||
| Line 35: | Line 34: | ||
| def shred_unicode(s): | def shred_unicode(s): | ||
| - | return unicodedata.normalize(' | + | |
| - | # divide a list into equal pieces. | + | # divide a list into equal pieces. |
| # chunk ranges from 0 to num_chunks-1 | # chunk ranges from 0 to num_chunks-1 | ||
| def divide_list(lst, | def divide_list(lst, | ||
| - | i = int( float(chunk)/ | + | |
| - | j = int( float(chunk+1)/ | + | j = int( float(chunk+1)/ |
| - | return lst[i:j] | + | 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 77: | Line 76: | ||
| heuristics = {} | heuristics = {} | ||
| def heuristic(func): | def heuristic(func): | ||
| - | name = func.__name__.replace(' | + | |
| - | heuristics[name]=func | + | heuristics[name]=func |
| - | return func | + | return func |
| # unlike set(), this is order preserving | # unlike set(), this is order preserving | ||
| - | def uniq(seq, idfunc=None): | + | def uniq(seq, idfunc=None): |
| - | seen = set() | + | seen = set() |
| - | for item in seq: | + | for item in seq: |
| - | marker = idfunc(item) if idfunc else item | + | marker = idfunc(item) if idfunc else item |
| - | if marker not in seen: | + | if marker not in seen: |
| - | seen.add(marker) | + | seen.add(marker) |
| - | yield item | + | yield item |
| </ | </ | ||
| Drop into python shell: | Drop into python shell: | ||
| - | <code=python> | + | |
| + | <code python> | ||
| import code | import code | ||
| code.interact(local=locals()) | code.interact(local=locals()) | ||
| + | |||
| </ | </ | ||
| - | <code=python> | + | <code python> |
| class ConsoleThrobber(object): | class ConsoleThrobber(object): | ||
| - | def __init__(self, | + | |
| - | self.chars=chars | + | self.chars=chars |
| - | self.i=0 | + | self.i=0 |
| - | def __call__(self): | + | def __call__(self): |
| - | sys.stdout.write(" | + | sys.stdout.write(" |
| - | sys.stdout.flush() | + | sys.stdout.flush() |
| - | self.i = (self.i + 1) % len(self.chars) | + | self.i = (self.i + 1) % len(self.chars) |
| def workload(): | def workload(): | ||
| - | throb = ConsoleThrobber(" | + | |
| - | while True: | + | while True: |
| - | # ... do stuff ... | + | # ... do stuff ... |
| - | throb() | + | throb() |
| - | + | ||
| def recognize_type(v): | def recognize_type(v): | ||
| Line 122: | Line 122: | ||
| return float(v) | return float(v) | ||
| return v | return v | ||
| + | |||
| </ | </ | ||
| Parse tab-delimited tables, especially pasted from excel: | Parse tab-delimited tables, especially pasted from excel: | ||
| - | <code=python> | + | <code python> |
| def recognize_type(v): | def recognize_type(v): | ||
| try: return float(v) | try: return float(v) | ||
| except ValueError: pass | except ValueError: pass | ||
| - | | + | |
| try: return int(v) | try: return int(v) | ||
| except ValueError: pass | except ValueError: pass | ||
| - | | + | |
| return v | return v | ||
| - | | + | |
| def parse_table(table_tsv, | def parse_table(table_tsv, | ||
| table_rows = table_tsv.strip().split(" | table_rows = table_tsv.strip().split(" | ||
| Line 151: | Line 152: | ||
| return (row_headers, | return (row_headers, | ||
| - | + | engines_tsv = """ | |
| - | engines_tsv = """ | + | LV-1R Liquid Fuel Engine |
| - | LV-1R Liquid Fuel Engine 650 0.03 4 13.6 220 290 | + | O-10 MonoPropellant Engine |
| - | O-10 MonoPropellant Engine 800 0.09 20 22.7 220 290 | + | Rockomax 24-77 480 0.09 20 22.7 250 300 |
| - | Rockomax 24-77 480 0.09 20 22.7 250 300 | + | Rockomax Mark 55 Radial Mount Liquid Engine |
| - | Rockomax Mark 55 Radial Mount Liquid Engine 850 0.9 120 13.6 290 320 | + | LV-1 Liquid Fuel Engine |
| - | LV-1 Liquid Fuel Engine 350 0.03 4 13.6 220 290 | + | Rockomax 48-7S 300 0.1 30 30.6 300 350 |
| - | Rockomax 48-7S 300 0.1 30 30.6 300 350 | + | LV-T30 Liquid Fuel Engine |
| - | LV-T30 Liquid Fuel Engine 850 1.25 215 17.5 320 370 | + | LV-T45 Liquid Fuel Engine |
| - | LV-T45 Liquid Fuel Engine 950 1.5 200 13.6 320 370 | + | LV-909 Liquid Fuel Engine |
| - | LV-909 Liquid Fuel Engine 750 0.5 50 10.2 300 390 | + | R.A.P.I.E.R. Engine |
| - | R.A.P.I.E.R. Engine 3600 1.75 175 10.2 320 360 | + | Toroidal Aerospike Rocket |
| - | Toroidal Aerospike Rocket 3850 1.5 175 11.9 388 390 | + | Rockomax " |
| - | Rockomax " | + | Rockomax " |
| - | Rockomax " | + | Rockomax " |
| - | Rockomax " | + | LV-N Atomic Rocket Motor 8700 2.25 60 2.7 220 800 |
| - | LV-N Atomic Rocket Motor 8700 2.25 60 2.7 220 800 | + | LFB KR-1x2 |
| - | LFB KR-1x2 16400 10 2000 20.4 320 360 | + | Kerbodyne KR-2L Advanced Engine |
| - | Kerbodyne KR-2L Advanced Engine 20850 6.5 2500 39.2 320 380 | + | S3 KS-25x4 Engine Cluster |
| - | S3 KS-25x4 Engine Cluster 32400 9.75 3200 33.5 320 360 | + | |
| """ | """ | ||
| Line 176: | Line 176: | ||
| + | </ | ||
| - | </ | ||
python/index.1405903722.txt.gz · Last modified: by tkbletsc
