Skip to content

Commit 632c444

Browse files
committed
change scope sep
Signed-off-by: reiase <[email protected]>
1 parent 07102f2 commit 632c444

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

hyperparameter/hyperparameter.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ def __init__(self, root, path=None, scope=None):
7777
def get_or_else(self, default: Any = None):
7878
"""Get value for the parameter, or get default value if the parameter is not defined."""
7979
if self._scope is not None:
80-
suffixes = self._scope.replace(".", "#").split("#")
80+
suffixes = self._scope.split(".")
8181
_tracker.read(f"{self._path}@{self._scope}")
8282
while suffixes:
83-
suffix = "#".join(suffixes)
83+
suffix = ".".join(suffixes)
8484
full_name = f"{self._path}@{suffix}"
8585
value = self._root.get(full_name)
8686
if not isinstance(value, _Accessor):
@@ -107,9 +107,7 @@ def __setattr__(self, name: str, value: Any):
107107
return self.__setitem__(name, value)
108108
full_name = f"{self._path}.{name}" if self._path is not None else name
109109
full_name = (
110-
f"{full_name}@{self._scope.replace('.', '#')}"
111-
if self._scope is not None
112-
else full_name
110+
f"{full_name}@{self._scope}" if self._scope is not None else full_name
113111
)
114112
_tracker.write(full_name)
115113
self._root.put(full_name, value)
@@ -256,13 +254,19 @@ def put(self, name: str, value: Any) -> None:
256254
{'param1': 1, 'obj1': {'propA': 'A'}}
257255
"""
258256

259-
path = name.split(".")
257+
if "@" in name:
258+
name, scope = name.split("@", 1)
259+
path = name.split(".")
260+
scope = f"@{scope}"
261+
else:
262+
path = name.split(".")
263+
scope = ""
260264
obj = self
261265
for p in path[:-1]:
262266
if p not in obj or (not isinstance(obj[p], dict)):
263267
obj[p] = HyperParameter()
264268
obj = obj[p]
265-
obj[path[-1]] = value
269+
obj[path[-1] + scope] = value
266270

267271
def get(self, name: str) -> Any:
268272
"""get the parameter with the given name
@@ -286,13 +290,20 @@ def get(self, name: str) -> Any:
286290
2
287291
"""
288292

289-
path = name.split(".")
293+
if "@" in name:
294+
name, scope = name.split("@", 1)
295+
path = name.split(".")
296+
scope = f"@{scope}"
297+
else:
298+
path = name.split(".")
299+
scope = ""
290300
obj = self
291301
for p in path[:-1]:
292302
if p not in obj:
293303
return _Accessor(obj, p)
294304
obj = obj[p]
295-
return obj[path[-1]] if path[-1] in obj else _Accessor(self, name)
305+
name_with_scope = path[-1] + scope
306+
return obj[name_with_scope] if name_with_scope in obj else _Accessor(self, name)
296307

297308
def __setitem__(self, key: str, value: Any) -> None:
298309
"""set value and convert the value into `HyperParameter` if necessary
@@ -436,10 +447,10 @@ def __call__(self, scope=None) -> Any:
436447
... test()
437448
1 1 c None
438449
439-
>>> with param_scope(**{"myns.foo.params.b@sec1#sec2": 3}) as ps:
450+
>>> with param_scope(**{"myns.foo.params.b@sec1.sec2": 3}) as ps:
440451
... print(f"ps = {ps}")
441452
... test()
442-
ps = {'myns': {'foo': {'params': {'b@sec1#sec2': 3}}}}
453+
ps = {'myns': {'foo': {'params': {'b@sec1.sec2': 3}}}}
443454
1 3 c None
444455
"""
445456
scope = dict.get(self, "_scope", None) if scope is None else scope
@@ -468,7 +479,7 @@ def __call__(self, *args, **kwargs):
468479
retval = _param_scope(*args, **kwargs)
469480
if self._index is not None:
470481
if dict.get(retval, "_scope", None) is not None:
471-
retval._scope = f"{retval._scope}#{self._index}"
482+
retval._scope = f"{retval._scope}.{self._index}"
472483
else:
473484
retval._scope = self._index
474485
return retval

0 commit comments

Comments
 (0)