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

4

1 回答 1

0

如果我很好地理解了您的问题,您希望有条件地将(导入)模块链接到您的项目。有多种方法可以做到这一点。

一种方法是:

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

runner().run()
于 2014-05-15T12:44:01.887 回答