views:

116

answers:

4

So I want to physically get rid of debugging code before deploying it to appengine. What I'm doing right now is a simple check:

settings.py:
DEBUG = os.environ['HTTP_HOST'] == 'localhost'

from settings import DEBUG
if DEBUG:
    #ensure I haven't screwed smth up during refactoring

But this check will be consuming CPU cycles during each call, right? In Java, there's a pattern that would strip debugging code at compile time:

public static final boolean DEBUG = true; // change to false before going production

if(DEBUG){
 //debug logging
}

Is there a clean way to achieve the same effect in Python or should I wrap the code to be stripped with smth like #%STRIP_ME% and then run a custom script against it?

+3  A: 

No, there isn't clean way.

http://stackoverflow.com/questions/560040/conditional-compilation-in-python

Differrent question, similar goals.

Yossarian
+2  A: 

If you are worried about the code and able to do this, you could use a decorator approach to inject the debugs, and then for production code, redefine the decorator functions to be no-ops.

Honestly, I wouldn't worry about it though since the if statement should correspond to a jump statement in assembly, or maybe a couple more than that.

James Deville
+2  A: 

First, I'd suggest making your check "os.environ['SERVER_SOFTWARE'].startswith('Dev')". This will work even if you access the dev server on another host.

Also, your 'DEBUG' statement will only be evaluated the first time the config module is imported on each App server instance. This is fine, since the debug status won't change from request to request, and it also saves a few CPU cycles. I wouldn't worry about the CPU cycles consumed checking if you're in debug mode later, though - there is much, much lower hanging fruit, and I doubt you can even measure the performance degradation of a simple if statement.

Nick Johnson
+1  A: 

You would most likely benefit from using the logging module and importance levels.

Code wouldn't need to be removed before publishing, you can granularly configure the importance level should you ever need to debug in production and it becomes easy to search your code for where notices originate from.

Dan Carley