Code design problem

Marco Nawijn nawijn at gmail.com
Wed Aug 29 08:09:35 EDT 2007


Hello,

I have a hard time figuring out an elegant and efficient design for
the following problem.

I am working on automation of structural design problems. In the
majority of cases, this boils down to executing programs in batch in
one or more loops. The scripts to control the execution differ from
fortran to bash to python and so on. Most of them are ad hoc and what
I call 'throw away scripts'. In order to improve the situation I would
like to develop a Python module that supports the execution of
external programs. Ideally I would like to make running locally or
remote trivial for the users of the module. As an example, I would
like the following (pseudo)-code to work:

app = Application('patran')                        # Run on local
machine
app.start(args)


app = Application('patran', host='myhost')   # Run on remote machine
app.start(args)

The problem I face is that the implementation of the application class
is completely
different for the local and remote case. The local case is a
straightforward implemenation using the subprocess module, the remote
case is a CORBA implementation. Somehow I would like to switch from
implementation class at runtime depending on whether or not the host
parameter is specified or not.

The Application, local implementation and remote implementation all
have the same interface, so a possibility might be something like the
following:

class Interface(object):
     .....
     def start(self): pass
     def stop(self): pass

class LocalImplementation(Interface):
       .....

class GlobalImplementation(CorbaGlobalImplementation, Interface):
       .....


class Application(Interface):

      def __init__(self, program, host=None):
            ....
            if host:
                   self.__impl = LocalImplementation(program)
            else:
                   self.__impl = GlobalImplementation(program, host)

      #  Forward all methods to the implementation class
      def start(self):
          self.__impl.start()

      def stop(self):
          self.__impl.stop()


To me forwarding each call in the Application class looks a little bit
redundant and I would like to get rid of it. Does anyone have any
comments or suggestions? Can metaclass programming come to rescue?

Kind regards,


Marco Nawijn




More information about the Python-list mailing list