472,992 Members | 3,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,992 software developers and data experts.

Calling a function in __main__ from a module?

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
2 4413
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Uwe Mayer | last post by:
Hi, I've got a class that receives a function in the constructor and uses the __call__ method to execute the aforementioned function when the instance object is called: class foo(object):...
7
by: Doug Rosser | last post by:
I'm writing a fairly complicated test framework and keeping configuration data inside ini files that are parsed at runtime by the ConfigParser module. For example, there would be a section...
9
by: Dave Ekhaus | last post by:
hi i'd like to call a python function programmatically - when all i have is the functions name as a string. i.e. fnames = for func in fnames:
5
by: Pekka Niiranen | last post by:
Hi there, I have two scripts. The first "main.py" sets some variables and then imports another called "gen.py". The idea is to provide "main.py" that defines some paths, variables etc. without...
2
by: wen | last post by:
and, in which case, the following case will happen: if __name__!='__main__': do_sth() any help would be appreciated.
2
by: Brian | last post by:
I just have a basic style question here. Suppose you have the program: def foo1(): do something def foo2() do something else Assume that you want to call these functions at execution. Is...
3
by: Fuzzyman | last post by:
Hello all, I am messing with namespaces, so that code I exec thinks it is executing in the __main__ module. I have the following code : import imp import sys
9
by: jezonthenet | last post by:
I started using Python a couple of days ago - here are a few questions: * Doesn't the __main__() method automatically execute when I run my python program? * Only when I do an import of my...
1
by: Joe Strout | last post by:
I've been using docstring to exercise each of my modules, with code like this: def _test(): import doctest doctest.testmod() if __name__ == "__main__": _test()
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.