Popular recipes by David Klaffenbach http://code.activestate.com/recipes/users/2007923/2010-07-24T17:24:13-07:00ActiveState Code Recipessimple state machine implementation (Python)
2010-07-15T17:54:17-07:00David Klaffenbachhttp://code.activestate.com/recipes/users/2007923/http://code.activestate.com/recipes/577308-simple-state-machine-implementation/
<p style="color: grey">
Python
recipe 577308
by <a href="/recipes/users/2007923/">David Klaffenbach</a>
(<a href="/recipes/tags/machine/">machine</a>, <a href="/recipes/tags/state/">state</a>).
</p>
<p>A very simple idiom for implementing a state machine.</p>
Numerical type with units (dimensions.py) (Python)
2010-07-24T17:24:13-07:00David Klaffenbachhttp://code.activestate.com/recipes/users/2007923/http://code.activestate.com/recipes/577333-numerical-type-with-units-dimensionspy/
<p style="color: grey">
Python
recipe 577333
by <a href="/recipes/users/2007923/">David Klaffenbach</a>
(<a href="/recipes/tags/dimensions/">dimensions</a>, <a href="/recipes/tags/numerical/">numerical</a>, <a href="/recipes/tags/units/">units</a>).
</p>
<p>I implemented dimensions.py perhaps eight years ago as an exercise and have used it occasionally ever since.</p>
<p>It allows doing math with dimensioned values in order to automate unit conversions (you can add m/s to mile/hour) and dimensional checking (you can't add m/s to mile/lightyear). It specifically does not convert 212F to 100C but rather will convert 9F to 5C (valid when converting temperature differences).</p>
<p>It is similar to unums (<a href="http://home.scarlet.be/be052320/Unum.html" rel="nofollow">http://home.scarlet.be/be052320/Unum.html</a>) but with a significant difference:</p>
<p>I used a different syntax Q(25,'m/s') as opposed to 100*m/s (I recall not wanting to have all the base SI units directly in the namespace). I'm not entirely sure which approach is really better.</p>
<p>I also had a specific need to have fractional exponents on units, allowing the following:</p>
<pre class="prettyprint"><code>>>> km=Q(10,'N*m/W^(1/2)')
>>> km
Q(10.0, 'kg**0.5*m/s**0.5')
</code></pre>
<p>Looking back I see a few design decisions I might do differently today, but I'll share it anyway.</p>
<p>Some examples are in the source below the line with if __name__ == "__main__":</p>
<p>Note that I've put two files into the code block below, dimensions.py and dimensions.data, so please cut them apart if you want to try it.</p>
A simple equation solver using attribute access and introspection (Python)
2004-09-03T16:29:43-07:00David Klaffenbachhttp://code.activestate.com/recipes/users/2007923/http://code.activestate.com/recipes/303396-a-simple-equation-solver-using-attribute-access-an/
<p style="color: grey">
Python
recipe 303396
by <a href="/recipes/users/2007923/">David Klaffenbach</a>
.
</p>
<p>This is a little equation solver somewhat modelled on the solvers available in some scientific calculators. You pass it a function which returns zero when the desired relation is true. Once you create a solver object, you can solve for any variable.</p>
Generating combinations of objects from multiple sequences (Python)
2004-08-29T00:32:23-07:00David Klaffenbachhttp://code.activestate.com/recipes/users/2007923/http://code.activestate.com/recipes/302478-generating-combinations-of-objects-from-multiple-s/
<p style="color: grey">
Python
recipe 302478
by <a href="/recipes/users/2007923/">David Klaffenbach</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>The function combine takes multiple sequences and creates a list in which each item is constructed from items from each input sequence, and all possible combinations are created. If that description is confusing, look at the example in the docstring. It's a pretty simple transformation. The function xcombine is similar, but returns a generator rather than creating the output all at once.</p>
A time value of money class demonstrating properties (Python)
2004-08-30T23:48:47-07:00David Klaffenbachhttp://code.activestate.com/recipes/users/2007923/http://code.activestate.com/recipes/302697-a-time-value-of-money-class-demonstrating-properti/
<p style="color: grey">
Python
recipe 302697
by <a href="/recipes/users/2007923/">David Klaffenbach</a>
.
</p>
<p>This class can be used to do interest rate calculations much like a financial calculator, and demonstrates the use of Python properties.</p>