开发者

Python Dynamic module loading based on input

I wrote a program that takes in a partial rss feed and outputs a full one, but it is one a case by case basis. The recipe for one site is not the same as the recipe for the other. So what I do is look at the domain basename(for instance nyt or wsj) and choose a module based on that. Though I need to load each and every module before hand and have a logical condition for each recipe.

What I need is a way to just have the individual modules in their own respective folder and when I parse out the url basename I want it to look for the module, load it and take some action. So I want the main code base to be independent from the modules. I want to be able to add the modules in the future and never touch the portion of code which interact with them.

Here is a code example

if "nyt" == feed:
        nyt.parser(posixpath.basename(url), urldir, rss_file_path, url, feed)

As you can see I call the 开发者_运维问答parser of the individual module. I have many of these based on each website. I want to reed feed and then be able to look for the module, load it and call it, and then if it doesn't exist report it and try the default method.


It sounds like you're looking for the __import__ function. This function does the same thing as the import statement, but allows you to pass a name to import that might only be known at runtime.

So you might do:

parsemodule = __import__(feed)
parsemodule.parser(posixpath.basename(url), urldir, rss_file_path, url, feed)

You will want to catch exceptions such as ImportError.


You can use the imp module.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜