Popular recipes by Sam Denton http://code.activestate.com/recipes/users/4172262/2014-02-13T20:55:55-08:00ActiveState Code RecipesTopological Sort (Python) 2012-09-27T12:21:23-07:00Sam Dentonhttp://code.activestate.com/recipes/users/4172262/http://code.activestate.com/recipes/578272-topological-sort/ <p style="color: grey"> Python recipe 578272 by <a href="/recipes/users/4172262/">Sam Denton</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/sort/">sort</a>, <a href="/recipes/tags/sorting/">sorting</a>, <a href="/recipes/tags/topological/">topological</a>, <a href="/recipes/tags/toposort/">toposort</a>). </p> <p>A topological sort (sometimes abbreviated topsort or toposort) or topological ordering of a directed graph is a linear ordering of its vertices such that, for every edge uv, u comes before v in the ordering. For instance, the vertices of the graph may represent tasks to be performed, and the edges may represent constraints that one task must be performed before another; in this application, a topological ordering is just a valid sequence for the tasks. A topological ordering is possible if and only if the graph has no directed cycles, that is, if it is a directed acyclic graph (DAG).</p> String-like partition and rpartition functions for lists (Python) 2013-01-30T06:44:03-08:00Sam Dentonhttp://code.activestate.com/recipes/users/4172262/http://code.activestate.com/recipes/578442-string-like-partition-and-rpartition-functions-for/ <p style="color: grey"> Python recipe 578442 by <a href="/recipes/users/4172262/">Sam Denton</a> . </p> <p>Python 2.5 added the partition and rpartition methods for strings, but nothing similar for lists. These two functions provide that, with the addition of an optional key parameter with the same functionality as in itertools.groupby(. </p> Using doctests to verify a module's export list (Python) 2012-09-19T19:13:20-07:00Sam Dentonhttp://code.activestate.com/recipes/users/4172262/http://code.activestate.com/recipes/578266-using-doctests-to-verify-a-modules-export-list/ <p style="color: grey"> Python recipe 578266 by <a href="/recipes/users/4172262/">Sam Denton</a> (<a href="/recipes/tags/doctest/">doctest</a>, <a href="/recipes/tags/export/">export</a>, <a href="/recipes/tags/module/">module</a>, <a href="/recipes/tags/python/">python</a>). Revision 2. </p> <p>If you aren't very careful, modules can end up exporting more symbols than you intend. For example, everything that you import is added to your module's name-space. Then there's scaffolding and other stuff intended for internal use. The usual advice is to name everything with a leading underscore, but that gets complicated fast: "import sys as _sys", "from os import environ as _environ". The alternative is to use "__all__ " to define exactly what you want to export, but then you need to maintain it as you add things to your module. <em>Or do you?</em></p> "public" decorator, adds an item to __all__ (Python) 2009-12-30T12:53:29-08:00Sam Dentonhttp://code.activestate.com/recipes/users/4172262/http://code.activestate.com/recipes/576993-public-decorator-adds-an-item-to-__all__/ <p style="color: grey"> Python recipe 576993 by <a href="/recipes/users/4172262/">Sam Denton</a> (<a href="/recipes/tags/decorators/">decorators</a>). </p> <p>The DRY principle says, "Don't repeat yourself". A Python module typically defines a global variable named "__all__" that holds the names of everything the author wants visible. This means you have to type each function or class name a second time, and you have to maintain the contents of __all__ manually. This fixes that.</p> Simple polynomial class (Python) 2014-02-13T20:55:55-08:00Sam Dentonhttp://code.activestate.com/recipes/users/4172262/http://code.activestate.com/recipes/576953-simple-polynomial-class/ <p style="color: grey"> Python recipe 576953 by <a href="/recipes/users/4172262/">Sam Denton</a> (<a href="/recipes/tags/arithmetic/">arithmetic</a>, <a href="/recipes/tags/calculator/">calculator</a>, <a href="/recipes/tags/polynomial/">polynomial</a>). Revision 7. </p> <p>This implements polynomial functions over a single variable in Python. It represents the polynomial as a list of numbers and allows most arithmetic operations, using conventional Python syntax. It does not do symbolic manipulations. Instead, you can do things like this:</p> <pre class="prettyprint"><code>x = SimplePolynomial() eq = (x-1)*(x*1) print eq # prints 'X**2 - 1' print eq(4) # prints 15 </code></pre>