Skip to content

Commit

Permalink
Allow models to be iterable.
Browse files Browse the repository at this point in the history
Signed-off-by: Lee Trager <lee.trager@canonical.com>
  • Loading branch information
ltrager committed May 21, 2020
1 parent d7b489e commit 4b8c3ae
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pylxd/models/_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ def __setattr__(self, name, value):
self.__dirty__.add(name)
return super(Model, self).__setattr__(name, value)

def __iter__(self):
for attr in self.__attributes__.keys():
yield attr, getattr(self, attr)

@property
def dirty(self):
return len(self.__dirty__) > 0
Expand Down
8 changes: 8 additions & 0 deletions pylxd/tests/models/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ def test_unset_attribute_sync(self):

self.assertEqual(1000, item.age)

def test_iter(self):
"""Test models can be iterated over."""
item = Item(self.client, name='an-item')

self.assertDictEqual(
{'name': 'an-item', 'age': 1000, 'data': {'key': 'val'}},
dict(item))

def test_sync(self):
"""A sync will update attributes from the server."""
item = Item(self.client, name='an-item')
Expand Down

0 comments on commit 4b8c3ae

Please sign in to comment.