1
1
import requests
2
2
import json
3
-
3
+ from . version import __version__
4
4
5
5
def signup (un , email ):
6
6
''' Remote signup to plot.ly and plot.ly API
@@ -12,7 +12,7 @@ def signup(un, email):
12
12
:un: <string> username
13
13
:email: <string> email address
14
14
'''
15
- payload = {'version' : '0.5.3' , 'un' : un , 'email' : email , 'platform' :'Python' }
15
+ payload = {'version' : __version__ , 'un' : un , 'email' : email , 'platform' :'Python' }
16
16
r = requests .post ('https://plot.ly/apimkacct' , data = payload )
17
17
r = json .loads (r .text )
18
18
if 'error' in r .keys ():
@@ -139,56 +139,55 @@ def style(self, *args, **kwargs):
139
139
r = self .__makecall (args , un , key , origin , kwargs )
140
140
return r
141
141
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 )
183
183
184
184
def __makecall (self , args , un , key , origin , kwargs ):
185
- version = '0.5.3'
186
185
platform = 'Python'
187
186
188
187
args = json .dumps (args , cls = self .__plotlyJSONEncoder )
189
188
kwargs = json .dumps (kwargs , cls = self .__plotlyJSONEncoder )
190
189
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 }
192
191
r = requests .post (url , data = payload )
193
192
r = json .loads (r .text )
194
193
if 'error' in r .keys ():
0 commit comments