Popular recipes by Alan Franzoni http://code.activestate.com/recipes/users/4169882/2013-06-11T08:00:25-07:00ActiveState Code RecipesTruth Value Aware Iterable (Python)
2013-06-11T08:00:25-07:00Alan Franzonihttp://code.activestate.com/recipes/users/4169882/http://code.activestate.com/recipes/578549-truth-value-aware-iterable/
<p style="color: grey">
Python
recipe 578549
by <a href="/recipes/users/4169882/">Alan Franzoni</a>
(<a href="/recipes/tags/boolean/">boolean</a>, <a href="/recipes/tags/iterable/">iterable</a>, <a href="/recipes/tags/truth/">truth</a>).
</p>
<p>This small recipe enables truth value testing on iterables.</p>
<p>It is quite common to do things like:</p>
<pre class="prettyprint"><code>if somesequence:
...
else:
...
</code></pre>
<p>Such constructs, that enter the if block if the sequence's got one or more elements and the else block if it's empty, work fine on non-lazy builtin sequences (lists, strings, tuples) and dictionaries as well, but doesn't necessarily work on generic iterables - most of them are always true regardless of their contents, since they're some kind of object. A classical example is generators, but such behaviour can be extended to any object implementing the Iterable interface.</p>
<p>Just wrap your iterable with this decorator and you'll get a truth-aware iterable which supports proper truth testing by doing a small first element prefetching and can then be used just like the original iterable.</p>
Async subprocess check_output replacement for Twisted (Python)
2012-01-20T09:48:27-08:00Alan Franzonihttp://code.activestate.com/recipes/users/4169882/http://code.activestate.com/recipes/578021-async-subprocess-check_output-replacement-for-twis/
<p style="color: grey">
Python
recipe 578021
by <a href="/recipes/users/4169882/">Alan Franzoni</a>
(<a href="/recipes/tags/check_output/">check_output</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/spawnprocess/">spawnprocess</a>, <a href="/recipes/tags/subprocess/">subprocess</a>, <a href="/recipes/tags/twisted/">twisted</a>).
</p>
<p>As any twisted user knows, the original python subprocess module can yield to interferences with twisted's own reactor - at least unless installSignalHandlers is false, which can lead to other consequences. </p>
<p>This recipe simulates a stripped down version of subprocess.check_output() which returns a deferred and is twisted friendly.</p>
Maybe pattern (Python)
2010-07-21T11:08:11-07:00Alan Franzonihttp://code.activestate.com/recipes/users/4169882/http://code.activestate.com/recipes/577248-maybe-pattern/
<p style="color: grey">
Python
recipe 577248
by <a href="/recipes/users/4169882/">Alan Franzoni</a>
(<a href="/recipes/tags/design/">design</a>, <a href="/recipes/tags/maybe/">maybe</a>, <a href="/recipes/tags/pattern/">pattern</a>).
Revision 2.
</p>
<p>An example Maybe pattern implementation for Python.</p>
Minimal Dependency Injection Container (Python)
2010-06-05T13:50:03-07:00Alan Franzonihttp://code.activestate.com/recipes/users/4169882/http://code.activestate.com/recipes/577254-minimal-dependency-injection-container/
<p style="color: grey">
Python
recipe 577254
by <a href="/recipes/users/4169882/">Alan Franzoni</a>
(<a href="/recipes/tags/container/">container</a>, <a href="/recipes/tags/control/">control</a>, <a href="/recipes/tags/dependency/">dependency</a>, <a href="/recipes/tags/di/">di</a>, <a href="/recipes/tags/dic/">dic</a>, <a href="/recipes/tags/injection/">injection</a>, <a href="/recipes/tags/inversion/">inversion</a>, <a href="/recipes/tags/ioc/">ioc</a>, <a href="/recipes/tags/iocc/">iocc</a>).
</p>
<p>An example of a minimal dependency injection ( aka Inversion of Control ) container for Python.</p>
Twisted Public Deferred (Python)
2009-12-08T09:47:16-08:00Alan Franzonihttp://code.activestate.com/recipes/users/4169882/http://code.activestate.com/recipes/576978-twisted-public-deferred/
<p style="color: grey">
Python
recipe 576978
by <a href="/recipes/users/4169882/">Alan Franzoni</a>
(<a href="/recipes/tags/deferred/">deferred</a>, <a href="/recipes/tags/twisted/">twisted</a>).
Revision 5.
</p>
<p>This small piece of code helps separating the "public" and "private" interface parts of deferred objects in Twisted.</p>
<p>Although it might not be really useful in production code, it is great for newcomers who are still learning the framework.</p>