views:

85

answers:

2

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.

+2  A: 

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.

Greg Hewgill
+1  A: 

You can use the imp module.

KangOl