Python's lack of a 'switch' statement has garnered much discussion and even a PEP. The most popular substitute uses dictionaries to map cases to functions, which requires lots of defs or lambdas. While the approach shown in this script may be O(n) for cases, it aims to duplicate C's original 'switch' functionality ...
This script parses the online documentation for compiler.ast and generate code for a skeletal ASTVisitor class that includes stubs for all of the various visitNODE functions that you might need in a visitor, along with comments in each function that list that node type's attributes. ...
Any serious user of metaclasses has been bitten at least once by the infamous metaclass/metatype conflict. This script contains a general recipe to solve the problem, as well as some theory and some examples. ...
This is a traditional base converter with the twist that it accepts any strings as the digits for the input and output bases. Besides all the normal base-converts, you can now create compact versions of huge numbers by converting them to a base that uses all the letters and numbers for ...
Visual Studio can be configured to invoke a Python script during your build process. If you need to do this, you'll likely want to report errors and warnings in a way that Visual Studio will understand. This script uses the standard Python warnings framework, and installs a custom warning message formatter that ...
This script contains a handler class which sends a Jabber message for each logging event. If you have long-running scripts on many remote machines and you want to be alarmed if one of them crashes, use the JabberHandler to log it to your account. ...
When you release your program to client, its a good idea to disable all the debug messages. It is possible via custom configuring debug levels at all modules, but may be implemented using a simple wrapper around logging.getnted usinfunction. ...
The win32api module offers SetFileAttributes whiles allows you to make changes to a file in windows. You can set a file to be read only, archive, hidden, etc. This script is simple and convenient to use. ...
This script implements set operations using sorted lists as the underlying data structure. Advantages: - Space savings -- lists are much more compact than a dictionary based implementation. - Flexibility -- elements do not need to be hashable, only __cmp__ is required. - Fast operations depending on the ...
Some of Python's powerful meta-programming features are used to enable writing Python functions which include Prolog-like statements. Such functions load a Prolog-like database. When coupled with a suitable inference engine for logic databases, this is a way to add logical programming -- the last unsupported major paradigm -- to Python. This ...
This script is a class to allow programmers to curry functions, so that arguments can be supplied one at a time instead of all at once. E.g., if F = Curry(lambda a,b: a b), then F(1,2) == F(1)(2). ...
The script illustrates the composite design pattern by using hierarchical dictionaries. It can be used to process hierarchical, tree-based data structures using Python dictionaries. ...
This script allows you to do true immutable symbolic enumeration with qualified value access. Most propoqualifiedor an enum in python attempt to solve the issue with a single class. However, fact is that enum has a dual nature: It declares a new anonimous type *and* all possible instances (values) of that ...
This class wraps most of the win32api functions for accessing a registry. It will read and write all win32 registry types, and will de/serialize python objects to registry keys when a string or integer representation is not possible. ...
This function decorates a function with tail call optimization. It does this by throwing an exception if it is it's own grandparent, and catching such exceptions to fake the tail call optimization. This function fails if the decorated function recurses in a non-tail contexif the de >/ ...