views:

37

answers:

1

I am brand new to python/GAE and am wondering how to quickly define and use global settings variables, so say you git clone my GAE app and you just open config.yaml, add change the settings, and the app is all wired up, like this:

# config.yaml (or whatever)
settings:
  name: "Lance"
  domain: "http://example.com"

# main.py
class Main(webapp.RequestHandler):
  def get(self):
    logging.info(settings.name) #=> "Lance"

What's the basic way to do something like that (I'm coming from Ruby)?

+4  A: 

You can use any Python persistance module, you aren't limited to YAML.

Examples: ConfigParser, PyYAML, an XML parser like ElementTree, a settings module like used in Django...

# settings.py

NAME = "Lance"
DOMAIN = "http://example.com"

# main.py

import settings

settings.DOMAIN # [...]

# settings.ini

[basic]
name = Lance
domain = http://example.com

# main.py

import ConfigParser

parser = ConfigParser.ConfigParser()
parser.read('setting.ini')

try:
    name = get('basic', 'name')
except (NoOptionError, NoSectionError):
    # no settings
leoluk