Skip to content

Commit 8896b22

Browse files
committed
:: and #
Signed-off-by: Wenqi Li <wenqil@nvidia.com>
1 parent ef7debe commit 8896b22

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

monai/bundle/config_parser.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ def __getitem__(self, id: str | int) -> Any:
150150
if id == "":
151151
return self.config
152152
config = self.config
153+
id = str(id).replace("#", ID_SEP_KEY)
153154
for k in str(id).split(ID_SEP_KEY):
154155
if not isinstance(config, (dict, list)):
155156
raise ValueError(f"config must be dict or list for key `{k}`, but got {type(config)}: {config}.")
@@ -178,7 +179,8 @@ def __setitem__(self, id: str | int, config: Any) -> None:
178179
self.config = config
179180
self.ref_resolver.reset()
180181
return
181-
keys = str(id).split(ID_SEP_KEY)
182+
id = str(id).replace("#", ID_SEP_KEY)
183+
keys = id.split(ID_SEP_KEY)
182184
# get the last parent level config item and replace it
183185
last_id = ID_SEP_KEY.join(keys[:-1])
184186
conf_ = self[last_id]
@@ -213,7 +215,8 @@ def set(self, config: Any, id: str = "", recursive: bool = True) -> None:
213215
default to `True`. for the nested id, only support `dict` for the missing section.
214216
215217
"""
216-
keys = str(id).split(ID_SEP_KEY)
218+
id = str(id).replace("#", ID_SEP_KEY)
219+
keys = id.split(ID_SEP_KEY)
217220
conf_ = self.get()
218221
if recursive:
219222
if conf_ is None:
@@ -458,6 +461,7 @@ def split_path_id(cls, src: str) -> tuple[str, str]:
458461
src: source string to split.
459462
460463
"""
464+
src = str(src).replace("#", ID_SEP_KEY)
461465
result = re.compile(rf"({cls.suffix_match}(?=(?:{ID_SEP_KEY}.*)|$))", re.IGNORECASE).findall(src)
462466
if not result:
463467
return "", src # the src is a pure id
@@ -488,6 +492,7 @@ def resolve_relative_ids(cls, id: str, value: str) -> str:
488492
489493
"""
490494
# get the prefixes like: "@####", "%###", "@#"
495+
value = str(value).replace("#", ID_SEP_KEY)
491496
prefixes = sorted(set().union(cls.relative_id_prefix.findall(value)), reverse=True)
492497
current_id = id.split(ID_SEP_KEY)
493498

monai/bundle/reference_resolver.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ def get_item(self, id: str, resolve: bool = False, **kwargs: Any) -> ConfigItem
101101
"""
102102
if resolve and id not in self.resolved_content:
103103
self._resolve_one_item(id=id, **kwargs)
104+
id = str(id).replace("#", self.sep)
104105
return self.items.get(id)
105106

106107
def _resolve_one_item(
@@ -121,6 +122,7 @@ def _resolve_one_item(
121122
if the `id` is not in the config content, must be a `ConfigItem` object.
122123
123124
"""
125+
id = str(id).replace("#", self.sep)
124126
if id in self.resolved_content:
125127
return self.resolved_content[id]
126128
try:
@@ -202,6 +204,7 @@ def match_refs_pattern(cls, value: str) -> dict[str, int]:
202204
"""
203205
refs: dict[str, int] = {}
204206
# regular expression pattern to match "@XXX" or "@XXX#YYY"
207+
value = str(value).replace("#", cls.sep)
205208
result = cls.id_matcher.findall(value)
206209
value_is_expr = ConfigExpression.is_expression(value)
207210
for item in result:
@@ -224,6 +227,7 @@ def update_refs_pattern(cls, value: str, refs: dict) -> str:
224227
225228
"""
226229
# regular expression pattern to match "@XXX" or "@XXX#YYY"
230+
value = str(value).replace("#", cls.sep)
227231
result = cls.id_matcher.findall(value)
228232
# reversely sort the matched references by length
229233
# and handle the longer first in case a reference item is substring of another longer item

monai/bundle/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
__all__ = ["ID_REF_KEY", "ID_SEP_KEY", "EXPR_KEY", "MACRO_KEY", "DEFAULT_MLFLOW_SETTINGS", "DEFAULT_EXP_MGMT_SETTINGS"]
2525

2626
ID_REF_KEY = "@" # start of a reference to a ConfigItem
27-
ID_SEP_KEY = "#" # separator for the ID of a ConfigItem
27+
ID_SEP_KEY = "::" # separator for the ID of a ConfigItem
2828
EXPR_KEY = "$" # start of a ConfigExpression
2929
MACRO_KEY = "%" # start of a macro of a config
3030

tests/test_reference_resolver.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def test_resolve(self, configs, expected_id, output_type):
7676
resolver = ReferenceResolver()
7777
# add items to resolver
7878
for k, v in configs.items():
79+
k = k.replace("#", "::")
7980
if ConfigComponent.is_instantiable(v):
8081
resolver.add_item(ConfigComponent(config=v, id=k, locator=locator))
8182
elif ConfigExpression.is_expression(v):

0 commit comments

Comments
 (0)