Skip to content

Commit 1a1b52a

Browse files
committed
JSON serializing empty np array patch
1 parent 8569afd commit 1a1b52a

File tree

4 files changed

+48
-47
lines changed

4 files changed

+48
-47
lines changed

Diff for: plotly/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
from .version import __version__
12
from .plotly import signup
23
from .plotly import plotly

Diff for: plotly/plotly.py

+44-45
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import requests
22
import json
3-
3+
from .version import __version__
44

55
def signup(un, email):
66
''' Remote signup to plot.ly and plot.ly API
@@ -12,7 +12,7 @@ def signup(un, email):
1212
:un: <string> username
1313
:email: <string> email address
1414
'''
15-
payload = {'version': '0.5.3', 'un': un, 'email': email, 'platform':'Python'}
15+
payload = {'version': __version__, 'un': un, 'email': email, 'platform':'Python'}
1616
r = requests.post('https://plot.ly/apimkacct', data=payload)
1717
r = json.loads(r.text)
1818
if 'error' in r.keys():
@@ -139,56 +139,55 @@ def style(self, *args, **kwargs):
139139
r = self.__makecall(args, un, key, origin, kwargs)
140140
return r
141141

142-
class __plotlyJSONEncoder(json.JSONEncoder):
143-
def numpyJSONEncoder(self, obj):
144-
try:
145-
import numpy
146-
if type(obj).__module__.split('.')[0] == numpy.__name__:
147-
l = obj.tolist()
148-
d = self.datetimeJSONEncoder(l)
149-
return d if d else l
150-
except:
151-
pass
152-
return None
153-
def datetimeJSONEncoder(self, obj):
154-
# if datetime or iterable of datetimes, convert to a string that plotly understands
155-
import datetime
156-
try:
157-
if isinstance(obj,(datetime.datetime, datetime.date)):
158-
return obj.strftime('%Y-%m-%d %H:%M:%S')
159-
elif isinstance(obj[0],(datetime.datetime, datetime.date)):
160-
return [o.strftime('%Y-%m-%d %H:%M:%S') for o in obj]
161-
except:
162-
pass
163-
return None
164-
def pandasJSONEncoder(self, obj):
165-
try:
166-
import pandas
167-
if isinstance(obj, pandas.DataFrame):
168-
return obj.to_json()
169-
except:
170-
pass
171-
return None
172-
def default(self, obj):
173-
try:
174-
return json.dumps(obj)
175-
except TypeError as e:
176-
encoders = (self.datetimeJSONEncoder, self.numpyJSONEncoder, self.pandasJSONEncoder)
177-
for encoder in encoders:
178-
s = encoder(obj)
179-
if s:
180-
return s
181-
raise e
182-
return json.JSONEncoder.default(self,obj)
142+
class __plotlyJSONEncoder(json.JSONEncoder):
143+
def numpyJSONEncoder(self, obj):
144+
try:
145+
import numpy
146+
if type(obj).__module__.split('.')[0] == numpy.__name__:
147+
l = obj.tolist()
148+
d = self.datetimeJSONEncoder(l)
149+
return d if d is not None else l
150+
except:
151+
pass
152+
return None
153+
def datetimeJSONEncoder(self, obj):
154+
# if datetime or iterable of datetimes, convert to a string that plotly understands
155+
import datetime
156+
try:
157+
if isinstance(obj,(datetime.datetime, datetime.date)):
158+
return obj.strftime('%Y-%m-%d %H:%M:%S')
159+
elif isinstance(obj[0],(datetime.datetime, datetime.date)):
160+
return [o.strftime('%Y-%m-%d %H:%M:%S') for o in obj]
161+
except:
162+
pass
163+
return None
164+
def pandasJSONEncoder(self, obj):
165+
try:
166+
import pandas
167+
if isinstance(obj, pandas.DataFrame):
168+
return obj.to_json()
169+
except:
170+
pass
171+
return None
172+
def default(self, obj):
173+
try:
174+
return json.dumps(obj)
175+
except TypeError as e:
176+
encoders = (self.datetimeJSONEncoder, self.numpyJSONEncoder, self.pandasJSONEncoder)
177+
for encoder in encoders:
178+
s = encoder(obj)
179+
if s is not None:
180+
return s
181+
raise e
182+
return json.JSONEncoder.default(self,obj)
183183

184184
def __makecall(self, args, un, key, origin, kwargs):
185-
version = '0.5.3'
186185
platform = 'Python'
187186

188187
args = json.dumps(args, cls=self.__plotlyJSONEncoder)
189188
kwargs = json.dumps(kwargs, cls=self.__plotlyJSONEncoder)
190189
url = 'https://plot.ly/clientresp'
191-
payload = {'platform': platform, 'version': version, 'args': args, 'un': un, 'key': key, 'origin': origin, 'kwargs': kwargs}
190+
payload = {'platform': platform, 'version': __version__, 'args': args, 'un': un, 'key': key, 'origin': origin, 'kwargs': kwargs}
192191
r = requests.post(url, data=payload)
193192
r = json.loads(r.text)
194193
if 'error' in r.keys():

Diff for: plotly/version.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__version__ = '0.5.5'

Diff for: setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from setuptools import setup
2-
2+
exec(open('plotly/version.py').read())
33
def readme():
44
with open('README.txt') as f:
55
return f.read()
66

77
setup(name='plotly',
8-
version='0.5.3',
8+
version=__version__,
99
description='',
1010
url='https://plot.ly/api/python',
1111
author='Chris P',

0 commit comments

Comments
 (0)