Popular recipes by Justin Shaw http://code.activestate.com/recipes/users/1523109/2013-12-29T15:38:33-08:00ActiveState Code RecipesFreshFish (Python)
2013-12-29T15:38:33-08:00Justin Shawhttp://code.activestate.com/recipes/users/1523109/http://code.activestate.com/recipes/578800-freshfish/
<p style="color: grey">
Python
recipe 578800
by <a href="/recipes/users/1523109/">Justin Shaw</a>
.
Revision 2.
</p>
<p>FreshFish is a function decorator for functions with no arguments (hardware query).</p>
<p>The first time the function is called, the wrapper caches the result (the fish). Subsequent calls to the wrapped function simply return the cached result as long as the fish is still fresh. When the fish goes stale, FreshFish calls the underlying function again for FreshFish.</p>
<p>I used this on a BeagleBone project to monitor heart rate, speed, and cadence.</p>
Inverse modulo p (Python)
2013-01-31T23:41:21-08:00Justin Shawhttp://code.activestate.com/recipes/users/1523109/http://code.activestate.com/recipes/576737-inverse-modulo-p/
<p style="color: grey">
Python
recipe 576737
by <a href="/recipes/users/1523109/">Justin Shaw</a>
(<a href="/recipes/tags/mod/">mod</a>, <a href="/recipes/tags/modulo/">modulo</a>, <a href="/recipes/tags/number/">number</a>, <a href="/recipes/tags/prime/">prime</a>, <a href="/recipes/tags/theory/">theory</a>).
Revision 4.
</p>
<p>Very rarely it is necessary to find the multiplicative inverse of a number in the ring of integers modulo p. Thie recipe handles those rare cases. That is, given x, an integer, and p the modulus, we seek a integer x^-1 such that x * x^-1 = 1 mod p. For example 38 is the inverse of 8 modulo 101 since 38 * 8 = 304 = 1 mod 101. The inverse only exists when a and p are relatively prime.</p>
Simple Cached Functions (Python)
2008-07-20T19:51:17-07:00Justin Shawhttp://code.activestate.com/recipes/users/1523109/http://code.activestate.com/recipes/576365-simple-cached-functions/
<p style="color: grey">
Python
recipe 576365
by <a href="/recipes/users/1523109/">Justin Shaw</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 2.
</p>
<p>This is a simplified version of George Sakkis recipe entitled "Cache decorator in python 2.4" (<a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/325205" rel="nofollow">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/325205</a>). In this version, no extra classes are required and this is small enough to implement on the fly.</p>
Upgradable Pickles (Python)
2007-06-16T16:17:06-07:00Justin Shawhttp://code.activestate.com/recipes/users/1523109/http://code.activestate.com/recipes/521901-upgradable-pickles/
<p style="color: grey">
Python
recipe 521901
by <a href="/recipes/users/1523109/">Justin Shaw</a>
.
Revision 5.
</p>
<p>Stale Pickles ... New Classes. Pickles are by far the easiest way to achieve persistent objects in Python. A problem arises however, when a class is modified between the time when an object is pickled and when it is un-pickled. When a stale pickle is extracted, the old data is coupled with the new class code. This is not a problem as long as no new data attributes are required. For instance, a new method that relies only on old data may be added and still work with the old pickle.</p>
<p>Of course, if attributes are added, the old pickle files will no longer function correctly with the new class definition.</p>
<p>This recipe provides a framework for writing upgradable classes that support backward compatibility with stale pickles.</p>
All k-subsets from an n-set. (Python)
2007-01-20T11:37:11-08:00Justin Shawhttp://code.activestate.com/recipes/users/1523109/http://code.activestate.com/recipes/500268-all-k-subsets-from-an-n-set/
<p style="color: grey">
Python
recipe 500268
by <a href="/recipes/users/1523109/">Justin Shaw</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>This recipe yields each subset of size k from a super set of size n. I thought I had seen a recipe that did this before, but when I needed it I couldn't find it. There are two methods. The first operates on sets of integers of the form range(n). The seconds operates on arbitrary sets or lists.</p>
Non-Linear Units (Python)
2007-01-05T04:29:20-08:00Justin Shawhttp://code.activestate.com/recipes/users/1523109/http://code.activestate.com/recipes/499350-non-linear-units/
<p style="color: grey">
Python
recipe 499350
by <a href="/recipes/users/1523109/">Justin Shaw</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
Revision 2.
</p>
<p>This is a simple way of expressing non-linear scales (such as decibels) in python. In stead of:
gain = 10 ** (12/10.)</p>
<p>Use
gain = 12 * dB</p>
Lego Mindstorms Bluetooth interface (Python)
2006-09-13T00:29:50-07:00Justin Shawhttp://code.activestate.com/recipes/users/1523109/http://code.activestate.com/recipes/498085-lego-mindstorms-bluetooth-interface/
<p style="color: grey">
Python
recipe 498085
by <a href="/recipes/users/1523109/">Justin Shaw</a>
(<a href="/recipes/tags/network/">network</a>).
Revision 4.
</p>
<p>The new Lego Mindstorms NXT brick has an on-board Bluetooth transceiver that can
connect to a serial port service on a PC. The NXT uses a simple format to pass
raw bytes between connected Bluetooth devices. This interface allows the NXT
brick to be controlled from Python. Robot programming in Python anyone?</p>
Sudoku Solver (Python)
2005-09-13T01:35:30-07:00Justin Shawhttp://code.activestate.com/recipes/users/1523109/http://code.activestate.com/recipes/440542-sudoku-solver/
<p style="color: grey">
Python
recipe 440542
by <a href="/recipes/users/1523109/">Justin Shaw</a>
(<a href="/recipes/tags/text/">text</a>).
Revision 2.
</p>
<p>This is a simple text based Sadoku puzzle solver.</p>
<p>See <a href="http://www.websudoku.com/" rel="nofollow">http://www.websudoku.com/</a> for more details.</p>
Implementing "Future"s with decorators (Python)
2004-12-30T05:26:18-08:00Justin Shawhttp://code.activestate.com/recipes/users/1523109/http://code.activestate.com/recipes/355651-implementing-futures-with-decorators/
<p style="color: grey">
Python
recipe 355651
by <a href="/recipes/users/1523109/">Justin Shaw</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 5.
</p>
<p>How to kick off a slow process without waiting around for the result. The process is run in the background until the value is actually needed at which time it blocks until the value is ready.</p>
Type checked argument lists with decorators. (Python)
2004-12-05T14:27:11-08:00Justin Shawhttp://code.activestate.com/recipes/users/1523109/http://code.activestate.com/recipes/355638-type-checked-argument-lists-with-decorators/
<p style="color: grey">
Python
recipe 355638
by <a href="/recipes/users/1523109/">Justin Shaw</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>Here is how to use the new decorator feature of python 2.4 to systematically check the argument types for type-sensitive functions.</p>
Just in time instantiation (Python)
2003-12-10T19:28:45-08:00Justin Shawhttp://code.activestate.com/recipes/users/1523109/http://code.activestate.com/recipes/252525-just-in-time-instantiation/
<p style="color: grey">
Python
recipe 252525
by <a href="/recipes/users/1523109/">Justin Shaw</a>
(<a href="/recipes/tags/oop/">oop</a>).
</p>
<p>I frequently find myself adding "just in time" (JIT) object creation to avoid wasting cycles on objects that are never used. This class enables JIT object creation by basically currying the __init__ method.</p>
<p>The object is instianted only when an attribute is got or set. Then automatic delegation is used to front for the object (see <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52295%29" rel="nofollow">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52295)</a></p>