Skip to content
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

lib.util.Namespace raises the expected exception #1169

Merged
merged 4 commits into from
Jan 19, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ Fixes
* Fixed selections using operators backwards ('prop 10 > mass') and sensitivity
about whitespace around these (PR #1156 Issue #1011 #1009)
* Fixed PSA analysis is now using AlignTraj
* The namespace used for auxiliary data now raises an AttributeError instead
Copy link
Member

Choose a reason for hiding this comment

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

Wasn't NameSpace new in 0.16? If so, we don't have to document any fixes to it, (because CHANGELOG is for release to relrease changes)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed. I always forget what gets introduced when.

of a KeyError when accessing data by attributes (Issue #1166)

Changes
* Started unifying the API of analysis classes (named internally
Expand Down
25 changes: 22 additions & 3 deletions package/MDAnalysis/lib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1335,25 +1335,44 @@ class Namespace(object):
"""Class to allow storing attributes in new namespace. """
def __getattr__(self, key):
# a.this causes a __getattr__ call for key = 'this'
return self.__dict__[key]
try:
return self.__dict__[key]
except KeyError:
raise AttributeError('"{}" is not known in the namespace.'
.format(key))

def __setattr__(self, key, value):
# a.this = 10 causes a __setattr__ call for key='this' value=10
self.__dict__[key] = value
try:
self.__dict__[key] = value
except KeyError:
raise AttributeError('"{}" is not known in the namespace.'
.format(key))

def __delattr__(self, key):
del self.__dict__[key]
try:
del self.__dict__[key]
except KeyError:
raise AttributeError('"{}" is not known in the namespace.'
.format(key))

def __eq__(self, other):
try:
# this'll allow us to compare if we're storing arrays
assert_equal(self.__dict__, other.__dict__)
except AssertionError:
return False
return True

def __str__(self):
return str(self.__dict__)

def __len__(self):
return len(self.__dict__)

def __getitem__(self, key):
return self.__dict__[key]

def __iter__(self):
for i in self.__dict__:
yield i
4 changes: 2 additions & 2 deletions testsuite/MDAnalysisTests/coordinates/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def test_add_same_auxname_raises_ValueError(self):
def test_remove_auxiliary(self):
self.reader.remove_auxiliary('lowf')
assert_raises(AttributeError, getattr, self.reader._auxs, 'lowf')
assert_raises(KeyError, getattr, self.reader.ts.aux, 'lowf')
assert_raises(AttributeError, getattr, self.reader.ts.aux, 'lowf')

@raises(ValueError)
def test_remove_nonexistant_auxiliary_raises_ValueError(self):
Expand Down Expand Up @@ -344,7 +344,7 @@ def test_rename_aux(self):
assert_equal(self.reader.ts.aux.lowf_renamed,
self.ref.aux_lowf_data[0])
# old name should be removed
assert_raises(KeyError, getattr, self.reader.ts.aux, 'lowf')
assert_raises(AttributeError, getattr, self.reader.ts.aux, 'lowf')
# new name should be retained
next(self.reader)
assert_equal(self.reader.ts.aux.lowf_renamed,
Expand Down