Latest recipes tagged "metaclasses"http://code.activestate.com/recipes/tags/metaclasses/new/2013-10-24T11:31:44-07:00ActiveState Code Recipesdynamically changing encoder (for json) with metaclass (class factory) (Python)
2013-10-24T11:31:44-07:00-http://code.activestate.com/recipes/users/4188267/http://code.activestate.com/recipes/578696-dynamically-changing-encoder-for-json-with-metacla/
<p style="color: grey">
Python
recipe 578696
by <a href="/recipes/users/4188267/">-</a>
(<a href="/recipes/tags/class_factory/">class_factory</a>, <a href="/recipes/tags/encode/">encode</a>, <a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/metaclass/">metaclass</a>, <a href="/recipes/tags/metaclasses/">metaclasses</a>).
</p>
<p>The <em>json.dumps</em> need to be feed with some class (cls =someClass) but what if we want to change the class dynamically?
This example can be done by declaring the <em>listOfClasses</em> in class level of course, but the idea is to be changeable. This can be done by the class factory function <em>encoderFacory</em></p>
Bless Classes into Singletons (Python)
2011-09-21T05:34:52-07:00Anand B Pillaihttp://code.activestate.com/recipes/users/4169530/http://code.activestate.com/recipes/577875-bless-classes-into-singletons/
<p style="color: grey">
Python
recipe 577875
by <a href="/recipes/users/4169530/">Anand B Pillai</a>
(<a href="/recipes/tags/metaclasses/">metaclasses</a>, <a href="/recipes/tags/patterns/">patterns</a>, <a href="/recipes/tags/singleton/">singleton</a>).
</p>
<p>A pattern using metaclasses for "blessing" classes into Singletons.</p>
Using Metaclasses and Class Decorators to Inherit Function Docstrings (Python)
2011-06-11T00:59:35-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577743-using-metaclasses-and-class-decorators-to-inherit-/
<p style="color: grey">
Python
recipe 577743
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/docstring/">docstring</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/metaclass/">metaclass</a>, <a href="/recipes/tags/metaclasses/">metaclasses</a>).
Revision 4.
</p>
<p>You'll find three different approaches to copying the method's docstring to the overriding method on a child class.</p>
<p>The function decorator approach is limited by the fact that you have to know the class when you call the decorator, so it can't be used inside a class body. However, <a href="http://code.activestate.com/recipes/577746/">recipe #577746</a> provides a function decorator that does not have this limitation.</p>
Easy State Pattern - support for implementing state machines (Python)
2009-06-15T11:25:17-07:00Rodney Drenthhttp://code.activestate.com/recipes/users/4050661/http://code.activestate.com/recipes/576613-easy-state-pattern-support-for-implementing-state-/
<p style="color: grey">
Python
recipe 576613
by <a href="/recipes/users/4050661/">Rodney Drenth</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/metaclasses/">metaclasses</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/state_machine/">state_machine</a>, <a href="/recipes/tags/state_pattern/">state_pattern</a>).
Revision 3.
</p>
<p>Provides is a module that gives support for implementing state machines. States are implemented as subclasses, derived from the state machine class. Methods that are state dependant or which cause transitions are declared using decorators. Because states can be subclasses of other states, common behaviour among several states is easily supported. The implementation allows for implementing multiple states or substates within a class.</p>
<p>This module best support statem achines implemeting controllers for embedded systems, implementing user interfaces, or in discrete event model simulation.
Parsers, which generally have many states and where you would need to define
a Transaction method for each different character encountered would be more easily implemented by other means.</p>