用于在整个应用程序中共享配置的 Python 模式
我有一个应用程序,其中包含一个引入多个模块的基本应用程序。基本应用程序将参数文件读取到配置哈希中,我想在所有模块之间共享它。
目前,我将“父”对象传递给模块,然后这些模块执行诸如 self.parent.config 之类的操作来获取配置。
然而,由于模块层次结构有多个级别,我发现自己在做诸如 self.parent.parent.config 之类的事情,这开始看起来很糟糕。
在应用程序及其模块之间共享配置对象有哪些更好的模式?我正在考虑有一个“配置”模块,它基本上创建一个全局配置变量,可以由基本应用程序设置,然后由其他模块导入和访问,但我不确定使用这样的全局变量对于其他模块来说是否是一个坏习惯原因。
我对 Python 还算陌生,所以要友善 =)
I have an application consisting of a base app that brings in several modules. The base app reads a parameter file into a configuration hash, and I want to share it across all my modules.
Currently, I am passing a 'parent' object down to modules, and then those modules are doing stuff like self.parent.config to obtain the configuration.
However, as there are several levels to the module hierarchy, I find myself doing things like self.parent.parent.config, which is starting to look bad.
What are some better patterns for sharing a config object across an application and it's modules? I am thinking about having a 'config' module which basically creates a global config variable that can be set by the base app, then imported and accessed by other modules, but I am not sure if using globals like that is a bad practice for other reasons.
I am reasonably new to Python so be nice =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以:
并从我的评论中摘录一个全局配置模块
:
你总是可以通过说
oddValue if isOddSituation() else config.normalValue 来为奇怪的情况添加特殊规则。
如果您想让配置模块可以分层子类化(就像我的其他答案所描述的那样),那么您可以将配置表示为类,或者您可以使用
copy
模块并进行浅表复制并修改它,或者您可以使用“配置字典”,例如:您所在的范围并不重要。
You could just:
and have a global config module
excerpts from my comments:
You can always add special rules for odd situations by just saying
oddValue if isOddSituation() else config.normalValue
.If you want to have configuration modules be hierarchically subclassable (like my other answer describes), then you can represent a config as a class, or you can use the
copy
module and make a shallow copy and modify it, or you can use a "config dictionary", e.g.:It doesn't really matter too much which scope you're in.
回答老问题:
只需按照@Reed Copsey的建议使用依赖注入 此处。例如
Answering old question:
Just use dependency injection as suggested by @Reed Copsey here. E.g.
我认为“模块”实际上指的是“类/对象”。对象是类的实例,例如:
模块是您导入的 .py 文件,如下所示:
您实例化的所有类似乎不太可能想要访问全局配置。但是,如果您确实需要应用程序中的所有内容都可以访问某些全局参数,则可以将它们放入您自己的 config 模块中:
然后从任何模块或任何对象或任何地方,只要您做了
import config
,你可以直接说print(config.myParam1)
或者,如果你希望一个大的对象层次结构都共享对同一属性的访问,你不需要需要通过手动设置 self.parent 来引用它。只要使用继承,您就可以执行以下操作:
I think by 'module', you are actually referring to a 'class/object'. An object is an instance of a class, for example:
A module is a .py file you import, like so:
It seems unlikely that all the classes you instantiate would want to have access to a global configuration. However if you really need everything in your application to have access to some global parameters, you can put them in your own
config
module:and then from any module or any object or anywhere really, as long as you did
import config
, you could just sayprint(config.myParam1)
Alternatively if you want a large hierarchy of objects to all share access to the same property, you don't need to refer to it via manually setting a
self.parent
. As long as you use inheritance, you can do stuff like:看看这个。它可以帮助你:
https://gist.github.com/dgarana/c052a3287629dd7c0b0c9d7921081e9d
Take a look at this. It could help you:
https://gist.github.com/dgarana/c052a3287629dd7c0b0c9d7921081e9d