samedi 17 juin 2017

Register class so it can be loaded safely via reflection

How can I "register" a class in a global scope, so that I can later on safely refer to this class and instantiate it via reflection?

Example scenario:

  • Assume a user sends a workflow_name via REST to a python server
  • The python server makes sure the requested workflow_name is the name of a concrete workflow class name which can be loaded via reflection

In pseudo code:

ConcreteWorkflow.py:

class ConcreteWorkflow1
    def doSomething(self):
        ...

# Somehow register the class in a global scope...???
regsiterWorkflow(ConcreteWorkflow1)

WorkflowRouter.py:

class WorkflowRouter
    def getConcreteWorkflow(self, workflowNameFromAPI):
        # somehow check if workflowNameFromAPI is one of the registered
        # concrete workflows. If so, load the class via reflection.
        ....
        concreteWorkflow.doSomething()

I hope the pseudo code makes it clear what I'm trying to achieve.





Aucun commentaire:

Enregistrer un commentaire