Popular recipes tagged "abc" but not "plugin"http://code.activestate.com/recipes/tags/abc-plugin/2011-08-13T03:49:26-07:00ActiveState Code Recipesnamedtuple.abc - abstract base class + mix-in for named tuples (Python) 2011-04-02T02:07:00-07:00Jan Kaliszewskihttp://code.activestate.com/recipes/users/4172762/http://code.activestate.com/recipes/577629-namedtupleabc-abstract-base-class-mix-in-for-named/ <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 Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577711-validating-classes-and-objects-against-an-abstract/ <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/abc.py and objects/typeobject.c</p> Adding __implements__ to subclasses during ABCMeta.register (Python) 2011-08-13T03:49:26-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577712-adding-__implements__-to-subclasses-during-abcmeta/ <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="http://code.activestate.com/recipes/577711/">recipe 577711</a>.</p> <p>Basically it has ABCMeta.register add __implements__ to any subclass that gets registered.</p> Immutable Type Hierarchies (Python) 2010-10-11T23:32:31-07:00Aaron Sterlinghttp://code.activestate.com/recipes/users/4174675/http://code.activestate.com/recipes/577424-immutable-type-hierarchies/ <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>