Popular recipes tagged "extending"http://code.activestate.com/recipes/tags/extending/popular/2012-02-27T12:32:11-08:00ActiveState Code RecipesLazy Load Object Proxying (Python) 2012-01-13T07:56:29-08:00Cory Virokhttp://code.activestate.com/recipes/users/4180495/http://code.activestate.com/recipes/578014-lazy-load-object-proxying/ <p style="color: grey"> Python recipe 578014 by <a href="/recipes/users/4180495/">Cory Virok</a> (<a href="/recipes/tags/extending/">extending</a>). </p> <p>A very slight modification on Tomer Filiba's original Proxy class to use a factory function instead of an instance to create an object proxy the first time it is required. The only other modification is to add an instance variable to LazyLoadProxy to store data specific to the proxy and not the delegated instance. </p> Automatic Python PyObject ref-count management in C++ using a smart ptr (C++) 2011-12-19T15:24:03-08:00samhhttp://code.activestate.com/recipes/users/4180289/http://code.activestate.com/recipes/577985-automatic-python-pyobject-ref-count-management-in-/ <p style="color: grey"> C++ recipe 577985 by <a href="/recipes/users/4180289/">samh</a> (<a href="/recipes/tags/extending/">extending</a>). </p> <p>Managing ref-counting is a complex and error prone business. If you choose C++ to extend or embed Python, you can simply use a modification on <code>std::auto_ptr</code>. Instead of calling delete on the managed pointer, it will decref it.</p> <p>So now you can do:</p> <pre class="prettyprint"><code>auto_pyptr pyHelloStr(PyStr_FromString("Hello World!")); </code></pre> <p>and forget about having to decref it altogether! Just like <code>auto_ptr</code> you can get the <code>PyObject *</code> with <code>get()</code>, release it from managed control with <code>release()</code>, and rebind it with <code>reset(new_ptr)</code>. You can also incref it by calling <code>inc()</code>, but be cautious as you can easily create a leak by increfing once to much.</p> PluginManager - Extending Project Functionality By Using Custom Modules/Plugins On The Fly. (Python) 2010-05-07T15:32:10-07:00AJ. Mayorgahttp://code.activestate.com/recipes/users/4173476/http://code.activestate.com/recipes/577216-pluginmanager-extending-project-functionality-by-u/ <p style="color: grey"> Python recipe 577216 by <a href="/recipes/users/4173476/">AJ. Mayorga</a> (<a href="/recipes/tags/extending/">extending</a>, <a href="/recipes/tags/extensible/">extensible</a>, <a href="/recipes/tags/modules/">modules</a>, <a href="/recipes/tags/plugin/">plugin</a>). Revision 4. </p> <p>This demo shows how you can create and manage your own custom plugins for extending functionality in your Python projects. There are no safety wrappers in this demo for restricting plugins aside from that fact that plugins are run as an extention of a management class which is run in its own instance only receiving data passed to it by the RumModules method, that said security should ideally be applied to the ModuleAPI class, by restricting __builtins__ on eval calls and/or validating plugin content and parameters which can be done by extending the Prepaser Class. For a great recipe/demo of restricting eval call see <a href="http://code.activestate.com/recipes/496746" rel="nofollow">http://code.activestate.com/recipes/496746</a>. </p> <p>That aside it was alot of fun to write and use. Enjoy</p> <p>PS: you will need <a href="http://code.activestate.com/recipes/577144/" rel="nofollow">http://code.activestate.com/recipes/577144/</a> to import Xceptions</p> Find all subclasses of a given class (Python) 2009-11-04T20:26:08-08:00Gabriel Genellinahttp://code.activestate.com/recipes/users/924636/http://code.activestate.com/recipes/576949-find-all-subclasses-of-a-given-class/ <p style="color: grey"> Python recipe 576949 by <a href="/recipes/users/924636/">Gabriel Genellina</a> (<a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/extending/">extending</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/plugin/">plugin</a>, <a href="/recipes/tags/subclass/">subclass</a>, <a href="/recipes/tags/subclasses/">subclasses</a>, <a href="/recipes/tags/type/">type</a>). Revision 3. </p> <p>itersubclasses(cls) returns a generator over all subclasses of cls, in depth first order. cls must be a new-style class; old-style classes are <em>not</em> supported.</p> Utility Mill Import (Python) 2008-12-29T08:17:29-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/576599-utility-mill-import/ <p style="color: grey"> Python recipe 576599 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/extending/">extending</a>). Revision 5. </p> <p>After looking at <a href="http://featurelist.org" rel="nofollow">featurelist.org</a> and noticing that someone wanted to import code stored on Utility Mill, the following code was written as an extension of previous work on automated testing. The original thread can be found at <a href="http://featurelist.org/features/details/238" rel="nofollow">http://featurelist.org/features/details/238</a> . The code imported by the test at the end of this code can be found at <a href="http://utilitymill.com/edit/SPICE/27" rel="nofollow">http://utilitymill.com/edit/SPICE/27</a> &amp; <a href="http://utilitymill.com/edit/nysiis/20" rel="nofollow">http://utilitymill.com/edit/nysiis/20</a> . Please note that this is Python 3.0 code importing Python 2.5 code and may not be useful in all contexts that you may wish to use it in. Also, modules stored on Utility Mill may need unconditionally executing code removed first before importing.</p> Object Proxying (Python) 2006-05-26T03:44:09-07:00tomer filibahttp://code.activestate.com/recipes/users/2520014/http://code.activestate.com/recipes/496741-object-proxying/ <p style="color: grey"> Python recipe 496741 by <a href="/recipes/users/2520014/">tomer filiba</a> (<a href="/recipes/tags/extending/">extending</a>). </p> <p>transparent object proxying for (almost) all objects.</p> Easier positional arguments for optparse (Python) 2012-02-27T12:32:11-08:00Shekhar Tiwatnehttp://code.activestate.com/recipes/users/2071240/http://code.activestate.com/recipes/574459-easier-positional-arguments-for-optparse/ <p style="color: grey"> Python recipe 574459 by <a href="/recipes/users/2071240/">Shekhar Tiwatne</a> (<a href="/recipes/tags/extending/">extending</a>). Revision 5. </p> <p>Many times I find myself write a cli that takes two/more positional arguments. Something like mycp file1 file2 [options] I have to write extra code everytime to show correct usage/hints to user if he invokes command this way mycp file1 Positional arguments are required ones unlike optional arguments.</p> <p>The solution below lets cli writer add a positional argument so parser can generate usage friendlier to positional args.</p> <p>Some inspiration: <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/573441" rel="nofollow">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/573441</a></p> py3to2 - test/develop/integrate python 3.0 code natively under robust, software-rich python 2.5 environment (Python) 2008-06-28T03:23:07-07:00kai zhuhttp://code.activestate.com/recipes/users/4160222/http://code.activestate.com/recipes/574436-py3to2-testdevelopintegrate-python-30-code-nativel/ <p style="color: grey"> Python recipe 574436 by <a href="/recipes/users/4160222/">kai zhu</a> (<a href="/recipes/tags/extending/">extending</a>). </p> py3to2 - test/develop/integrate python 3.0 code natively under robust, software-rich python 2.5 environment (Python) 2008-06-28T03:24:13-07:00kai zhuhttp://code.activestate.com/recipes/users/4160222/http://code.activestate.com/recipes/574437-py3to2-testdevelopintegrate-python-30-code-nativel/ <p style="color: grey"> Python recipe 574437 by <a href="/recipes/users/4160222/">kai zhu</a> (<a href="/recipes/tags/extending/">extending</a>). </p> Selective cleanup (deletion) of files (based on category and extension) for use by a SABnzbd+ external post-processing script (Python) 2009-07-19T01:18:17-07:00A Bhttp://code.activestate.com/recipes/users/4145641/http://code.activestate.com/recipes/573440-selective-cleanup-deletion-of-files-based-on-categ/ <p style="color: grey"> Python recipe 573440 by <a href="/recipes/users/4145641/">A B</a> (<a href="/recipes/tags/extending/">extending</a>, <a href="/recipes/tags/post_processing/">post_processing</a>, <a href="/recipes/tags/post_processing_script/">post_processing_script</a>, <a href="/recipes/tags/sabnzbd/">sabnzbd</a>, <a href="/recipes/tags/sabnzbdp/">sabnzbdp</a>). Revision 11. </p> <p>This script clean's up (deletes) files with specific extensions, e.g. .sfv, .nzb, but only for downloads belonging to a particular category.</p> A simple calculator that works with whole numbers written in C/Python. (Python) 2008-05-16T14:07:41-07:00Nycholas Oliveira e Oliveirahttp://code.activestate.com/recipes/users/2761295/http://code.activestate.com/recipes/572205-a-simple-calculator-that-works-with-whole-numbers-/ <p style="color: grey"> Python recipe 572205 by <a href="/recipes/users/2761295/">Nycholas Oliveira e Oliveira</a> (<a href="/recipes/tags/extending/">extending</a>). </p> <p>A simple calculator that works with whole numbers written in C/Python.</p> <p>The purpose of this implementation is only to show how a simple extension C/Python that can help people who are starting with C/Python.</p> <p>In this cookbook show the two codes in C, the first is the pure C and the second is the implementation for Python.</p> <p>I hope this cookbook possar help.</p> <p>-- Until more and good luck, Nycholas de Oliveira and Oliveira.</p> List wrapper for generators (indexable, subscriptable) (Python) 2007-10-19T08:53:32-07:00Florian Leitnerhttp://code.activestate.com/recipes/users/4049249/http://code.activestate.com/recipes/534114-list-wrapper-for-generators-indexable-subscriptabl/ <p style="color: grey"> Python recipe 534114 by <a href="/recipes/users/4049249/">Florian Leitner</a> (<a href="/recipes/tags/extending/">extending</a>). Revision 4. </p> <p>Similar to &lt;a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/523026"&gt;Andrew Moffat's recipe&lt;/a&gt;, this class takes a generator function as an argument and returns a list object where the generator's items can be accessed by indices (as indices or slices). Only once a certain index value was requested, it actually iterates the generator to that point. See docstrings for more. The values are stored in a Berkeley DB which is created in a temporary file on the fly (it would not need much to modify the code to store in memory if you would prefer that) in such a manner that for any object where id(obj1) == id(obj2) is True, only one entry is stored.</p> Automatic ref-count management in C++ using a smart ptr (Python) 2007-08-14T15:41:44-07:00Ian Eloffhttp://code.activestate.com/recipes/users/2227021/http://code.activestate.com/recipes/528875-automatic-ref-count-management-in-c-using-a-smart-/ <p style="color: grey"> Python recipe 528875 by <a href="/recipes/users/2227021/">Ian Eloff</a> (<a href="/recipes/tags/extending/">extending</a>). </p> <p>Managing ref-counting is a complex and error prone business. If you choose C++ to extend or embed Python, you can simply use a modification on std::auto_ptr. Instead of calling delete on the managed pointer, it will decref it.</p> <p>So now you can do:</p> <p>auto_py str = PyStr_FromString("Hello World!");</p> <p>and forget about having to decref it altogether! Just like auto_ptr you can get the PyObject * with get(), release it from managed control with release(), and rebind it with reset(new_ptr). You can also incref it by calling inc(), but be cautious as you can easily create a leak by increfing once to much.</p> subscriptable generator (Python) 2007-07-08T11:00:48-07:00Andrew Moffathttp://code.activestate.com/recipes/users/2979564/http://code.activestate.com/recipes/523026-subscriptable-generator/ <p style="color: grey"> Python recipe 523026 by <a href="/recipes/users/2979564/">Andrew Moffat</a> (<a href="/recipes/tags/extending/">extending</a>). Revision 4. </p> <p>let's you define a generator with an internal cache that can be accessed like a list</p> Python distutils + pkg-config (Python) 2007-02-26T23:14:40-08:00Micah Dowtyhttp://code.activestate.com/recipes/users/4037444/http://code.activestate.com/recipes/502261-python-distutils-pkg-config/ <p style="color: grey"> Python recipe 502261 by <a href="/recipes/users/4037444/">Micah Dowty</a> (<a href="/recipes/tags/extending/">extending</a>). </p> <p>This is a short and sweet recipe for using pkg-config with your distutils extension modules.</p> Keyed Dictionary -- strict key values in a dictionary (Python) 2006-12-04T21:52:58-08:00Mike Hostetlerhttp://code.activestate.com/recipes/users/138739/http://code.activestate.com/recipes/499296-keyed-dictionary-strict-key-values-in-a-dictionary/ <p style="color: grey"> Python recipe 499296 by <a href="/recipes/users/138739/">Mike Hostetler</a> (<a href="/recipes/tags/extending/">extending</a>). Revision 2. </p> <p>The KeyedDict object keeps a list of approved key values. If you try to assigned an unapproved key to a value, an exception is thrown.</p> <p>When you create the object, you give it a sequence (tuple, list, etc.) of key objects and then, when you set an item with a key, it will make sure that the key is in the "approved" list. If you try to set an item with a key that isn't in your approved list, you get an TypeError exception.</p> weakmethod (Python) 2006-09-29T01:45:31-07:00tomer filibahttp://code.activestate.com/recipes/users/2520014/http://code.activestate.com/recipes/498147-weakmethod/ <p style="color: grey"> Python recipe 498147 by <a href="/recipes/users/2520014/">tomer filiba</a> (<a href="/recipes/tags/extending/">extending</a>). </p> <p>Weakly-bound methods: use this decorator to create methods that weakly-reference their instance (im_self). This means that the method itself will not keep the object alive. Useful for callbacks, caches, and avoiding cyclic references.</p> Readable switch construction without lambdas or dictionaries (Python) 2005-04-26T10:51:04-07:00Brian Beckhttp://code.activestate.com/recipes/users/2425329/http://code.activestate.com/recipes/410692-readable-switch-construction-without-lambdas-or-di/ <p style="color: grey"> Python recipe 410692 by <a href="/recipes/users/2425329/">Brian Beck</a> (<a href="/recipes/tags/extending/">extending</a>). Revision 8. </p> <p>Python's lack of a 'switch' statement has garnered much discussion and even a PEP. The most popular substitute uses dictionaries to map cases to functions, which requires lots of defs or lambdas. While the approach shown here may be O(n) for cases, it aims to duplicate C's original 'switch' functionality and structure with reasonable accuracy.</p> A more clean implementation for Ordered Dictionary (Python) 2006-05-31T20:56:26-07:00Igor Ghisihttp://code.activestate.com/recipes/users/2907747/http://code.activestate.com/recipes/496761-a-more-clean-implementation-for-ordered-dictionary/ <p style="color: grey"> Python recipe 496761 by <a href="/recipes/users/2907747/">Igor Ghisi</a> (<a href="/recipes/tags/extending/">extending</a>). </p> <p>An Ordered Dict records the order in which items are added. This implementation uses much less code than the others by extending not well-known class DictMixin.</p> Real Mixins (Python) 2006-09-22T14:58:53-07:00tomer filibahttp://code.activestate.com/recipes/users/2520014/http://code.activestate.com/recipes/498124-real-mixins/ <p style="color: grey"> Python recipe 498124 by <a href="/recipes/users/2520014/">tomer filiba</a> (<a href="/recipes/tags/extending/">extending</a>). Revision 2. </p> <p>This code here creates real mixed-in classes: it actually merges one class into another (c-python specific), taking care of name-mangling, some complications with __slots__, and everything else. As a side-effect, you can also use it to mix modules into classes. Similar to ruby's include statement.</p>