/usr/share/doc/python-egenix-mxtools-doc/examples/Acquisition.py is in python-egenix-mxtools-doc 3.2.1-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | import mx.Tools.NewBuiltins
class AcquisitionMixin:
""" Mixin class allowing implicit acquisition of attributes from
objects up a containment hierachy.
To enable this feature, an object 'b' contained in another
object 'a' must set the instance variable b.baseobj to
a. The search is done recursively until either an exception
is raised by the queried object or the object is found.
Attributes that strt with an underscore ('_') can not
be acquired. Be careful when acquiring methods: the method
will be executed in the baseobjects context, not the one
it is acquired into.
See the documentation of acquire() for details.
"""
baseobj = None # Base object ("containing" this object)
# Implicit acquisition
__getattr__ = acquire # Use the (new) builtin acquire() to implement
# the acquisition mechanism
def __init__(self,*args,**kw):
""" Create the object and call startup(*args,**kw)
- if you give a keyword 'baseobj' then its parameter
is used to set the baseobj for acquisition; the keyword
is then deleted from kw
"""
# Set baseobj for acquisition to work properly
baseobj = kw.get('baseobj',None)
if baseobj is not None:
self.baseobj = baseobj
del kw['baseobj']
# call startup()
apply(self.startup,args,kw)
def startup(self,*args,**kw):
""" Override this to init the instance.
"""
pass
class PythonAcquisitionMixin(AcquisitionMixin):
def __getattr__(self,name,
getattr=getattr,AttributeError=AttributeError):
if name[0] != '_':
return getattr(self.baseobj,name)
raise AttributeError,name
# Localize these here for speed
baseobj = AcquisitionMixin.baseobj
__init__ = AcquisitionMixin.__init__
startup = AcquisitionMixin.startup
def _test():
class C(AcquisitionMixin):
a = 2
x = 9
class D(AcquisitionMixin):
b = 3
class E(AcquisitionMixin):
c = 4
# Setup acquisition chain
c = C()
d = D(baseobj=c)
e = E(baseobj=d)
# Give implicit acquisition a go...
print c.a, d.a, e.a
print d.b, e.b
print e.c
d.a = 5
print c.a, d.a, e.a
c.b = 1
print c.b, d.b, e.b
print
# Performance comparism:
print 'Performance:'
print
import time
l = range(100000)
class C(AcquisitionMixin):
x = 9
class D(AcquisitionMixin):
y = 8
class E(AcquisitionMixin):
z = 7
# Setup acquisition chain
c = C()
d = D(baseobj=c)
e = E(baseobj=d)
t = time.time()
for i in l:
e.x
e.y
e.z
print 'AcquisitionMixin:',time.time() - t,'seconds'
class C(PythonAcquisitionMixin):
x = 9
class D(PythonAcquisitionMixin):
y = 8
class E(PythonAcquisitionMixin):
z = 7
# Setup acquisition chain
c = C()
d = D(baseobj=c)
e = E(baseobj=d)
t = time.time()
for i in l:
e.x
e.y
e.z
print 'PythonAcquisitionMixin:',time.time() - t,'seconds'
if __name__ == '__main__':
_test()
|