Skip to content

Commit 9bd3755

Browse files
committed
Move default function up. It should be seen 1st.
1 parent 0bd4dc3 commit 9bd3755

File tree

1 file changed

+49
-48
lines changed

1 file changed

+49
-48
lines changed

plotly/utils.py

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,55 @@ class PlotlyJSONEncoder(json.JSONEncoder):
112112
See PlotlyJSONEncoder.default for more implementation information.
113113
114114
"""
115+
116+
def default(self, obj):
117+
"""
118+
Accept an object (of unknown type) and try to encode with priority:
119+
1. builtin: user-defined objects
120+
2. sage: sage math cloud
121+
3. pandas: dataframes/series
122+
4. numpy: ndarrays
123+
5. datetime: time/datetime objects
124+
125+
Each method throws a NotEncoded exception if it fails.
126+
127+
The default method will only get hit if the object is not a type that
128+
is naturally encoded by json:
129+
130+
Normal objects:
131+
dict object
132+
list, tuple array
133+
str, unicode string
134+
int, long, float number
135+
True true
136+
False false
137+
None null
138+
139+
Extended objects:
140+
float('nan') 'NaN'
141+
float('infinity') 'Infinity'
142+
float('-infinity') '-Infinity'
143+
144+
Therefore, we only anticipate either unknown iterables or values here.
145+
146+
"""
147+
# TODO: The ordering if these methods is *very* important. Is this OK?
148+
encoding_methods = (
149+
self.encode_as_plotly,
150+
self.encode_as_sage,
151+
self.encode_as_numpy,
152+
self.encode_as_pandas,
153+
self.encode_as_datetime,
154+
self.encode_as_date,
155+
self.encode_as_list # because some values have `tolist` do last.
156+
)
157+
for encoding_method in encoding_methods:
158+
try:
159+
return encoding_method(obj)
160+
except NotEncodable:
161+
pass
162+
return json.JSONEncoder.default(self, obj)
163+
115164
@staticmethod
116165
def encode_as_plotly(obj):
117166
"""Attempt to use a builtin `ploty_to_json` method."""
@@ -185,54 +234,6 @@ def encode_as_date(obj):
185234
else:
186235
return iso_to_plotly_time_string(time_string)
187236

188-
def default(self, obj):
189-
"""
190-
Accept an object (of unknown type) and try to encode with priority:
191-
1. builtin: user-defined objects
192-
2. sage: sage math cloud
193-
3. pandas: dataframes/series
194-
4. numpy: ndarrays
195-
5. datetime: time/datetime objects
196-
197-
Each method throws a NotEncoded exception if it fails.
198-
199-
The default method will only get hit if the object is not a type that
200-
is naturally encoded by json:
201-
202-
Normal objects:
203-
dict object
204-
list, tuple array
205-
str, unicode string
206-
int, long, float number
207-
True true
208-
False false
209-
None null
210-
211-
Extended objects:
212-
float('nan') 'NaN'
213-
float('infinity') 'Infinity'
214-
float('-infinity') '-Infinity'
215-
216-
Therefore, we only anticipate either unknown iterables or values here.
217-
218-
"""
219-
# TODO: The ordering if these methods is *very* important. Is this OK?
220-
encoding_methods = (
221-
self.encode_as_plotly,
222-
self.encode_as_sage,
223-
self.encode_as_numpy,
224-
self.encode_as_pandas,
225-
self.encode_as_datetime,
226-
self.encode_as_date,
227-
self.encode_as_list # because some values have `tolist` do last.
228-
)
229-
for encoding_method in encoding_methods:
230-
try:
231-
return encoding_method(obj)
232-
except NotEncodable:
233-
pass
234-
return json.JSONEncoder.default(self, obj)
235-
236237

237238
### unicode stuff ###
238239
def decode_unicode(coll):

0 commit comments

Comments
 (0)