Accessing variables in __main__ from modules.... 
Author Message
 Accessing variables in __main__ from modules....

All:

When writing programs, I like to add print statements that are only
displayed when the user provides a command line switch such as '--
debug'.

  Ex:  %myprog spam.txt ham
       Found the word 'ham' in 'spam.txt' 45 times

       %myprog --debug spam.txt ham
       -I-: Reading 'spam.txt'....has 213 lines....
       Found the word 'ham' in 'spam.txt' 45 times

Now, say most of the work is done in a module 'spam.py'. If I wrote
code in this module like:

  lines=open(filename).readlines()
  if (DEBUG):
    print "-I-: Reading '%s'....has %s lines...."%(filename,len
(lines))

DEBUG would be searched for at the module scope. But I want it to
look for DEBUG back in __main__, like:

  if (sys.modules["__main__"].DEBUG):
    print "-I-: Reading '%s'....has %s lines...."%len(filename,lines)

Is this the "best" way to access variable in the "__main__" scope?
How else might I solve this (in the python way)??

Quentin Crain

Note: I might load many modules all wanting to know if DEBUG is
turned on. I do not want to pass DEBUG to functions or classes and I
do not want to set a module variable DEBUG to the same value
(module1.DEBUG=DEBUG, module2.DEBUG=DEBUG, etc. in __main__). Unless
these are "better" solutions! :)



Tue, 16 Sep 2003 02:38:51 GMT  
 Accessing variables in __main__ from modules....

Quote:

> All:

> When writing programs, I like to add print statements that are only
> displayed when the user provides a command line switch such as '--
> debug'.

>   Ex:  %myprog spam.txt ham
>        Found the word 'ham' in 'spam.txt' 45 times

>        %myprog --debug spam.txt ham
>        -I-: Reading 'spam.txt'....has 213 lines....
>        Found the word 'ham' in 'spam.txt' 45 times

> Now, say most of the work is done in a module 'spam.py'. If I wrote
> code in this module like:

>   lines=open(filename).readlines()
>   if (DEBUG):
>     print "-I-: Reading '%s'....has %s lines...."%(filename,len
> (lines))

> DEBUG would be searched for at the module scope. But I want it to
> look for DEBUG back in __main__, like:

>   if (sys.modules["__main__"].DEBUG):
>     print "-I-: Reading '%s'....has %s lines...."%len(filename,lines)

> Is this the "best" way to access variable in the "__main__" scope?
> How else might I solve this (in the python way)??

> Quentin Crain

> Note: I might load many modules all wanting to know if DEBUG is
> turned on. I do not want to pass DEBUG to functions or classes and I
> do not want to set a module variable DEBUG to the same value
> (module1.DEBUG=DEBUG, module2.DEBUG=DEBUG, etc. in __main__). Unless
> these are "better" solutions! :)

How about a module named globals, which contains one line:

DEBUG = 0

Have all your modules import globals.

In __main__ when you want to perform debugging, set DEBUG = 1 in
globals.

Then write your conditional as:

if (globals.DEBUG):
  print "-I-: Reading '%s'....has %s lines...."%(filename,len(lines))

Roland Schlenker



Tue, 16 Sep 2003 04:45:16 GMT  
 Accessing variables in __main__ from modules....

Quote:

> DEBUG would be searched for at the module scope. But I want it to
> look for DEBUG back in __main__, like:

The most straightforwarth way to achieve this would be to import Debug
from main, or better to have a seperate options module and import from
there. like:

module options:

   DEBUG = 0

main module:

   import options
   ...
   options.DEBUG = 1

other module:

   from options import DEBUG

   if DEBUG:
     print "-I-: Reading '%s'....has %s lines...."%len(filename,lines)

--
groetjes, carel



Tue, 16 Sep 2003 07:02:21 GMT  
 Accessing variables in __main__ from modules....

Quote:

> The most straightforwarth way to achieve this would be to import Debug
> from main, or better to have a seperate options module and import from
> there. like:
...
> other module:

Oh, and importing something in many modules is relative cheap.  Only
the first import compiles and executes that module, subsequent imports
only bind the module object to a name in the current scope.
--
groetjes, carel


Tue, 16 Sep 2003 07:06:00 GMT  
 Accessing variables in __main__ from modules....

How about writing a function in a module that everyone can import called
like dprint or something like that that you can pass the string to print
to and it will decide whether or not to actually print it based on
whether or not --debug was in sys.argv, which could even be set in a
variable there by __main__ or something like that.

Quote:

> All:

> When writing programs, I like to add print statements that are only
> displayed when the user provides a command line switch such as '--
> debug'.

>   Ex:  %myprog spam.txt ham
>        Found the word 'ham' in 'spam.txt' 45 times

>        %myprog --debug spam.txt ham
>        -I-: Reading 'spam.txt'....has 213 lines....
>        Found the word 'ham' in 'spam.txt' 45 times

> Now, say most of the work is done in a module 'spam.py'. If I wrote
> code in this module like:

>   lines=open(filename).readlines()
>   if (DEBUG):
>     print "-I-: Reading '%s'....has %s lines...."%(filename,len
> (lines))

> DEBUG would be searched for at the module scope. But I want it to
> look for DEBUG back in __main__, like:

>   if (sys.modules["__main__"].DEBUG):
>     print "-I-: Reading '%s'....has %s lines...."%len(filename,lines)

> Is this the "best" way to access variable in the "__main__" scope?
> How else might I solve this (in the python way)??

> Quentin Crain

> Note: I might load many modules all wanting to know if DEBUG is
> turned on. I do not want to pass DEBUG to functions or classes and I
> do not want to set a module variable DEBUG to the same value
> (module1.DEBUG=DEBUG, module2.DEBUG=DEBUG, etc. in __main__). Unless
> these are "better" solutions! :)

--
Eric Renouf
Software Engineer
Opticom Inc.
www.getiview.com


Sat, 27 Sep 2003 10:41:53 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Access to main namespace from imported module?

2. How to access main module?

3. EQUIVALENCE(variable inside module, variable outside module) ????

4. how to access variables across the module boundry?

5. Accessing MODULE variables from within subroutines in the CONTAINS section

6. accessing Digital f90 module variables within decladebug

7. Accessing variables with rexec module ?

8. Accessing "variables" in an extension module

9. Accessing and updating global variables among several modules

10. How to access a module's namespace within the module at import

11. Importing via Access 97 odbc driver variable length Access Memo fiels

12. import selected module variables within that module's procedures

 

 
Powered by phpBB® Forum Software