Popular recipes by George Sakkis http://code.activestate.com/recipes/users/2591466/2010-04-21T20:16:09-07:00ActiveState Code RecipesSimultaneous topdown and bottomup variant of os.walk() (alt. title: "Delete .pyc files and empty directories recursively") (Python)
2010-04-21T20:16:09-07:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/577206-simultaneous-topdown-and-bottomup-variant-of-oswal/
<p style="color: grey">
Python
recipe 577206
by <a href="/recipes/users/2591466/">George Sakkis</a>
(<a href="/recipes/tags/os_walk/">os_walk</a>).
</p>
<p>The standard lib os.walk() function provides a topdown parameter that determines whether entries are yielded in a top-down or a bottom-up order. Sometimes though you may want each directory yielded twice; once before any of its children directories (and recursively their descendants) are yielded and once after they are all yielded. The walk2() function below does this by yielding 4-tuples; the first 3 elements are the same yielded by os.walk() and the 4th is True the first time (topdown) and False the second (bottomup).</p>
<p>An example is deleting all .pyc files and empty directories under some root dir, but excluding specific directories (e.g. VCS specific dirs). The exclusion check should be done topdown (we don't want to descend into any directory that must be excluded) but the check for empty directories has to be done bottom up, since a directory containing only .pyc files will be non-empty initially but empty after removing the files.</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>
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>
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>
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>>>> 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>>>> nsmallest(3, [4,3,-2,-3,2], key=abs, ties=True)
[-2, 2]
</code></pre>
Yet another Null object (Python)
2008-11-11T15:24:20-08:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/576562-yet-another-null-object/
<p style="color: grey">
Python
recipe 576562
by <a href="/recipes/users/2591466/">George Sakkis</a>
(<a href="/recipes/tags/design_pattern/">design_pattern</a>, <a href="/recipes/tags/dummy/">dummy</a>, <a href="/recipes/tags/null/">null</a>).
</p>
<p>This recipe builds on two previously posted recipes for a <a href="http://code.activestate.com/recipes/68205/">null</a> or <a href="http://code.activestate.com/recipes/576447/">dummy</a> object by modifying a few methods (e.g. as in SQL, <code>Null == Null</code> is Null, not True), supporting most (all?) special methods (e.g. int(Null)) and providing correct pickling/unpickling.</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>
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
<pre>process(all_items)</pre>with
<pre>for some_items in iterblock(all_items, 100):
process(some_items)</pre></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>>>> list(roundrobin('abc', [], range(4), (True,False)))
['a', 0, True, 'b', 1, False, 'c', 2, 3]
</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>
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>
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>
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>
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>
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>
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>
Instantiating pickled instances transparently (Python)
2005-09-19T22:26:08-07:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/440579-instantiating-pickled-instances-transparently/
<p style="color: grey">
Python
recipe 440579
by <a href="/recipes/users/2591466/">George Sakkis</a>
(<a href="/recipes/tags/oop/">oop</a>).
</p>
<p>This recipe addresses the following two needs:
- Object construction for some class is expensive.
- Objects of this class need to be instantiated across multiple runs of the program.
For example, object instantiaton may involve reading one or more big files, connecting to a database or a network socket with considerable expected delay, etc.</p>
<p>The autopickle decorator deals with this problem by wrapping the __init__ of the class. The first time a specific instance is created, it is also pickled to a file. In all subsequent attempts to create the same instance, the pickled instance is loaded and returned instead. If unpickling the file is faster than creating the instance normally, all but the first instantiations are faster than the normal one.</p>
<p>The instance determines the path of the file to be pickled to by calling its getPickleFilename() method. This takes the same arguments given in __init__ and it has the responsibility to specify a valid and distinct path for the instance.</p>
Property 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>
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>