1 #=======================================================================
3 __version__
= '''0.0.04'''
4 __sub_version__
= '''20040911234403'''
5 __copyright__
= '''(c) Alex A. Naanou 2003'''
8 #-----------------------------------------------------------------------
15 #-----------------------------------------------------------------------
16 #----------------------------------------createmethodwrappersinobject---
17 ##!!! REVISE (rewrite or remove) !!!##
18 def createmethodwrappersinobject(source_obj
, method_list
, wrapper
, target_obj
):
20 this will attach methods mentioned in method_list to the target object
21 that will wrap methods of the source object.
23 WARNING: this will render the target object unpicklable...
24 NOTE: this can be used for classes...
26 for meth
in method_list
:
27 if hasattr(source_obj
, meth
):
28 setattr(target_obj
, meth
, wrapper(getattr(source_dict
, meth
)))
32 #------------------------------------------------createmethodwrappers---
33 ##!!! REVISE (rewrite or remove) !!!##
34 def createmethodwrappers(source_dict
, method_list
, wrapper
, target_dict
=None):
36 this will wrap methods mentioned in method_list from the source dict and
37 will return a dict containing the wrappers (will update and return target_dict
40 if target_dict
!= None:
44 for meth
in method_list
:
45 if meth
in source_dict
:
46 res
[meth
] = wrapper(source_dict
[meth
])
51 #-----------------------------------------------------------------------
52 #--------------------------------------------------genericproxymethod---
53 def proxymethod(method_name
, source_attr
, depth
=1):
55 this will create a proxy to the method name in the containing namespace.
57 NOTE: this will add the method_name to the containing namespace.
58 NOTE: source_attr is to be used as the attr name referencing the source object.
60 # text of the new function....
62 def %(method_name)s(self, *p, **n):
64 this is the proxy to %(method_name)s method.
66 return self.%(source_attr)s.%(method_name)s(*p, **n)
67 proxy = %(method_name)s'''
68 # execute the above code...
69 exec (txt
% {'method_name': method_name
, 'source_attr': source_attr
})
71 sys
._getframe
(depth
).f_locals
[method_name
] = proxy
74 #--------------------------------------------------------proxymethods---
75 def proxymethods(names
, source_attr
):
77 this will generate a direct proxy for each name.
80 proxymethod(name
, source_attr
, depth
=2)
84 #-----------------------------------------------------------------------
85 #------------------------------------------------------swapmethodself---
86 def swapmethodself(meth
, wrapper
, use_wrapper_as_class
=True):
89 return new
.instancemethod(meth
.im_func
,
91 use_wrapper_as_class
and \
92 wrapper
or meth
.im_class
)
95 #------------------------------------------------------wrapmethodself---
96 def wrapmethodself(meth
, wrapper
, use_wrapper_as_class
=True):
99 return new
.instancemethod(meth
.im_func
,
100 wrapper(meth
.im_self
),
101 use_wrapper_as_class
and \
102 wrapper
or meth
.im_class
)
106 #=======================================================================
107 # vim:set ts=4 sw=4 nowrap :