forked from omry/omegaconf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new resolvers
oc.dict.keys
and oc.dict.values
Fixes omry#643
- Loading branch information
1 parent
6b634a3
commit 77b5f3c
Showing
5 changed files
with
132 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
New resolvers `oc.dict.keys` and `oc.dict.values` allow extracting the lists of keys and values of a DictConfig | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
from typing import Any | ||
|
||
from pytest import mark, param | ||
|
||
from omegaconf import OmegaConf | ||
|
||
|
||
@mark.parametrize( | ||
("cfg", "key", "expected"), | ||
[ | ||
param( | ||
{"foo": "${oc.dict.keys:{a: 0, b: 1}}"}, | ||
"foo", | ||
OmegaConf.create(["a", "b"]), | ||
id="dict", | ||
), | ||
param( | ||
{"foo": "${oc.dict.keys:${bar}}", "bar": {"a": 0, "b": 1}}, | ||
"foo", | ||
OmegaConf.create(["a", "b"]), | ||
id="dictconfig", | ||
), | ||
param( | ||
{"foo": "${sum:${oc.dict.keys:{1: one, 2: two}}}"}, | ||
"foo", | ||
3, | ||
id="nested", | ||
), | ||
], | ||
) | ||
def test_dict_keys(restore_resolvers: Any, cfg: Any, key: Any, expected: Any) -> None: | ||
OmegaConf.register_new_resolver("sum", lambda x: sum(x)) | ||
|
||
cfg = OmegaConf.create(cfg) | ||
val = cfg[key] | ||
assert val == expected | ||
assert type(val) is type(expected) | ||
|
||
|
||
@mark.parametrize( | ||
("cfg", "key", "expected"), | ||
[ | ||
param( | ||
{"foo": "${oc.dict.values:{a: 0, b: 1}}"}, | ||
"foo", | ||
OmegaConf.create([0, 1]), | ||
id="dict", | ||
), | ||
param( | ||
{"foo": "${oc.dict.values:${bar}}", "bar": {"a": 0, "b": 1}}, | ||
"foo", | ||
OmegaConf.create([0, 1]), | ||
id="dictconfig", | ||
), | ||
param( | ||
{"foo": "${sum:${oc.dict.values:{one: 1, two: 2}}}"}, | ||
"foo", | ||
3, | ||
id="nested", | ||
), | ||
param( | ||
{ | ||
"foo": "${oc.dict.values:${bar}}", | ||
"bar": {"x": {"x0": 0, "x1": 1}, "y": {"y0": 0}}, | ||
}, | ||
"foo", | ||
OmegaConf.create([{"x0": 0, "x1": 1}, {"y0": 0}]), | ||
id="convert_node_to_list", | ||
), | ||
param( | ||
{ | ||
"foo": "${oc.dict.values:{key: ${val_ref}}}", | ||
"val_ref": "value", | ||
}, | ||
"foo", | ||
OmegaConf.create(["value"]), | ||
id="dict_with_interpolated_value", | ||
), | ||
], | ||
) | ||
def test_dict_values(restore_resolvers: Any, cfg: Any, key: Any, expected: Any) -> None: | ||
OmegaConf.register_new_resolver("sum", lambda x: sum(x)) | ||
|
||
cfg = OmegaConf.create(cfg) | ||
val = cfg[key] | ||
assert val == expected | ||
assert type(val) is type(expected) |