存储配置详细信息 [英] Storing Configuration details

查看:34
本文介绍了存储配置详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆字符串和整数常量,我在我的应用程序的不同地方使用它们.我打算将它们放在一个集中的位置,以便将来更容易更改它们.我可以想到以下方法:

I have a bunch of string and integer constants that I use at various places in my app. I am planning to put them up in a centralized location, so that it is easier to change them in the future. I could think of the following approaches:

1) 将它们作为单独的变量存储在模型 db.py

1) Have them as individual variables, stored in the model db.py

settings_title = "My Amazing App"
settings_ver = 2.0
settings_desc = "Moar and Moar cats"

2) 将它们作为字典存储在 db.py

2) Have them as a dict, stored in db.py

settings = { "title": "My Amazing App",
    "ver" = 2.0,
    "desc" = "Moar and Moar cats"
}

  • 使用模型 db.py 是个好主意吗?我听说它会针对每个请求进行评估.将设置放在那里会产生明显的开销吗?
  • 这两种方法在性能方面有什么区别吗?
  • 有没有更好的方法来做到这一点?
    • Is it a good idea to use the model db.py? I've heard that it is evaluated for every request. Could putting settings there have a noticeable overhead?
    • Is there any difference, performance-wise between the two approaches?
    • Is there a better way of doing this?
    • 推荐答案

      您的 db.py 模型文件在任何情况下都会在每个请求上执行,因此向代码添加一些设置分配将增加可忽略不计的开销.为了更好的组织,您可以考虑创建一个单独的模型文件,而不是将设置放在 db.py 中.请注意,模型文件按字母顺序执行,因此如果必须在后续模型文件中提供这些设置,请将设置文件命名为 0_settings.py 以确保它在任何其他模型文件之前执行.

      Your db.py model file will be executed on every request in any case, so adding a few setting assignments to the code will add negligible overhead. Instead of putting the settings in db.py, for better organization you might consider creating a separate model file. Note, model files are executed in alphabetical order, so if the settings have to be available in subsequent model files, name the settings file something like 0_settings.py to ensure it is executed before any other model files.

      如果您愿意,您可以改为将设置放在应用程序/modules 文件夹中的模块(例如,settings.py)中,并在您需要的应​​用程序代码中的任何位置导入设置对象(在这种情况下,模块将解释器只能加载一次).但是,如果需要根据传入请求动态设置任何设置,则最好将设置保存在模型文件中.

      If you prefer, you can instead put the settings in a module (e.g., settings.py) in the application's /modules folder and import the settings object in your application code wherever you need it (in that case, the module will only be loaded once by the interpreter). If any of the settings need to be set dynamically based on the incoming request, though, you are probably better off keeping the settings in a model file.

      最后,您可以考虑使用 web2py Storage 对象,而不是标准字典,它类似于字典,但允许您访问值作为属性并返回 None 而不是 KeyError 如果您尝试访问不存在的键/属性:

      Finally, rather than a standard dictionary, you might consider using a web2py Storage object, which is like a dictionary but allows you to access values as attributes and returns None rather than a KeyError if you try to access a key/attribute that doesn't exist:

      from gluon.storage import Storage
      settings = Storage()
      settings.title = 'My Amazing App'
      

      settings = Storage({'title': 'My Amazing App'})
      

      注意,web2py requestresponsesession 对象都是 Storage 类的实例.

      Note, the web2py request, response, and session objects are all instances of the Storage class.

      这篇关于存储配置详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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