代码拉取完成,页面将自动刷新
import argparse
import json
from utils import dotdict
def make_as_dotdict(obj):
if type(obj) is dict:
obj = dotdict(obj)
for key in obj:
if type(obj[key]) is dict:
obj[key] = make_as_dotdict(obj[key])
return obj
def parse():
print('Parsing config file...')
parser = argparse.ArgumentParser(description="config")
parser.add_argument(
"--config",
type=str,
default="configs/base.json",
help="Configuration file to use"
)
cli_args = parser.parse_args()
with open(cli_args.config) as fp:
config = make_as_dotdict(json.loads(fp.read()))
print(json.dumps(config, indent=4, sort_keys=True))
return config
class Singleton(object):
_instance = None
def __new__(cls, *args, **kw):
if not cls._instance:
cls._instance = super(Singleton, cls).__new__(cls, *args, **kw)
return cls._instance
class Config(Singleton):
def __init__(self):
self._cfg = dotdict({})
try:
self._cfg = parse()
except:
pass
def __getattr__(self, name):
if name == '_cfg':
super().__setattr__(name)
else:
return self._cfg.__getattr__(name)
def __setattr__(self, name, val):
if name == '_cfg':
super().__setattr__(name, val)
else:
self._cfg.__setattr__(name, val)
def __delattr__(self, name):
return self._cfg.__delitem__(name)
def copy(self, new_config):
self._cfg = make_as_dotdict(new_config)
cfg = Config()
def parse_from_dict(d):
global cfg
assert type(d) == dict
cfg.copy(d)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。