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
a5b51937
Unverified
Commit
a5b51937
authored
May 01, 2020
by
Jerry Jiarui XU
Committed by
GitHub
May 01, 2020
Browse files
Fixed config import in config file (#261)
* fixed config import in code * fixed isort
parent
45111e19
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
23 additions
and
3 deletions
+23
-3
mmcv/utils/config.py
mmcv/utils/config.py
+9
-3
tests/data/config/code.py
tests/data/config/code.py
+3
-0
tests/test_config.py
tests/test_config.py
+11
-0
No files found.
mmcv/utils/config.py
View file @
a5b51937
...
@@ -85,10 +85,14 @@ class Config(object):
...
@@ -85,10 +85,14 @@ class Config(object):
check_file_exist
(
filename
)
check_file_exist
(
filename
)
if
filename
.
endswith
(
'.py'
):
if
filename
.
endswith
(
'.py'
):
with
tempfile
.
TemporaryDirectory
()
as
temp_config_dir
:
with
tempfile
.
TemporaryDirectory
()
as
temp_config_dir
:
temp_config_file
=
tempfile
.
NamedTemporaryFile
(
dir
=
temp_config_dir
,
suffix
=
'.py'
)
temp_config_name
=
osp
.
basename
(
temp_config_file
.
name
)
shutil
.
copyfile
(
filename
,
shutil
.
copyfile
(
filename
,
osp
.
join
(
temp_config_dir
,
'_tempconfig.py'
))
osp
.
join
(
temp_config_dir
,
temp_config_name
))
temp_module_name
=
osp
.
splitext
(
temp_config_name
)[
0
]
sys
.
path
.
insert
(
0
,
temp_config_dir
)
sys
.
path
.
insert
(
0
,
temp_config_dir
)
mod
=
import_module
(
'_
temp
config'
)
mod
=
import_module
(
temp
_module_name
)
sys
.
path
.
pop
(
0
)
sys
.
path
.
pop
(
0
)
cfg_dict
=
{
cfg_dict
=
{
name
:
value
name
:
value
...
@@ -96,7 +100,9 @@ class Config(object):
...
@@ -96,7 +100,9 @@ class Config(object):
if
not
name
.
startswith
(
'__'
)
if
not
name
.
startswith
(
'__'
)
}
}
# delete imported module
# delete imported module
del
sys
.
modules
[
'_tempconfig'
]
del
sys
.
modules
[
temp_module_name
]
# close temp file
temp_config_file
.
close
()
elif
filename
.
endswith
((
'.yml'
,
'.yaml'
,
'.json'
)):
elif
filename
.
endswith
((
'.yml'
,
'.yaml'
,
'.json'
)):
import
mmcv
import
mmcv
cfg_dict
=
mmcv
.
load
(
filename
)
cfg_dict
=
mmcv
.
load
(
filename
)
...
...
tests/data/config/code.py
0 → 100644
View file @
a5b51937
from
mmcv
import
Config
# isort:skip
cfg
=
Config
.
fromfile
(
'./tests/data/config/a.py'
)
item5
=
cfg
.
item1
[
0
]
+
cfg
.
item2
.
a
tests/test_config.py
View file @
a5b51937
...
@@ -134,6 +134,17 @@ def test_merge_intermediate_variable():
...
@@ -134,6 +134,17 @@ def test_merge_intermediate_variable():
assert
cfg
.
item6
==
dict
(
cfg
=
dict
(
b
=
2
))
assert
cfg
.
item6
==
dict
(
cfg
=
dict
(
b
=
2
))
def
test_fromfile_in_config
():
cfg_file
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'data/config/code.py'
)
cfg
=
Config
.
fromfile
(
cfg_file
)
# cfg.field
assert
cfg
.
cfg
.
item1
==
[
1
,
2
]
assert
cfg
.
cfg
.
item2
==
dict
(
a
=
0
)
assert
cfg
.
cfg
.
item3
is
True
assert
cfg
.
cfg
.
item4
==
'test'
assert
cfg
.
item5
==
1
def
test_dict
():
def
test_dict
():
cfg_dict
=
dict
(
item1
=
[
1
,
2
],
item2
=
dict
(
a
=
0
),
item3
=
True
,
item4
=
'test'
)
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