13
13
import re
14
14
import datetime
15
15
16
+ try :
17
+ import numpy
18
+ _numpy_imported = True
19
+ except ImportError :
20
+ _numpy_imported = False
21
+
22
+ try :
23
+ import pandas
24
+ _pandas_imported = True
25
+ except ImportError :
26
+ _pandas_imported = False
27
+
28
+ try :
29
+ from sage .all import RR , ZZ
30
+ _sage_imported = True
31
+ except ImportError :
32
+ _sage_imported = False
33
+
34
+
16
35
### incase people are using threading, we lock file reads
17
36
lock = threading .Lock ()
18
37
@@ -76,11 +95,10 @@ def ensure_dir_exists(directory):
76
95
class NotEncodable (Exception ):
77
96
pass
78
97
98
+
79
99
class _plotlyJSONEncoder (json .JSONEncoder ):
80
100
def numpyJSONEncoder (self , obj ):
81
- try :
82
- import numpy
83
- except :
101
+ if not _numpy_imported :
84
102
raise NotEncodable
85
103
86
104
if type (obj ).__module__ .split ('.' )[0 ] == numpy .__name__ :
@@ -101,13 +119,8 @@ def datetimeJSONEncoder(self, obj):
101
119
%Y-%m-%d
102
120
depending on what non-zero resolution was provided
103
121
"""
104
- try :
105
- import pandas
106
- pandas_importable = True
107
- except :
108
- pandas_importable = False
109
122
110
- if pandas_importable and obj is pandas .NaT :
123
+ if _pandas_imported and obj is pandas .NaT :
111
124
return None
112
125
113
126
if isinstance (obj , (datetime .datetime , datetime .date )):
@@ -127,9 +140,7 @@ def datetimeJSONEncoder(self, obj):
127
140
raise NotEncodable
128
141
129
142
def pandasJSONEncoder (self , obj ):
130
- try :
131
- import pandas
132
- except :
143
+ if not _pandas_imported :
133
144
raise NotEncodable
134
145
135
146
if isinstance (obj , pandas .Series ):
@@ -150,9 +161,7 @@ def pandasJSONEncoder(self, obj):
150
161
raise NotEncodable
151
162
152
163
def sageJSONEncoder (self , obj ):
153
- try :
154
- from sage .all import RR , ZZ
155
- except :
164
+ if not _sage_imported :
156
165
raise NotEncodable
157
166
158
167
if obj in RR :
0 commit comments