Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions package/MDAnalysis/analysis/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UserDict allow to initlize the instance with passing a dictionary. This does not work anymore. I suggest you copy the four lines

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.

Copy link
Member Author

@lilyminium lilyminium May 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The 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__

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
Expand Down
73 changes: 69 additions & 4 deletions testsuite/MDAnalysisTests/analysis/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,24 @@ def test_get(self, results):
assert results.a == results["a"] == 1

def test_no_attr(self, results):
with pytest.raises(AttributeError):
msg = "'Results' object has no attribute 'c'"
with pytest.raises(AttributeError, match=msg):
results.c

def test_set_attr(self, results):
value = [1, 2, 3, 4]
results.c = value
assert results.c == results["c"] == value
assert results.c is results["c"] is value

def test_set_key(self, results):
value = [1, 2, 3, 4]
results["c"] = value
assert results.c == results["c"] == value
assert results.c is results["c"] is value

@pytest.mark.parametrize('key', dir(UserDict) + ["data"])
def test_existing_dict_attr(self, results, key):
msg = f"'{key}' is a protected dictionary attribute"
with pytest.raises(AttributeError, match=key):
with pytest.raises(AttributeError, match=msg):
results[key] = None

@pytest.mark.parametrize('key', dir(UserDict) + ["data"])
Expand All @@ -76,6 +77,70 @@ def test_weird_key(self, results, key):
with pytest.raises(ValueError, match=msg):
results[key] = None

def test_setattr_modify_item(self, results):
mylist = [1, 2]
mylist2 = [3, 4]
results.myattr = mylist
assert results.myattr is mylist
results["myattr"] = mylist2
assert results.myattr is mylist2
mylist2.pop(0)
assert len(results.myattr) == 1
assert results.myattr is mylist2

def test_setitem_modify_item(self, results):
mylist = [1, 2]
mylist2 = [3, 4]
results["myattr"] = mylist
assert results.myattr is mylist
results.myattr = mylist2
assert results.myattr is mylist2
mylist2.pop(0)
assert len(results["myattr"]) == 1
assert results["myattr"] is mylist2

def test_delattr(self, results):
assert hasattr(results, "a")
delattr(results, "a")
assert not hasattr(results, "a")

def test_missing_delattr(self, results):
assert not hasattr(results, "d")
msg = "'Results' object has no attribute 'd'"
with pytest.raises(AttributeError, match=msg):
delattr(results, "d")

def test_pop(self, results):
assert hasattr(results, "a")
results.pop("a")
assert not hasattr(results, "a")

def test_update(self, results):
assert not hasattr(results, "spudda")
results.update({"spudda": "fett"})
assert results.spudda == "fett"

def test_update_data_fail(self, results):
msg = f"'data' is a protected dictionary attribute"
with pytest.raises(AttributeError, match=msg):
results.update({"data": 0})

@pytest.mark.parametrize("args, kwargs, length", [
(({"darth": "tater"},), {}, 1),
([], {"darth": "tater"}, 1),
(({"darth": "tater"},), {"yam": "solo"}, 2),
(({"darth": "tater"},), {"darth": "vader"}, 1),
])
def test_initialize_arguments(self, args, kwargs, length):
results = base.Results(*args, **kwargs)
ref = dict(*args, **kwargs)
assert ref == results
assert len(results) == length

def test_different_instances(self, results):
new_results = base.Results(darth="tater")
assert new_results.data is not results.data


class FrameAnalysis(base.AnalysisBase):
"""Just grabs frame numbers of frames it goes over"""
Expand Down