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

Allow models to be iterable. #396

Merged
merged 1 commit into from
May 21, 2020
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
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