Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5886831

Browse filesBrowse files
committed
Use requests.compat.json instead of plain json.
The `requests` package kindly manages 2/3 compat for json for us, might as well be consistent and use the same tool! As a side note, I’d like to move away from `six` and depend directly on `requests.compat`. I believe it has everything we need and then we can ditch the `six` dep and know that we’re always in sync with whatever requests is doing (which is really what we care about).
1 parent 4cd52bd commit 5886831
Copy full SHA for 5886831

File tree

Expand file treeCollapse file tree

15 files changed

+87
-79
lines changed
Filter options
Expand file treeCollapse file tree

15 files changed

+87
-79
lines changed

‎plotly/exceptions.py

Copy file name to clipboardExpand all lines: plotly/exceptions.py
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
A module that contains plotly's exception hierarchy.
66
77
"""
8-
import json
8+
from __future__ import absolute_import
9+
10+
from plotly.api.utils import to_native_utf8_string
911

1012

1113
# Base Plotly Error
@@ -21,7 +23,7 @@ class PlotlyRequestError(PlotlyError):
2123
"""General API error. Raised for *all* failed requests."""
2224

2325
def __init__(self, message, status_code, content):
24-
self.message = message
26+
self.message = to_native_utf8_string(message)
2527
self.status_code = status_code
2628
self.content = content
2729

‎plotly/graph_reference.py

Copy file name to clipboardExpand all lines: plotly/graph_reference.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
from __future__ import absolute_import
66

77
import hashlib
8-
import json
98
import os
109
import re
1110
from pkg_resources import resource_string
1211

1312
import six
13+
from requests.compat import json as _json
1414

1515
from plotly import exceptions, files, utils
1616
from plotly.api import v2
@@ -86,7 +86,7 @@ def get_graph_reference():
8686
if not graph_reference:
8787
path = os.path.join('graph_reference', 'default-schema.json')
8888
s = resource_string('plotly', path).decode('utf-8')
89-
graph_reference = json.loads(s)
89+
graph_reference = _json.loads(s)
9090
else:
9191
data = response.json()
9292
if data['modified']:

‎plotly/grid_objs/grid_objs.py

Copy file name to clipboardExpand all lines: plotly/grid_objs/grid_objs.py
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
"""
66
from __future__ import absolute_import
77

8-
import json
98
from collections import MutableSequence
109

10+
from requests.compat import json as _json
11+
1112
from plotly import exceptions, utils
1213

1314
__all__ = None
@@ -66,7 +67,7 @@ def __init__(self, data, name):
6667

6768
def __str__(self):
6869
max_chars = 10
69-
jdata = json.dumps(self.data, cls=utils.PlotlyJSONEncoder)
70+
jdata = _json.dumps(self.data, cls=utils.PlotlyJSONEncoder)
7071
if len(jdata) > max_chars:
7172
data_string = jdata[:max_chars] + "...]"
7273
else:

‎plotly/offline/offline.py

Copy file name to clipboardExpand all lines: plotly/offline/offline.py
+8-5Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
"""
66
from __future__ import absolute_import
77

8-
import json
98
import os
109
import uuid
1110
import warnings
1211
from pkg_resources import resource_string
1312
import time
1413
import webbrowser
1514

15+
from requests.compat import json as _json
16+
1617
import plotly
1718
from plotly import tools, utils
1819
from plotly.exceptions import PlotlyError
@@ -183,10 +184,12 @@ def _plot_html(figure_or_data, config, validate, default_width,
183184
height = str(height) + 'px'
184185

185186
plotdivid = uuid.uuid4()
186-
jdata = json.dumps(figure.get('data', []), cls=utils.PlotlyJSONEncoder)
187-
jlayout = json.dumps(figure.get('layout', {}), cls=utils.PlotlyJSONEncoder)
187+
jdata = _json.dumps(figure.get('data', []), cls=utils.PlotlyJSONEncoder)
188+
jlayout = _json.dumps(figure.get('layout', {}),
189+
cls=utils.PlotlyJSONEncoder)
188190
if 'frames' in figure_or_data:
189-
jframes = json.dumps(figure.get('frames', {}), cls=utils.PlotlyJSONEncoder)
191+
jframes = _json.dumps(figure.get('frames', {}),
192+
cls=utils.PlotlyJSONEncoder)
190193

191194
configkeys = (
192195
'editable',
@@ -211,7 +214,7 @@ def _plot_html(figure_or_data, config, validate, default_width,
211214
)
212215

213216
config_clean = dict((k, config[k]) for k in configkeys if k in config)
214-
jconfig = json.dumps(config_clean)
217+
jconfig = _json.dumps(config_clean)
215218

216219
# TODO: The get_config 'source of truth' should
217220
# really be somewhere other than plotly.plotly

‎plotly/plotly/plotly.py

Copy file name to clipboardExpand all lines: plotly/plotly/plotly.py
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
from __future__ import absolute_import
1818

1919
import copy
20-
import json
2120
import os
2221
import warnings
2322

2423
import six
2524
import six.moves
25+
from requests.compat import json as _json
2626

2727
from plotly import exceptions, tools, utils, files
2828
from plotly.api import v1, v2
@@ -643,7 +643,7 @@ def write(self, trace, layout=None, validate=True,
643643
stream_object.update(dict(layout=layout))
644644

645645
# TODO: allow string version of this?
646-
jdata = json.dumps(stream_object, cls=utils.PlotlyJSONEncoder)
646+
jdata = _json.dumps(stream_object, cls=utils.PlotlyJSONEncoder)
647647
jdata += "\n"
648648

649649
try:
@@ -1057,7 +1057,7 @@ def append_columns(cls, columns, grid=None, grid_url=None):
10571057

10581058
# This is sorta gross, we need to double-encode this.
10591059
body = {
1060-
'cols': json.dumps(columns, cls=utils.PlotlyJSONEncoder)
1060+
'cols': _json.dumps(columns, cls=utils.PlotlyJSONEncoder)
10611061
}
10621062
fid = grid_id
10631063
response = v2.grids.col_create(fid, body)

‎plotly/tests/test_core/test_get_requests/test_get_requests.py

Copy file name to clipboardExpand all lines: plotly/tests/test_core/test_get_requests/test_get_requests.py
+13-13Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
77
"""
88
import copy
9-
import json
10-
import requests
119

10+
import requests
1211
import six
1312
from nose.plugins.attrib import attr
13+
from requests.compat import json as _json
1414

1515

1616
default_headers = {'plotly-username': '',
@@ -37,9 +37,9 @@ def test_user_does_not_exist():
3737
resource = "/apigetfile/{0}/{1}/".format(file_owner, file_id)
3838
response = requests.get(server + resource, headers=hd)
3939
if six.PY3:
40-
content = json.loads(response.content.decode('unicode_escape'))
40+
content = _json.loads(response.content.decode('unicode_escape'))
4141
else:
42-
content = json.loads(response.content)
42+
content = _json.loads(response.content)
4343
print(response.status_code)
4444
print(content)
4545
assert response.status_code == 404
@@ -60,9 +60,9 @@ def test_file_does_not_exist():
6060
resource = "/apigetfile/{0}/{1}/".format(file_owner, file_id)
6161
response = requests.get(server + resource, headers=hd)
6262
if six.PY3:
63-
content = json.loads(response.content.decode('unicode_escape'))
63+
content = _json.loads(response.content.decode('unicode_escape'))
6464
else:
65-
content = json.loads(response.content)
65+
content = _json.loads(response.content)
6666
print(response.status_code)
6767
print(content)
6868
assert response.status_code == 404
@@ -100,9 +100,9 @@ def test_private_permission_defined():
100100
resource = "/apigetfile/{0}/{1}/".format(file_owner, file_id)
101101
response = requests.get(server + resource, headers=hd)
102102
if six.PY3:
103-
content = json.loads(response.content.decode('unicode_escape'))
103+
content = _json.loads(response.content.decode('unicode_escape'))
104104
else:
105-
content = json.loads(response.content)
105+
content = _json.loads(response.content)
106106
print(response.status_code)
107107
print(content)
108108
assert response.status_code == 403
@@ -122,9 +122,9 @@ def test_missing_headers():
122122
del hd[header]
123123
response = requests.get(server + resource, headers=hd)
124124
if six.PY3:
125-
content = json.loads(response.content.decode('unicode_escape'))
125+
content = _json.loads(response.content.decode('unicode_escape'))
126126
else:
127-
content = json.loads(response.content)
127+
content = _json.loads(response.content)
128128
print(response.status_code)
129129
print(content)
130130
assert response.status_code == 422
@@ -142,13 +142,13 @@ def test_valid_request():
142142
resource = "/apigetfile/{0}/{1}/".format(file_owner, file_id)
143143
response = requests.get(server + resource, headers=hd)
144144
if six.PY3:
145-
content = json.loads(response.content.decode('unicode_escape'))
145+
content = _json.loads(response.content.decode('unicode_escape'))
146146
else:
147-
content = json.loads(response.content)
147+
content = _json.loads(response.content)
148148
print(response.status_code)
149149
print(content)
150150
assert response.status_code == 200
151-
# content = json.loads(res.content)
151+
# content = _json.loads(res.content)
152152
# response_payload = content['payload']
153153
# figure = response_payload['figure']
154154
# if figure['data'][0]['x'] != [u'1', u'2', u'3']:

‎plotly/tests/test_core/test_graph_reference/test_graph_reference.py

Copy file name to clipboardExpand all lines: plotly/tests/test_core/test_graph_reference/test_graph_reference.py
+3-5Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44
"""
55
from __future__ import absolute_import
66

7-
import json
87
import os
98
from pkg_resources import resource_string
109
from unittest import TestCase
1110

12-
import requests
13-
import six
1411
from nose.plugins.attrib import attr
12+
from requests.compat import json as _json
1513

1614
from plotly import files, graph_reference as gr, tools, utils
1715
from plotly.api import v2
@@ -58,7 +56,7 @@ def test_get_graph_reference_bad_request_no_copy(self):
5856
self.set_graph_reference(empty_graph_reference)
5957
path = os.path.join('graph_reference', 'default-schema.json')
6058
s = resource_string('plotly', path).decode('utf-8')
61-
default_graph_reference = json.loads(s)
59+
default_graph_reference = _json.loads(s)
6260
graph_reference = gr.get_graph_reference()
6361
self.assertEqual(graph_reference, default_graph_reference)
6462

@@ -69,7 +67,7 @@ def test_default_schema_is_up_to_date(self):
6967

7068
path = os.path.join('graph_reference', 'default-schema.json')
7169
s = resource_string('plotly', path).decode('utf-8')
72-
default_schema = json.loads(s)
70+
default_schema = _json.loads(s)
7371

7472
msg = (
7573
'The default, hard-coded plot schema we ship with pip is out of '

‎plotly/tests/test_core/test_offline/test_offline.py

Copy file name to clipboardExpand all lines: plotly/tests/test_core/test_offline/test_offline.py
+7-5Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
"""
55
from __future__ import absolute_import
66

7-
from nose.tools import raises
87
from unittest import TestCase
9-
from plotly.tests.utils import PlotlyTestCase
10-
import json
8+
9+
from requests.compat import json as _json
1110

1211
import plotly
12+
from plotly.tests.utils import PlotlyTestCase
13+
1314

1415
fig = {
1516
'data': [
@@ -35,8 +36,9 @@ def _read_html(self, file_url):
3536
return f.read()
3637

3738
def test_default_plot_generates_expected_html(self):
38-
data_json = json.dumps(fig['data'], cls=plotly.utils.PlotlyJSONEncoder)
39-
layout_json = json.dumps(
39+
data_json = _json.dumps(fig['data'],
40+
cls=plotly.utils.PlotlyJSONEncoder)
41+
layout_json = _json.dumps(
4042
fig['layout'],
4143
cls=plotly.utils.PlotlyJSONEncoder)
4244

‎plotly/tests/test_core/test_plotly/test_plot.py

Copy file name to clipboardExpand all lines: plotly/tests/test_core/test_plotly/test_plot.py
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
"""
88
from __future__ import absolute_import
99

10-
import json
1110
import requests
1211
import six
12+
from requests.compat import json as _json
1313

1414
from unittest import TestCase
1515
from nose.plugins.attrib import attr
@@ -296,10 +296,10 @@ def generate_conflicting_plot_options_with_json_writes_of_config():
296296
"""
297297
def gen_test(plot_options):
298298
def test(self):
299-
config = json.load(open(CONFIG_FILE))
299+
config = _json.load(open(CONFIG_FILE))
300300
with open(CONFIG_FILE, 'w') as f:
301301
config.update(plot_options)
302-
f.write(json.dumps(config))
302+
f.write(_json.dumps(config))
303303
self.assertRaises(PlotlyError, py._plot_option_logic, {})
304304
return test
305305

‎plotly/tests/test_core/test_utils/test_utils.py

Copy file name to clipboardExpand all lines: plotly/tests/test_core/test_utils/test_utils.py
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
from __future__ import absolute_import
22

3-
import json
43
from unittest import TestCase
54

5+
from requests.compat import json as _json
6+
67
from plotly.utils import PlotlyJSONEncoder, get_by_path, node_generator
78

89

910
class TestJSONEncoder(TestCase):
1011

1112
def test_nan_to_null(self):
1213
array = [1, float('NaN'), float('Inf'), float('-Inf'), 'platypus']
13-
result = json.dumps(array, cls=PlotlyJSONEncoder)
14+
result = _json.dumps(array, cls=PlotlyJSONEncoder)
1415
expected_result = '[1, null, null, null, "platypus"]'
1516
self.assertEqual(result, expected_result)
1617

‎plotly/tests/test_optional/test_offline/test_offline.py

Copy file name to clipboardExpand all lines: plotly/tests/test_optional/test_offline/test_offline.py
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
from nose.tools import raises
88
from nose.plugins.attrib import attr
9+
from requests.compat import json as _json
910

1011
from unittest import TestCase
11-
import json
1212

1313
import plotly
1414

@@ -75,8 +75,8 @@ def test_default_mpl_plot_generates_expected_html(self):
7575
figure = plotly.tools.mpl_to_plotly(fig)
7676
data = figure['data']
7777
layout = figure['layout']
78-
data_json = json.dumps(data, cls=plotly.utils.PlotlyJSONEncoder)
79-
layout_json = json.dumps(layout, cls=plotly.utils.PlotlyJSONEncoder)
78+
data_json = _json.dumps(data, cls=plotly.utils.PlotlyJSONEncoder)
79+
layout_json = _json.dumps(layout, cls=plotly.utils.PlotlyJSONEncoder)
8080
html = self._read_html(plotly.offline.plot_mpl(fig))
8181

8282
# just make sure a few of the parts are in here

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.