Hi All:
Suppose I've got 2 different modules which have the uniform(same) interfaces. The files list like this:
root/
logic.py
sns_api/
__init__.py
facebook/
pyfacebook.py
__init__.py
myspace/
pymyspace.py
__init__.py
And pyfacebook.py and pymyspace.py have the same interfaces, which means:
# in pyfacebook.py
class Facebook:
def __init__(self, a, b):
# do the init
def method1(self, a, b, ...):
# do the logic
# in pymyspace.py
class Myspace:
def __init__(self, a, b):
# do the init
def method1(self, a, b, ...):
# do the logic
Now I have a question. I want to do the logic in logic.py without duplicating the codes, so I'm wondering how can I just set a flag to show which module I use and python will load the right codes automatically, which means:
# in logic.py
PLATFORM = "facebook"
# import the right modules in, complete the logic with the current platform
# create the right instance and invoke the right methods
Then I change PLATFORM = 'myspace', the logic will work automatically.
So how can I do this?
I'm wondering whether using the dynamic importing will work, or eval raw python codes, but seems not a good solution. Or if I can make a uniform wrapper in
sns_api/__init__.py
Anyone can help?