Difference between revisions of "Python notes"
m |
m (→Memoize) |
||
Line 34: | Line 34: | ||
I have a few recipes on ActiveState's fun and useful [http://aspn.activestate.com/ASPN/Cookbook/Python?author=103276 Python cookbook]. | I have a few recipes on ActiveState's fun and useful [http://aspn.activestate.com/ASPN/Cookbook/Python?author=103276 Python cookbook]. | ||
− | == | + | == Thread synchronization -- mutex section == |
− | + | In the example below, the code under the '''with''' section will run exclusively. | |
− | + | This is based on an idea by Raymond Hettinger to create a simple way to synchronize threads in a small section of code. It is intended more for spot checking and debugging. The problem with this code is that the interpreter could give another thread execution context between the two instructions, '''sys.setcheckinterval(sys.maxint)''' and '''yield'''. The results would be disaster since the other thread would be running in the interpreter with thread switching 2 billion times less than normal, so it would be the only thread ever allowed to run.The original thread running '''other_threads_suspended''' would never be given a chance to run the '''finally''' block necessary to return the thread switching interval back to normal. | |
− | This is based on an idea by Raymond Hettinger to create a simple way to synchronize threads in a small section of code. It is intended more for spot checking and debugging. | ||
<pre> | <pre> | ||
Line 45: | Line 44: | ||
@contextmanager | @contextmanager | ||
def other_threads_suspended(): | def other_threads_suspended(): | ||
+ | checkinterval_original = sys.getcheckinterval() | ||
try: | try: | ||
sys.setcheckinterval(sys.maxint) | sys.setcheckinterval(sys.maxint) | ||
yield None | yield None | ||
finally: | finally: | ||
− | sys.setcheckinterval( | + | sys.setcheckinterval(checkinterval_original) |
# Example usage: | # Example usage: |
Revision as of 15:54, 13 February 2011
Contents
Pexpect
I loved Expect, but I hated TCL, so I wrote this 100% pure Python module that does the same thing. Click here for more details:
Pexpect -- An Expect-like module in pure Python
Free Python software
You can find other free software I wrote (mostly in Python) here: Free Software
Python Style
PEP-8 gives the recommended style for Python code. It isn't very strict and none of that mixed-case stuff they do in Java: http://www.python.org/dev/peps/pep-0008/
A short summary:
- Class Names
- class names use the CapWords convention. Classes for internal use have a leading underscore in addition.
- Function Names
- function names should be lowercase, with words separated by underscores as necessary to improve readability.
- Line Length
- limit all lines to a maximum of 79 characters.
- Indentation
- use 4 spaces per indentation level. Avoid hard tabs.
- Encoding
- UTF-8 is preferred over Latin-1.
.vimrc for Python
I like Vim. This is how I set my .vimrc file.
This adds sensible tab settings for Python. It also defines simple folding. I never liked the built-in indent folding for Python because it folds too much -- every indent becomes a fold! For code folding I just want to see class names and method names. I don't need every single nested loop folded many levels deep... In this .vimrc see the section on "folding using /search/ pattern". This maps the normal mode key sequence, 'zff' to set the search pattern to find all class and method names. This also maps '\z' to refold every search pattern. The neat thing is that this pattern also works on PHP code! The '\z' mapping is also handy for editing other documents.
Python Cookbook
I have a few recipes on ActiveState's fun and useful Python cookbook.
Thread synchronization -- mutex section
In the example below, the code under the with section will run exclusively. This is based on an idea by Raymond Hettinger to create a simple way to synchronize threads in a small section of code. It is intended more for spot checking and debugging. The problem with this code is that the interpreter could give another thread execution context between the two instructions, sys.setcheckinterval(sys.maxint) and yield. The results would be disaster since the other thread would be running in the interpreter with thread switching 2 billion times less than normal, so it would be the only thread ever allowed to run.The original thread running other_threads_suspended would never be given a chance to run the finally block necessary to return the thread switching interval back to normal.
import sys from contextlib import contextmanager @contextmanager def other_threads_suspended(): checkinterval_original = sys.getcheckinterval() try: sys.setcheckinterval(sys.maxint) yield None finally: sys.setcheckinterval(checkinterval_original) # Example usage: with other_threads_suspended(): print "Hello world!"
GUI toolkits
I hope tkinter will be deprecated someday. I think wxPython is the best alternative at the moment: http://wxpython.org/quotes.php which is a Python interface to wxWidgets.
Other libraries to consider: GTK+ used by Gnome; Qt used by KDE; Pygame wrapper on SDL.
hashlib versus sha (python2.4 vs. python2.5 vs. python3.0)
Python 2.4:
import sha p = 'password' sha.new(p).hexdigest()
Python 2.5:
import hashlib p = 'password' hashlib.sha1(p).hexdigest()
Python 3.0:
import hashlib p = 'password' hashlib.sha1(bytes(p,'utf-8')).hexdigest()
SHA1 for any version of Python
This works on any version of Python that has at least 'sha' or 'hashlib':
def any_sha1_hex_digest (s): try: # Python <= 2.4 import sha return sha.new(s).hexdigest() except ImportError: try: import hashlib if 'bytes' in __builtins__.__dict__: # Python >= 3.0 return hashlib.sha1(bytes(s,'utf-8')).hexdigest() else: # Python >= 2.5 return hashlib.sha1(s).hexdigest() except ImportError: raise ImportError("No module 'sha' or 'hashlib'. You must " + "have at least one of these modules.")
Parse Boolean strings
This will convert boolean strings into Boolean types. This is useful for parsing conf files and the like. This will handle strings like "yes", "1", "true", "no", "false", "0" with any sort of capitalization and whitespace padding. It will raise a ValueError if the string cannot be parsed.
def ParseBoolean (b): # Handle case where b is already a Boolean type. if b == False or b == True: return b b = b.strip() if len(b) < 1: raise ValueError ('Cannot parse empty string into boolean.') b = b[0].lower() if b == 't' or b == 'y' or b == '1': return True if b == 'f' or b == 'n' or b == '0': return False raise ValueError ('Cannot parse string into boolean.')
WTF?
This will test for primality with a regular expression. No, I'm serious...
import re def is_prime (num): return re.match(r"^1?$|^(11+?)\1+$",'1'*num) is None