[albatross-users] pagination with al-for
Matt Goodall
matt at pollenation.net
Wed Jan 7 12:45:33 EST 2004
Andrew McNamara wrote:
[snip]
>>There have been a couple of suggestions for how to fix the problem:
>>
>> 1. Hack the import machinery. Instead of importing page modules as
>> is, concoct a package and module name to avoid clashes. I think I
>> found a problem with this approach (something to do with importing
>> builtin packages like os and os.path) but I can't remember the
>> details now. I think I also found a way to avoid the problem.
>>
>>
>
>I've done this before - I don't remember running into any real problems.
>I've discarded my earlier implementation, but here's something I just
>knocked up - it appears to work as desired:
>
> import imp, sys
>
> mod_holder_name = '__snot__'
>
> def load_page_module(path, name):
> try:
> mod_holder = sys.modules[mod_holder_name]
> except KeyError:
> mod_holder = imp.new_module(mod_holder_name)
> sys.modules[mod_holder_name] = mod_holder
> globals()[mod_holder_name] = mod_holder
>
> f, path, desc = imp.find_module(name, [path])
> try:
> abs_name = mod_holder_name + '.' + name
> mod = imp.load_module(abs_name, f, path, desc)
> setattr(mod_holder, name, mod)
> finally:
> f.close()
>
> >>> load_page_module('.', 't')
> >>> __snot__.t.Sigh
> 'Hi'
>
>I've played around loading stuff from the "page" module t.py, and various
>other things, and it just seems to work. It's probably not necessary or
>desirable to poke everything back into globals() in the context of the
>Albatross machinery.
>
>Can anyone see any problems with this scheme?
>
>
Ah yes, now I remember what the problem was.
My initial experiment with this built a "flat" module name such as
__albatross_pages__one_two. The fix was to create a holding module,
__albatross_pages__, to hold the page modules ... just like you've done
above.
I don't think my code was as neat as yours though, and the name of your
holding module is much better ;-).
Matt
--
Matt Goodall, Pollenation Internet Ltd
w: http://www.pollenation.net
e: matt at pollenation.net
More information about the Albatross-users
mailing list