English 中文(简体)
你们如何界定谷歌应用发动机(Python)的汇合变量/常数?
原标题:How do you define config variables / constants in Google App Engine (Python)?

我是Python/GAE的新陈词,并想知道如何迅速界定和使用全球环境变量,因此,我要说,你是我的GAE和你刚刚开放的<条码>config.yaml,增加了环境变化,而且所有电线也一样:

# 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"

采取什么基本方法做这样的事情(从鲁比拉到)?

最佳回答

你可以使用任何沙捞越模块,你只使用亚马列。

实例:ConferigParser, PyYAML, an XML parser such as ElementTree, a context model as 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
问题回答

如果有敏感数据,你就不应将其储存在源代码中,因为它将受到源源控制。 错误的人(在贵组织内外)可能发现。 此外,你的发展环境可能使用与生产环境不同的组合价值。 如果这些价值观被储存在法典中,那么,在发展和生产方面,你将不得不实行不同的法典,而这是令人迷惑和不良的做法。

在我的项目中,我利用这一类别将数据储存起来:

from google.appengine.ext import ndb

class Settings(ndb.Model):
  name = ndb.StringProperty()
  value = ndb.StringProperty()

  @staticmethod
  def get(name):
    NOT_SET_VALUE = "NOT SET"
    retval = Settings.query(Settings.name == name).get()
    if not retval:
      retval = Settings()
      retval.name = name
      retval.value = NOT_SET_VALUE
      retval.put()
    if retval.value == NOT_SET_VALUE:
      raise Exception(( Setting %s not found in the database. A placeholder   +
         record has been created. Go to the Developers Console for your app   +
         in App Engine, look up the Settings record with name=%s and enter   +
         its value in that record s value field. ) % (name, name))
    return retval.value

您的申请将发挥这一作用:

DOMAIN = Settings.get( DOMAIN )

如果在数据库中这一关键点具有价值,你将获得这一价值。 如果存在吨位,将建立一个持户记录,并将提出例外。 该例外将提醒各位去开发商康索尔,并更新股东记录。

我认为,这是对确定混淆价值观的猜测。 如果你们不相信要确定什么样的价值观,那么,这就只是管理法典,并将告诉你们!





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签