-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add nested dot-notation access to ConfigParser (#6837) #8858
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -326,6 +326,56 @@ def test_get_via_attributes(self): | |
| result = trans(np.ones(64)) | ||
| self.assertTupleEqual(result.shape, (1, 8, 8)) | ||
|
|
||
| def test_nested_dot_notation(self): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be broken up into multiple tests focused on specific areas. Checking exception raises should be separate, for example. You can put |
||
| config = { | ||
| "A": {"B": {"C": 1, "D": [10, 20]}}, | ||
| "training": {"trainer": {"max_epochs": 100, "lr": 0.001}}, | ||
| "transforms": [{"keys": "image"}, {"keys": "label"}], | ||
| "my_dims": 2, | ||
| "dims_1": "$@my_dims + 1", | ||
| } | ||
| parser = ConfigParser(config=config, globals={"monai": "monai"}) | ||
|
|
||
| self.assertEqual(parser.A.B.C, 1) | ||
| self.assertEqual(parser.training.trainer.max_epochs, 100) | ||
| self.assertEqual(parser.training.trainer.lr, 0.001) | ||
| self.assertEqual(parser.dims_1, 3) | ||
|
|
||
| self.assertEqual(parser.A.B.D[0], 10) | ||
| self.assertEqual(parser.A.B.D[1], 20) | ||
|
|
||
| self.assertEqual(parser.transforms[0].keys, "image") | ||
| self.assertEqual(parser.transforms[1].keys, "label") | ||
|
|
||
| self.assertEqual(parser.A._raw, {"B": {"C": 1, "D": [10, 20]}}) | ||
|
|
||
| # container protocol delegates to the underlying dict/list | ||
| self.assertEqual(len(parser.A.B.D), 2) | ||
| self.assertEqual(list(parser.A.B.D), [10, 20]) | ||
| self.assertIn("B", parser.A) | ||
| self.assertTrue(parser.A.B.D) | ||
| self.assertFalse(ConfigParser(config={"e": []}, globals={"monai": "monai"}).e) | ||
|
|
||
| # bracket access falls back to native container semantics when there is no | ||
| # config key of that name: negative indexing, IndexError, and dict KeyError. | ||
| self.assertEqual(parser.A.B.D[-1], 20) | ||
| with self.assertRaises(IndexError): | ||
| _ = parser.A.B.D[5] | ||
| with self.assertRaises(KeyError): | ||
| _ = parser.A.B["nonexistent"] | ||
|
|
||
| with self.assertRaises(AttributeError): | ||
| _ = parser.A.nonexistent | ||
|
|
||
| # item assignment/deletion writes through to the underlying container, | ||
| # preserving the pre-proxy behaviour where ``parser.x`` was the raw dict/list. | ||
| parser.A.B["C"] = 99 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
| self.assertEqual(parser.A.B._raw["C"], 99) | ||
| parser.A.B.D[0] = 11 | ||
| self.assertEqual(parser.A.B.D._raw, [11, 20]) | ||
| del parser.A.B["C"] | ||
| self.assertNotIn("C", parser.A.B._raw) | ||
|
|
||
| def test_builtin(self): | ||
| config = {"import statements": "$import math", "calc": {"_target_": "math.isclose", "a": 0.001, "b": 0.001}} | ||
| self.assertEqual(ConfigParser(config).calc, True) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.