best practice to resolve module dependency within a reusable application

Marc Aymerich glicerinu at calmisko.org
Tue Mar 22 14:34:45 EDT 2011


Hi,
I'm developing a reusable app splited into modules. The end user
chooses what modules wants to keep installed.
Most of this modules are quite independent from each other, but I have
one of them (called moduleP) with a pretty strong dependency with
another another(called moduleBase). So I need to change some of the
moduleBase behaviour whenever moduleP is installed.

What is the best practice to resolve this dependency in a reusable
scenario?
Where should the specific moduleP code for moduleBase live? in moduleP
or in moduleBase?

I thought in two possible solutions:

1) moduleP specific code lives in moduleBase an it is called if "is
moduleP installed":

   class moduleBase(otherClass):
     def method(self):
       super(moduleBase, self).method()
       # moduleBase stuff
       .
       .
       if 'moduleP' in INSTALLED_MODULES:
        # moduleP stuff
        .
        .

2) moduleP code lives in moduleP and it swaps moduleBase method with
their own, like this:

   # backup moduleBase method
   moduleBase.back_methodBase = moduleBase.method

   def Pmethod(self):
    # calls moduleBase.method
    self.back_methodBase_method
    # moduleP stuff
    .
    .

   # swap moduleP Pmethod
   moduleBase.method = Pmethod


I'm missing some other mechanism to achieve this? Which one do you
think is best?
Many thanks!!



More information about the Python-list mailing list