1

Using python and django we are creating a framework to run a number of scientific models. Python runs the models, and django is used to keep track of status and to create output to webpages. In operational mode this works fine, as the entire platform (database, django executable) is available. However for the offline creation and testing of new models, my colleages often work on stand-alone code, with no connection to the environment. Then the django part is often an burden (environment, database need to be set up, computational overhead). So we are looking for a way to toggle the Django-functions on and off.

Currently we have a general class iModel, which has all functionality onboard to run any model in the framework, also some django calls. Childclasses inherit from this class, and contain the model-specific functionality.

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "framework.settings")
import django.some.modules

class iModel(iModel):
    def run(self):
        some django calls (eg. change status in DB to 'running')
        self.prepareInputdata()
        self.runModel()
        some more calls (eg. change status in DB to 'finished')

    def prepareInputData(self):
        example of some code that is used by all child models

class CATmodel(iModel):
    def runModel(self):
        some specific code for CAT-model

class DOGmodel(iModel):
    def runModel(self):
        some specific code for DOG-model

I was thinking of splitting the class iModel into two classes. an extra class iClassWithDjango(), that would inherit from iModel and that adds the Django functionality. As a wrapper around the class iModel.

first class

class iModel(Object):
    def run(self):
        self.prepareInputDate()
        self.runModel()

and second class

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "framework.settings")
import django.some.modules

class iModelWithDjango(iModel):
    def run(self):
        some django calls (eg. change status in DB to 'running')
        super(iModelWithDjango,run)
        some more calls (eg. change status in DB to 'finished')

Depending on the situation (operational or offline) DOGmodel and CATmodel would inherit resp. from iModelwithDjango() or iModel(). So change the parentclass dynamically at runtime.

In fact my biggest problem is how to handle the import of the django modules. How can I prevent this to happen in offline mode (as it will fail because the django environment is not set up). My idea was to put iModelWithDjango() in a separate module file (as done above), but maybe there are better ways to do this.

Maybe with a lot of messy coding I could pull this off, but I was hoping some of you could steer me to some clean coding practice. Any advice welcome

Bart P.
  • 849
  • 1
  • 8
  • 11

1 Answers1

0

If I understand your question well, you would like to conditionally link (import) modules to your project. There are various ways to do so.

one way would be:

if standAlone:
  from iModel import IModel
  runner = IModel
else:
  from iModelWithDjango import IModelWithDjango
  runner = IModelWithDjango

runner().run()
user508402
  • 496
  • 1
  • 4
  • 19
  • I think this will not be enough to solve the problem. The model is started with CATmodel.run(). At that moment the correct parent should already be in use, no? Or maybe there is a way around that? – Bart P. Jun 03 '14 at 13:06
  • What do you mean with 'parent'? It will work, for if standAlone is True, the Django stuff is never imported and as far as Python is concerned, it doesn't even need to exist on your computer. In fact, this way you could instantiate and run two completely unrelated classes provided both are duck-typed identically (that is, they provide a run()-method, in this exmple). Personally, I would encourage a common super-class, though. – user508402 Jun 04 '14 at 09:01