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
+ """
2
23
3
24
4
25
import json as json_pkg
5
26
6
27
7
28
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
+ """
8
40
if isinstance (json , dict ):
9
41
return json
10
42
elif isinstance (json , basestring ):
@@ -16,13 +48,39 @@ def _json_dict(json):
16
48
17
49
18
50
class SparkData (object ):
19
- """Represents Spark JSON as native Python objects."""
51
+ """Model Spark JSON objects as native Python objects."""
20
52
21
53
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
+ """
22
62
super (SparkData , self ).__init__ ()
23
63
self ._json = _json_dict (json )
24
64
25
65
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
+ """
26
84
if item in self ._json .keys ():
27
85
item_data = self ._json [item ]
28
86
if isinstance (item_data , dict ):
@@ -35,11 +93,14 @@ def __getattr__(self, item):
35
93
raise AttributeError (error )
36
94
37
95
def __str__ (self ):
96
+ """Return a human-readable string representation of this object."""
38
97
class_str = self .__class__ .__name__
39
98
json_str = json_pkg .dumps (self ._json , indent = 2 )
40
99
return "%s:\n %s" % (class_str , json_str )
41
100
42
101
def __repr__ (self ):
102
+ """Return a string representing this object as valid Python expression.
103
+ """
43
104
class_str = self .__class__ .__name__
44
105
json_str = json_pkg .dumps (self ._json , ensure_ascii = False )
45
106
return "%s(%r)" % (class_str , json_str )
0 commit comments