Popular recipes by Sean Ross http://code.activestate.com/recipes/users/761068/2008-01-18T19:51:11-08:00ActiveState Code RecipesFresh function defaults (Python) 2004-09-04T05:30:54-07:00Sean Rosshttp://code.activestate.com/recipes/users/761068/http://code.activestate.com/recipes/303440-fresh-function-defaults/ <p style="color: grey"> Python recipe 303440 by <a href="/recipes/users/761068/">Sean Ross</a> . </p> <p>This recipe provides a decorator for keeping mutable default function values fresh between calls.</p> A tidy property idiom (Python) 2003-08-01T14:57:43-07:00Sean Rosshttp://code.activestate.com/recipes/users/761068/http://code.activestate.com/recipes/205183-a-tidy-property-idiom/ <p style="color: grey"> Python recipe 205183 by <a href="/recipes/users/761068/">Sean Ross</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 5. </p> <p>This recipe suggests an idiom for property creation that avoids cluttering the class space with get/set/del methods that will not be used directly.</p> sample with replacement (Python) 2004-03-08T00:21:23-08:00Sean Rosshttp://code.activestate.com/recipes/users/761068/http://code.activestate.com/recipes/273085-sample-with-replacement/ <p style="color: grey"> Python recipe 273085 by <a href="/recipes/users/761068/">Sean Ross</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 3. </p> <p>For taking k random samples (with replacement) from a population, where k may be greater than len(population).</p> Combining simple and specific property creation (Python) 2003-11-28T13:16:44-08:00Sean Rosshttp://code.activestate.com/recipes/users/761068/http://code.activestate.com/recipes/252180-combining-simple-and-specific-property-creation/ <p style="color: grey"> Python recipe 252180 by <a href="/recipes/users/761068/">Sean Ross</a> (<a href="/recipes/tags/oop/">oop</a>). </p> <p>This recipe provides a function that will automate simple property creation, while allowing its users to provide specific fget/fset/fdel methods.</p> a simple genetic algorithm (Python) 2008-01-18T19:51:11-08:00Sean Rosshttp://code.activestate.com/recipes/users/761068/http://code.activestate.com/recipes/199121-a-simple-genetic-algorithm/ <p style="color: grey"> Python recipe 199121 by <a href="/recipes/users/761068/">Sean Ross</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> Dictionary of Function Parameters (Python) 2004-04-28T23:19:59-07:00Sean Rosshttp://code.activestate.com/recipes/users/761068/http://code.activestate.com/recipes/201195-dictionary-of-function-parameters/ <p style="color: grey"> Python recipe 201195 by <a href="/recipes/users/761068/">Sean Ross</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 2. </p> <p>This recipe is based on <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/157572." rel="nofollow">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/157572.</a></p> <p>Calling parameters() inside a function will return that function's parameters as a dictionary. The dictionary does not include <em>varargs, since *varargs items do not have a "name" that can be used as a key. However, *</em>varkw is added to the dictionary, as an update.</p> <p>There are three optional parameters that can be used to filter the information returned.</p> Determining the current functions name (at the time it is called) (Python) 2003-05-22T01:30:08-07:00Sean Rosshttp://code.activestate.com/recipes/users/761068/http://code.activestate.com/recipes/200892-determining-the-current-functions-name-at-the-time/ <p style="color: grey"> Python recipe 200892 by <a href="/recipes/users/761068/">Sean Ross</a> (<a href="/recipes/tags/debugging/">debugging</a>). Revision 4. </p> <p>whoaminow() can be used inside a function to determine, at the time it is called, the name under which that function has been invoked.</p> <p>NOTE: This solution is <em>extremely</em> brittle and provides very limited utility, as it stands. However, it does serve to highlight an interesting avenue for introspection, namely, the dis module.</p> Automating simple property creation (Python) 2003-09-07T04:49:13-07:00Sean Rosshttp://code.activestate.com/recipes/users/761068/http://code.activestate.com/recipes/157768-automating-simple-property-creation/ <p style="color: grey"> Python recipe 157768 by <a href="/recipes/users/761068/">Sean Ross</a> (<a href="/recipes/tags/oop/">oop</a>). </p> <p>The following are a set of functions for creating simple properties - like Ruby's attr_reader, attr_writer, and attr_accessor.</p> <p>If, inside a class definition, you write:</p> <p>&amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp attribute(foo=1, bar=2)</p> <p>simple properties named 'foo' and 'bar' are created for this class. Also, private instance variables '__foo' and '__bar' will be added to instances of this class.</p> <p>By "simple properties", I mean something like the following:</p> <p>&amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp ''' assume we're inside a class definition and &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp self.__foo and self.__bar have been instantiated. &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp ''' &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp def get_foo(self): &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp return self.__foo &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp def set_foo(self, value): &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp self.__foo = value &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp def del_foo(self): &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp del self.__foo &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp def get_bar(self): &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp return self.__bar &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp def set_bar(self, value): &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp self.__bar = value &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp def del_bar(self): &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp del self.__bar &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp foo = property(fget=get_foo, fset=set_foo, fdel=del_foo, doc="foo") &amp;nbsp &amp;nbsp &amp;nbsp &amp;nbsp bar = property(fget=get_bar, fset=set_bar, fdel=del_bar, doc="bar")</p>