- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试为 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/
我正在开发一个需要能够平均三个数字的 Facebook 应用程序。但是,它总是返回 0 作为答案。这是我的代码: $y = 100; $n = 250; $m = 300; $number = ($y
我只是无法弄清楚这一点,也找不到任何对我来说有意义的类似问题。我的问题:我从数据库中提取记录,并在我的网页上以每个面板 12 条的倍数显示它们。因此,我需要知道有多少个面板可以使用 JavaScrip
我是一名优秀的程序员,十分优秀!