I have a module in some path. I want to create a class with the same attributes as the module (so that it can be used the same way as the module) but perform some custom actions before accessing the attributes - such as reloading the module.
def get_method_with_extra(method_name, module):
def method_with_extra(self, *args):
imp.reload(module)
func_to_call = getattr(module, method_name)
func_to_call(*args)
return method_with_extra
class tester():
def __init__(self, module_path):
self.module = imp.load_source('module', module_path)
method_list = [func for func in dir(self.module) if
callable(getattr(self.module, func))]
for method_name in method_list:
method_with_extra = get_method_with_extra(method_name,
self.module)
setattr(type(self), method_name, method_with_extra)
So if for example the module has a method named "Parse", I would like an instance of tester - tess - to have it as well, and for me to be able to call tess.parse() which should reload the inner module and then call the module's parse(). Instead, I get this error:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "<string>", line 4, in __init__
AttributeError: attribute '__call__' of 'namespace#' object is read-only