gpt4 book ai didi

python - 以一种行为类似于属性的方式从类中加载配置

转载 作者:太空宇宙 更新时间:2023-11-04 02:34:04 25 4
gpt4 key购买 nike

我正在尝试为 python 创建一个 yaml 驱动的配置模块,它既可以从平面 yaml 文件加载,也可以选择从 sys.argv 加载参数。它还将加载的属性保存到局部变量(这很丑陋,但每次引用 config 时解析 yaml 文件仍然更可取)。

我有这个工作,但我必须导入它的方式似乎过于夸张,而且不是很 pythonic。有没有更优雅的方法来构造它以便:

  • 我可以使用单个导入语句
  • 我可以确保配置的加载次数(在使用它的不同模块中)不会超过绝对必要的次数

结构:

/app
/__init__.py
/__main__.py # calls __init__.py
/config.yaml
/applib
/__init__.py
/__config_loader.py

在应用程序的主 __init__.py 中,我像这样加载配置:

from applib.config_loader import Config; config = Config()
# ^ ew!

config_loader.py 中,我有:

import argparse
import yaml

class Config:
_config = None

@property
def config(self):
if self._config is not None:
print('Using what we already loaded')
return self._config or Config()

def __init__(self):
if self._config is None:
print('Loading config')

with open(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'config.yaml')) as yaml_config_file:
self._config = yaml.load(yaml_config_file)

parser = argparse.ArgumentParser(description = 'MyApp')
parser.add_argument('--an_arg')
args = parser.parse_args()

def __getattr__(self, name):
return self._config[name] or None

当然不是让它工作的最漂亮的方法,而且我似乎无法理解在 python 中做事的旧方法和新方法,这让情况变得过于复杂。

我希望能够像这样加载它:

from applib.config_loader import config

print(config['an_arg'])

最佳答案

您的问题的简单解决方案是放置以下行:

config = Config()

类Config的定义之后。但还有一个更简单的解决方案:可以删除 config_loader.py 中大约一半的代码。像这样:

import argparse
import yaml

class _Config:
def __init__(self):
print('Loading config')
with open(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'config.yaml')) as yaml_config_file:
self.config = yaml.load(yaml_config_file)
parser = argparse.ArgumentParser(description = 'MyApp')
parser.add_argument('--an_arg')
self.args = parser.parse_args()

def __getattr__(self, name):
try:
return self.config[name]
except KeyError:
return getattr(self.args, name)

config = _Config()

您的客户端代码现在可以:

from config_loader import config

您以通常的方式访问配置中的属性,config.xxx。您不需要处理单例对象行为或类变量。 _Config 前面的下划线保证在加载模块时只调用一次类构造函数。该类只能通过名为 config 的变量访问。

我还解决了第二个未讨论的问题:您没有在 Config 构造函数中使用变量“args”,因此实际上没有对命令行解析器的输出做任何事情。我将 args 设为成员变量,并适当修改了 __getattr__ 函数。

关于python - 以一种行为类似于属性的方式从类中加载配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48351139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com