Skip to content

Instantly share code, notes, and snippets.

@jehiah
Created September 24, 2010 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jehiah/595574 to your computer and use it in GitHub Desktop.
Save jehiah/595574 to your computer and use it in GitHub Desktop.
"""
This module encapsulates all knowledge about what environment your
code is running in (ie: development / production).
Note: make sure all calls to settings.get() are done at runtime, and not at import time.
usage:
import settings
def test():
remote_endpoint = settings.get("remote_endpoint")
...
"""
import tornado.options
import random
tornado.options.define("environment", default="dev", help="environment")
def randomize(values):
""" this is a wrapper that returns a function which when called returns a random value"""
def picker():
return random.choice(values)
return picker
# settings that are different for each environment
# (ie: pointers to remote endpoints)
options = {
"dev" : {
"remote_endpoint" : "http://127.0.0.1:8080",
},
"prod" : {
"remote_endpoint" : randomize(["http://remote-server1.com",
"http://remote-server2.com"]),
}
}
# settings that are the same for each environment
# (ie: they are here just to make them easy to find/change/update)
default_options = {
"num_records_to_return" : 15,
}
def get(key):
env = tornado.options.options.environment
if env not in options:
raise Exception("Invalid Environment (%s)" % env)
v = options.get(env).get(key) or default_options.get(key)
if callable(v):
return v()
return v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment