-
Notifications
You must be signed in to change notification settings - Fork 825
alias __setattr__ to __setitem__ in Results #3281
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
Changes from all commits
26f6fee
44803eb
fd70a31
867f37b
beb6e27
54c69f3
5ed868a
dc7c0f1
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 |
|---|---|---|
|
|
@@ -84,38 +84,37 @@ class in `scikit-learn`_. | |
|
|
||
| .. versionadded:: 2.0.0 | ||
| """ | ||
|
|
||
| def _validate_key(self, key): | ||
| if key in dir(UserDict) or (key == "data" and self._dict_frozen): | ||
| if key in dir(self): | ||
| raise AttributeError(f"'{key}' is a protected dictionary " | ||
| "attribute") | ||
| elif isinstance(key, str) and not key.isidentifier(): | ||
| raise ValueError(f"'{key}' is not a valid attribute") | ||
|
|
||
| def __init__(self, **kwargs): | ||
| def __init__(self, *args, **kwargs): | ||
| kwargs = dict(*args, **kwargs) | ||
| if "data" in kwargs.keys(): | ||
| raise AttributeError(f"'data' is a protected dictionary attribute") | ||
|
|
||
| self._dict_frozen = False | ||
| for key in kwargs: | ||
| self._validate_key(key) | ||
| super().__init__(**kwargs) | ||
| self._dict_frozen = True | ||
| self.__dict__["data"] = {} | ||
| self.update(kwargs) | ||
|
Comment on lines
-101
to
+100
Contributor
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.
if dict is not None:
self.update(dict)
if kwargs:
self.update(kwargs)from cpython, adjust the arguments, add a test and everything is fine.
Member
Author
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. Are you happy with my somewhat lazier solution? :)
Contributor
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. Totally! |
||
|
|
||
| def __setitem__(self, key, item): | ||
| self._validate_key(key) | ||
| super().__setitem__(key, item) | ||
|
|
||
| def __setattr__(self, attr, value): | ||
| self._validate_key(attr) | ||
| super().__setattr__(attr, value) | ||
|
|
||
| # attribute available as key | ||
| if self._dict_frozen and attr != "_dict_frozen": | ||
| super().__setitem__(attr, value) | ||
| __setattr__ = __setitem__ | ||
lilyminium marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def __getattr__(self, attr): | ||
| try: | ||
| return self.data[attr] | ||
| return self[attr] | ||
| except KeyError as err: | ||
| raise AttributeError("'Results' object has no " | ||
| f"attribute '{attr}'") from err | ||
|
|
||
| def __delattr__(self, attr): | ||
| try: | ||
| del self[attr] | ||
| except KeyError as err: | ||
| raise AttributeError("'Results' object has no " | ||
| f"attribute '{attr}'") from err | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.