-
Notifications
You must be signed in to change notification settings - Fork 47
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
Methods like update() and pop() do not manage attributes correctly #3
Comments
Yes, indeed, I confirm :) Thanks for reporting ! This isn't too much work, if you want to give it a try, I would be pleased to review your pull-request ! |
@leplatrem Hi, I am happy to submit a PR to fix this issue. However, I would add two functions 'update' and 'pop' to override the builtins, and have to prevent 'update' and 'pop' to be collected in the attribute, like class EasyDict(dict):
def __init__(self, d=None, **kwargs):
if d is None:
d = {}
if kwargs:
d.update(**kwargs)
for k, v in d.items():
setattr(self, k, v)
# Class attributes
for k in self.__class__.__dict__.keys():
if not (k.startswith('__') and k.endswith('__')) and not k in ('update', 'pop'):
setattr(self, k, getattr(self, k))
def __setattr__(self, name, value):
if isinstance(value, (list, tuple)):
value = [self.__class__(x)
if isinstance(x, dict) else x for x in value]
elif isinstance(value, dict) and not isinstance(value, self.__class__):
value = self.__class__(value)
super(EasyDict, self).__setattr__(name, value)
super(EasyDict, self).__setitem__(name, value)
__setitem__ = __setattr__
def update(self, E=None, **F):
# Fix update error of easydict
d = E or dict()
d.update(F)
for k in d:
setattr(self, k, d[k])
def pop(self, k, d=None):
delattr(self, k)
return super(Config, self).pop(k, d) Does this ok for you? |
Thanks @LoSealL for your proposal. There are a couple of things to adjust in your proposition. It's a lot easier to use the pull requests feature to comment lines of changes. Also you will need to add/modify the tests to assert that your changes don't get broken by some other developers in the future :) |
@leplatrem Yep, made my PR |
Related to this, I'm having an issue with the pop function. When calling pop with a default value it raises an exception if the key isn't found in the dictionary. The following code reproduces the problem.
The issue seems related to delatr being called without checking for k's existance? |
|
Methods like update() and pop() do not have the results that I would expect. For example:
The text was updated successfully, but these errors were encountered: