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...
If it's sensitive data, you should not store it in source code as it will be checked into source control. The wrong people (inside or outside your organization) may find it there. Also, your development environment probably uses different config values from your production environment. If these values are stored in code, you will have to run different code in development and production, which is messy and bad practice.
In my projects, I put config data in the datastore using this class:
Your application would do this to get a value:
If there is a value for that key in the datastore, you will get it. If there isn't, a placeholder record will be created and an exception will be thrown. The exception will remind you to go to the Developers Console and update the placeholder record.
I find this takes the guessing out of setting config values. If you are unsure of what config values to set, just run the code and it will tell you!