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)?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With