Recipes
All of the following are published as "public domain", or, if you prefer, under the MIT license. Some of my older recipes can be found on the Python Cookbook
| Property Classes | posted on May 14, 2011 |
| Tired of creating properties the old way? Python 3 brings an improvement in the form of multi-stage properties, i.e., @property def foo(self): ... # getter @foo.setter def foo(self, value): ...... | |
| ctypes - Pointer from Address | posted on April 27, 2011 |
| There are times you need to construct a ctypes pointer from an integer address you have, say the id of a python object. I scratched my head for quite a... | |
| Copy Function Defaults | posted on February 06, 2007 |
| Default arguments to functions are evaluated when the function is created, and are stored in the function object. This cause irritating problems when the default values are in fact mutable,... | |
| Weak Methods | posted on September 29, 2006 |
| Many times you would pass bound methods to other functions, as callbacks (i.e., to simulate events, etc.). However, bound methods (the instancemethod type) hold a strong reference to their owning... | |
| Real Mixins | posted on September 22, 2006 |
| The normal python paradigm for implementing mixins is using multiple inheritance. Mixin classes take some measures of precaution as of their design (not to interfere with the derivee's MRO as... | |
| Hooking dir() | posted on September 07, 2006 |
| While working with proxy objects I found introspection quite hard, as the mechanism of dir() just looks at the object's __dict__ (with some exceptions), and there's no way to customize... | |
| Module Mixins a la Ruby | posted on August 29, 2006 |
| Multiple inheritance is usually considered bad, and many ways to avoid it have been developed. The first and most notable is interfaces, which are basically abstract classes (only declaration, no... | |
| Killable Threads | posted on August 13, 2006 |
| The thread2 module is an extension of the standard threading module, and provides the means to raise exceptions at the context of the given thread. You can use raise_exc() to... | |
| HTML Codec | posted on July 26, 2006 |
| A very simple and straight-forward text/HTML codec. When encoding text, it escapes all HTML-delimiters (< becomes <, etc.), so the encoded text can be safely viewed by an HTML renderer... | |
| Weak Attributes | posted on June 04, 2006 |
| Weak attributes are attributes of an instance that hold only a weak reference to another object. They are very useful for automatic breaking of cyclic references. This weakattr class implements... | |
| SeqAttr | posted on June 03, 2006 |
| Sequenced attributes are useful for types that behave as sequences, but provide direct attribute access. For examples of such types, see time.struct_time of the result of os.fstat. Note: since the... | |
| HElement | posted on May 30, 2006 |
| HElement, or HTML-element, is a simple, straight-forward, and non-magical HTML generation engine. An HElement is made of two parts: attrs - the attributes of the tag, and elems - the... | |
| AttrDict | posted on May 29, 2006 |
| A dictionary that can be accessed with the attribute notation, as well as the item notation: using AttrDict, a.b is the same as a["b"]. Also, iterating over an AttrDict yields... | |
| ExceptionContainer | posted on May 28, 2006 |
| An exception baseclass inspired by Container, which takes keyword arguments instead of positional ones, and provides pretty-printing in tracebacks. Code # # module ExceptionContainer.py # requires the Container module #... | |
| Container | posted on May 27, 2006 |
| A general container of attributes, with support for pretty printing. Very useful for debugging, etc. Used extensively in Construct to represent parsed objects. Code # # module Container.py # def... | |
| Templite | posted on May 26, 2006 |
| Templite is a fast, light-weight, general purpose, and fully featured templating engine in ~40 lines of code. Unlike many templating engines, this one is not specific to any format (HTML,... | |
