Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
MMCV
Commits
45a39dc6
Unverified
Commit
45a39dc6
authored
Apr 29, 2020
by
Jerry Jiarui XU
Committed by
GitHub
Apr 29, 2020
Browse files
make config merge a into b non-inplace (#254)
parent
f7de63fd
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
33 additions
and
4 deletions
+33
-4
mmcv/utils/config.py
mmcv/utils/config.py
+9
-4
tests/data/config/i_base.py
tests/data/config/i_base.py
+7
-0
tests/data/config/i_child.py
tests/data/config/i_child.py
+3
-0
tests/test_config.py
tests/test_config.py
+14
-0
No files found.
mmcv/utils/config.py
View file @
45a39dc6
...
...
@@ -126,7 +126,7 @@ class Config(object):
raise
KeyError
(
'Duplicate key is not allowed among bases'
)
base_cfg_dict
.
update
(
c
)
Config
.
_merge_a_into_b
(
cfg_dict
,
base_cfg_dict
)
base_cfg_dict
=
Config
.
_merge_a_into_b
(
cfg_dict
,
base_cfg_dict
)
cfg_dict
=
base_cfg_dict
# merge cfg_text
...
...
@@ -137,7 +137,10 @@ class Config(object):
@
staticmethod
def
_merge_a_into_b
(
a
,
b
):
# merge dict `a` into dict `b`. values in `a` will overwrite `b`.
# merge dict `a` into dict `b` (non-inplace). values in `a` will
# overwrite `b`.
# copy first to avoid inplace modification
b
=
b
.
copy
()
for
k
,
v
in
a
.
items
():
if
isinstance
(
v
,
dict
)
and
k
in
b
and
not
v
.
pop
(
DELETE_KEY
,
False
):
if
not
isinstance
(
b
[
k
],
dict
):
...
...
@@ -145,9 +148,10 @@ class Config(object):
f
'
{
k
}
=
{
v
}
cannot be inherited from base because
{
k
}
'
'is a dict in the child config. You may '
f
'set `
{
DELETE_KEY
}
=True` to ignore the base config'
)
Config
.
_merge_a_into_b
(
v
,
b
[
k
])
b
[
k
]
=
Config
.
_merge_a_into_b
(
v
,
b
[
k
])
else
:
b
[
k
]
=
v
return
b
@
staticmethod
def
fromfile
(
filename
):
...
...
@@ -312,7 +316,8 @@ class Config(object):
d
[
subkey
]
=
v
cfg_dict
=
super
(
Config
,
self
).
__getattribute__
(
'_cfg_dict'
)
Config
.
_merge_a_into_b
(
option_cfg_dict
,
cfg_dict
)
super
(
Config
,
self
).
__setattr__
(
'_cfg_dict'
,
Config
.
_merge_a_into_b
(
option_cfg_dict
,
cfg_dict
))
class
DictAction
(
Action
):
...
...
tests/data/config/i_base.py
0 → 100644
View file @
45a39dc6
item1
=
[
1
,
2
]
item2
=
{
'a'
:
0
}
item3
=
True
item4
=
'test'
item_cfg
=
{
'b'
:
1
}
item5
=
{
'cfg'
:
item_cfg
}
item6
=
{
'cfg'
:
item_cfg
}
tests/data/config/i_child.py
0 → 100644
View file @
45a39dc6
_base_
=
'./i_base.py'
item_cfg
=
{
'b'
:
2
}
item6
=
{
'cfg'
:
item_cfg
}
tests/test_config.py
View file @
45a39dc6
...
...
@@ -120,6 +120,20 @@ def test_merge_delete():
assert
'_delete_'
not
in
cfg
.
item2
def
test_merge_intermediate_variable
():
cfg_file
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'data/config/i_child.py'
)
cfg
=
Config
.
fromfile
(
cfg_file
)
# cfg.field
assert
cfg
.
item1
==
[
1
,
2
]
assert
cfg
.
item2
==
dict
(
a
=
0
)
assert
cfg
.
item3
is
True
assert
cfg
.
item4
==
'test'
assert
cfg
.
item_cfg
==
dict
(
b
=
2
)
assert
cfg
.
item5
==
dict
(
cfg
=
dict
(
b
=
1
))
assert
cfg
.
item6
==
dict
(
cfg
=
dict
(
b
=
2
))
def
test_dict
():
cfg_dict
=
dict
(
item1
=
[
1
,
2
],
item2
=
dict
(
a
=
0
),
item3
=
True
,
item4
=
'test'
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment