Package grizzled :: Module proxy :: Class Forwarder
[hide private]
[frames] | no frames]

Class Forwarder

source code

object --+
         |
        Forwarder

The grizzled.forwarder.Forwarder class is intended to be used as a mixin, to make it easier for classes to forward calls to another class. The mix Forwarder into a class, simply include it as one of the base classes.

WARNING: Forwarder intercepts calls to __getattr__, so don't mix it in if your class is already overriding __getattr__.

Examples

Forward all unimplemented methods to a file:

from grizzled.forwarder import Forwarder

class MyFileWrapper(Forwarder):
    def __init__(self, file):
        Forwarder.__init__(self, file)

w = MyFileWrapper(open('/tmp/foo'))
for line in w.readlines():
    print line

Forward all unimplemented calls, except name, to the specified object. Calls to name will raise an AttributeError:

from grizzled.forwarder import Forwarder

class MyFileWrapper(Forwarder):
    def __init__(self, file):
        Forwarder.__init__(self, file, 'name')
Instance Methods [hide private]
 
__init__(self, wrapped, *exceptions)
Initialize a new Forwarder that will pass unimplemented calls (method calls, attribute accesses, etc.) to the specified object.
source code
 
__getattr__(self, name) source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, wrapped, *exceptions)
(Constructor)

source code 
Initialize a new Forwarder that will pass unimplemented calls (method calls, attribute accesses, etc.) to the specified object.
Parameters:
  • wrapped (object) - the object to which to pass unknown attributes
  • exceptions (str) - one or more names (as separate arguments) of methods that should not be intercepted (and will, therefore, result in AttributeError exceptions if invoked, absent any other intervention).
Overrides: object.__init__