Skip to content

Commit

Permalink
Use requests.compat.json instead of plain json.
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
theengineear committed Jan 18, 2017
1 parent 4304f0e commit 6634971
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 74 deletions.
6 changes: 4 additions & 2 deletions plotly/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
A module that contains plotly's exception hierarchy.
"""
import json
from __future__ import absolute_import

from plotly.api.utils import to_native_utf8_string


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

def __init__(self, message, status_code, content):
self.message = message
self.message = to_native_utf8_string(message)
self.status_code = status_code
self.content = content

Expand Down
5 changes: 3 additions & 2 deletions plotly/grid_objs/grid_objs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
"""
from __future__ import absolute_import

import json
from collections import MutableSequence

from requests.compat import json as _json

from plotly import exceptions, utils

__all__ = None
Expand Down Expand Up @@ -66,7 +67,7 @@ def __init__(self, data, name):

def __str__(self):
max_chars = 10
jdata = json.dumps(self.data, cls=utils.PlotlyJSONEncoder)
jdata = _json.dumps(self.data, cls=utils.PlotlyJSONEncoder)
if len(jdata) > max_chars:
data_string = jdata[:max_chars] + "...]"
else:
Expand Down
13 changes: 8 additions & 5 deletions plotly/offline/offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
"""
from __future__ import absolute_import

import json
import os
import uuid
import warnings
from pkg_resources import resource_string
import time
import webbrowser

from requests.compat import json as _json

import plotly
from plotly import tools, utils
from plotly.exceptions import PlotlyError
Expand Down Expand Up @@ -183,10 +184,12 @@ def _plot_html(figure_or_data, config, validate, default_width,
height = str(height) + 'px'

plotdivid = uuid.uuid4()
jdata = json.dumps(figure.get('data', []), cls=utils.PlotlyJSONEncoder)
jlayout = json.dumps(figure.get('layout', {}), cls=utils.PlotlyJSONEncoder)
jdata = _json.dumps(figure.get('data', []), cls=utils.PlotlyJSONEncoder)
jlayout = _json.dumps(figure.get('layout', {}),
cls=utils.PlotlyJSONEncoder)
if 'frames' in figure_or_data:
jframes = json.dumps(figure.get('frames', {}), cls=utils.PlotlyJSONEncoder)
jframes = _json.dumps(figure.get('frames', {}),
cls=utils.PlotlyJSONEncoder)

configkeys = (
'editable',
Expand All @@ -211,7 +214,7 @@ def _plot_html(figure_or_data, config, validate, default_width,
)

config_clean = dict((k, config[k]) for k in configkeys if k in config)
jconfig = json.dumps(config_clean)
jconfig = _json.dumps(config_clean)

# TODO: The get_config 'source of truth' should
# really be somewhere other than plotly.plotly
Expand Down
6 changes: 3 additions & 3 deletions plotly/plotly/plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
from __future__ import absolute_import

import copy
import json
import os
import warnings

import six
import six.moves
from requests.compat import json as _json

from plotly import exceptions, tools, utils, files
from plotly.api import v1, v2
Expand Down Expand Up @@ -642,7 +642,7 @@ def write(self, trace, layout=None, validate=True,
stream_object.update(dict(layout=layout))

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

try:
Expand Down Expand Up @@ -1056,7 +1056,7 @@ def append_columns(cls, columns, grid=None, grid_url=None):

# This is sorta gross, we need to double-encode this.
body = {
'cols': json.dumps(columns, cls=utils.PlotlyJSONEncoder)
'cols': _json.dumps(columns, cls=utils.PlotlyJSONEncoder)
}
fid = grid_id
response = v2.grids.col_create(fid, body)
Expand Down
26 changes: 13 additions & 13 deletions plotly/tests/test_core/test_get_requests/test_get_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
"""
import copy
import json
import requests

import requests
import six
from nose.plugins.attrib import attr
from requests.compat import json as _json


default_headers = {'plotly-username': '',
Expand All @@ -37,9 +37,9 @@ def test_user_does_not_exist():
resource = "/apigetfile/{0}/{1}/".format(file_owner, file_id)
response = requests.get(server + resource, headers=hd)
if six.PY3:
content = json.loads(response.content.decode('unicode_escape'))
content = _json.loads(response.content.decode('unicode_escape'))
else:
content = json.loads(response.content)
content = _json.loads(response.content)
print(response.status_code)
print(content)
assert response.status_code == 404
Expand All @@ -60,9 +60,9 @@ def test_file_does_not_exist():
resource = "/apigetfile/{0}/{1}/".format(file_owner, file_id)
response = requests.get(server + resource, headers=hd)
if six.PY3:
content = json.loads(response.content.decode('unicode_escape'))
content = _json.loads(response.content.decode('unicode_escape'))
else:
content = json.loads(response.content)
content = _json.loads(response.content)
print(response.status_code)
print(content)
assert response.status_code == 404
Expand Down Expand Up @@ -100,9 +100,9 @@ def test_private_permission_defined():
resource = "/apigetfile/{0}/{1}/".format(file_owner, file_id)
response = requests.get(server + resource, headers=hd)
if six.PY3:
content = json.loads(response.content.decode('unicode_escape'))
content = _json.loads(response.content.decode('unicode_escape'))
else:
content = json.loads(response.content)
content = _json.loads(response.content)
print(response.status_code)
print(content)
assert response.status_code == 403
Expand All @@ -122,9 +122,9 @@ def test_missing_headers():
del hd[header]
response = requests.get(server + resource, headers=hd)
if six.PY3:
content = json.loads(response.content.decode('unicode_escape'))
content = _json.loads(response.content.decode('unicode_escape'))
else:
content = json.loads(response.content)
content = _json.loads(response.content)
print(response.status_code)
print(content)
assert response.status_code == 422
Expand All @@ -142,13 +142,13 @@ def test_valid_request():
resource = "/apigetfile/{0}/{1}/".format(file_owner, file_id)
response = requests.get(server + resource, headers=hd)
if six.PY3:
content = json.loads(response.content.decode('unicode_escape'))
content = _json.loads(response.content.decode('unicode_escape'))
else:
content = json.loads(response.content)
content = _json.loads(response.content)
print(response.status_code)
print(content)
assert response.status_code == 200
# content = json.loads(res.content)
# content = _json.loads(res.content)
# response_payload = content['payload']
# figure = response_payload['figure']
# if figure['data'][0]['x'] != [u'1', u'2', u'3']:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"""
from __future__ import absolute_import

import json
import os
from pkg_resources import resource_string
from unittest import TestCase

from nose.plugins.attrib import attr
from requests.compat import json as _json

from plotly import graph_reference as gr
from plotly.api import v2
Expand All @@ -28,7 +28,7 @@ def test_default_schema_is_up_to_date(self):

path = os.path.join('package_data', 'default-schema.json')
s = resource_string('plotly', path).decode('utf-8')
default_schema = json.loads(s)
default_schema = _json.loads(s)

msg = (
'The default, hard-coded plot schema we ship with pip is out of '
Expand Down
12 changes: 7 additions & 5 deletions plotly/tests/test_core/test_offline/test_offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
"""
from __future__ import absolute_import

from nose.tools import raises
from unittest import TestCase
from plotly.tests.utils import PlotlyTestCase
import json

from requests.compat import json as _json

import plotly
from plotly.tests.utils import PlotlyTestCase


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

def test_default_plot_generates_expected_html(self):
data_json = json.dumps(fig['data'], cls=plotly.utils.PlotlyJSONEncoder)
layout_json = json.dumps(
data_json = _json.dumps(fig['data'],
cls=plotly.utils.PlotlyJSONEncoder)
layout_json = _json.dumps(
fig['layout'],
cls=plotly.utils.PlotlyJSONEncoder)

Expand Down
6 changes: 3 additions & 3 deletions plotly/tests/test_core/test_plotly/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"""
from __future__ import absolute_import

import json
import requests
import six
from requests.compat import json as _json

from unittest import TestCase
from nose.plugins.attrib import attr
Expand Down Expand Up @@ -296,10 +296,10 @@ def generate_conflicting_plot_options_with_json_writes_of_config():
"""
def gen_test(plot_options):
def test(self):
config = json.load(open(CONFIG_FILE))
config = _json.load(open(CONFIG_FILE))
with open(CONFIG_FILE, 'w') as f:
config.update(plot_options)
f.write(json.dumps(config))
f.write(_json.dumps(config))
self.assertRaises(PlotlyError, py._plot_option_logic, {})
return test

Expand Down
5 changes: 3 additions & 2 deletions plotly/tests/test_core/test_utils/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
from __future__ import absolute_import

import json
from unittest import TestCase

from requests.compat import json as _json

from plotly.utils import PlotlyJSONEncoder, get_by_path, node_generator


class TestJSONEncoder(TestCase):

def test_nan_to_null(self):
array = [1, float('NaN'), float('Inf'), float('-Inf'), 'platypus']
result = json.dumps(array, cls=PlotlyJSONEncoder)
result = _json.dumps(array, cls=PlotlyJSONEncoder)
expected_result = '[1, null, null, null, "platypus"]'
self.assertEqual(result, expected_result)

Expand Down
6 changes: 3 additions & 3 deletions plotly/tests/test_optional/test_offline/test_offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

from nose.tools import raises
from nose.plugins.attrib import attr
from requests.compat import json as _json

from unittest import TestCase
import json

import plotly

Expand Down Expand Up @@ -75,8 +75,8 @@ def test_default_mpl_plot_generates_expected_html(self):
figure = plotly.tools.mpl_to_plotly(fig)
data = figure['data']
layout = figure['layout']
data_json = json.dumps(data, cls=plotly.utils.PlotlyJSONEncoder)
layout_json = json.dumps(layout, cls=plotly.utils.PlotlyJSONEncoder)
data_json = _json.dumps(data, cls=plotly.utils.PlotlyJSONEncoder)
layout_json = _json.dumps(layout, cls=plotly.utils.PlotlyJSONEncoder)
html = self._read_html(plotly.offline.plot_mpl(fig))

# just make sure a few of the parts are in here
Expand Down
Loading

0 comments on commit 6634971

Please sign in to comment.