Most viewed recipes tagged "abc" Code - abstract base class + mix-in for named tuples (Python) 2011-04-02T02:07:00-07:00Jan Kaliszewski <p style="color: grey"> Python recipe 577629 by <a href="/recipes/users/4172762/">Jan Kaliszewski</a> (<a href="/recipes/tags/abc/">abc</a>, <a href="/recipes/tags/collections/">collections</a>, <a href="/recipes/tags/dry/">dry</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/namedtuple/">namedtuple</a>, <a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 7. </p> <p>If you need</p> <ul> <li>to define <strong>named tuple subclasses</strong> (including reusable abstract ones), adding/overriding some methods, in a convenient way;</li> <li>to have the named tuple ABC (abstract base class) for <strong>isinstance/issubclass</strong> tests;</li> <li>or simply would like to define your named tuple classes in a <strong>class-syntax-based and DRY way</strong> (without repeating type names...)</li> </ul> <p>-- <strong>this recipe is for you.</strong></p> Validating classes and objects against an Abstract Base Class (Python) 2011-05-21T19:14:19-07:00Eric Snow <p style="color: grey"> Python recipe 577711 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/abc/">abc</a>, <a href="/recipes/tags/validation/">validation</a>). Revision 4. </p> <p>Abstract Bases Classes in Python provide great features for describing interfaces programmatically. By default a subclass is validated against all its ABC parents at instantiation time (in object.__new__). This recipe aims to provide for validation against an ABC of:</p> <ul> <li>any class at definition time (including subclasses and registered classes),</li> <li>any object at any time.</li> </ul> <p>I have included an example of the reason I did all this. It allows you to implement an ABC in the instance rather than the class.</p> <p>If the classes argument to validate is None then it tries to build the list of classes from the object's MRO. If the ABCMeta.register method facilitated an __implements__ list on classes, we could also use that to validate against the registered "base" classes.</p> <p>The code I have provided is for Python 3, but it should work in 2.7 with a little modification.</p> <p>This code borrows from Lib/ and objects/typeobject.c</p> Component architecture through data descriptors and function decorators (Python) 2009-07-23T11:13:48-07:00Danny G <p style="color: grey"> Python recipe 576852 by <a href="/recipes/users/4164396/">Danny G</a> (<a href="/recipes/tags/abc/">abc</a>, <a href="/recipes/tags/abstract_base_class/">abstract_base_class</a>, <a href="/recipes/tags/component/">component</a>, <a href="/recipes/tags/interface/">interface</a>, <a href="/recipes/tags/plugin/">plugin</a>). Revision 5. </p> <p>My desire was to design a class with defined attributes that when assigned on instances, would expand the instance's functionality. In other words if I create an instance of class A, then assign a 'component' attribute upon that instance, I should be able to call methods of the component object through the original instance. I believe this is somewhat similar to interfaces and abstract base classes (and I read up on both a bit), but I want to rely more on introspection of the object to see what it can do versus confining it to a set interface.</p> Components and Abilities (different implementation of Component architecture) (Python) 2009-07-23T13:26:32-07:00Danny G <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=""><a href="">Recipe 576852</a></a>, but I believe is different enough to warrant a new recipe.</p> Immutable Type Hierarchies (Python) 2010-10-11T23:32:31-07:00Aaron Sterling <p style="color: grey"> Python recipe 577424 by <a href="/recipes/users/4174675/">Aaron Sterling</a> (<a href="/recipes/tags/abc/">abc</a>, <a href="/recipes/tags/hierarchy/">hierarchy</a>, <a href="/recipes/tags/immutable/">immutable</a>). Revision 4. </p> <p>Allows for type hierarchies of immutable types by faking inheritance using dict updates and abc's.</p> Adding __implements__ to subclasses during ABCMeta.register (Python) 2011-08-13T03:49:26-07:00Eric Snow <p style="color: grey"> Python recipe 577712 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/abc/">abc</a>). </p> <p>This is an extension to the abc.ABCMeta class. It is related to <a href="">recipe 577711</a>.</p> <p>Basically it has ABCMeta.register add __implements__ to any subclass that gets registered.</p>