views:

224

answers:

2

I want to separate the debug outputs from production ones by defining a variable that can be used throughput the module. It cannot be defined in environment. Any suggestions for globals reused across classes in modules? Additionally is there a way to configure this variable flag for telling appengine that dont use this code.

+9  A: 

Have a look at the logging module, which is fully supported by Google App Engine. You can specify logging levels such as debug, warning, error, etc. They will show up in the dev server console, and will also be stored in the request log.

If you're after executing specific code only when running the dev server, you can do this:

if os.environ['SERVER_SOFTWARE'].startswith('Development'):
    print 'Hello world!'

The SERVER_SOFTWARE variable is always set by Google App Engine.

As for module specific variables; modules are objects and can have values just as any other object:

my_module.debug = True
Blixt
+1 for the logging module, which is really the best answer to @dhaval's actual problem as he explains it.
Alex Martelli
thx Blixit, I was not aware of the server_software variable
dhaval
@dhaval: Environment variables are available in all OS's, so this technique works everywhere -- just be sure to set the environment variable yourself if you're not on GAE.
S.Lott
+1  A: 

All module-level variables are global to all classes in the module.

Here's my file: mymodule.py

import this
import that

DEBUG = True

class Foo( object ):
    def __init__( self ):
        if DEBUG: print self.__class__, "__init__"
        # etc.

class Bar( object ):
    def do_work( self ):
        if DEBUG: print self.__class__, "do_work"
        # etc.

A single, module-level DEBUG variable will be found by all instances of these two classes. Other modules (e.g,. this.py and that.py) can have their own DEBUG variables. These would be this.DEBUG or that.DEBUG, and are unrelated.

S.Lott
thx @Lott, I was defining the variable in __main and failing
dhaval