[lxc-devel] [pylxd/master] Add dict method which includes readonly attributes to the model.
pcdummy on Github
lxc-bot at linuxcontainers.org
Mon Jul 25 13:01:26 UTC 2016
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 466 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160725/cbd2ce65/attachment.bin>
-------------- next part --------------
From 06c54c272b87de259895f952057fae73b70d487b Mon Sep 17 00:00:00 2001
From: Rene Jochum <rene at jochums.at>
Date: Mon, 25 Jul 2016 14:57:34 +0200
Subject: [PATCH] Add dict method which includes readonly attributes to the
model.
Signed-off-by: Rene Jochum <rene at jochums.at>
---
pylxd/model.py | 8 ++++++++
pylxd/tests/test_model.py | 11 +++++++++++
2 files changed, 19 insertions(+)
diff --git a/pylxd/model.py b/pylxd/model.py
index 9385afe..aaf1480 100644
--- a/pylxd/model.py
+++ b/pylxd/model.py
@@ -182,3 +182,11 @@ def marshall(self):
if not val.readonly:
marshalled[key] = getattr(self, key)
return marshalled
+
+ def dict(self):
+ """ Same as marshall but includes readonly attributes."""
+ marshalled = {}
+ for key, val in self.__attributes__.items():
+ if hasattr(self, key):
+ marshalled[key] = getattr(self, key)
+ return marshalled
diff --git a/pylxd/tests/test_model.py b/pylxd/tests/test_model.py
index a76b1fa..b8974e9 100644
--- a/pylxd/tests/test_model.py
+++ b/pylxd/tests/test_model.py
@@ -159,6 +159,17 @@ def test_marshall(self):
self.assertEqual({'age': 15, 'data': {'key': 'val'}}, result)
+ def test_dict(self):
+ """ The object marshalled as dict with readonly attributes."""
+ item = Item(self.client, name='an-item', age=15, data={'key': 'val'})
+
+ result = item.dict()
+
+ self.assertEqual(
+ {'name': 'an-item', 'age': 15, 'data': {'key': 'val'}},
+ result
+ )
+
def test_delete(self):
"""The object is deleted, and client is unset."""
item = Item(self.client, name='an-item', age=15, data={'key': 'val'})
More information about the lxc-devel
mailing list