Most viewed recipes by George Sakkis http://code.activestate.com/recipes/users/2591466/views/2010-01-09T07:14:06-08:00ActiveState Code RecipesProperty decorator for python 2.4 (Python) 2005-04-25T23:32:11-07:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/410698-property-decorator-for-python-24/ <p style="color: grey"> Python recipe 410698 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 3. </p> <p>This recipe refines an older recipe on creating class properties ( <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/205183" rel="nofollow">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/205183</a>). The refinement consists of: - Using a decorator (introduced in python 2.4) to "declare" a function in class scope as property. - Using a trace function to capture the locals() of the decorated function, instead of requiring the latter to return locals(), as in the older recipe.</p> Cache decorator in python 2.4 (Python) 2004-11-01T19:11:21-08:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/325205-cache-decorator-in-python-24/ <p style="color: grey"> Python recipe 325205 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 2. </p> <p>The latest version of Python introduced a new language feature, function and method decorators (PEP 318, <a href="http://www.python.org/peps/pep-0318.html" rel="nofollow">http://www.python.org/peps/pep-0318.html</a>). This recipe show a common callable transformation that can benefit from the new syntax, often referred to as Memoization pattern.</p> Records (Python) 2008-11-04T06:52:42-08:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/576555-records/ <p style="color: grey"> Python recipe 576555 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/namedtuple/">namedtuple</a>, <a href="/recipes/tags/record/">record</a>). </p> <p>This is a recipe similar in functionality and exec-style optimized implementation to the very well received namedtuple (<a href="http://code.activestate.com/recipes/500261/" rel="nofollow">http://code.activestate.com/recipes/500261/</a>) that was included in Python 2.6. The main difference is that <strong>records</strong>, unlike named tuples, are mutable. In addition, fields can have a default value. Instead of subclassing tuple or list, the implementation create a regular class with __slots__.</p> Table indentation (Python) 2004-02-14T03:50:24-08:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/267662-table-indentation/ <p style="color: grey"> Python recipe 267662 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/text/">text</a>). Revision 7. </p> <p>A function for pretty-printing a table.</p> Roundrobin generator (Python) 2007-09-07T19:09:36-07:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/528936-roundrobin-generator/ <p style="color: grey"> Python recipe 528936 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>This recipe implements a round-robin generator, a generator that cycles through N iterables until all of them are exhausted:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; list(roundrobin('abc', [], range(4), (True,False))) ['a', 0, True, 'b', 1, False, 'c', 2, 3] </code></pre> Padding variable length sequences (Python) 2005-03-25T04:27:43-08:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/392768-padding-variable-length-sequences/ <p style="color: grey"> Python recipe 392768 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 2. </p> <p>Python tuple unpacking works only for fixed length sequences, that is one cannot write something like: for (x,y,z=0,*rest) in [(1,2,3), (4,5), (6,7,8,9,10)]: print x,y,z,rest</p> <p>This recipe returns a pad function that implements this functionality (albeit with less concise syntax). Using the recipe, the example above then can be written as: pad = padfactory(minLength=2,defaults=(0,),extraItems=True) for x,y,z,rest in map(pad, [(1,2,3), (4,5), (6,7,8,9,10)]): print x,y,z,rest</p> Yet another threadpool module (Python) 2007-03-25T05:37:06-07:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/502291-yet-another-threadpool-module/ <p style="color: grey"> Python recipe 502291 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 4. </p> <p>A small yet flexible threadpool implementation. I wasn't quite satisfied with other existing solutions, so I rolled one on my own, building on top of the threadpool module by Christopher Arndt.</p> <p>See the example in the end and the docstrings (in Epydoc format) for the details.</p> Implementing Java inner classes using descriptors (Python) 2005-07-08T22:08:04-07:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/409366-implementing-java-inner-classes-using-descriptors/ <p style="color: grey"> Python recipe 409366 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 2. </p> <p>Java programs use extensively "inner" (or nested) classes. There are actually four kinds of such classes (<a href="http://www.unix.org.ua/orelly/java-ent/jnut/ch03_08.htm" rel="nofollow">http://www.unix.org.ua/orelly/java-ent/jnut/ch03_08.htm</a>), namely static and non-static member classes, local classes and anonymous inner classes. Python supports directly the last two through classes defined in nested scopes and lambdas. This recipe implements the first two kinds, static and non-static member classes.</p> <p>A non-static member class is associated with an instance of the outer class and has implicit access to its __dict__. Thus, if the normal attribute lookup fails for an instance of the inner class, it continues to the outer instance.</p> <p>Similarly, a static member class is associated with the outer class, instead of a specific instance of the outer class.</p> <p>In both cases, the outer object (instance or class) can be explicitly accessed though the '__outer__' attribute.</p> Handling ties for top largest/smallest elements (Python) 2009-04-07T18:57:35-07:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/576712-handling-ties-for-top-largestsmallest-elements/ <p style="color: grey"> Python recipe 576712 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/heapq/">heapq</a>, <a href="/recipes/tags/largest/">largest</a>, <a href="/recipes/tags/smallest/">smallest</a>, <a href="/recipes/tags/top/">top</a>). Revision 8. </p> <p>The heapq module provides efficient functions for getting the top-N smallest and largest elements of an iterable. A caveat of these functions is that if there are ties (i.e. equal elements with respect to the comparison key), some elements may end up in the returned top-N list while some equal others may not:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; nsmallest(3, [4,3,-2,-3,2], key=abs) [-2, 2, 3] </code></pre> <p>Although 3 and -3 are equal with respect to the key function, only one of them is chosen to be returned. For several applications, an all-or-nothing approach with respect to ties is preferable or even required.</p> <p>A new optional boolean parameter 'ties' is proposed to accomodate these cases. If ties=True and the iterable contains more than N elements, the length of the returned sorted list can be lower than N if not all ties at the last position can fit in the list:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; nsmallest(3, [4,3,-2,-3,2], key=abs, ties=True) [-2, 2] </code></pre> Generic block iterator (Python) 2008-01-17T03:55:45-08:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/521877-generic-block-iterator/ <p style="color: grey"> Python recipe 521877 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 4. </p> <p>A common task, especially in text processing, is to break some input sequence into chunks (lines, paragraphs, records, etc.) and process them one by one. The iterators of builtin strings and files can be considered such chunkers, breaking the object into characters and lines respectively. This recipe is a generic way to break any iterable into consecutive blocks, specified by a delimiter or a pair of (start,end) delimiters.</p> Rich iterator wrapper (Python) 2006-11-19T00:03:21-08:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/498272-rich-iterator-wrapper/ <p style="color: grey"> Python recipe 498272 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/oop/">oop</a>). </p> <p>This recipe may be of interest to those who make heavy use of the itertools module. It provides a wrapper class that exposes most itertools functions as methods, plus a few more. Moreover, two frequently used itertools functions, chain and islice, are conveniently exposed as addition and slicing operators, respectively.</p> Reusing default function arguments (Python) 2005-10-12T20:37:53-07:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/440702-reusing-default-function-arguments/ <p style="color: grey"> Python recipe 440702 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 2. </p> <p>This recipe applies the "once and only once" principle to function default values. Often two or more callables specify the same default values for one or more arguments. This is especially typical when overriding a method. Using the defaultsfrom(func) decorator, a method may 'inherit' the default values from the super method. More generally, any function may inherit the default values from another one.</p> Introspecting call arguments (Python) 2009-05-10T11:32:18-07:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/551779-introspecting-call-arguments/ <p style="color: grey"> Python recipe 551779 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 2. </p> <p>This recipe implements in pure Python the algorithm used by the interpreter for binding the values passed as parameters in a function call to the formal arguments of the function.</p> <p>This is useful for decorators that want to take into account this binding when wrapping a function (e.g. for type checking).</p> <p>Edit (2008/5/10): Added a second returned value, missing_args: a tuple of the formal parameters whose value was not provided (i.e. those using the respective default value). This is useful in cases where one want to distinguish f() from f(None) given "def f(x=None):".</p> Generating combinations in blocks (Python) 2007-01-26T02:30:08-08:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/501151-generating-combinations-in-blocks/ <p style="color: grey"> Python recipe 501151 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 2. </p> <p>This recipe is yet another combination generator, with an extra twist: the generated combinations can be yielded in blocks of a given size. This comes useful (or necessary) when processing arbitrarily long or infinite iterables in bounded-size blocks.</p> Fixed keys mapping type (Python) 2007-01-12T00:29:57-08:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/499373-fixed-keys-mapping-type/ <p style="color: grey"> Python recipe 499373 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 3. </p> <p>A memory efficient implementation of a mapping type for mappings with fixed keys.</p> Browser history data structure (Python) 2009-12-25T17:26:57-08:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/576991-browser-history-data-structure/ <p style="color: grey"> Python recipe 576991 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/datastuctures/">datastuctures</a>). Revision 6. </p> <p>The <code>BrowserHistory</code> class encapsulates the history of moving from location to location, as in Web browsing context; the recipe is not restricted to Web browsing though. See docstrings for more details and usage.</p> <p>The current implementation requires Python 2.6.</p> Safe heap queue class (Python) 2005-10-09T14:57:08-07:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/440673-safe-heap-queue-class/ <p style="color: grey"> Python recipe 440673 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 3. </p> <p>The Heap class is an improvement over a previous recipe (<a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/437116" rel="nofollow">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/437116</a>) that also wraps the heapq module. There are two main differences from the other recipe: - All methods on this heap preserve the heap property invariant; therefore there is no need for is_heap(). - When creating a new heap, an optional 'key' argument can be specified to determine the comparison key for the items to be pushed into the heap.</p> Re-evaluatable default argument expressions (Python) 2007-02-04T05:04:41-08:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/502206-re-evaluatable-default-argument-expressions/ <p style="color: grey"> Python recipe 502206 by <a href="/recipes/users/2591466/">George Sakkis</a> . Revision 2. </p> <p>This recipe extends the standard python semantics with respect to default function arguments by allowing "deferred" expressions, expressions that are evaluated on every call instead of just once at function definition time.</p> Context manager for restoring a value (Python) 2010-01-09T07:14:06-08:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/576977-context-manager-for-restoring-a-value/ <p style="color: grey"> Python recipe 576977 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/contextlib/">contextlib</a>, <a href="/recipes/tags/context_manager/">context_manager</a>). Revision 8. </p> <p>Often one wants to rebind a name or modify a mutable object, perform a bunch of actions and finally restore the name/object to its original state. An example is redirecting stdout/stderr temporarily (<a href="http://www.diveintopython.org/scripts_and_streams/stdin_stdout_stderr.html" rel="nofollow">http://www.diveintopython.org/scripts_and_streams/stdin_stdout_stderr.html</a>). The <em>restoring</em> context manager shown below simplifies this pattern::</p> <pre class="prettyprint"><code>import sys # prints in console print "hello world!" with restoring('sys.stdout'): with open('hello.txt', 'w') as sys.stdout: # prints in file print "hello world!" # prints in console again print "hello world!" </code></pre> Iterating over fixed size blocks (Python) 2008-01-12T17:58:28-08:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/542194-iterating-over-fixed-size-blocks/ <p style="color: grey"> Python recipe 542194 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>This recipe shows a generator that breaks an iterable into chunks of fixed size. It addresses the general use case of having to (or wanting to) constrain the number of items to be processed at a time, for example because of resource limitations. It can very easily wrap blocks of code that work on iterables: just replace &lt;pre&gt;process(all_items)&lt;/pre&gt;with &lt;pre&gt;for some_items in iterblock(all_items, 100): process(some_items)&lt;/pre&gt;</p>