Popular recipes by Shannon -jj Behrens http://code.activestate.com/recipes/users/2269827/2008-08-08T23:48:22-07:00ActiveState Code RecipesWalkRecursiveGenerators (Python)
2007-11-29T19:20:33-08:00Shannon -jj Behrenshttp://code.activestate.com/recipes/users/2269827/http://code.activestate.com/recipes/535145-walkrecursivegenerators/
<p style="color: grey">
Python
recipe 535145
by <a href="/recipes/users/2269827/">Shannon -jj Behrens</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 3.
</p>
<p>Walk a tree of generators without yielding things recursively. Check out the docstring. It has an example ;)</p>
GroupBySorted (Python)
2008-08-08T23:48:22-07:00Shannon -jj Behrenshttp://code.activestate.com/recipes/users/2269827/http://code.activestate.com/recipes/499379-groupbysorted/
<p style="color: grey">
Python
recipe 499379
by <a href="/recipes/users/2269827/">Shannon -jj Behrens</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 5.
</p>
<p>Updated: Use itertools.groupby instead. See my comment below.</p>
<p>This is a variation of itertools.groupby. The itertools.groupby iterator assumes that the input is not sorted but will fit in memory. This iterator has the same API, but assumes the opposite.</p>
ProtectUTF8 (Python)
2006-04-28T06:46:25-07:00Shannon -jj Behrenshttp://code.activestate.com/recipes/users/2269827/http://code.activestate.com/recipes/492223-protectutf8/
<p style="color: grey">
Python
recipe 492223
by <a href="/recipes/users/2269827/">Shannon -jj Behrens</a>
(<a href="/recipes/tags/text/">text</a>).
</p>
<p>protect_utf8 is a function decorator that can prevent naive
functions from breaking UTF-8.</p>
TryFinally (Python)
2005-10-17T22:37:51-07:00Shannon -jj Behrenshttp://code.activestate.com/recipes/users/2269827/http://code.activestate.com/recipes/442419-tryfinally/
<p style="color: grey">
Python
recipe 442419
by <a href="/recipes/users/2269827/">Shannon -jj Behrens</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>This is a convenient way to deeply nest try/finally statements.</p>
RunningCalc (Python)
2005-02-18T23:22:55-08:00Shannon -jj Behrenshttp://code.activestate.com/recipes/users/2269827/http://code.activestate.com/recipes/363786-runningcalc/
<p style="color: grey">
Python
recipe 363786
by <a href="/recipes/users/2269827/">Shannon -jj Behrens</a>
(<a href="/recipes/tags/programs/">programs</a>).
Revision 2.
</p>
<p>This is a trivial calculator "shell" with a running total.
As trivial as it is, I find it to be more useful than a normal calculator when
doing my checkbook because of the ever-present running total.</p>
<p>Sed is to Vi as
RunningCalc is to Python</p>
<p>python ~/programming/python/hacks/RunningCalc.py
$ 0.0+50
$ 50.0-10
$ 40.0*0
$ 0.0+5
$ 5.0-2
$ 3.0</p>
FunctionDecorators (Python)
2005-02-15T02:48:07-08:00Shannon -jj Behrenshttp://code.activestate.com/recipes/users/2269827/http://code.activestate.com/recipes/363779-functiondecorators/
<p style="color: grey">
Python
recipe 363779
by <a href="/recipes/users/2269827/">Shannon -jj Behrens</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
Revision 2.
</p>
<p>Use a function decorator to tell when and how a function should be
logged. All kinds of things can be logged automatically, including the
function arguments and (theoretically) even the stack.</p>
InverseExtendGenerators (Python)
2005-01-19T18:46:46-08:00Shannon -jj Behrenshttp://code.activestate.com/recipes/users/2269827/http://code.activestate.com/recipes/363782-inverseextendgenerators/
<p style="color: grey">
Python
recipe 363782
by <a href="/recipes/users/2269827/">Shannon -jj Behrens</a>
(<a href="/recipes/tags/oop/">oop</a>).
Revision 2.
</p>
<p>This is the same as InverseExtend, except it uses a yield for
non-local flow of control in a way yield wasn't really meant to be used ;)</p>
AutoLoader (Python)
2005-01-19T18:34:03-08:00Shannon -jj Behrenshttp://code.activestate.com/recipes/users/2269827/http://code.activestate.com/recipes/363772-autoloader/
<p style="color: grey">
Python
recipe 363772
by <a href="/recipes/users/2269827/">Shannon -jj Behrens</a>
(<a href="/recipes/tags/oop/">oop</a>).
Revision 4.
</p>
<p>If you have an instance "db", then accessing "db.Foo"
automatically imports, instantiates, caches, and returns an instance of "Foo"
from the "db" package.</p>
ThreadSafeProxy (Python)
2005-01-19T10:16:49-08:00Shannon -jj Behrenshttp://code.activestate.com/recipes/users/2269827/http://code.activestate.com/recipes/363777-threadsafeproxy/
<p style="color: grey">
Python
recipe 363777
by <a href="/recipes/users/2269827/">Shannon -jj Behrens</a>
(<a href="/recipes/tags/threads/">threads</a>).
</p>
<p>This is a proxy for an instance of some class called Foo. Any
thread can use the one proxy, and the proxy will automatically look up the
thread specific instance of Foo. This is great if you have a global function
in someone else's code that you need to swap out for something "thread safe".</p>
InitReplacement (Python)
2005-01-19T10:54:34-08:00Shannon -jj Behrenshttp://code.activestate.com/recipes/users/2269827/http://code.activestate.com/recipes/363785-initreplacement/
<p style="color: grey">
Python
recipe 363785
by <a href="/recipes/users/2269827/">Shannon -jj Behrens</a>
(<a href="/recipes/tags/oop/">oop</a>).
</p>
<p>Hack a class's __init__ method without subclassing the class
because a) you can't modify the original class and b) you can't modify other
classes already using the first class. This is a gross hack that should only
be used to work around flaws in libraries you have no control over. I've
changed the names to protect the innocent.</p>
ClearModules (Python)
2005-01-19T17:55:17-08:00Shannon -jj Behrenshttp://code.activestate.com/recipes/users/2269827/http://code.activestate.com/recipes/363774-clearmodules/
<p style="color: grey">
Python
recipe 363774
by <a href="/recipes/users/2269827/">Shannon -jj Behrens</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 3.
</p>
<p>Clear "sys.modules" of stale code without having to restart your
server. It's a hell of a lot harder to do right then it sounds.</p>
Forward (Python)
2005-01-19T10:20:35-08:00Shannon -jj Behrenshttp://code.activestate.com/recipes/users/2269827/http://code.activestate.com/recipes/363778-forward/
<p style="color: grey">
Python
recipe 363778
by <a href="/recipes/users/2269827/">Shannon -jj Behrens</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
</p>
<p>Calling the "forward" method transfers processing to a new screen.
It never returns. This code is taken from multiple places, so I haven't
bothered to keep the classes intact, I've just taken the meat. More generally,
this is an example of state machine with a main loop. At any point, you can
say, "I'm ready to change state. Do it. Go back to the loop. Don't
return--just throw away my current stack."</p>
PackagePath (Python)
2005-01-19T18:40:54-08:00Shannon -jj Behrenshttp://code.activestate.com/recipes/users/2269827/http://code.activestate.com/recipes/363781-packagepath/
<p style="color: grey">
Python
recipe 363781
by <a href="/recipes/users/2269827/">Shannon -jj Behrens</a>
(<a href="/recipes/tags/oop/">oop</a>).
Revision 2.
</p>
<p>If you have a hierarchy of packages in a library, permit the user
of your library to have his own hierarchy of packages that "overlays" yours.
That means he can even have classes named the same as your classes, and have
his classes "shadow" yours. It also means that a project can be broken up into
several top-level directories, all of which have the same package hierarchy
within.</p>
Curry (Python)
2005-01-19T18:02:22-08:00Shannon -jj Behrenshttp://code.activestate.com/recipes/users/2269827/http://code.activestate.com/recipes/363775-curry/
<p style="color: grey">
Python
recipe 363775
by <a href="/recipes/users/2269827/">Shannon -jj Behrens</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
Revision 2.
</p>
<p>Suppose you have a function "sum(a, b)". This class lets you
do things like:</p>
<pre class="prettyprint"><code>plus4 = Curry(sum, 4)
print plus4(5)
</code></pre>
AutomaticClass (Python)
2005-01-19T17:50:41-08:00Shannon -jj Behrenshttp://code.activestate.com/recipes/users/2269827/http://code.activestate.com/recipes/363773-automaticclass/
<p style="color: grey">
Python
recipe 363773
by <a href="/recipes/users/2269827/">Shannon -jj Behrens</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
Revision 2.
</p>
<p>BASIC IDEA: Using this class as a base class, you can quickly create new
classes that know what its attributes are. This is useful for simple,
struct-like classes that have no methods of their own. Afterall, class syntax
is more convenient than dict syntax.</p>
InverseExtend (Python)
2005-01-19T10:46:22-08:00Shannon -jj Behrenshttp://code.activestate.com/recipes/users/2269827/http://code.activestate.com/recipes/363783-inverseextend/
<p style="color: grey">
Python
recipe 363783
by <a href="/recipes/users/2269827/">Shannon -jj Behrens</a>
(<a href="/recipes/tags/oop/">oop</a>).
</p>
<p>This is like the object oriented concept of extending a super
class. However, instead of starting at the furthest subclass and working your
way up, you start at the top super class and work your way down.</p>
LazyGettext (Python)
2005-01-19T10:50:26-08:00Shannon -jj Behrenshttp://code.activestate.com/recipes/users/2269827/http://code.activestate.com/recipes/363784-lazygettext/
<p style="color: grey">
Python
recipe 363784
by <a href="/recipes/users/2269827/">Shannon -jj Behrens</a>
(<a href="/recipes/tags/oop/">oop</a>).
</p>
<p>In some code I have to work with but don't have much control over,
there are a bunch of strings declared at the module level. I need to figure
out what all those strings are, and wrap them in something. Once they are
wrapped, they must behave as strings, but lazily "translate" themselves
whenever used in order for internationalization to work. Since each Web
request might request a different language, I can't just do this once and be
done with it.</p>
HasFriends (Python)
2005-01-19T10:35:19-08:00Shannon -jj Behrenshttp://code.activestate.com/recipes/users/2269827/http://code.activestate.com/recipes/363780-hasfriends/
<p style="color: grey">
Python
recipe 363780
by <a href="/recipes/users/2269827/">Shannon -jj Behrens</a>
(<a href="/recipes/tags/oop/">oop</a>).
</p>
<p>Permit anyone to add methods to a class in a library without having
to change the name of the class by subclassing. This is like having a plugins
directory. Also, given a normal function, treat it as a method of an arbitrary
class (i.e. mimic a bound method).</p>
ExtendedIfElse (Python)
2005-01-19T10:11:13-08:00Shannon -jj Behrenshttp://code.activestate.com/recipes/users/2269827/http://code.activestate.com/recipes/363776-extendedifelse/
<p style="color: grey">
Python
recipe 363776
by <a href="/recipes/users/2269827/">Shannon -jj Behrens</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
</p>
<p>A million things can go wrong, and there are several ways to
succeed. You want to check for the ways that can succeed and continue on as
soon as one of them does. You want the non-local flow of control that
exceptions provide, but you need it for both success <em>and</em> failure.</p>