Popular recipes by Scott David Daniels http://code.activestate.com/recipes/users/98131/2006-01-12T09:07:50-08:00ActiveState Code RecipesBroken Test decorator (Python) 2006-01-12T09:07:50-08:00Scott David Danielshttp://code.activestate.com/recipes/users/98131/http://code.activestate.com/recipes/466288-broken-test-decorator/ <p style="color: grey"> Python recipe 466288 by <a href="/recipes/users/98131/">Scott David Daniels</a> . </p> <p>broken_test_XXX(reason) is a decorator for "inverting" the sense of the following unit test. Such tests will succeed where they would have failed (or failed because of a raised exception), and fail if the decorated test succeeds.</p> Lazy property evaluation (Python) 2005-01-18T13:59:28-08:00Scott David Danielshttp://code.activestate.com/recipes/users/98131/http://code.activestate.com/recipes/363602-lazy-property-evaluation/ <p style="color: grey"> Python recipe 363602 by <a href="/recipes/users/98131/">Scott David Daniels</a> . </p> <p>Lazy properties can be easily built in Python 2.4 -- properties whose value may require some effort to calculate, but whose values remain constant once calculated. This recipe uses decorators to implements such properties.</p> Using decorators to load data structures (Python) 2005-03-28T07:02:10-08:00Scott David Danielshttp://code.activestate.com/recipes/users/98131/http://code.activestate.com/recipes/393010-using-decorators-to-load-data-structures/ <p style="color: grey"> Python recipe 393010 by <a href="/recipes/users/98131/">Scott David Daniels</a> . </p> <p>Decorators can be used to load data structures with a function. Using this technique can reduce the 'lots of declarations; large table definition; startup' structure of some larger programs. The insight is remembering that a decorator can return the original function unchanged.</p> Error reporting via decorator (Python) 2005-03-10T16:13:58-08:00Scott David Danielshttp://code.activestate.com/recipes/users/98131/http://code.activestate.com/recipes/391249-error-reporting-via-decorator/ <p style="color: grey"> Python recipe 391249 by <a href="/recipes/users/98131/">Scott David Daniels</a> . </p> <p>A Python 2.4 (or later) decorator can be used to watch for unhandled exceptions, even in hybrid environments like wxPython. You place the decorator around methods that will be invoked by the wxWidgets code. The only big trick is a dance that may be necessary to provide access to values only available after the wxPthon app is started.</p> ilines -- universal newlines from any data source (Python) 2004-06-23T11:33:52-07:00Scott David Danielshttp://code.activestate.com/recipes/users/98131/http://code.activestate.com/recipes/286165-ilines-universal-newlines-from-any-data-source/ <p style="color: grey"> Python recipe 286165 by <a href="/recipes/users/98131/">Scott David Daniels</a> . </p> <p>ilines is a generator that takes an iterable and produces lines of text. The input iterable should produce blocks of bytes (as type str) such as might be produced by reading a file in binary. The output lines are formed by the same rule as the "universal newlines" file mode [f = file(name, 'U')] and are produced "on-line" -- when lines are discovered, they are produced.</p> String substitution shorty. (Python) 2003-10-24T21:36:36-07:00Scott David Danielshttp://code.activestate.com/recipes/users/98131/http://code.activestate.com/recipes/231347-string-substitution-shorty/ <p style="color: grey"> Python recipe 231347 by <a href="/recipes/users/98131/">Scott David Daniels</a> (<a href="/recipes/tags/text/">text</a>). </p> <p>A simple (but not hopelessly fragile) approach for string substitution.</p> curry -- associating parameters with a function (Python) 2001-04-18T03:32:32-07:00Scott David Danielshttp://code.activestate.com/recipes/users/98131/http://code.activestate.com/recipes/52549-curry-associating-parameters-with-a-function/ <p style="color: grey"> Python recipe 52549 by <a href="/recipes/users/98131/">Scott David Daniels</a> (<a href="/recipes/tags/programs/">programs</a>). Revision 3. </p> <p>In functional programming, currying is a way to bind arguments with a function and wait for the rest of the arguments to show up later. You "curry in" the first few parameters to a function, giving you a function that takes subsequent parameters as input and calls the original with all of those parameters. This recipe uses a class instance to hold the parameters before their first use. For example:</p> <pre class="prettyprint"><code>double = curry(operator.mul, 2) triple = curry(operator.mul, 3) </code></pre> Heap-based priority queue (Python) 2001-10-14T17:04:26-07:00Scott David Danielshttp://code.activestate.com/recipes/users/98131/http://code.activestate.com/recipes/81693-heap-based-priority-queue/ <p style="color: grey"> Python recipe 81693 by <a href="/recipes/users/98131/">Scott David Daniels</a> . </p> <p>This is a Priority Queue based on a heap data strucuture. Elements come out of the queue least first. The heap is a complete binary tree with root at _a[1] and, for a node N, its parent is _a[N>&gt;1] and children are _a[2<em>N] and _a[2</em>N+1].</p> farey: Numeric to rational via Farey fractions (Python) 2001-04-02T20:09:38-07:00Scott David Danielshttp://code.activestate.com/recipes/users/98131/http://code.activestate.com/recipes/52317-farey-numeric-to-rational-via-farey-fractions/ <p style="color: grey"> Python recipe 52317 by <a href="/recipes/users/98131/">Scott David Daniels</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>This converts a Numeric to a rational. The result is always in reduced form, but the proof, while possible, is subtle.</p> <p>farey(math.pi,100) = (22,7)</p> function composition (Python) 2001-04-17T22:31:28-07:00Scott David Danielshttp://code.activestate.com/recipes/users/98131/http://code.activestate.com/recipes/52902-function-composition/ <p style="color: grey"> Python recipe 52902 by <a href="/recipes/users/98131/">Scott David Daniels</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>These two classes show two styles of function composition. The difference is only when the second function (g) returns a tuple. compose passes the results of g as a tuple, mcompose treats it as a tuple of args to pass along. Note that extra args provided to (m)compose are treated as extra args to f (there is no standard functional behavior here to follow).</p> <pre class="prettyprint"><code>compose(f,g, x...)(y...) = f(g(y...), x...) </code></pre> <p>mcompose(f,g, x...)(y...) = f(*g(y...), x...)</p>