Search code examples
pythondynamic-import

Dynamic module import of class list from module given its full path


First, it seems proper to me to state that I've read over the following questions:

And, I don't feel like they address my use case. That said, here's my question:

How can I dynamically import from a configuration file like this:

[imports]
/var/imports/foo.py = baz monkey
/var/imports/bar.py = ape

So that I can call the known objects via RESTful interface like this.

# http://localhost/data?operation=get&class=baz&id=1
def handler(object):
  def handle(self):
    cls = instantiate(request.args["class"])
    meth = request.args["operation"]
    return getattr(cls,meth,request.args)

And it could respond with the result of get() on an instance of that class.


Solution

  • Here's a rough sketch of a class registry you could use like this:

    class ClassRegistry(object):
    
        def __init__(self):
            self.classes = {}
    
        def add_file_classes(self, fname, class_list):
            globals = {}
            exec open(fname) in globals
            for class_name in class_list:
                self.classes[class_name] = getattr(globals, class_name)
    
        def get_class(self, class_name):
            return self.classes[class_name]
    

    Read your config file and invoke add_file_classes like this:

    reg = ClassRegistry()
    reg.add_file_classes("/var/imports/foo.py", ["baz", "monkey"])
    reg.add_file_classes("/var/imports/bar.py", ["ape"])
    

    then later:

    cls = reg.get_class("baz")
    

    Note that we aren't actually importing the files, simply executing them and collecting their results.