Skip to content

Commit 379591f

Browse files
committed
Merge pull request #18 from dannyroberts/repr
add __repr__ to JsonObject
2 parents a74ec8f + b55fb16 commit 379591f

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

jsonobject/base.py

+14
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,17 @@ def __setattr__(self, name, value):
294294
if self.__save_dynamic_properties and name not in self._properties_by_attr:
295295
self[name] = value
296296
super(JsonObject, self).__setattr__(name, value)
297+
298+
def __repr__(self):
299+
name = self.__class__.__name__
300+
predefined_properties = self._properties_by_attr.keys()
301+
predefined_property_keys = set(self._properties_by_attr[p].name for p in predefined_properties)
302+
dynamic_properties = set(self.keys()) - predefined_property_keys
303+
properties = sorted(predefined_properties) + sorted(dynamic_properties)
304+
return u'{name}({keyword_args})'.format(
305+
name=name,
306+
keyword_args=', '.join('{key}={value!r}'.format(
307+
key=key,
308+
value=getattr(self, key))
309+
for key in properties),
310+
)

0 commit comments

Comments
 (0)