Skip to content

Instantly share code, notes, and snippets.

@rctay
Created August 7, 2012 17:01
Show Gist options
  • Save rctay/3287315 to your computer and use it in GitHub Desktop.
Save rctay/3287315 to your computer and use it in GitHub Desktop.
[python] (fork) object proxying
## Based on http://code.activestate.com/recipes/519639/ (r1)
## and http://code.activestate.com/recipes/496741-object-proxying/ (r1)
import copy
def proxy(cls, target):
"""
A, the class of the object to be proxied, intentionally takes an argument.
This foils any proxy techniques that instantiate the class of the proxee.
Yet there is no relation between its constructor's arity and any objects
that desire to proxy it.
>>> class A(object):
... def __init__(self, bar, _):
... print 'A init'
... self.bar = bar
... super(A, self).__init__()
... def foobar(self):
... print 'A foobar', self.bar
...
B, the class of the proxy object, may have a constructor that takes
arguments, but this proxy technique pays no regard to it.
>>> class B(object):
... def __init__(self, a, b):
... print 'B init, not'
... def foobar(self):
... print 'B foobar'
... super(B, self).foobar()
...
>>> class C(object):
... pass
...
>>> a = A(1, "hello")
A init
>>> a.foobar()
A foobar 1
>>> a.baz = 5
>>> b = proxy(B, a)
>>> c = proxy(C, b)
Changes to the object being proxied are *not* available to the proxy:
>>> a.bar = 3
>>> a.foobar()
A foobar 3
>>> b.foobar()
B foobar
A foobar 1
Attributes on the proxy shadow the object being proxied:
>>> a.foobar()
A foobar 3
>>> b.bar = 2
>>> a.foobar()
A foobar 3
>>> b.foobar()
B foobar
A foobar 2
Attributes added to the object being proxied before instantiating the
proxy are available:
>>> b.baz
5
>>> c.baz
5
Not so for attributes added after instantiating:
>>> a.bax = 4
>>> b.bax
Traceback (most recent call last):
...
AttributeError: 'B(A)' object has no attribute 'bax'
"""
ins = copy.copy(target)
target_cls = type(ins)
ins.__class__ = type("%s(%s)" % (cls.__name__, target_cls.__name__), (cls, target_cls), {})
return ins
## Based on http://code.activestate.com/recipes/519639/ (r1)
## and http://code.activestate.com/recipes/496741-object-proxying/ (r1)
from types import MethodType
class Proxy(object):
"""
A, the class of the object to be proxied, intentionally takes an argument.
This foils any proxy techniques that instantiate the class of the proxee.
Yet there is no relation between its constructor's arity and any objects
that desire to proxy it.
>>> class A(object):
... def __init__(self, bar, _):
... print 'A init'
... self.bar = bar
... super(A, self).__init__()
... def foobar(self):
... print 'A foobar', self.bar
...
B, the class of the proxy object, if it is to have a constructor, must take
an argument, due to its parentage (the Proxy class).
>>> class B(Proxy):
... def __init__(self, target):
... print 'B init'
... super(B, self).__init__(target)
... def foobar(self):
... print 'B foobar'
... super(B, self).foobar()
...
>>> class C(Proxy):
... def __init__(self, target):
... print 'C init'
... super(C, self).__init__(target)
...
>>> a = A(1, "hello")
A init
>>> a.foobar()
A foobar 1
>>> a.baz = 5
>>> b = B(a)
B init
>>> c = C(b)
C init
B init
Changes to the object being proxied are available to the proxy:
>>> a.bar = 3
>>> a.foobar()
A foobar 3
>>> b.foobar()
B foobar
A foobar 3
Attributes on the proxy shadow the object being proxied:
>>> a.foobar()
A foobar 3
>>> b.bar = 2
>>> a.foobar()
A foobar 3
>>> b.foobar()
B foobar
A foobar 2
Attributes added to the object being proxied before instantiating the
proxy are available:
>>> b.baz
5
>>> c.baz
5
This holds for attributes added after instantiating:
>>> a.bax = 4
>>> b.bax
4
"""
# for orthogonal __init__'s()
def __init__(self, target):
pass
def __new__(cls, target, *args, **kwargs):
target_cls = type(target)
newcls = type("%s(%s)" % (cls.__name__, target_cls.__name__),
(cls, target_cls),
dict(__getattr__=lambda self, name: getattr(target, name)))
ins = super(Proxy, newcls).__new__(newcls)
return ins
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment