Calling a function in __main__ from a module?

M

Marc Shapiro

I am relatively new to python (I have used it on and off for a few small
projects over the last few years) so I imagine that what I am trying to do
has already been done, but practical experience, even if it is reinventing
the wheel, is still useful, so...

I am trying to write a module to handle drop down menus using curses (on
linux). Curses seems to have a wrapper for the panels library, but not
forms, or menus. I am not even sure that the menus library would give me
what I want (as I also do not have much curses experience in other
languages), so I am writing my own module.

The code works fine if I include it with my application code in a singe
file. If I seperate out the menu class and related functions into a
seperate module, however, then when I try to have it call a funtion in the
man (calling) file I get an error saying:

Traceback (most recent call last):
File "./hp", line 62, in ?
curses.wrapper(main)
File "/usr/lib/python2.3/curses/wrapper.py", line 44, in wrapper
res = func(stdscr, *rest)
File "./hp", line 58, in main
mb.mainloop(items)
File "/home/mns/menu.py", line 258, in mainloop
exec cmd + "('" + name + "')"
File "<string>", line 1, in ?
NameError: name 'NOP' is not defined

'NOP' is the name of the function that I am trying to call. It does not
exist in the menu module, but is in the calling (hp) module. I can't
qualify the name (i.e. __main__.NOP() ) since __main__ can not be used for
qualification, only called module names can be used that way.

How do I get the module to call functions in a parent module. Tkinter
would set up a callback function and bind it to an event. Can something
similar be done for a console program using curses?
 
J

John J. Lee

Marc Shapiro said:
Traceback (most recent call last):
File "./hp", line 62, in ?
curses.wrapper(main)
File "/usr/lib/python2.3/curses/wrapper.py", line 44, in wrapper
res = func(stdscr, *rest)
File "./hp", line 58, in main
mb.mainloop(items)
File "/home/mns/menu.py", line 258, in mainloop
exec cmd + "('" + name + "')"
File "<string>", line 1, in ?
NameError: name 'NOP' is not defined

'NOP' is the name of the function that I am trying to call. It does
not exist in the menu module, but is in the calling (hp) module. I
can't qualify the name (i.e. __main__.NOP() ) since __main__ can not
be used for qualification, only called module names can be used that
way.

How do I get the module to call functions in a parent module. Tkinter
would set up a callback function and bind it to an event. Can
something similar be done for a console program using curses?

Stop thinking about 'the parent module'. The question of *which*
module wants to get at a particular function is irrelevant. Think
like this instead: you have a bunch of modules, which any piece of
Python code is free to import stuff from, and you have a main program
which can be a module or not as you choose (if it's on sys.path, it's
a module, if it's not, it ain't; if it *is* a module, you'll also have
an if __name__ == "__main__" to prevent the program startup code
getting executed at times other than program startup).

So, you want to get at your NOP function. Two obvious choices:

1. import it

2. pass it as a function argument

If 1, just make sure the file you want to import it from is on
sys.path (or in a package that's on sys.path; a package is a directory
on sys.path that contains an __init__.py file). You can always just
grab your program startup code (say, main()), and stick it in a tiny
executable file somewhere convenient like ~/bin, and move any other
code that was originally in the same file into a module.

BTW, I have never yet used exec or eval, and I've written a fair
amount (tens of thousands of lines) of Python code. Whatever you
think you need them for, you probably don't.


John
 
M

Marc Shapiro

Stop thinking about 'the parent module'. The question of *which*
module wants to get at a particular function is irrelevant. Think
like this instead: you have a bunch of modules, which any piece of
Python code is free to import stuff from, and you have a main program
which can be a module or not as you choose (if it's on sys.path, it's
a module, if it's not, it ain't; if it *is* a module, you'll also have
an if __name__ == "__main__" to prevent the program startup code
getting executed at times other than program startup).

So, you want to get at your NOP function. Two obvious choices:

1. import it

2. pass it as a function argument

If 1, just make sure the file you want to import it from is on
sys.path (or in a package that's on sys.path; a package is a directory
on sys.path that contains an __init__.py file). You can always just
grab your program startup code (say, main()), and stick it in a tiny
executable file somewhere convenient like ~/bin, and move any other
code that was originally in the same file into a module.

BTW, I have never yet used exec or eval, and I've written a fair
amount (tens of thousands of lines) of Python code. Whatever you
think you need them for, you probably don't.


John

I was just about to post back that I had solved the problem. As it turns
out, I used method 2. I passed a function argument in place of a string
with the function name. Thanks to Python being an untyped language this
required only a few minor changes in the module and it is now working as
expected. My planned next step was, indeed, to see if I could rewrite so
as to avoid using 'exec'. It can certainly be confusing to determine what
is being evaluated by which sections of code and at what time. I am sure
that when it comes to maintaining this at a later date that it will be a
much easier task if I can get rid of the 'exec' lines.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,184
Latest member
ZNOChrista

Latest Threads

Top