如何在Google App Engine(Python)中定义配置变量/常量? [英] How do you define config variables / constants in Google App Engine (Python)?

查看:163
本文介绍了如何在Google App Engine(Python)中定义配置变量/常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python / GAE的新手,想知道如何快速定义和使用全局设置变量,所以说你git克隆我的GAE应用程序,然后打开 config.yaml ,添加更改设置,并且应用程序全部连线,如下所示:

 #config.yaml )
设置:
名称:Lance
域名:http://example.com

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

做类似这样的事情的基本方式是什么(我来自Ruby)?

解决方案

您可以使用任何Python持久性模块,您不限于YAML。 b $ b

示例:ConfigParser,PyYAML,像ElementTree这样的XML解析器,像Django中使用的设置模块...

 #---------- settings.py 

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

#---------- main.py

导入设置

settings.DOMAIN#[...]






 #------- --- settings.ini 

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

#---- ------ main.py

import ConfigParser

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

尝试:
name = get('basic','name')
除了(NoOptionError,NoSectionError):
#没有settin gs


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

这篇关于如何在Google App Engine(Python)中定义配置变量/常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆