Wrap external python library classes/functions in a new python module

1.3k views Asked by At

We are using an external python library (e.g. Lib1) which have couple of classes (e.g. Class A, Class B). We want to create a wrapper component on top of that library so that in future if we want to replace that library with some other library, our application does not need to change the code. For initial implementation of this component, we are fine with creating this component with same Classes and methods as in Lib 1.

If Class A have method m1 and m2 and Class B have method m3 and m4, and m3 and m4 take Class A instance as parameter; We do not want to end up with Class Wrapped_A with its method m1 and m2 and Class Wrapped_B with its method m3 and m4.

Is there any way to implement a generic proxy/delegate object or class so that we do not have to wrap every methods in Wrapped_A and Wrapped_B but still our application can call def m1, m2, m3 and m4 on them (also, still an object of Class A being passed to m3 and m4).

I am new to python, tried to understand decorator and metaprogramming but that did not helped. Also, our requirement currently is not to add any new logic in class or their methods but just to create proxy object/methods which will just delegate the calls to external library objects/methods.

Thanks for the help !

0

There are 0 answers