2 Star 0 Fork 0

陌路微尘/CustomDataProcess

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
configuration.py 1.59 KB
一键复制 编辑 原始数据 按行查看 历史
陌路微尘 提交于 2022-09-04 18:58 +08:00 . 日志转换为可配置
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
"""
@software: PyCharm
@time: 2022/9/3 13:03
"""
import os
import yaml
class conf(object):
pass
class Configuration:
config = conf()
def __init__(self, conf_file="config.yaml"):
self._file = conf_file
self.conf_dict = dict()
if not os.path.exists(self._file):
raise FileNotFoundError("配置文件未找到!")
else:
self.read()
def read(self):
with open(self._file, 'r', encoding='utf8') as f:
self.conf_dict = yaml.load(f, yaml.FullLoader)
@classmethod
def get_config(cls, conf_file="config.yaml"):
conf_obj = cls(conf_file)
for key, value in conf_obj.conf_dict.items():
convert_obj(cls.config, key, value)
return cls.config
def convert_obj(obj, key, value):
if isinstance(value, list):
setattr(obj, key, [])
for item in value:
if not isinstance(item, dict) and not isinstance(item, list):
getattr(obj, key).append(item)
elif isinstance(item, dict):
item_obj = conf()
getattr(obj, key).append(item_obj)
for sub_key, sub_value in item.items():
convert_obj(item_obj, sub_key, sub_value)
elif not isinstance(value, dict):
setattr(obj, key, value)
else:
setattr(obj, key, conf())
for sub_key, sub_value in value.items():
convert_obj(getattr(obj, key), sub_key, sub_value)
return obj
configuration = Configuration.get_config()
print(configuration.log.maxBytes)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/InsignificantDust/custom-data-process.git
git@gitee.com:InsignificantDust/custom-data-process.git
InsignificantDust
custom-data-process
CustomDataProcess
master

搜索帮助