Implements proxies and facilities to create them.
New in version 0.4.
Class decorator which returns a proxy based on the handlers defined in the given class defined as methods:
@as_proxy
class MyProxy(object):
"""
This is an example proxy, every method defined is optional.
"""
def method(self, proxied, name, get_result, *args, **kwargs):
"""
Gets called when a special method is called on the proxy
:param proxied:
The object wrapped by the proxy.
:param name:
The name of the called method.
:param get_result:
A function which takes `proxied`, `*args` and `**kwargs`
as arguments and returns the appropriate result for the
called method.
:param \*args:
The positional arguments passed to the method.
:param \*\*kwargs:
The keyword arguments passed to the method.
"""
return missing
def getattr(self, proxied, name):
"""
Gets called when a 'regular' attribute is accessed.
:param name:
The name of the attribute.
"""
return getattr(proxied, name)
def setattr(self, proxied, name, obj):
"""
Gets called when a 'regular' attribute is set.
:param obj:
The object which is set as attribute.
"""
setattr(proxied, name, obj)
def force(self, proxied):
"""
Returns a 'real' version of `proxied`. This is required when
`proxied` is something abstract like a function which returns
an object like which the proxy is supposed to behave.
Internally this is used when a binary operator is used with the
proxy on the left side. Built-in types complain if we call the
special method with the proxy given on the right side of the
operator, therefore the proxy on the right side is 'forced'.
"""
return proxied
def repr(self, proxied):
"""
Gets called for the representation of the proxy.
"""
return repr(proxied)
foo = MyProxy(1)
Returns the item wrapped by a given proxy whereas proxy is an instance of a class as returned by as_proxy().