从 class 加载配置,使其表现得像 属性
Loading config from class in such a way that it behaves like a property
我正在尝试为 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()
在classConfig 的定义之后。但是有一个更简单的解决方案:可以删除 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。您无需处理单例对象行为或 class 变量。 _Config 前面的下划线保证 class 构造函数在加载模块时只会被调用一次。 class 只能通过名为 config
.
的变量访问
我还解决了第二个未讨论的问题:您没有在 Config 构造函数中使用变量 "args",因此实际上没有对命令行解析器的输出做任何事情。我把args做成了成员变量,适当修改了__getattr__
函数。
我正在尝试为 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()
在classConfig 的定义之后。但是有一个更简单的解决方案:可以删除 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。您无需处理单例对象行为或 class 变量。 _Config 前面的下划线保证 class 构造函数在加载模块时只会被调用一次。 class 只能通过名为 config
.
我还解决了第二个未讨论的问题:您没有在 Config 构造函数中使用变量 "args",因此实际上没有对命令行解析器的输出做任何事情。我把args做成了成员变量,适当修改了__getattr__
函数。