[albatross-users] page module problem

Matt Goodall matt at pollenation.net
Sun Aug 10 12:03:29 EST 2003


Could something like this be used:

    ns = {}; execfile('./modules/one/list.py', ns)
    page_enter = ns['page_enter']
    ...
    page_enter(ctx)

The namespace and the page methods could easily be cached (perhaps in
some Page object) and it's still easy to reload the module if list.py is
changed. Best of all nothing gets added to sys.modules so it's nice and
clean.

Can anyone think of any problems? Is this likely to break anything else?

Cheers, Matt


On Sun, 2003-08-10 at 01:54, Matt Goodall wrote:
> I'm using a random app but I suspect the problem would occur with
> non-random apps too.
> 
> If I store my page modules in a directory structure and two directories
> have a python module with the same name, something like this ...
> 
>     ./modules/one/list.py
>     ./modules/two/list.py
> 
> then **both** modules get imported with a name of 'list' (as seen in
> sys.modules.keys()). This happens because the directory structure is not
> considered a package structure, just somewhere to store modules.
> 
> I'm trying to find a solution to this limitation but, so far, I'm not
> happy with the fix. I'm currently abusing the imp module in a truly
> horrible way by passing an application generated name to load_module()
> to stop the name clash. It goes something like this:
> 
>     dirname, filename = os.path.split(path)
>     file, filepath, desc = imp.find_module(filename, [dirname])
>     mod_name = '_albatross_page_%s' % name.replace('/', '_')
>     page = imp.load_module(mod_name, file, filepath, desc)
> 
> Well, I did warn you it was horrible ;-).
> 
> Another possibility is to use the execfile() builtin to (sort of) load
> the module into a blank namespace. This seems cleaner since it does not
> create a module but I'm concerned it might break things in ways I don't
> yet understand. It would probably also mean changing the way in which
> the page modules are cached.
> 
> If anyone has more experience with this sort of thing I would be
> grateful for any ideas/help.
> 
> Cheers, Matt
-- 
Matt Goodall, Pollenation Internet Ltd
w: http://www.pollenation.net
e: matt at pollenation.net
t: +44 (0)113 2252500



More information about the Albatross-users mailing list