用于在整个应用程序中共享配置的 Python 模式

发布于 2024-11-02 18:00:24 字数 368 浏览 0 评论 0原文

我有一个应用程序,其中包含一个引入多个模块的基本应用程序。基本应用程序将参数文件读取到配置哈希中,我想在所有模块之间共享它。

目前,我将“父”对象传递给模块,然后这些模块执行诸如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

注定孤独终老 2024-11-09 18:00:24

你可以:

import config

并从我的评论中摘录一个全局配置模块


你总是可以通过说 oddValue if isOddSituation() else config.normalValue 来为奇怪的情况添加特殊规则。

如果您想让配置模块可以分层子类化(就像我的其他答案所描述的那样),那么您可以将配置表示为类,或者您可以使用 copy 模块并进行浅表复制并修改它,或者您可以使用“配置字典”,例如:

import config as baseConfig
config = dict(baseConfig, overriddenValue=etc)

您所在的范围并不重要。

You could just:

import config

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.:

import config as baseConfig
config = dict(baseConfig, overriddenValue=etc)

It doesn't really matter too much which scope you're in.

迷爱 2024-11-09 18:00:24

回答老问题:

只需按照@Reed Copsey的建议使用依赖注入 此处。例如

class MyClass:
    def __init__(self, myConfig):
        self.myConfig = myConfig
        ...

    def foo(self):
        self.myConfig.getConfig(key)
        ...
        self.myConfig.setConfig(key,val)
        ...

...
# myConfig is your configuration management Module/Class
obj = SomeClass(myConfig)

Answering old question:

Just use dependency injection as suggested by @Reed Copsey here. E.g.

class MyClass:
    def __init__(self, myConfig):
        self.myConfig = myConfig
        ...

    def foo(self):
        self.myConfig.getConfig(key)
        ...
        self.myConfig.setConfig(key,val)
        ...

...
# myConfig is your configuration management Module/Class
obj = SomeClass(myConfig)
手心的温暖 2024-11-09 18:00:24

我认为“模块”实际上指的是“类/对象”。对象是类的实例,例如:

class MyClass(object):
    def __init__(self, ...):
        ...
    ...

myObject = MyClass()

模块是您导入的 .py 文件,如下所示:

import mymodule

您实例化的所有类似乎不太可能想要访问全局配置。但是,如果您确实需要应用程序中的所有内容都可以访问某些全局参数,则可以将它们放入您自己的 config 模块中:

myParam1 = 1
myParam2 = 2

然后从任何模块或任何对象或任何地方,只要您做了 import config,你可以直接说 print(config.myParam1)

或者,如果你希望一个大的对象层次结构都共享对同一属性的访问,你不需要需要通过手动设置 self.parent 来引用它。只要使用继承,您就可以执行以下操作:

class Parent(object):
    def __init__(self, theConfig):
        self.theConfig = theConfig

class Child(Parent):
    ...
    def method(self,...):
        print(self.theConfig)

I think by 'module', you are actually referring to a 'class/object'. An object is an instance of a class, for example:

class MyClass(object):
    def __init__(self, ...):
        ...
    ...

myObject = MyClass()

A module is a .py file you import, like so:

import mymodule

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:

myParam1 = 1
myParam2 = 2

and then from any module or any object or anywhere really, as long as you did import config, you could just say print(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:

class Parent(object):
    def __init__(self, theConfig):
        self.theConfig = theConfig

class Child(Parent):
    ...
    def method(self,...):
        print(self.theConfig)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文