-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
5804 Simplify the configparser usage to get parsed attributes easily #5813
5804 Simplify the configparser usage to get parsed attributes easily #5813
Conversation
…ect-MONAI#5804) Signed-off-by: Felix Schnabel <f.schnabel@tum.de>
…ibute (not nested). Signed-off-by: Felix Schnabel <f.schnabel@tum.de>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, it looks good to me.
One Idea was to change the class ConfigItem:
...
def get_config(self) -> Any:
"""
Get the config content of current config item.
If the content is a dict it will wrap it with `DictWrapper` to allow attribute like access.
"""
return self.config if not isinstance(self.config, dict) else DictWrapper(self.config)
class DictWrapper(UserDict):
"""
Wrapper class to support attribute-style access to the dict items.
"""
def __getattr__(self, key: str) -> Any:
if key in self.data:
item = self.data[key]
return item if not isinstance(item, dict) else DictWrapper(item)
raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{key}'") This implementation wraps each |
The problem with this implementation is, that nested content is not parsed/instantiated... |
I'll merge this PR for now... for the nested cases, perhaps we'll introduce a new class for |
Signed-off-by: Wenqi Li <wenqil@nvidia.com>
Thanks @wyli, I think it would be better if you continue since I'm not sure how this new class should look like and there are some design decisions to make, which should be in line with the rest of the project. If you have a general plan on how this should look like, I'm happy to implement it :) |
/build |
Signed-off-by: Felix Schnabel <f.schnabel@tum.de>
for more information, see https://pre-commit.ci
Signed-off-by: Wenqi Li <wenqil@nvidia.com>
/build |
part of #5804.
Description
This adds a simple attribute access for ConfigParser so
can be rewritten to
This only works for variables/methods that are not included in ConfigParser so e.g.
parser.config
would still get the whole config.This PR only supports shallow attribute accesses, since the returned value will be a
dict
where you need to use["key"]
to get the content.Types of changes
./runtests.sh --quick --unittests --disttests
.