Skip to content

Commit efa3d2c

Browse files
committed
Add __repr__ for box objects.
Box objects will now be repr'd including the object type and either the object's name or its Box id. Also, add support for dir() and vars() being called on Box objects. Those builtin functions will now include API response fields as attributes of the object.
1 parent abe87b8 commit efa3d2c

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

HISTORY.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Release History
66
Upcoming
77
++++++++
88

9+
- Box objects have an improved ``__repr__``, making them easier to identify during debugging sessions.
10+
- Box objects now implement ``__dir__``, making them easier to explore. When created with a Box API response,
11+
these objects will now include the API response fields as attributes.
12+
913

1014
1.5.0 (2016-03-02)
1115
++++++++++++++++++

boxsdk/object/base_object.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,17 @@ def __init__(self, session, object_id, response_object=None):
4949
super(BaseObject, self).__init__(session)
5050
self._object_id = object_id
5151
self._response_object = response_object or {}
52-
53-
def __getattr__(self, item):
54-
"""Base class override. Try to get the attribute from the API response object."""
55-
return self._response_object[item]
52+
self.__dict__.update(self._response_object)
5653

5754
def __getitem__(self, item):
5855
"""Base class override. Try to get the attribute from the API response object."""
5956
return self._response_object[item]
6057

58+
def __repr__(self):
59+
"""Base class override. Return a human-readable representation using the Box ID or name of the object."""
60+
description = self.name if 'name' in self._response_object else self.object_id
61+
return '<Box {0} ({1})>'.format(self.__class__.__name__, description)
62+
6163
def get_url(self, *args):
6264
"""
6365
Base class override.

0 commit comments

Comments
 (0)