views:

58

answers:

3

Hello app engineers! I'm on an app engine project where I'd like to put in a link to a Javascript test runner that I'd like to only exist when running the development server. I've made some experiments on a local shell with configuration loaded using the technique found in NoseGAE versus live on the 'App Engine Console' [1] and it looks to me like a distinction btw real instance and dev server is the presence of the module google.appengine.tools. Which lead me to this utility function:

def is_dev():
    """
    Tells us if we're running under the development server or not.
    :return:
    ``True`` if the code is running under the development server.
    """
    try:
        from google.appengine import tools
        return True
    except ImportError:
        return False

The question (finally!) would be: is this a bad idea? And in that case, can anyone suggest a better approach?

[1] http://con.appspot.com/console/ (try it! very handy indeed)

A: 

I'm not a google app developer, but I wouldn't make this 100% dynamic, but also look at a value from a config file. I'm pretty sure you will be running into the problem, that you want to see this console on the prod system (google servers) or run your local version without the dev code (for testing).

To sum it up: Such a logic is fine for small stuff, like adding a debug link, but provide a way to overwrite it (e.g. by a configuration value)

ZeissS
+1  A: 

I'd recommend doing it this way:

import os
def onDevServer():
    return os.environ['SERVER_SOFTWARE'].find('Development') >= 0

This looks at the environment you're running in, and returns true if you're running on the development server. However, its a much cleaner way than checking an import, in my opinion.

Ink-Jet
Though, following on ZeissS's comment, it may be an idea to put a variable in there to check, so you can override it.
Ink-Jet
Why not just "return expression" rather than "if expression: return True else: return False"?
Nick Johnson
Find() returns the index of where the substring is found in the string. Although 1 will evaluate as true, if the substring is found anywhere later in the string, that would fail.
Ink-Jet
Ink-Jet, return os.environ['SERVER_SOFTWARE'].find('Development') >= 0 is exactly identical to the 4-line construct you posted.
Wooble
Oh sorry, yes. I was thinking just returning the find(), not the comparison. Yes, obviously. I'll edit my post.
Ink-Jet
+4  A: 

The standard way to test for the development server is as follows:

DEBUG = os.environ['SERVER_SOFTWARE'].startswith("Dev")

Relying on the existence or nonexistence of a particular module - especially an undocumented one - is probably a bad idea.

Nick Johnson
os.environ -- typo.
chryss
Fixed, thanks .
Nick Johnson