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
31e684fc
Commit
31e684fc
authored
Sep 20, 2018
by
Kai Chen
Browse files
bug fix: raise exceptions for missing config keys
parent
685e8f99
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
35 additions
and
6 deletions
+35
-6
mmcv/utils/config.py
mmcv/utils/config.py
+25
-4
tests/data/config/a.b.py
tests/data/config/a.b.py
+0
-0
tests/test_config.py
tests/test_config.py
+10
-2
No files found.
mmcv/utils/config.py
View file @
31e684fc
...
...
@@ -6,6 +6,26 @@ from importlib import import_module
from
addict
import
Dict
from
.path
import
check_file_exist
class
ConfigDict
(
Dict
):
def
__missing__
(
self
,
name
):
raise
KeyError
(
name
)
def
__getattr__
(
self
,
name
):
try
:
value
=
super
(
ConfigDict
,
self
).
__getattr__
(
name
)
except
KeyError
:
ex
=
AttributeError
(
"'{}' object has no attribute '{}'"
.
format
(
self
.
__class__
.
__name__
,
name
))
except
Exception
as
e
:
ex
=
e
else
:
return
value
raise
ex
def
add_args
(
parser
,
cfg
,
prefix
=
''
):
for
k
,
v
in
cfg
.
items
():
...
...
@@ -55,6 +75,7 @@ class Config(object):
@
staticmethod
def
fromfile
(
filename
):
filename
=
osp
.
abspath
(
osp
.
expanduser
(
filename
))
check_file_exist
(
filename
)
if
filename
.
endswith
(
'.py'
):
sys
.
path
.
append
(
osp
.
dirname
(
filename
))
module_name
=
osp
.
basename
(
filename
)[:
-
3
]
...
...
@@ -93,7 +114,7 @@ class Config(object):
raise
TypeError
(
'cfg_dict must be a dict, but got {}'
.
format
(
type
(
cfg_dict
)))
super
(
Config
,
self
).
__setattr__
(
'_cfg_dict'
,
Dict
(
cfg_dict
))
super
(
Config
,
self
).
__setattr__
(
'_cfg_dict'
,
Config
Dict
(
cfg_dict
))
super
(
Config
,
self
).
__setattr__
(
'_filename'
,
filename
)
if
filename
:
with
open
(
filename
,
'r'
)
as
f
:
...
...
@@ -110,7 +131,7 @@ class Config(object):
return
self
.
_text
def
__repr__
(
self
):
return
'Config
[
path: {}
]
: {}'
.
format
(
self
.
filename
,
return
'Config
(
path: {}
)
: {}'
.
format
(
self
.
filename
,
self
.
_cfg_dict
.
__repr__
())
def
__len__
(
self
):
...
...
@@ -124,12 +145,12 @@ class Config(object):
def
__setattr__
(
self
,
name
,
value
):
if
isinstance
(
value
,
dict
):
value
=
Dict
(
value
)
value
=
Config
Dict
(
value
)
self
.
_cfg_dict
.
__setattr__
(
name
,
value
)
def
__setitem__
(
self
,
name
,
value
):
if
isinstance
(
value
,
dict
):
value
=
Dict
(
value
)
value
=
Config
Dict
(
value
)
self
.
_cfg_dict
.
__setitem__
(
name
,
value
)
def
__iter__
(
self
):
...
...
tests/data/config/a.b.py
0 → 100644
View file @
31e684fc
tests/test_config.py
View file @
31e684fc
import
os.path
as
osp
import
pytest
from
mmcv
import
Config
from
mmcv
import
Config
,
FileNotFoundError
def
test_empty
():
...
...
@@ -20,8 +20,10 @@ def test_fromfile():
assert
cfg
.
filename
==
cfg_file
assert
cfg
.
text
==
open
(
cfg_file
,
'r'
).
read
()
with
pytest
.
raises
(
FileNotFoundError
):
Config
.
fromfile
(
'no_such_file.py'
)
with
pytest
.
raises
(
ValueError
):
Config
.
fromfile
(
'a/b/c.d
.py'
)
Config
.
fromfile
(
osp
.
join
(
osp
.
dirname
(
__file__
),
'data/config/a.b
.py'
)
)
def
test_dict
():
...
...
@@ -49,12 +51,18 @@ def test_dict():
assert
cfg
.
item2
.
a
==
0
assert
cfg
.
item3
==
cfg_dict
[
'item3'
]
assert
cfg
.
item4
==
cfg_dict
[
'item4'
]
with
pytest
.
raises
(
AttributeError
):
cfg
.
not_exist
# field in cfg, cfg[field], cfg.get()
for
name
in
[
'item1'
,
'item2'
,
'item3'
,
'item4'
]:
assert
name
in
cfg
assert
cfg
[
name
]
==
cfg_dict
[
name
]
assert
cfg
.
get
(
name
)
==
cfg_dict
[
name
]
assert
cfg
.
get
(
'not_exist'
)
is
None
assert
cfg
.
get
(
'not_exist'
,
0
)
==
0
with
pytest
.
raises
(
KeyError
):
cfg
[
'not_exist'
]
assert
'item1'
in
cfg
assert
'not_exist'
not
in
cfg
# cfg.update()
cfg
.
update
(
dict
(
item1
=
0
))
...
...
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