Skip to content

Commit

Permalink
Merge pull request #15 from dls-controls/getattr-magic
Browse files Browse the repository at this point in the history
Added getattr magic to Response
  • Loading branch information
coretl committed Jun 3, 2016
2 parents 488a1c6 + 3fd61cd commit 91d591f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions malcolm/core/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ def to_dict(self):
serialized[field] = value
return serialized

def __getattr__(self, attr):
return self.fields[attr]

@classmethod
def Return(cls, id_, context, value=None):
"""Create a Return response object with the provided parameters.
Expand Down
5 changes: 5 additions & 0 deletions tests/test_core/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,10 @@ def test_return_response_to_dict(self):
expected["value"] = None
self.assertEquals(expected, r.to_dict())

def test_getattr(self):
r = Response.Return(22, None, "bar")
self.assertEquals(r.value, "bar")
self.assertRaises(KeyError, lambda: r.ffff)

if __name__ == "__main__":
unittest.main(verbosity=2)

0 comments on commit 91d591f

Please sign in to comment.