Skip to content

Commit 66b50fb

Browse files
committed
address comments, style format
Signed-off-by: Wenqi Li <wenqil@nvidia.com>
1 parent 97ebf2e commit 66b50fb

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

monai/bundle/reference_resolver.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class ReferenceResolver:
5252
_vars = "__local_refs"
5353
sep = ID_SEP_KEY # separator for key indexing
5454
ref = ID_REF_KEY # reference prefix
55-
# match a reference string, e.g. "@id#key", "@id#key#0", "@_target_#key"
55+
# match a reference string, e.g. "@id::key", "@id::key::0", "@_target_::key"
5656
id_matcher = re.compile(rf"{ref}(?:\w*)(?:{sep}\w*)*")
5757
# if `allow_missing_reference` and can't find a reference ID, will just raise a warning and don't update the config
5858
allow_missing_reference = allow_missing_reference
@@ -99,9 +99,9 @@ def get_item(self, id: str, resolve: bool = False, **kwargs: Any) -> ConfigItem
9999
kwargs: keyword arguments to pass to ``_resolve_one_item()``.
100100
Currently support ``instantiate`` and ``eval_expr``. Both are defaulting to True.
101101
"""
102+
id = self.normalize_id(id)
102103
if resolve and id not in self.resolved_content:
103104
self._resolve_one_item(id=id, **kwargs)
104-
id = self.normalize_id(id)
105105
return self.items.get(id)
106106

107107
def _resolve_one_item(
@@ -199,14 +199,13 @@ def normalize_id(cls, id: str | int) -> str:
199199
200200
Args:
201201
id: id string to be normalized.
202-
203202
"""
204203
return str(id).replace("#", cls.sep) # backward compatibility `#` is the old separator
205204

206205
@classmethod
207206
def split_id(cls, id: str | int, last: bool = False) -> list[str]:
208207
"""
209-
Split the id string into a tuple of strings.
208+
Split the id string into a list of strings by `cls.sep`.
210209
211210
Args:
212211
id: id string to be split.
@@ -220,7 +219,7 @@ def split_id(cls, id: str | int, last: bool = False) -> list[str]:
220219
@classmethod
221220
def iter_subconfigs(cls, id: str, config: Any) -> Iterator[tuple[str, str, Any]]:
222221
"""
223-
Iterate over the sub-configs of the input config.
222+
Iterate over the sub-configs of the input config, the output `sub_id` uses `cls.sep` to denote substructure.
224223
225224
Args:
226225
id: id string of the current input config.
@@ -277,11 +276,10 @@ def update_refs_pattern(cls, value: str, refs: dict) -> str:
277276
ref_id = item[len(cls.ref) :] # remove the ref prefix "@"
278277
if ref_id not in refs:
279278
msg = f"can not find expected ID '{ref_id}' in the references."
280-
if cls.allow_missing_reference:
281-
warnings.warn(msg)
282-
continue
283-
else:
279+
if not cls.allow_missing_reference:
284280
raise KeyError(msg)
281+
warnings.warn(msg)
282+
continue
285283
if value_is_expr:
286284
# replace with local code, `{"__local_refs": self.resolved_content}` will be added to
287285
# the `globals` argument of python `eval` in the `evaluate`
@@ -307,7 +305,7 @@ def find_refs_in_config(cls, config: Any, id: str, refs: dict[str, int] | None =
307305
"""
308306
refs_: dict[str, int] = refs or {}
309307
if isinstance(config, str):
310-
for id, count in cls.match_refs_pattern(value=config).items():
308+
for id, count in cls.match_refs_pattern(value=config).items(): # ref count is not currently used
311309
refs_[id] = refs_.get(id, 0) + count
312310
if not isinstance(config, (list, dict)):
313311
return refs_

0 commit comments

Comments
 (0)