Popular Python recipes tagged "meta:requires=new"http://code.activestate.com/recipes/langs/python/tags/meta:requires=new/2011-12-20T06:37:15-08:00ActiveState Code RecipesDelayed Decorator (Python) 2011-12-20T06:37:15-08:00Peter Donishttp://code.activestate.com/recipes/users/4180313/http://code.activestate.com/recipes/577993-delayed-decorator/ <p style="color: grey"> Python recipe 577993 by <a href="/recipes/users/4180313/">Peter Donis</a> (<a href="/recipes/tags/decorators/">decorators</a>). </p> <p>This is a decorator wrapper class to delay decorating the base function until it is actually invoked. This can be useful when decorating ordinary functions if some decorator parameters depend on data that is only known at function invocation. It can also be used (and was written) to ensure that a decorated method of a class gets decorated once per instance instead of once per class; the use case that prompted this was the need to memoize a generator (see the <a href="http://code.activestate.com/recipes/577992-memoize-generator/">Memoized Generator</a> recipe), but the implementation is general.</p> Composition of classes instead of multiple inheritance (Python) 2011-04-16T03:40:19-07:00Ethan Furmanhttp://code.activestate.com/recipes/users/4177684/http://code.activestate.com/recipes/577658-composition-of-classes-instead-of-multiple-inherit/ <p style="color: grey"> Python recipe 577658 by <a href="/recipes/users/4177684/">Ethan Furman</a> (<a href="/recipes/tags/class_decorator/">class_decorator</a>, <a href="/recipes/tags/composition/">composition</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/multiple_inheritance/">multiple_inheritance</a>). </p> <p>MI can be difficult and confusing, and if the base classes don't cooperate -- well, cooperative MI won't work.</p> <p>One way around this is to use composition instead. This class decorator will combine the source classes with the target class, ensuring that no duplications occur (raises a TypeError if there are any).</p> The goto decorator (Python) 2009-11-03T13:14:57-08:00Carl Cereckehttp://code.activestate.com/recipes/users/4172182/http://code.activestate.com/recipes/576944-the-goto-decorator/ <p style="color: grey"> Python recipe 576944 by <a href="/recipes/users/4172182/">Carl Cerecke</a> (<a href="/recipes/tags/bytecodes/">bytecodes</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/goto/">goto</a>). Revision 5. </p> <p>A function-decorator that provides the goto command.</p> Decoupled Proxy of a State Bearing Object in a Multiprocessing Environment (Python) 2010-04-08T04:36:05-07:00Mateyuzohttp://code.activestate.com/recipes/users/4172453/http://code.activestate.com/recipes/577180-decoupled-proxy-of-a-state-bearing-object-in-a-mul/ <p style="color: grey"> Python recipe 577180 by <a href="/recipes/users/4172453/">Mateyuzo</a> (<a href="/recipes/tags/access/">access</a>, <a href="/recipes/tags/multiprocessing/">multiprocessing</a>, <a href="/recipes/tags/process/">process</a>, <a href="/recipes/tags/processing/">processing</a>, <a href="/recipes/tags/subprocess/">subprocess</a>, <a href="/recipes/tags/synchronization/">synchronization</a>, <a href="/recipes/tags/syncmanager/">syncmanager</a>, <a href="/recipes/tags/threading/">threading</a>, <a href="/recipes/tags/__getattribute__/">__getattribute__</a>). Revision 2. </p> <p>Describes a simple example of the potential for shared Singleton or Borg object methods to be proxy'd by an object that SyncManger can present to requesting subprocesses.</p> Basic class code generator (Python) 2010-03-22T10:58:19-07:00Mateyuzohttp://code.activestate.com/recipes/users/4172453/http://code.activestate.com/recipes/577130-basic-class-code-generator/ <p style="color: grey"> Python recipe 577130 by <a href="/recipes/users/4172453/">Mateyuzo</a> (<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/prototyping/">prototyping</a>, <a href="/recipes/tags/template/">template</a>). Revision 5. </p> <p>This little script takes a yaml file as a class definition and generates code stubs and unit tests.</p> SuperFly: Separating class heirarchy from class definition. (Python) 2009-02-15T20:47:51-08:00Ted Skolnickhttp://code.activestate.com/recipes/users/4169200/http://code.activestate.com/recipes/576652-superfly-separating-class-heirarchy-from-class-def/ <p style="color: grey"> Python recipe 576652 by <a href="/recipes/users/4169200/">Ted Skolnick</a> (<a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/polymorphism/">polymorphism</a>). Revision 2. </p> <p>This is a recipe for defining class hierarchies outside of class definitions. This way you could at, runtime, decide what class should act as the parent of a given class. A class could sometimes have one parent, sometimes another.</p> Simple shelve with Linux file locking (Python) 2008-12-21T05:50:07-08:00Michael Ihdehttp://code.activestate.com/recipes/users/4168518/http://code.activestate.com/recipes/576591-simple-shelve-with-linux-file-locking/ <p style="color: grey"> Python recipe 576591 by <a href="/recipes/users/4168518/">Michael Ihde</a> (<a href="/recipes/tags/dynamic_method/">dynamic_method</a>, <a href="/recipes/tags/locking/">locking</a>, <a href="/recipes/tags/shelve/">shelve</a>). </p> <p>The shelve module is a easy way to add persistence to your application via a DBM database. However, if you have multiple reader/writer combination you need to lock the file to prevent corruption. The shelve module itself does not provide locking because it is platform specific. If you only need Linux, this simple module provide an easy way to support locking using dynamically added methods.</p> Pickle the interactive interpreter state (Python) 2008-05-20T10:36:50-07:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/572213-pickle-the-interactive-interpreter-state/ <p style="color: grey"> Python recipe 572213 by <a href="/recipes/users/2033964/">Oren Tirosh</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). Revision 2. </p> <p>This code extends the pickle module to enable pickling of functions and classes defined interactively at the command prompt. You can save the interpreter state by pickling the __main__ module and restore it later.</p> Copying Generators (Python) 2007-10-09T07:38:06-07:00kay schluehrhttp://code.activestate.com/recipes/users/2398921/http://code.activestate.com/recipes/528949-copying-generators/ <p style="color: grey"> Python recipe 528949 by <a href="/recipes/users/2398921/">kay schluehr</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 5. </p> <p>This recipe presents copy_generator(...) which a pure Python function keeping a running generator object and returns a copy of the generator object being in the same state as the original generator object.</p> True Lieberman-style delegation in Python. (Python) 2007-05-14T11:14:33-07:00Martin Blaishttp://code.activestate.com/recipes/users/1643324/http://code.activestate.com/recipes/519639-true-lieberman-style-delegation-in-python/ <p style="color: grey"> Python recipe 519639 by <a href="/recipes/users/1643324/">Martin Blais</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>Proxies are usually implemented as objects that forward method calls to a "target" object. This approach has a major problem: forwarding makes the target object the receiver of the method call; this means that calls originating from the body of a method in the target will not go through the proxy (and thus their behavior cannot be modified by the proxy).</p> <p>For example, suppose we want a proxy to an instance of Target (shown below) that is "safe", i.e., does not do anything bad like firing missiles. We can just define a class that forwards calls to the safe methods, namely send_flowers() and hang_out(). This class can have its own version of fire_missiles() that does nothing. Now consider what happens when we call the proxy object's innocent-looking hang_out() method. The call is forwarded to the target object, which in turn calls the target object's (not the proxy's) fire_missiles() method, and BOOM! (The proxy's version of fire_missiles() is not called because forwarding has made the target object the receiver of the new method call.)</p> <p>By using delegation, one can implement proxies without the drawbacks of the method-forwarding approach. This recipe shows how Python's __getattr__ method can be used to implement the kind of delegation present in prototype-based languages like Self and Javascript, and how delegation can be used to implement better proxies.</p> Implementing the make statement by hacking bytecodes (Python) 2008-12-31T09:47:48-08:00Nilton Volpatohttp://code.activestate.com/recipes/users/2927430/http://code.activestate.com/recipes/498242-implementing-the-make-statement-by-hacking-bytecod/ <p style="color: grey"> Python recipe 498242 by <a href="/recipes/users/2927430/">Nilton Volpato</a> (<a href="/recipes/tags/programs/">programs</a>). Revision 4. </p> <p>The make statement from PEP 359 is implemented using decorators and function definitions.</p> <p>The make statement:</p> <pre class="prettyprint"><code>make &lt;callable&gt; &lt;name&gt; &lt;tuple&gt;: &lt;block&gt; </code></pre> <p>is implemented this way:</p> <pre class="prettyprint"><code>@make(&lt;callable&gt;, &lt;tuple&gt;) def &lt;name&gt;(): &lt;block&gt; </code></pre> <p>The code also has some examples, using the make decorator to create a namespace and a class.</p> Extract a inner function from a class or a function (Python) 2006-06-19T22:03:22-07:00Sunjoong LEEhttp://code.activestate.com/recipes/users/2925478/http://code.activestate.com/recipes/496806-extract-a-inner-function-from-a-class-or-a-functio/ <p style="color: grey"> Python recipe 496806 by <a href="/recipes/users/2925478/">Sunjoong LEE</a> (<a href="/recipes/tags/debugging/">debugging</a>). Revision 2. </p> <p>This function can extract a inner function from a class or a function. It may be useful when writing a unit test code.</p> Changing a closed-over value (in a "cell") (Python) 2005-09-02T18:40:43-07:00paul cannonhttp://code.activestate.com/recipes/users/2551140/http://code.activestate.com/recipes/440515-changing-a-closed-over-value-in-a-cell/ <p style="color: grey"> Python recipe 440515 by <a href="/recipes/users/2551140/">paul cannon</a> (<a href="/recipes/tags/debugging/">debugging</a>). </p> <p>In most languages with closures, it is possible to change a closed-over value and have the change visible in other closures which share a reference to the same variable. Python's syntax makes this impossible.</p> <p>Even if you're content (I am) with the work-around-- putting your changeable values inside a mutable object like a list-- it may occasionally happen that you wish you could change the closed-over values, found in "cell" objects in a function's func_closure tuple. Another recipe I submitted shows how to get at the values in standard Python; this one will demonstrate a way to actually change that value, so that functions which also close over that value (share a reference to the same cell) will see the change.</p> Thread-safe caching object with file and HTTP implementations (Python) 2006-02-08T10:04:16-08:00Nicolas Lehuenhttp://code.activestate.com/recipes/users/1599156/http://code.activestate.com/recipes/302997-thread-safe-caching-object-with-file-and-http-impl/ <p style="color: grey"> Python recipe 302997 by <a href="/recipes/users/1599156/">Nicolas Lehuen</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 9. </p> <p>Implementation of an abstract, thread-safe cache with minimal locking. Four concrete implementations : a validating file cache, a validating HTTP cache, an experimental Python module cache and a function cache. Plus, an abstract cache with weak references to its values.</p> A meta-class that provides class behavior like Ruby (Python) 2004-12-29T19:37:12-08:00Ikkei Shimomurahttp://code.activestate.com/recipes/users/987178/http://code.activestate.com/recipes/361167-a-meta-class-that-provides-class-behavior-like-rub/ <p style="color: grey"> Python recipe 361167 by <a href="/recipes/users/987178/">Ikkei Shimomura</a> (<a href="/recipes/tags/oop/">oop</a>). </p> <p>It brings Ruby-like class behavior. When a class is declared, it extends the old class if the class name exists already.</p> mixins for proxy construction... (Python) 2004-09-18T20:54:37-07:00Alex Naanouhttp://code.activestate.com/recipes/users/104183/http://code.activestate.com/recipes/305308-mixins-for-proxy-construction/ <p style="color: grey"> Python recipe 305308 by <a href="/recipes/users/104183/">Alex Naanou</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 2. </p> <p>Here we will demonstrate a technique that will make it possible to semi-transparently proxy an object and override/proxy methods or data including special methods like __call__. this module will define several general tools and components that can be used as a basis for creation of various proxy classes...</p> high-performance currying with instancemethod (Python) 2003-10-17T15:57:11-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/229472-high-performance-currying-with-instancemethod/ <p style="color: grey"> Python recipe 229472 by <a href="/recipes/users/97991/">Alex Martelli</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>instancemethod provides a way to perform currying such that the curried function runs much faster than one produced by closure (the second-fastest way). [Currying is explained and covered in detail in other recipes]</p> co_pickle , a module to serialize code objects (Python) 2003-07-27T07:10:50-07:00Andres Tremolshttp://code.activestate.com/recipes/users/1180386/http://code.activestate.com/recipes/212565-co_pickle-a-module-to-serialize-code-objects/ <p style="color: grey"> Python recipe 212565 by <a href="/recipes/users/1180386/">Andres Tremols</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). </p> <p>Compiles a string of python code and returns a printable representation of the byte codes, the resulting string can then be restored back to a code object.</p> Dynamic Classes (Python) 2003-05-22T15:24:23-07:00Bud P. Brueggerhttp://code.activestate.com/recipes/users/1174711/http://code.activestate.com/recipes/201294-dynamic-classes/ <p style="color: grey"> Python recipe 201294 by <a href="/recipes/users/1174711/">Bud P. Bruegger</a> . </p> <p>some simple functions for dynamically adding methods, properties,and classmethods to classes at runtime</p> Allowing the Python profiler to profile C modules (Python) 2001-10-14T09:01:29-07:00Richie Hindlehttp://code.activestate.com/recipes/users/135761/http://code.activestate.com/recipes/81535-allowing-the-python-profiler-to-profile-c-modules/ <p style="color: grey"> Python recipe 81535 by <a href="/recipes/users/135761/">Richie Hindle</a> (<a href="/recipes/tags/debugging/">debugging</a>). Revision 3. </p> <p>This recipe lets you take into account time spent in C modules when profiling your Python code. Normally the profiler only profiles Python code, so finding out how much time is spent accessing a database, running encryption code, sleeping and so on is difficult. Profilewrap makes it easy to profile C code as well as Python code, giving you a clearer picture of where your application is spending its time.</p> <p>Profilewrap demonstrates how to create proxy objects at runtime that intercept calls between pre-existing pieces of code. It also demonstrates the use of the 'new' module to create new functions on the fly.</p>