python:python
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
python:python [2013/03/13 21:51] – removed dblume | python:python [2024/02/27 15:43] (current) – [Linux script that takes either stdin or files] dblume | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== General Python Notes ====== | ||
+ | Man, there' | ||
+ | |||
+ | * [[http:// | ||
+ | * They mention using [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * Of course, there' | ||
+ | * There' | ||
+ | * Look into the microframework [[http:// | ||
+ | * [[https:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[https:// | ||
+ | * A Power Point deck by Alex Martelli describing [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | |||
+ | Would be nice to create a binary search in text files in Python. | ||
+ | |||
+ | This [[http:// | ||
+ | |||
+ | Would be good to experiment with [[http:// | ||
+ | |||
+ | ===== Data Analysis ===== | ||
+ | |||
+ | Looks like I should dive into: | ||
+ | |||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | |||
+ | ===== Template Files to Start With ===== | ||
+ | |||
+ | You have some template files in svn: | ||
+ | |||
+ | < | ||
+ | / | ||
+ | / | ||
+ | / | ||
+ | / | ||
+ | / | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== For / Else (Nobreak) ===== | ||
+ | |||
+ | Python has a [[http:// | ||
+ | |||
+ | ===== Sorting ===== | ||
+ | |||
+ | From an old [[http:// | ||
+ | <code python> | ||
+ | import operator | ||
+ | |||
+ | rows.sort(key=operator.itemgetter(4)) | ||
+ | # or | ||
+ | rows.sort(lambda x, y : x[4] == y[4] and cmp(x[2], | ||
+ | </ | ||
+ | |||
+ | ...not like I could just find the same info at the [[http:// | ||
+ | |||
+ | ===== Prepopulating lists with objects ===== | ||
+ | |||
+ | Remember when you lost a couple of hours thinking that the following line created a list of objects. | ||
+ | |||
+ | <code python> | ||
+ | l = [Obj()] * n | ||
+ | </ | ||
+ | |||
+ | It doesn' | ||
+ | |||
+ | What you meant to write was this: | ||
+ | |||
+ | <code python> | ||
+ | l = [Obj() for _ in range(n)] | ||
+ | </ | ||
+ | |||
+ | ===== Linux script that takes either stdin or files ===== | ||
+ | |||
+ | Newer way, **use [[https:// | ||
+ | <code python> | ||
+ | if __name__==' | ||
+ | if len(sys.argv) < 2: | ||
+ | # Process lines coming from stdin. | ||
+ | while 1: | ||
+ | line = sys.stdin.readline() | ||
+ | if not line: | ||
+ | break | ||
+ | my_process_line(line.rstrip()) | ||
+ | else: | ||
+ | # Process lines of the files specified. | ||
+ | for fname in sys.argv[1: | ||
+ | if not os.path.exists(fname): | ||
+ | treat_argument_as_literal(fname) | ||
+ | continue | ||
+ | with open(fname, ' | ||
+ | while 1: | ||
+ | line = f.readline() | ||
+ | if not line: | ||
+ | break | ||
+ | my_process_line(line.rstrip()) | ||
+ | </ | ||
+ | |||
+ | ===== The With statement ===== | ||
+ | |||
+ | [[http:// | ||
+ | |||
+ | ===== cProfile vs. line_profiler and kernprof ===== | ||
+ | |||
+ | Raymond Hettinger [[https:// | ||
+ | |||
+ | Here it is: [[http:// | ||
+ | |||
+ | ===== timeit ===== | ||
+ | |||
+ | <code python> | ||
+ | import timeit | ||
+ | |||
+ | def Use_a(): | ||
+ | pass | ||
+ | |||
+ | def Use_b(): | ||
+ | pass | ||
+ | |||
+ | def Run_all_tests(): | ||
+ | my_setup() | ||
+ | Use_a() | ||
+ | Use_b() | ||
+ | |||
+ | if __name__ == ' | ||
+ | |||
+ | # t = timeit.timeit( ' | ||
+ | # print dir( t ) | ||
+ | # print t | ||
+ | |||
+ | t = timeit.Timer( ' | ||
+ | print t.timeit() | ||
+ | </ | ||
+ | |||
+ | When diving in, cProfile may come in handy. | ||
+ | |||
+ | <code python> | ||
+ | import cProfile | ||
+ | |||
+ | def my_function(): | ||
+ | # Complicated stuff | ||
+ | pass | ||
+ | | ||
+ | if __name__ == ' | ||
+ | cProfile.run(" | ||
+ | </ | ||
+ | |||
+ | ===== Dynamically Calculating Column Size ====== | ||
+ | |||
+ | [[http:// | ||
+ | |||
+ | <code python> | ||
+ | data = ''' | ||
+ | 234 127 34 23 45567 | ||
+ | 23 12 4 4 45 | ||
+ | 23456 2 1 444 567''' | ||
+ | |||
+ | # Split input data by row and then on spaces | ||
+ | rows = [line.strip().split(' | ||
+ | |||
+ | # Reorganize data by columns | ||
+ | cols = zip(*rows) | ||
+ | |||
+ | # Compute column widths by taking maximum length of values per column | ||
+ | col_widths = [max(len(value) for value in col) for col in cols] | ||
+ | |||
+ | # Create a suitable format string | ||
+ | format = ' ' | ||
+ | |||
+ | # Print each row using the computed format | ||
+ | for row in rows: | ||
+ | print format % tuple(row) | ||
+ | </ | ||
+ | |||
+ | Which outputs: | ||
+ | < | ||
+ | 234 127 34 23 45567 | ||
+ | | ||
+ | 23456 | ||
+ | </ | ||
+ | |||
+ | Also, here's a [[http:// | ||
+ | |||
+ | ===== Different Types of Objects ===== | ||
+ | |||
+ | <code python> | ||
+ | class A: | ||
+ | """ | ||
+ | def __init__(self): | ||
+ | self.__m_x = 0 | ||
+ | |||
+ | def getx(self): | ||
+ | return self.__m_x | ||
+ | |||
+ | def setx(self, x): | ||
+ | if x < 0: x = 0 | ||
+ | self.__m_x = x | ||
+ | |||
+ | x = property(getx, | ||
+ | |||
+ | class B: | ||
+ | """ | ||
+ | __slots__ = [" | ||
+ | def __init__(self): | ||
+ | self.__m_x = 0 | ||
+ | |||
+ | def getx(self): | ||
+ | return self.__m_x | ||
+ | |||
+ | def setx(self, x): | ||
+ | if x < 0: x = 0 | ||
+ | self.__m_x = x | ||
+ | |||
+ | x = property(getx, | ||
+ | |||
+ | class C(object): | ||
+ | """ | ||
+ | def __init__(self): | ||
+ | self.__x = 0 | ||
+ | |||
+ | def getx(self): | ||
+ | return self.__x | ||
+ | |||
+ | def setx(self, x): | ||
+ | if x < 0: x = 0 | ||
+ | self.__x = x | ||
+ | |||
+ | x = property(getx, | ||
+ | </ | ||
+ | |||
+ | ====== filelock ====== | ||
+ | |||
+ | Evan Fosmark has a filelock module. | ||
+ | |||
+ | <code python> | ||
+ | import os | ||
+ | import fcntl | ||
+ | import inspect | ||
+ | # Maybe use os.path.abspath(__file__) ? | ||
+ | with open(os.path.abspath inspect.getfile(inspect.currentframe())), | ||
+ | try: | ||
+ | fcntl.flock(f, | ||
+ | call_that_cannot_be_concurrent() | ||
+ | finally: | ||
+ | fcntl.flock(f, | ||
+ | </ | ||
+ | |||
+ | ====== Various Approaches to threaded URL Requests ====== | ||
+ | |||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * Or, use the doc's [[https:// | ||
+ | * And, as mentioned in Parallelism in One Line, [[https:// | ||
+ | |||
+ | ====== Fibonacci Generator with Itertools ====== | ||
+ | |||
+ | <code python> | ||
+ | import itertools | ||
+ | |||
+ | def fib(n): | ||
+ | """ | ||
+ | a, b = 0, 1 | ||
+ | while True: | ||
+ | yield a | ||
+ | b = a + b | ||
+ | yield b | ||
+ | a = a + b | ||
+ | |||
+ | if __name__ == ' | ||
+ | for x in itertools.islice(fib(), | ||
+ | print x | ||
+ | |||
+ | # for i in range( 5 ): | ||
+ | # print i, fib( i ) | ||
+ | </ | ||
+ | |||
+ | ====== Parse C++ Code with nm and graphviz' | ||
+ | |||
+ | <code bash> | ||
+ | sudo apt-get install graphviz | ||
+ | </ | ||
+ | |||
+ | <code python> | ||
+ | # Read `nm` output | ||
+ | output = subprocess.check_output([' | ||
+ | for line in output.split(' | ||
+ | if ' | ||
+ | |||
+ | # Make a .dot file | ||
+ | with open(filename + ' | ||
+ | file.write(' | ||
+ | for f, destinations in self.graph.items(): | ||
+ | for d in destinations: | ||
+ | file.write(f' | ||
+ | |||
+ | |||
+ | def create_png_image(filename): | ||
+ | with open(filename + ' | ||
+ | p1 = subprocess.run([' | ||
+ | |||
+ | </ | ||
+ | |||
+ | ====== TODO ====== | ||
+ | |||
+ | TODO Link to my tips from LiveJournal and GMail, and why I chose which timing modules. |
python/python.1363236675.txt.gz · Last modified: 2023/04/12 20:44 (external edit)