python:python
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
Previous revisionNext revision | |||
— | python:python [2013/03/13 17:16] – dblume | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Python ====== | ||
+ | Man, there' | ||
+ | |||
+ | * [[http:// | ||
+ | * They mention using [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * Of course, there' | ||
+ | * There' | ||
+ | * Look into the microframework [[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: | ||
+ | |||
+ | < | ||
+ | / | ||
+ | / | ||
+ | / | ||
+ | / | ||
+ | / | ||
+ | </ | ||
+ | |||
+ | ===== Linux or Bash Tips ===== | ||
+ | |||
+ | Useful bash command for finding strings within python files... | ||
+ | |||
+ | <code bash> | ||
+ | find . -name \*.py -type f -print0 | xargs -0 grep -nI " | ||
+ | </ | ||
+ | |||
+ | Interesting way to use '' | ||
+ | |||
+ | <code bash> | ||
+ | #!/bin/bash | ||
+ | find $PWD -regex " | ||
+ | grep -v " \|unwantedpath/ | ||
+ | cscope -q -b | ||
+ | </ | ||
+ | |||
+ | And these two have nothing to do with Python. | ||
+ | |||
+ | <code bash> | ||
+ | nm obj-directory/ | ||
+ | find bindirectory/ | ||
+ | c++filt | grep -P " | ||
+ | </ | ||
+ | |||
+ | Also handy to merge two streams together... | ||
+ | |||
+ | <code bash> | ||
+ | ( cat file1 && cat file2 ) | sort | ||
+ | </ | ||
+ | |||
+ | When a little quick math is needed, use '' | ||
+ | <code bash> | ||
+ | $ bc <<< | ||
+ | F | ||
+ | $ bc -l <<< | ||
+ | .33333333333333333333 | ||
+ | $ bc <<< | ||
+ | .33 | ||
+ | $ bc <<< | ||
+ | 11 | ||
+ | </ | ||
+ | |||
+ | and, when coverting from hex to dec... | ||
+ | |||
+ | <code bash> | ||
+ | echo $((0x2dec)) | ||
+ | </ | ||
+ | |||
+ | But, then again, does that really seem easier than, | ||
+ | |||
+ | < | ||
+ | python -c "print int(' | ||
+ | </ | ||
+ | |||
+ | There' | ||
+ | |||
+ | <code bash> | ||
+ | $ echo $(( ($(date +%s) - $(date -d " | ||
+ | </ | ||
+ | |||
+ | And a Python way... | ||
+ | |||
+ | <code python> | ||
+ | python -c " | ||
+ | </ | ||
+ | |||
+ | And for displaying lines to get cut instead of wrapped: | ||
+ | |||
+ | <code bash> | ||
+ | cat_one_line_per_row() { | ||
+ | cat " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ctags' | ||
+ | |||
+ | <code bash> | ||
+ | ctags -n --if0=yes --c++-kinds=+p --langmap=c++: | ||
+ | --langmap=asm: | ||
+ | --exclude=unwanted_file.lst \ | ||
+ | --exclude=' | ||
+ | --regex-C++='/ | ||
+ | </ | ||
+ | |||
+ | ===== 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 ===== | ||
+ | |||
+ | <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:// | ||
+ | |||
+ | ===== 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, setx ) | ||
+ | |||
+ | 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, setx ) | ||
+ | |||
+ | 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, setx ) | ||
+ | </ | ||
+ | |||
+ | ====== TODO ====== | ||
+ | |||
+ | TODO Link to my tips from LiveJournal and GMail, and why I chose which timing modules. | ||
+ | Touching this page to see if the indexmenu will update its data. |
python/python.txt · Last modified: 2024/02/27 15:43 by dblume