I want to create a class that wraps another class so that when a function is run through the wrapper class a pre and post function is run as well. I want the wrapper class to work with any class without modification.
For example if i have this class.
class Simple(object):
def one(self):
print "one"
def two(self,two):
print "two" + two
def three(self):
print "three"
I could use it like this...
number = Simple()
number.one()
number.two("2")
I have so far written this wrapper class...
class Wrapper(object):
def __init__(self,wrapped_class):
self.wrapped_class = wrapped_class()
def __getattr__(self,attr):
return self.wrapped_class.__getattribute__(attr)
def pre():
print "pre"
def post():
print "post"
Which I can call like this...
number = Wrapper(Simple)
number.one()
number.two("2")
Which can be used the same as above apart from changing the first line.
What I want to happen is when calling a function through the wrapper class the pre function in the wrapper class gets called then the desired function in the wrapped class then the post function. I want to be able to do this without changing the wrapped class and also without changing the way the functions are called, only changing the syntax of how the instance of the class is created. eg number = Simple() vs number = Wrapper(Simple)