Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you define config variables / constants in Google App Engine (Python)?

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)?

like image 339
Lance Avatar asked Aug 26 '10 19:08

Lance


1 Answers

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
like image 152
leoluk Avatar answered Nov 15 '22 12:11

leoluk