mirror of
https://github.com/open-mmlab/mmengine.git
synced 2025-06-03 21:54:44 +08:00
Enhance config
This commit is contained in:
parent
17624d250e
commit
1c378d8ad1
@ -1,6 +1,7 @@
|
||||
# Copyright (c) OpenMMLab. All rights reserved.
|
||||
import ast
|
||||
import copy
|
||||
import logging
|
||||
import os
|
||||
import os.path as osp
|
||||
import platform
|
||||
@ -722,6 +723,15 @@ class Config:
|
||||
def _format_basic_types(k, v, use_mapping=False):
|
||||
if isinstance(v, str):
|
||||
v_str = repr(v)
|
||||
else:
|
||||
try:
|
||||
ast.parse(str(v))
|
||||
except SyntaxError:
|
||||
v_str = repr(str(v))
|
||||
print_log(
|
||||
f'Cannot parse the value: {v} of key "{k}"',
|
||||
logger='current',
|
||||
level=logging.WARNING)
|
||||
else:
|
||||
v_str = str(v)
|
||||
|
||||
|
@ -274,6 +274,14 @@ class TestConfig:
|
||||
text_cfg = Config.fromfile(text_cfg_filename)
|
||||
assert text_cfg._cfg_dict == cfg._cfg_dict
|
||||
|
||||
cfg_file = osp.join(self.data_path,
|
||||
'config/py_config/test_custom_class.py')
|
||||
cfg = Config.fromfile(cfg_file)
|
||||
with open(text_cfg_filename, 'w') as f:
|
||||
f.write(cfg.pretty_text)
|
||||
text_cfg = Config.fromfile(text_cfg_filename)
|
||||
assert text_cfg.item_a.a == ("<class 'A'>")
|
||||
|
||||
def test_repr(self, tmp_path):
|
||||
cfg_file = osp.join(self.data_path,
|
||||
'config/py_config/simple_config.py')
|
||||
|
Loading…
x
Reference in New Issue
Block a user