Skip to content

Commit 3bdd33c

Browse files
committed
Add docstrings for sparkdata module
Add docstrings for the sparkdata module.
1 parent c640e0d commit 3bdd33c

File tree

1 file changed

+63
-2
lines changed

1 file changed

+63
-2
lines changed

ciscosparkapi/sparkdata.py

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,42 @@
1-
"""SparkData base-class; represents Spark JSON as native Python objects."""
1+
"""SparkData base-class; models Spark JSON objects as native Python objects.
2+
3+
The SparkData class models any JSON object passed to it as a string or Python
4+
dictionary as a native Python object; providing attribute access access using
5+
native object.attribute syntax.
6+
7+
SparkData is intended to serve as a base-class, which provides inheritable
8+
functionality, for concrete sub-classes that model specific Cisco Spark data
9+
objects (rooms, messages, webhooks, etc.). The SparkData base-class provides
10+
attribute access to any additonal JSON attributes received from the Cisco Spark
11+
cloud, which haven't been implemented by the concrete sub-classes. This
12+
provides a measure of future-proofing when additional data attributes are added
13+
to objects by the Cisco Spark cloud.
14+
15+
Example:
16+
>>> json_obj = '{"created": "2012-06-15T20:36:48.914Z", "displayName": "Chris Lunsford (chrlunsf)", "id": "Y2lzY29zcGFyazovL3VzL1BFT1BMRS9mZjhlZTZmYi1hZmVmLTRhNGQtOTJiMS1kNmIyMTZiNTg5NDk", "avatar": "https://1efa7a94ed216783e352-c62266528714497a17239ececf39e9e2.ssl.cf1.rackcdn.com/V1~ba1ecf557a7e0b7cc3081998df965aad~cNFKqEjAQ5aQkyt_l1zsCQ==~1600", "emails": ["[email protected]"]}'
17+
>>> python_obj = SparkData(json_obj)
18+
>>> python_obj.displayName
19+
u'Chris Lunsford (chrlunsf)'
20+
>>> python_obj.created
21+
u'2012-06-15T20:36:48.914Z'
22+
"""
223

324

425
import json as json_pkg
526

627

728
def _json_dict(json):
29+
"""Given a JSON dictionary or string; return a dictionary.
30+
31+
Args:
32+
json(dict, unicode, str): Input JSON object.
33+
34+
Returns:
35+
A Python dictionary with the contents of the JSON object.
36+
37+
Raises:
38+
TypeError: If the input object is not a dictionary or string.
39+
"""
840
if isinstance(json, dict):
941
return json
1042
elif isinstance(json, basestring):
@@ -16,13 +48,39 @@ def _json_dict(json):
1648

1749

1850
class SparkData(object):
19-
"""Represents Spark JSON as native Python objects."""
51+
"""Model Spark JSON objects as native Python objects."""
2052

2153
def __init__(self, json):
54+
"""Create a new SparkData object from a JSON dictionary or string.
55+
56+
Args:
57+
json(dict, unicode, str): Input JSON object.
58+
59+
Raises:
60+
TypeError: If the input object is not a dictionary or string.
61+
"""
2262
super(SparkData, self).__init__()
2363
self._json = _json_dict(json)
2464

2565
def __getattr__(self, item):
66+
"""Provide native attribute access to the JSON object's attributes.
67+
68+
This method is called when attempting to access a object attribute that
69+
hasn't been defined for the object. For example trying to access
70+
object.attribute1 when attribute1 hasn't been defined.
71+
72+
SparkData.__getattr__() checks the original JSON object to see if the
73+
attribute exists, and if it does, it returns the attribute's value
74+
from the original JSON object. This provides native access to all of
75+
the JSON object's attributes.
76+
77+
Args:
78+
item(unicode, str): Name of the Attribute being accessed.
79+
80+
Raises:
81+
AttributeError: If the JSON object does not contain the attribute
82+
requested.
83+
"""
2684
if item in self._json.keys():
2785
item_data = self._json[item]
2886
if isinstance(item_data, dict):
@@ -35,11 +93,14 @@ def __getattr__(self, item):
3593
raise AttributeError(error)
3694

3795
def __str__(self):
96+
"""Return a human-readable string representation of this object."""
3897
class_str = self.__class__.__name__
3998
json_str = json_pkg.dumps(self._json, indent=2)
4099
return "%s:\n%s" % (class_str, json_str)
41100

42101
def __repr__(self):
102+
"""Return a string representing this object as valid Python expression.
103+
"""
43104
class_str = self.__class__.__name__
44105
json_str = json_pkg.dumps(self._json, ensure_ascii=False)
45106
return "%s(%r)" % (class_str, json_str)

0 commit comments

Comments
 (0)