By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,369 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,369 IT Pros & Developers. It's quick & easy.

Calling a function in __main__ from a module?

P: n/a
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?

--
Marc Shapiro
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Marc Shapiro <ms******@sunlitsurf.com> writes:
[...]
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
Jul 18 '05 #2

P: n/a
On 18 Dec 2003 19:21:14 +0000, John J. Lee <jj*@pobox.com> wrote:
Marc Shapiro <ms******@sunlitsurf.com> writes:
[...]
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


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.

--
Marc Shapiro
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.