Top-rated recipes tagged "extending" Code RecipesReadable switch construction without lambdas or dictionaries (Python) 2005-04-26T10:51:04-07:00Brian Beck <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> Find all subclasses of a given class (Python) 2009-11-04T20:26:08-08:00Gabriel Genellina <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> Container (Python) 2006-05-13T07:37:50-07:00tomer filiba <p style="color: grey"> Python recipe 496697 by <a href="/recipes/users/2520014/">tomer filiba</a> (<a href="/recipes/tags/extending/">extending</a>). </p> <p>A generic attribute container, with pretty printing and recursion protection</p> Python distutils + pkg-config (Python) 2007-02-26T23:14:40-08:00Micah Dowty <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> Object Proxying (Python) 2006-05-26T03:44:09-07:00tomer filiba <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> Windows Dll (with pointers parameters) calling with ctypes and calldll (Python) 2004-11-07T16:53:38-08:00Stefano Spinucci <p style="color: grey"> Python recipe 181063 by <a href="/recipes/users/787328/">Stefano Spinucci</a> (<a href="/recipes/tags/extending/">extending</a>). Revision 3. </p> <p>Code to call a Windows Dll """void FAR PASCAL hllapi(int FAR *, char FAR *, int FAR *, int FAR *)""" with ctypes (trivial) and calldll (a nightmare).</p> Defining Python class methods in C (Python) 2001-04-25T10:22:15-07:00Brent Burley <p style="color: grey"> Python recipe 54352 by <a href="/recipes/users/98036/">Brent Burley</a> (<a href="/recipes/tags/extending/">extending</a>). </p> <p>This recipe shows how to define a new Python class from a C extension module. The class methods are implemented in C, but the class can still be instantiated, extended, subclassed, etc. from Python. The same technique can also be used to extend an existing Python class with methods written in C.</p> weakmethod (Python) 2006-09-29T01:45:31-07:00tomer filiba <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> ExceptionContainer (Python) 2006-05-13T07:43:50-07:00tomer filiba <p style="color: grey"> Python recipe 496698 by <a href="/recipes/users/2520014/">tomer filiba</a> (<a href="/recipes/tags/extending/">extending</a>). </p> <p>an Exception base-class that supports keyword arguments and pretty printing</p> Automatic ref-count management in C++ using a smart ptr (Python) 2007-08-14T15:41:44-07:00Ian Eloff <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 Moffat <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> A more clean implementation for Ordered Dictionary (Python) 2006-05-31T20:56:26-07:00Igor Ghisi <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> ShelfProxy (Python) 2006-05-26T14:40:18-07:00tomer filiba <p style="color: grey"> Python recipe 496742 by <a href="/recipes/users/2520014/">tomer filiba</a> (<a href="/recipes/tags/extending/">extending</a>). Revision 2. </p> <p>Uses object proxying to expose a more intuitive interface to shelves. Requires the Proxy recipe (<a href="" rel="nofollow"></a></p> Import Modules/Discover methods from a directory name (Python) 2005-07-15T13:15:48-07:00Jesse Noller <p style="color: grey"> Python recipe 436873 by <a href="/recipes/users/2519072/">Jesse Noller</a> (<a href="/recipes/tags/extending/">extending</a>). Revision 2. </p> <p>For something I am working on, I needed the ability to scan a supplied directory, adding the directory to the sys.path within Python, and then blanket import the modules within that directory. Following that, I had to filter any builtin or special methods within those modules and return a list of the methods for the module I had imported.</p> <p>The script is very simplistic in what it does.</p> Simple Singleton (Python) 2005-04-28T12:14:52-07:00Daniel Brodie <p style="color: grey"> Python recipe 412551 by <a href="/recipes/users/1892511/">Daniel Brodie</a> (<a href="/recipes/tags/extending/">extending</a>). </p> <p>This is an implementation of the singleton without using the __new__ class but by implementing the __call__ method in the metaclass.</p> Functions with Strictly Typed Arguments (Python) 2004-11-05T08:32:24-08:00S W <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 simple extension type for Python (Python) 2001-08-15T16:35:12-07:00Alex Martelli <p style="color: grey"> Python recipe 66509 by <a href="/recipes/users/97991/">Alex Martelli</a> (<a href="/recipes/tags/extending/">extending</a>). Revision 2. </p> <p>C-coded Python extension types have an aura of mystery and difficulty they don't really deserve. Sure, it's a lot of work to implement every possible nicety, but a fundamental useful type doesn't take much.</p> Use good old INI files for configuration (Python) 2001-06-25T05:00:24-07:00Dirk Holtwick <p style="color: grey"> Python recipe 65334 by <a href="/recipes/users/98196/">Dirk Holtwick</a> (<a href="/recipes/tags/extending/">extending</a>). </p> <p>Many people use Python modules as config files, but this way your program may be manipulated or an syntax error may come into that file. Try the small script here to use the good old INI config files, known from Windows.</p> Lazy Load Object Proxying (Python) 2012-01-13T07:56:29-08:00Cory Virok <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:00samh <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>