Popular recipes by S W http://code.activestate.com/recipes/users/1759688/2006-01-18T20:09:02-08:00ActiveState Code Recipespack multiple images of different sizes into one image (Python)
2005-10-16T22:33:06-07:00S Whttp://code.activestate.com/recipes/users/1759688/http://code.activestate.com/recipes/442299-pack-multiple-images-of-different-sizes-into-one-i/
<p style="color: grey">
Python
recipe 442299
by <a href="/recipes/users/1759688/">S W</a>
(<a href="/recipes/tags/graphics/">graphics</a>).
</p>
<p>Packing images of different sizes into one image is often required in order to efficiently use hardware accelerated texture mapping functions of 3D video cards.</p>
A lazy, memoized attribute space. (Python)
2006-01-18T20:09:02-08:00S Whttp://code.activestate.com/recipes/users/1759688/http://code.activestate.com/recipes/466315-a-lazy-memoized-attribute-space/
<p style="color: grey">
Python
recipe 466315
by <a href="/recipes/users/1759688/">S W</a>
(<a href="/recipes/tags/oop/">oop</a>).
</p>
<p>This class provides thunk-like behavior in standard Python.</p>
<p>It allows a function to be evaluated only when needed, and the return value cached, or memoized, until explicitly deleted.</p>
A ThunkSpace for Python (Python)
2006-01-04T08:45:19-08:00S Whttp://code.activestate.com/recipes/users/1759688/http://code.activestate.com/recipes/465868-a-thunkspace-for-python/
<p style="color: grey">
Python
recipe 465868
by <a href="/recipes/users/1759688/">S W</a>
(<a href="/recipes/tags/programs/">programs</a>).
Revision 3.
</p>
<p>This useless hack allows normal functions to be attached to a 'ThunkSpace' which causes the function to be lazily evaluated when the thunk is referenced. It is just a experiment using closures and descriptors to try and change python function call syntax.</p>
A class which keeps a (weak) reference to it's instances. (Python)
2005-12-11T06:15:49-08:00S Whttp://code.activestate.com/recipes/users/1759688/http://code.activestate.com/recipes/464407-a-class-which-keeps-a-weak-reference-to-its-instan/
<p style="color: grey">
Python
recipe 464407
by <a href="/recipes/users/1759688/">S W</a>
(<a href="/recipes/tags/oop/">oop</a>).
Revision 2.
</p>
<p>This recipe implements a base class, which allows derived classes to track instances in self.__instances__. It uses a WeakValueDictionary to store instance references.</p>
call a function/method X times per second (Python)
2005-05-05T00:54:43-07:00S Whttp://code.activestate.com/recipes/users/1759688/http://code.activestate.com/recipes/413137-call-a-functionmethod-x-times-per-second/
<p style="color: grey">
Python
recipe 413137
by <a href="/recipes/users/1759688/">S W</a>
(<a href="/recipes/tags/oop/">oop</a>).
Revision 2.
</p>
<p>This simple generator function is used to call a function X times per second.</p>
Generator Based Concurrency, with Real Threads when Needed. (Python)
2005-12-12T00:49:44-08:00S Whttp://code.activestate.com/recipes/users/1759688/http://code.activestate.com/recipes/440670-generator-based-concurrency-with-real-threads-when/
<p style="color: grey">
Python
recipe 440670
by <a href="/recipes/users/1759688/">S W</a>
(<a href="/recipes/tags/threads/">threads</a>).
Revision 4.
</p>
<p>This recipe uses NanoThreads. <a href="http://lgt.berlios.de/" rel="nofollow">http://lgt.berlios.de/</a></p>
<p>It shows how simulated concurrency, (using generators as tasks), can be transparently combined with OS Level Python threads, as and when needed.</p>
A queue for string data, which looks like a File object. (Python)
2005-07-12T02:45:21-07:00S Whttp://code.activestate.com/recipes/users/1759688/http://code.activestate.com/recipes/426060-a-queue-for-string-data-which-looks-like-a-file-ob/
<p style="color: grey">
Python
recipe 426060
by <a href="/recipes/users/1759688/">S W</a>
(<a href="/recipes/tags/oop/">oop</a>).
Revision 4.
</p>
<p>A queue data structure, for string data only, which looks like a File object. This class takes care of the list.append and "".join mess, which is needed for fast string concatenation.</p>
safer serialization of simple python types, part two (Python)
2005-06-13T07:00:35-07:00S Whttp://code.activestate.com/recipes/users/1759688/http://code.activestate.com/recipes/415791-safer-serialization-of-simple-python-types-part-tw/
<p style="color: grey">
Python
recipe 415791
by <a href="/recipes/users/1759688/">S W</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 4.
</p>
<p>This recipe is a reimplemtation of this recipe,</p>
<p><a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/415503" rel="nofollow">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/415503</a></p>
<p>using Python 2.4 decorator syntax.</p>
<p>It also has added support for boolean and unicode types, and a keyword argument (compress=False) for the dumps function, which will compress the string.</p>
two dict classes which can lookup keys by value, and values by key (Python)
2005-06-22T08:44:20-07:00S Whttp://code.activestate.com/recipes/users/1759688/http://code.activestate.com/recipes/415903-two-dict-classes-which-can-lookup-keys-by-value-an/
<p style="color: grey">
Python
recipe 415903
by <a href="/recipes/users/1759688/">S W</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
Revision 2.
</p>
<p>Sometimes it is useful to be able to see fetch all keys in a dictionary which have a common value. The LookupDict class can do this.</p>
<p>Other times, when a 1 to 1 mapping of keys and values, and values to keys is needed, the ReverseDict class could be used.</p>
safer serialization of simple python types (Python)
2005-06-02T08:35:45-07:00S Whttp://code.activestate.com/recipes/users/1759688/http://code.activestate.com/recipes/415503-safer-serialization-of-simple-python-types/
<p style="color: grey">
Python
recipe 415503
by <a href="/recipes/users/1759688/">S W</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 7.
</p>
<p>Unpickling from an untrusted source, such as a network connection, can allow maliciously formed pickles to run arbitrary code.</p>
<p>This recipe presents a simple solution for serializing and unserializing simple Python types. Only simple Python types can be serialized, which makes the use of this algorithm safer than using the pickle module.</p>
<p>NB: I've changes this recipe drastically. It used to use a rather slow string slicing technique, which was a very bad example of how to use strings in Python! The cStringIO provided a faster, simpler replacement. This recipe now serializes faster than the Pickle module (not cPickle).</p>
<p>NB: A Python 2.4 version of this recipe is available here:
<a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/415791" rel="nofollow">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/415791</a></p>
Memoize Decorator with Timeout (Python)
2004-11-02T07:16:11-08:00S Whttp://code.activestate.com/recipes/users/1759688/http://code.activestate.com/recipes/325905-memoize-decorator-with-timeout/
<p style="color: grey">
Python
recipe 325905
by <a href="/recipes/users/1759688/">S W</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 5.
</p>
<p>This simple decorator is different to other memoize decorators in that it will only cache results for a period of time. It also provides a simple method of cleaning the cache of old entries via the .collect method. This will help prevent excessive or needless memory consumption.</p>
a time determinant logarithmic progression, with a movable target and energy factor. (Python)
2005-01-11T08:20:00-08:00S Whttp://code.activestate.com/recipes/users/1759688/http://code.activestate.com/recipes/362632-a-time-determinant-logarithmic-progression-with-a-/
<p style="color: grey">
Python
recipe 362632
by <a href="/recipes/users/1759688/">S W</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 2.
</p>
<p>This is a strange one. Strange, and interesting. This class creates an object which returns a logarithmic progression of numbers over a period of time. It allows the target value to change, causing the progression of values to speed up, slow down, or even reverse. Why is this useful? Read on below for more...</p>
Import modules from a remote server. (Python)
2004-09-14T06:03:38-07:00S Whttp://code.activestate.com/recipes/users/1759688/http://code.activestate.com/recipes/305277-import-modules-from-a-remote-server/
<p style="color: grey">
Python
recipe 305277
by <a href="/recipes/users/1759688/">S W</a>
(<a href="/recipes/tags/network/">network</a>).
</p>
<p>Storing modules in a central location, which are used by remote, seperate clients has obvious benefits. This recipe describes a method by which modules can be fetched from a remote server and compiled into bytecode modules which are used by the client.</p>
<p>The recipe has 3 sections (server.py, client.py, test.py) which need to be copied into 3 files.</p>
Pivot/Crosstab/Denormalization of a Normalized List (Python)
2004-11-08T07:15:41-08:00S Whttp://code.activestate.com/recipes/users/1759688/http://code.activestate.com/recipes/334695-pivotcrosstabdenormalization-of-a-normalized-list/
<p style="color: grey">
Python
recipe 334695
by <a href="/recipes/users/1759688/">S W</a>
(<a href="/recipes/tags/database/">database</a>).
Revision 2.
</p>
<p>This function creates a cross-tab or pivot table from a normalised input table. Use this function to 'denormalize' a table of normalized records.</p>
A speed limited, or governed range (Python)
2004-12-15T02:54:25-08:00S Whttp://code.activestate.com/recipes/users/1759688/http://code.activestate.com/recipes/358603-a-speed-limited-or-governed-range/
<p style="color: grey">
Python
recipe 358603
by <a href="/recipes/users/1759688/">S W</a>
(<a href="/recipes/tags/programs/">programs</a>).
Revision 3.
</p>
<p>This class implements a generator, which returns consecutive floats which are incremented by a speed * (time between calls).</p>
Automatic argument type conversion. (Python)
2004-12-08T00:37:52-08:00S Whttp://code.activestate.com/recipes/users/1759688/http://code.activestate.com/recipes/355806-automatic-argument-type-conversion/
<p style="color: grey">
Python
recipe 355806
by <a href="/recipes/users/1759688/">S W</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
Revision 4.
</p>
<p>This decorator can be used to cast argument types automatically, using a pre-specified list of types, when a function is called.</p>
A Sinus Plasma, using Pygame (Python)
2004-11-07T23:38:22-08:00S Whttp://code.activestate.com/recipes/users/1759688/http://code.activestate.com/recipes/334696-a-sinus-plasma-using-pygame/
<p style="color: grey">
Python
recipe 334696
by <a href="/recipes/users/1759688/">S W</a>
(<a href="/recipes/tags/graphics/">graphics</a>).
</p>
<p>An old-school demo effect, using sine wave interference patterns. Entertaining for at least a few minutes :)</p>
<p>Modify the freq variable for different patterns.</p>
Functions with Strictly Typed Arguments (Python)
2004-11-05T08:32:24-08:00S Whttp://code.activestate.com/recipes/users/1759688/http://code.activestate.com/recipes/325917-functions-with-strictly-typed-arguments/
<p style="color: grey">
Python
recipe 325917
by <a href="/recipes/users/1759688/">S W</a>
(<a href="/recipes/tags/extending/">extending</a>).
Revision 3.
</p>
<p>This fun little recipe uses Python 2.4 decorators to force function arguments to be of a specified type. If a type mismatch occurs, a TypeError is raised.</p>
<p>It fun, cheap, and dirty. And probably nasty.</p>
A List of Dictionaries, for the Memory Scrooge. (Python)
2004-11-09T00:39:18-08:00S Whttp://code.activestate.com/recipes/users/1759688/http://code.activestate.com/recipes/334621-a-list-of-dictionaries-for-the-memory-scrooge/
<p style="color: grey">
Python
recipe 334621
by <a href="/recipes/users/1759688/">S W</a>
(<a href="/recipes/tags/oop/">oop</a>).
Revision 3.
</p>
<p>I often return result sets from a database call using a list of dictionary objects. When transmitting the pickled list object over the wire, the size of the pickle greatly effects the speed of the transmission.</p>
<p>I wrote this small class to emulate a list of dictionary objects without the memory and pickle storage overhead which occurs when storing every item in the list as a dictionary.</p>
Iterate Over Regions in a 2D Range (Python)
2004-11-09T20:06:36-08:00S Whttp://code.activestate.com/recipes/users/1759688/http://code.activestate.com/recipes/334971-iterate-over-regions-in-a-2d-range/
<p style="color: grey">
Python
recipe 334971
by <a href="/recipes/users/1759688/">S W</a>
(<a href="/recipes/tags/graphics/">graphics</a>).
</p>
<p>I use this iterator when I need to iterate over an image, working on regions or subsets of the image at a time. It returns regions from a 2D range, left to right, top to bottom.</p>