Walter Mundt's Python bits and pieces.
This contains various bits of Python code I have written on my own time and find handy.
Contents:
nat_sort_keycan be used forkey=insortorsorted, and sorts strings with embedded numbers in a way that attempts to make sense.io_pipeis awithcontext manager to set up anos.piper/w pair wrapped in unbufferedio-module file objects, that is closed on exit from the block.WatchedThreadis a Thread subclass that tries to make it easier to gracefully handle uncaught exceptions in thread targets, dying messily by default and offering easy ways to re-raise exceptions across the thread boundary.ArgSpecis a wrapper aroundinspect.getargspecthat provides a defaults dict instead of a defaults list, and can invoke the inspected function from a dict containing all the arguments to pass (INCLUDING a sequence keyed by the name of the*varargs). This is not always a great idea, but it's handy for:ParserGeneratorinstances are decorators that automatically buildargparse.ArgumentParserinstances based on a function's signature, and attach them to the function asparserattributes. They also tack on a bunch of info about said arguments, and aparse_and_callmethod that takes an argv and calls the decorated function if the argv parses successfully. One can pass keyword arguments to the decorator in order to customize the syntax, add help text, or do anything else an argparseadd_argumentcan do.
Tests and examples are currently missing, since I'm using this stuff mostly in other code I haven't gotten into a publishable state yet. Feel free to use it if it is useful, but YMMV.
wmpy is Copyright (c) 2012 Walter Mundt. It is available under an MIT license; see the LICENSE file for details.