Most viewed recipes tagged "descriptor"http://code.activestate.com/recipes/tags/descriptor/views/2016-07-31T04:03:29-07:00ActiveState Code RecipesBound Inner Classes (Python) 2013-12-10T02:36:25-08:00Larry Hastingshttp://code.activestate.com/recipes/users/2467657/http://code.activestate.com/recipes/577070-bound-inner-classes/ <p style="color: grey"> Python recipe 577070 by <a href="/recipes/users/2467657/">Larry Hastings</a> (<a href="/recipes/tags/class_decorator/">class_decorator</a>, <a href="/recipes/tags/descriptor/">descriptor</a>). Revision 8. </p> <p>This recipe provides the class decorator <code>BoundInnerClass</code>. The decorator makes inner classes symmetric with method calls. Functions declared inside classes become "methods", and when you call them through an object they automatically get a reference to "self". The <code>BoundInnerClass</code> decorator makes this work for inner classes: an inner class decorated with <code>BoundInnerClass</code> gets a reference to that same (now "outer") object passed in automatically to the inner class's <code>__init__</code>.</p> <p>The recipe works unchanged in Python 2.6 and 3.1, and is licensed using the Zlib license.</p> Docstring inheritance decorator (Python) 2009-07-28T13:42:32-07:00Shai Bergerhttp://code.activestate.com/recipes/users/2014324/http://code.activestate.com/recipes/576862-docstring-inheritance-decorator/ <p style="color: grey"> Python recipe 576862 by <a href="/recipes/users/2014324/">Shai Berger</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/descriptor/">descriptor</a>, <a href="/recipes/tags/docstring/">docstring</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/metaprogramming/">metaprogramming</a>). </p> <p>In many cases, a subclass overrides a method in a parent class, just to change its implementation; in such cases, it would be nice to preserve the overridden method's docstring. The decorator below can be used to achieve this without explicit reference to the parent class. It does this by replacing the function with a descriptor, which accesses the parent class when the method is accessed as an attribute.</p> Observer pattern implemented with Descriptor class (Python) 2010-03-13T11:53:18-08:00Rodney Drenthhttp://code.activestate.com/recipes/users/4050661/http://code.activestate.com/recipes/576979-observer-pattern-implemented-with-descriptor-class/ <p style="color: grey"> Python recipe 576979 by <a href="/recipes/users/4050661/">Rodney Drenth</a> (<a href="/recipes/tags/descriptor/">descriptor</a>, <a href="/recipes/tags/observer/">observer</a>, <a href="/recipes/tags/publish/">publish</a>, <a href="/recipes/tags/subscribe/">subscribe</a>). Revision 5. </p> <p>The observer pattern is implemented using an observable descriptor. One creates instances of observable in a class, which allows observers to subscribe to changes in the observable. Assigning a value to the observable causes the suscribers to be notified. An observable can subscribe to another observable, in which case changes to the second propagate to subscribers of the first. The subscribe method returns a Subscription object. When this object is deleted or becomes unreferenced, the subscription is cancelled.</p> Alias class attributes (Python) 2009-06-01T01:19:55-07:00Euan Goddardhttp://code.activestate.com/recipes/users/4170559/http://code.activestate.com/recipes/576787-alias-class-attributes/ <p style="color: grey"> Python recipe 576787 by <a href="/recipes/users/4170559/">Euan Goddard</a> (<a href="/recipes/tags/alias/">alias</a>, <a href="/recipes/tags/attribute/">attribute</a>, <a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/descriptor/">descriptor</a>, <a href="/recipes/tags/metaclass/">metaclass</a>). </p> <p>The alias metaclass allows a declarative way of creating aliases to attributes on a class. The main purpose of which is to adapt classes for duck-typing (see doc-string for me detail).</p> Emulating super() in Python 3.x using Python 2.7 (Python) 2016-07-31T04:03:29-07:00sunqingyaohttp://code.activestate.com/recipes/users/4194518/http://code.activestate.com/recipes/580694-emulating-super-in-python-3x-using-python-27/ <p style="color: grey"> Python recipe 580694 by <a href="/recipes/users/4194518/">sunqingyao</a> (<a href="/recipes/tags/beginner/">beginner</a>, <a href="/recipes/tags/descriptor/">descriptor</a>, <a href="/recipes/tags/metaclass/">metaclass</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/python2/">python2</a>). </p> <p>Depending on the name of the first argument, <code>self.__sup</code> or <code>cls.__sup</code> behaves like <code>super()</code> in Python 3, while this code is written in Python 2.7.</p> <p>It works for both ordinary methods and class methods(static methods don't use <code>super()</code>). See my code for detailed examples:</p> dualmethod descriptor (Python) 2010-02-06T20:54:45-08:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577030-dualmethod-descriptor/ <p style="color: grey"> Python recipe 577030 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/descriptor/">descriptor</a>, <a href="/recipes/tags/method/">method</a>). Revision 3. </p> <p>This descriptor can be used to decorate methods, similar to the built-ins classmethod and staticmethod. It enables the caller to call methods on either the class or an instance, and the first argument passed to the method will be the class or the instance respectively.</p> <p>This differs from classmethods, which always passes the class, and staticmethods, which don't pass either.</p> <p>Like all descriptors, you can only use this in new-style classes.</p> Threadsafe observer pattern implemented as descriptor (Python) 2010-03-13T12:17:11-08:00Rodney Drenthhttp://code.activestate.com/recipes/users/4050661/http://code.activestate.com/recipes/577106-threadsafe-observer-pattern-implemented-as-descrip/ <p style="color: grey"> Python recipe 577106 by <a href="/recipes/users/4050661/">Rodney Drenth</a> (<a href="/recipes/tags/descriptor/">descriptor</a>, <a href="/recipes/tags/observer/">observer</a>, <a href="/recipes/tags/publish/">publish</a>, <a href="/recipes/tags/subscribe/">subscribe</a>, <a href="/recipes/tags/threads/">threads</a>). </p> <p>This is a threadsafe version of <a href="http://code.activestate.com/recipes/576979/">recipe 576979</a>. A publish-subscribe (observer) pattern is implemented as a descriptor. Assigning a value notifies the observers. Uses <a href="http://code.activestate.com/recipes/577105/">recipe 577105</a> as synchlock.py and <a href="http://code.activestate.com/recipes/576979/">recipe 576979</a> as Observer.py</p> Item Properties (Python) 2012-05-09T23:24:55-07:00Ian Kellyhttp://code.activestate.com/recipes/users/4178016/http://code.activestate.com/recipes/577703-item-properties/ <p style="color: grey"> Python recipe 577703 by <a href="/recipes/users/4178016/">Ian Kelly</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/descriptor/">descriptor</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/property/">property</a>). Revision 2. </p> <p>A property variation that allows property access using an index or key.</p> Lazy Dynamic Binding on Classes (You'll Never Go Back) (Python) 2011-08-12T23:42:27-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577745-lazy-dynamic-binding-on-classes-youll-never-go-bac/ <p style="color: grey"> Python recipe 577745 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/descriptor/">descriptor</a>, <a href="/recipes/tags/descriptors/">descriptors</a>). Revision 4. </p> <p>This recipe provides a descriptor class and a decorator to make the deferred binding. Before the first access to that name, the __dict__ of the class will have the descriptor object. After that first access it will have whatever was returned by the first access.</p> <p>One big advantage of deferring the binding is that the class object will be available at that time and can be passed to the object. During normal class creation the class body is exec'd before the class object is created, so the objects bound there can't be passed the class.</p> <p><a href="http://code.activestate.com/recipes/577746/">Recipe #577746</a> provides a concrete example of how the deferred_binder can be used.</p> Components and Abilities (different implementation of Component architecture) (Python) 2009-07-23T13:26:32-07:00Danny Ghttp://code.activestate.com/recipes/users/4164396/http://code.activestate.com/recipes/576854-components-and-abilities-different-implementation-/ <p style="color: grey"> Python recipe 576854 by <a href="/recipes/users/4164396/">Danny G</a> (<a href="/recipes/tags/abc/">abc</a>, <a href="/recipes/tags/ability/">ability</a>, <a href="/recipes/tags/abstract_base_class/">abstract_base_class</a>, <a href="/recipes/tags/component/">component</a>, <a href="/recipes/tags/descriptor/">descriptor</a>, <a href="/recipes/tags/dynamic/">dynamic</a>, <a href="/recipes/tags/interface/">interface</a>, <a href="/recipes/tags/plugin/">plugin</a>). Revision 6. </p> <p>I define a 'Component' as an attribute (typing optional) that instances can assign objects to. Nothing special there, but their usefulness comes in with 'Abilities'. If a class inherits from 'ClassWithAbilities', it will be given a special attribute 'abilities' that will grow/shrink when other classes with abilities are assigned to an instances attributes. It increases/decreases the functionality of the instance depending on what objects are assigned to it. All of these abilities are accessed through the 'abilities' attribute. This is a redesign of <a href="http://code.activestate.com/recipes/576852/"><a href="http://code.activestate.com/recipes/576852/">Recipe 576852</a></a>, but I believe is different enough to warrant a new recipe.</p> Observer pattern implemented with Descriptor class (Python) 2012-02-14T23:43:53-08:00Rodney Drenthhttp://code.activestate.com/recipes/users/4050661/http://code.activestate.com/recipes/578038-observer-pattern-implemented-with-descriptor-class/ <p style="color: grey"> Python recipe 578038 by <a href="/recipes/users/4050661/">Rodney Drenth</a> (<a href="/recipes/tags/descriptor/">descriptor</a>, <a href="/recipes/tags/observer/">observer</a>, <a href="/recipes/tags/publish/">publish</a>, <a href="/recipes/tags/reactive/">reactive</a>, <a href="/recipes/tags/subscribe/">subscribe</a>). </p> <p>The observer pattern is implemented using an observable descriptor. One creates instances of observable in a class, which allows observers to subscribe to changes in the observable. Assigning a value to the observable causes the suscribers to be notified. An observable can subscribe to another observable, in which case changes to the second propagate to subscribers of the first. The subscribe method returns a Subscription object. When this object is deleted or becomes unreferenced, the subscription is cancelled.</p> <p>This version compatible with Python 3.0 Example uses unittest to help in understanding the functionality.</p>