August 2010
Intermediate to advanced
376 pages
10h 6m
English
"""
DCI proof of concept
Context is a separate object to the Collaboration (again for
exploration of alternatives). Made a class for it, but a
Dictionary is also possible.
Author: David Byers, Serge Beaumont
7 October 2008
"""
import new
class Role(object):
"""A Role is a special class that never gets
instantiated directly. Instead, when the user wants
to create a new role instance, we create a new class
that has the role and another object's class
as its superclasses, then create an instance of that
class, and link the new object's dict to the original
object's dict."""
def __new__(cls, ob):
members=dict(__ob__=ob)
if hasattr(ob.__class__, '__slots__'):
members['__setattr__']=Role.__setattrmembers['__getattr__']=Role.__getattr members['__delattr__']=Role.__delattr c = new.classobj("%s as %s.%s" % (ob.__class__.__name__, cls.__module__, cls.__name__), (cls, ob.__class__), members) i = object.__new__(c) if hasattr(ob, '__dict__'): i.__dict__=ob.__dict__ return i def __init__(self, ob): """Do not call the superclass __init__. If we did, then we would call the __init__ function in the real class hierarchy too (i.e. Account, in this example)""" pass def __getattr(self, attr): """Proxy to object""" return getattr(self.__ob__, attr) def __setattr(self, attr, val): """Proxy to object""" setattr(self.__ob__, attr, val) def __delattr(self, attr): """Proxy to object""" delattr(self.__ob__, attr) class MoneySource(Role): def transfer_to(self, ctx, amount): ...Read now
Unlock full access