Function to import module to namespace
bob at mellowood.ca
Mon Jun 30 19:17:49 CEST 2008
John Machin wrote:
> On Jun 30, 11:45 am, bvdp <b... at mellowood.ca> wrote:
>> John Machin wrote:
>> Good questions. Short answer ... probably 'cause I've not thought the
>> problem though completely :)
>> > You are updating with *everything* in the 'more' module, not just the
>> > functions. This includes such things as __name__, __doc__, __file__.
>> > Could have interesting side-effects.
>> > One quick silly question: why do you want to do this anyway?
>> I'm writing a "simple" macro expander. I've got some mainline code which
>> reads an input file, parses out macros, and expands them. So, in my
>> input file I might have something like:
>> a fjas j kj sdklj sdkl jfsdkl [ link somewhere sometext ]
>> So, I need a function link() to evaluate and return "http://...."
>> Instead of putting funcs like link() in my mainline I've stuck them in a
>> module of their own.
> Why? Do they have any use at all other than to be called by your
>> In the mainline I
>> import funcs
>> and then when I need to expand I have the following code:
> Build a dictionary *ONCE*, not each time you want to look in it. E.g.
> funcs_vars = vars(funcs)
> ... much later:
> if not cmd in funcs_vars:
>> if not cmd in vars(funcs):
>> error("Unknown function/variable '%s'" % cmd)
>> if type(vars(funcs)[cmd]) == type(parse):
>> txt = eval("""funcs.%s("%s")""" % (cmd, arg))
> Any particular reason why you are going to the trouble of laboriously
> building a statement, compiling it, and running it, when you could
> simply do:
> txt = funcs_vars[cmd](arg)
>> else: # not a func, just expand the variable
>> if arg:
>> error("Argument to variable '%s' not permitted." % cmd)
>> txt = str(eval("funcs.%s" % cmd ))
> txt = str(funcs_vars[cmd])
>> Of course, the question comes up ... what if a user (probably me) wants
>> to add more functions? Easy enough to just edit funcs.py I suppose, but
>> I thought it'd be nice to use more modules.
> You may already have one more module than you need. If the only thing
> different about the extra functions is that they are an afterthought,
> then put them in the same module as the others.
>> So, I suppose that rather than adding the 2ndary module stuff to the
>> default 'funcs.py' I could just as well have a look and check for the
>> needed function in all the modules I've imported.
> Sounds quite unnecessary, inefficient and pointless to me.
>>> Sorry, *two* quick silly questions: are the add-on modules under your
>>> control, or do you want to be able to do this with arbitrary modules?
>>> [If under your control, you could insist that such modules had an
>>> __all__ attribute with appropriate contents]
>> Why would I want to do that ... and how?
> Why: so that you append only the functions that you want to the target
> namespace, and don't import stuff like __name__. How: read about
> __all__. Just append the stuff that's listed in __all__. However it
> sounds like you have seen the light and won't be doing anything like
> appending to a namespace.
>>> A third: why do you want to import into an existing namespace? Now
>>> that you know about __import__, why just not call the functions where
>>> they are?
>> Yeah, that would probably be cleaner (safer).
> Much cleaner, safer, etc is just to import, or even not to have a
> separate module at all.
Thanks. Much food for thought here. Let me digest and see what comes of
More information about the Python-list