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_key
can be used forkey=
insort
orsorted
, and sorts strings with embedded numbers in a way that attempts to make sense.io_pipe
is awith
context manager to set up anos.pipe
r/w pair wrapped in unbufferedio
-module file objects, that is closed on exit from the block.WatchedThread
is 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.ArgSpec
is a wrapper aroundinspect.getargspec
that 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:ParserGenerator
instances are decorators that automatically buildargparse.ArgumentParser
instances based on a function's signature, and attach them to the function asparser
attributes. They also tack on a bunch of info about said arguments, and aparse_and_call
method 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_argument
can 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.