Skip to content

Commit c3fb1a5

Browse files
committed
Remove dependency on six (py2 compat)
1 parent cfad786 commit c3fb1a5

37 files changed

+127
-231
lines changed

Diff for: doc/python/colorscales.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,10 @@ fig.show()
400400
```python
401401
import plotly.graph_objects as go
402402

403-
import six.moves.urllib
403+
import urllib
404404
import json
405405

406-
response = six.moves.urllib.request.urlopen(
406+
response = urllib.request.urlopen(
407407
"https://raw.githubusercontent.com/plotly/datasets/master/custom_heatmap_colorscale.json"
408408
)
409409

@@ -492,11 +492,11 @@ Like axes, you can customize the color bar ticks, labels, and values with `ticks
492492
```python
493493
import plotly.graph_objects as go
494494

495-
import six.moves.urllib
495+
import urllib
496496
import json
497497

498498
# Load heatmap data
499-
response = six.moves.urllib.request.urlopen(
499+
response = urllib.request.urlopen(
500500
"https://raw.githubusercontent.com/plotly/datasets/master/custom_heatmap_colorscale.json")
501501
dataset = json.load(response)
502502

@@ -525,11 +525,11 @@ By default, color bars are displayed vertically. You can change a color bar to b
525525
```python
526526
import plotly.graph_objects as go
527527

528-
import six.moves.urllib
528+
import urllib
529529
import json
530530

531531
# Load heatmap data
532-
response = six.moves.urllib.request.urlopen(
532+
response = urllib.request.urlopen(
533533
"https://raw.githubusercontent.com/plotly/datasets/master/custom_heatmap_colorscale.json")
534534
dataset = json.load(response)
535535

Diff for: doc/python/graphing-multiple-chart-types.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ import plotly.graph_objects as go
8585

8686
# Load data
8787
import json
88-
import six.moves.urllib
88+
import urllib
8989

90-
response = six.moves.urllib.request.urlopen(
90+
response = urllib.request.urlopen(
9191
"https://raw.githubusercontent.com/plotly/datasets/master/steepest.json")
9292

9393
data = json.load(response)

Diff for: packages/python/chart-studio/chart_studio/plotly/chunked_requests/chunked_request.py

+26-26
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import time
2-
import six
1+
import http.client
32
import os
43
import ssl
4+
import time
5+
from io import StringIO
56

6-
from six.moves import http_client
7-
from six.moves.urllib.parse import urlparse, unquote
7+
from urllib.parse import urlparse, unquote
88

99
from chart_studio.api import utils
1010

@@ -50,12 +50,12 @@ def write(self, data, reconnect_on=("", 200, 502)):
5050
# Reconnect depending on the status code.
5151
if (response == "" and "" in reconnect_on) or (
5252
response
53-
and isinstance(response, http_client.HTTPResponse)
53+
and isinstance(response, http.client.HTTPResponse)
5454
and response.status in reconnect_on
5555
):
5656
self._reconnect()
5757

58-
elif response and isinstance(response, http_client.HTTPResponse):
58+
elif response and isinstance(response, http.client.HTTPResponse):
5959
# If an HTTPResponse was recieved then
6060
# make the users aware instead of
6161
# auto-reconnecting in case the
@@ -85,7 +85,7 @@ def write(self, data, reconnect_on=("", 200, 502)):
8585
"{msglen}\r\n{msg}\r\n".format(msglen=msglen, msg=msg).encode("utf-8")
8686
)
8787
self._conn.sock.setblocking(0)
88-
except http_client.socket.error:
88+
except http.client.socket.error:
8989
self._reconnect()
9090
self.write(data)
9191

@@ -152,11 +152,11 @@ def _connect(self):
152152
if proxy_server and proxy_port:
153153
if ssl_enabled:
154154
context = self._get_ssl_context()
155-
self._conn = http_client.HTTPSConnection(
155+
self._conn = http.client.HTTPSConnection(
156156
proxy_server, proxy_port, context=context
157157
)
158158
else:
159-
self._conn = http_client.HTTPConnection(proxy_server, proxy_port)
159+
self._conn = http.client.HTTPConnection(proxy_server, proxy_port)
160160

161161
tunnel_headers = None
162162
if proxy_auth:
@@ -166,9 +166,9 @@ def _connect(self):
166166
else:
167167
if ssl_enabled:
168168
context = self._get_ssl_context()
169-
self._conn = http_client.HTTPSConnection(server, port, context=context)
169+
self._conn = http.client.HTTPSConnection(server, port, context=context)
170170
else:
171-
self._conn = http_client.HTTPConnection(server, port)
171+
self._conn = http.client.HTTPConnection(server, port)
172172

173173
self._conn.putrequest("POST", self._url)
174174
self._conn.putheader("Transfer-Encoding", "chunked")
@@ -179,14 +179,14 @@ def _connect(self):
179179
# Set blocking to False prevents recv
180180
# from blocking while waiting for a response.
181181
self._conn.sock.setblocking(False)
182-
self._bytes = six.b("")
182+
self._bytes = b""
183183
self._reset_retries()
184184
time.sleep(0.5)
185185

186186
def close(self):
187187
""" Close the connection to server.
188188
189-
If available, return a http_client.HTTPResponse object.
189+
If available, return a http.client.HTTPResponse object.
190190
191191
Closing the connection involves sending the
192192
Transfer-Encoding terminating bytes.
@@ -199,7 +199,7 @@ def close(self):
199199
# require an extra \r\n.
200200
try:
201201
self._conn.send("\r\n0\r\n\r\n".encode("utf-8"))
202-
except http_client.socket.error:
202+
except http.client.socket.error:
203203
# In case the socket has already been closed
204204
return ""
205205

@@ -219,28 +219,28 @@ def _getresponse(self):
219219
while True:
220220
try:
221221
_bytes = self._conn.sock.recv(1)
222-
except http_client.socket.error:
222+
except http.client.socket.error:
223223
# For error 54: Connection reset by peer
224224
# (and perhaps others)
225-
return six.b("")
226-
if _bytes == six.b(""):
225+
return b""
226+
if _bytes == b"":
227227
break
228228
else:
229229
response += _bytes
230230
# Set recv to be non-blocking again
231231
self._conn.sock.setblocking(False)
232232

233-
# Convert the response string to a http_client.HTTPResponse
233+
# Convert the response string to a http.client.HTTPResponse
234234
# object with a bit of a hack
235-
if response != six.b(""):
235+
if response != b"":
236236
# Taken from
237237
# http://pythonwise.blogspot.ca/2010/02/parse-http-response.html
238238
try:
239-
response = http_client.HTTPResponse(_FakeSocket(response))
239+
response = http.client.HTTPResponse(_FakeSocket(response))
240240
response.begin()
241241
except:
242242
# Bad headers ... etc.
243-
response = six.b("")
243+
response = b""
244244
return response
245245

246246
def _isconnected(self):
@@ -268,10 +268,10 @@ def _isconnected(self):
268268
# 3 - Check if the server has returned any data.
269269
# If they have, then start to store the response
270270
# in _bytes.
271-
self._bytes = six.b("")
271+
self._bytes = b""
272272
self._bytes = self._conn.sock.recv(1)
273273
return False
274-
except http_client.socket.error as e:
274+
except http.client.socket.error as e:
275275
# Check why recv failed
276276
# Windows machines are the error codes
277277
# that start with 1
@@ -320,7 +320,7 @@ def _reconnect(self):
320320
if not self._isconnected():
321321
try:
322322
self._connect()
323-
except http_client.socket.error as e:
323+
except http.client.socket.error as e:
324324
# Attempt to reconnect if the connection was refused
325325
if e.errno == 61 or e.errno == 10061:
326326
# errno 61 is the "Connection Refused" error
@@ -346,8 +346,8 @@ def _reset_retries(self):
346346
self._delay = 1
347347

348348

349-
class _FakeSocket(six.StringIO):
350-
# Used to construct a http_client.HTTPResponse object
349+
class _FakeSocket(StringIO):
350+
# Used to construct a http.client.HTTPResponse object
351351
# from a string.
352352
# Thx to: http://pythonwise.blogspot.ca/2010/02/parse-http-response.html
353353
def makefile(self, *args, **kwargs):

Diff for: packages/python/chart-studio/chart_studio/plotly/plotly.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@
2121
import json
2222
import os
2323
import time
24+
import urllib
2425
import warnings
2526
import webbrowser
2627

27-
import six
28-
import six.moves
2928
import json as _json
3029

3130
import _plotly_utils.utils
@@ -513,8 +512,6 @@ def get_figure(file_owner_or_url, file_id=None, raw=False):
513512
fid = "{}:{}".format(file_owner, file_id)
514513
response = v2.plots.content(fid, inline_data=True)
515514
figure = response.json()
516-
if six.PY2:
517-
figure = byteify(figure)
518515
# Fix 'histogramx', 'histogramy', and 'bardir' stuff
519516
for index, entry in enumerate(figure["data"]):
520517
try:
@@ -620,7 +617,7 @@ def get_streaming_specs(self):
620617

621618
# If no scheme (https/https) is included in the streaming_url, the
622619
# host will be None. Use streaming_url in this case.
623-
host = six.moves.urllib.parse.urlparse(streaming_url).hostname or streaming_url
620+
host = urllib.parse.urlparse(streaming_url).hostname or streaming_url
624621

625622
headers = {"Host": host, "plotly-streamtoken": self.stream_id}
626623
streaming_specs = {
@@ -1380,7 +1377,7 @@ def parse_grid_id_args(grid, grid_url):
13801377
else:
13811378
supplied_arg_name = supplied_arg_names.pop()
13821379
if supplied_arg_name == "grid_url":
1383-
path = six.moves.urllib.parse.urlparse(grid_url).path
1380+
path = urllib.parse.urlparse(grid_url).path
13841381
file_owner, file_id = path.replace("/~", "").split("/")[0:2]
13851382
return "{0}:{1}".format(file_owner, file_id)
13861383
else:
@@ -1392,7 +1389,7 @@ def add_share_key_to_url(plot_url, attempt=0):
13921389
Check that share key is enabled and update url to include the secret key
13931390
13941391
"""
1395-
urlsplit = six.moves.urllib.parse.urlparse(plot_url)
1392+
urlsplit = urllib.parse.urlparse(plot_url)
13961393
username = urlsplit.path.split("/")[1].split("~")[1]
13971394
idlocal = urlsplit.path.split("/")[2]
13981395
fid = "{}:{}".format(username, idlocal)

Diff for: packages/python/chart-studio/chart_studio/session.py

+11-13
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,37 @@
99

1010
import copy
1111

12-
import six
13-
1412
import _plotly_utils.exceptions
1513

1614

1715
_session = {"credentials": {}, "config": {}, "plot_options": {}}
1816

1917
CREDENTIALS_KEYS = {
20-
"username": six.string_types,
21-
"api_key": six.string_types,
22-
"proxy_username": six.string_types,
23-
"proxy_password": six.string_types,
18+
"username": str,
19+
"api_key": str,
20+
"proxy_username": str,
21+
"proxy_password": str,
2422
"stream_ids": list,
2523
}
2624

2725
CONFIG_KEYS = {
28-
"plotly_domain": six.string_types,
29-
"plotly_streaming_domain": six.string_types,
30-
"plotly_api_domain": six.string_types,
26+
"plotly_domain": str,
27+
"plotly_streaming_domain": str,
28+
"plotly_api_domain": str,
3129
"plotly_ssl_verification": bool,
3230
"plotly_proxy_authorization": bool,
3331
"world_readable": bool,
3432
"auto_open": bool,
35-
"sharing": six.string_types,
33+
"sharing": str,
3634
}
3735

3836
PLOT_OPTIONS = {
39-
"filename": six.string_types,
40-
"fileopt": six.string_types,
37+
"filename": str,
38+
"fileopt": str,
4139
"validate": bool,
4240
"world_readable": bool,
4341
"auto_open": bool,
44-
"sharing": six.string_types,
42+
"sharing": str,
4543
}
4644

4745
SHARING_OPTIONS = ["public", "private", "secret"]

Diff for: packages/python/chart-studio/chart_studio/tests/test_plot_ly/test_get_figure/test_get_figure.py

-5
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
"""
88
from __future__ import absolute_import
99

10-
from unittest import skipIf
11-
12-
import six
13-
1410
import _plotly_utils.exceptions
1511
from chart_studio import exceptions
1612
from chart_studio.plotly import plotly as py
@@ -93,7 +89,6 @@ def test_get_figure_raw(self):
9389

9490

9591
class TestBytesVStrings(PlotlyTestCase):
96-
@skipIf(six.PY2, "Decoding and missing escapes only seen in PY3")
9792
def test_proper_escaping(self):
9893
un = "PlotlyImageTest"
9994
ak = "786r5mecv0"

Diff for: packages/python/chart-studio/chart_studio/tests/test_plot_ly/test_get_requests/test_get_requests.py

+5-21
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import copy
99

1010
import requests
11-
import six
1211
import json as _json
1312

1413
from chart_studio.tests.utils import PlotlyTestCase
@@ -34,10 +33,7 @@ def test_user_does_not_exist(self):
3433
hd["plotly-apikey"] = api_key
3534
resource = "/apigetfile/{0}/{1}/".format(file_owner, file_id)
3635
response = requests.get(server + resource, headers=hd)
37-
if six.PY2:
38-
content = _json.loads(response.content)
39-
else:
40-
content = _json.loads(response.content.decode("unicode_escape"))
36+
content = _json.loads(response.content.decode("unicode_escape"))
4137
error_message = (
4238
"Aw, snap! We don't have an account for {0}. Want to "
4339
"try again? Sign in is not case sensitive.".format(username)
@@ -55,10 +51,7 @@ def test_file_does_not_exist(self):
5551
hd["plotly-apikey"] = api_key
5652
resource = "/apigetfile/{0}/{1}/".format(file_owner, file_id)
5753
response = requests.get(server + resource, headers=hd)
58-
if six.PY2:
59-
content = _json.loads(response.content)
60-
else:
61-
content = _json.loads(response.content.decode("unicode_escape"))
54+
content = _json.loads(response.content.decode("unicode_escape"))
6255
error_message = (
6356
"Aw, snap! It looks like this file does " "not exist. Want to try again?"
6457
)
@@ -91,10 +84,7 @@ def test_private_permission_defined(self):
9184
hd["plotly-apikey"] = api_key
9285
resource = "/apigetfile/{0}/{1}/".format(file_owner, file_id)
9386
response = requests.get(server + resource, headers=hd)
94-
if six.PY2:
95-
content = _json.loads(response.content)
96-
else:
97-
content = _json.loads(response.content.decode("unicode_escape"))
87+
content = _json.loads(response.content.decode("unicode_escape"))
9888
self.assertEqual(response.status_code, 403)
9989

10090
# Private File that is shared
@@ -109,10 +99,7 @@ def test_missing_headers(self):
10999
hd = copy.copy(default_headers)
110100
del hd[header]
111101
response = requests.get(server + resource, headers=hd)
112-
if six.PY2:
113-
content = _json.loads(response.content)
114-
else:
115-
content = _json.loads(response.content.decode("unicode_escape"))
102+
content = _json.loads(response.content.decode("unicode_escape"))
116103
self.assertEqual(response.status_code, 422)
117104

118105
def test_valid_request(self):
@@ -125,10 +112,7 @@ def test_valid_request(self):
125112
hd["plotly-apikey"] = api_key
126113
resource = "/apigetfile/{0}/{1}/".format(file_owner, file_id)
127114
response = requests.get(server + resource, headers=hd)
128-
if six.PY2:
129-
content = _json.loads(response.content)
130-
else:
131-
content = _json.loads(response.content.decode("unicode_escape"))
115+
content = _json.loads(response.content.decode("unicode_escape"))
132116
self.assertEqual(response.status_code, 200)
133117
# content = _json.loads(res.content)
134118
# response_payload = content['payload']

0 commit comments

Comments
 (0)