Skip to content

Commit

Permalink
Replace toyplot.compatibility with six, since the latter is already a…
Browse files Browse the repository at this point in the history
… transitive dependency.
  • Loading branch information
tshead2 committed Sep 13, 2017
1 parent f781b68 commit 835840f
Show file tree
Hide file tree
Showing 21 changed files with 64 additions and 97 deletions.
1 change: 0 additions & 1 deletion docs/reference.rst
Expand Up @@ -14,7 +14,6 @@ Python API
toyplot.canvas.rst
toyplot.color.rst
toyplot.coordinates.rst
toyplot.compatibility.rst
toyplot.config.rst
toyplot.data.rst
toyplot.font.rst
Expand Down
7 changes: 0 additions & 7 deletions docs/toyplot.compatibility.rst

This file was deleted.

3 changes: 2 additions & 1 deletion features/steps/canvas.py
Expand Up @@ -8,6 +8,7 @@

import nose.tools
import numpy.testing
import six
import toyplot

import testing
Expand Down Expand Up @@ -65,7 +66,7 @@ def step_impl(context):
@then(u'the canvas can be rendered in Jupyter as HTML')
def step_impl(context):
html = context.canvas._repr_html_()
nose.tools.assert_is_instance(html, toyplot.compatibility.string_type)
nose.tools.assert_is_instance(html, six.string_types)

@then(u'the canvas can be rendered in Jupyter as a PNG image')
def step_impl(context):
Expand Down
3 changes: 2 additions & 1 deletion features/steps/data-tables.py
Expand Up @@ -13,6 +13,7 @@
import sys
import tempfile

import six
import toyplot.data

import testing
Expand Down Expand Up @@ -418,7 +419,7 @@ def step_impl(context):
@then(u'the table can be rendered as format ipython html string')
def step_impl(context):
html = context.data._repr_html_()
nose.tools.assert_is_instance(html, toyplot.compatibility.unicode_type)
nose.tools.assert_is_instance(html, six.text_type)
testing.assert_html_equal(html, "data-table")


7 changes: 4 additions & 3 deletions features/steps/testing.py
Expand Up @@ -16,6 +16,7 @@

import numpy.testing
import png
import six

import toyplot.html
import toyplot.require
Expand Down Expand Up @@ -51,7 +52,7 @@ def _assert_string_equal(content, test_file, reference_file, encoding="utf-8"):
if os.path.exists(test_file):
os.remove(test_file)
if os.path.exists(reference_file):
reference = toyplot.compatibility.unicode_type(
reference = six.text_type(
open(reference_file, "rb").read(), encoding=encoding)
if content != reference:
if not os.path.exists(failed_dir):
Expand All @@ -76,7 +77,7 @@ def _json_comparison_string(o):
"""
if o is None:
return "null"
if isinstance(o, toyplot.compatibility.string_type):
if isinstance(o, six.string_types):
return "\"" + o + "\""
if isinstance(o, numbers.Integral):
return str(o)
Expand Down Expand Up @@ -235,7 +236,7 @@ def assert_canvas_equal(canvas, name, show_diff=True):
raise AssertionError(message)

def read_png(fobj):
if isinstance(fobj, toyplot.compatibility.string_type):
if isinstance(fobj, six.string_types):
reader = png.Reader(filename=fobj)
else:
reader = png.Reader(fobj)
Expand Down
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -27,6 +27,7 @@
"numpy>=1.8.0",
"pypng",
"reportlab",
"six",
],
long_description="""Toyplot is the kid-sized plotting toolkit for Python with grownup-sized goals:
* Develop beautiful interactive, animated plots that embrace the unique capabilities of electronic publishing and support repoducibility.
Expand Down
4 changes: 2 additions & 2 deletions tests/tests.py
Expand Up @@ -17,9 +17,9 @@
import tempfile
import xml.etree.ElementTree as xml

import six
import toyplot
import toyplot.color
import toyplot.compatibility
import toyplot.html
import toyplot.locator
import toyplot.svg
Expand Down Expand Up @@ -55,7 +55,7 @@ def json_comparison_string(o):
"""
if o is None:
return "null"
if isinstance(o, toyplot.compatibility.string_type):
if isinstance(o, six.string_types):
return "\"" + o + "\""
if isinstance(o, numbers.Integral):
return str(o)
Expand Down
1 change: 0 additions & 1 deletion toyplot/canvas.py
Expand Up @@ -13,7 +13,6 @@
import toyplot.coordinates
import toyplot.broadcast
import toyplot.color
import toyplot.compatibility
import toyplot.config
import toyplot.layout
import toyplot.style
Expand Down
27 changes: 13 additions & 14 deletions toyplot/color.py
Expand Up @@ -13,8 +13,7 @@
import xml.etree.ElementTree as xml

import numpy # pylint: disable=wrong-import-position

import toyplot.compatibility # pylint: disable=wrong-import-position
import six # pylint: disable=wrong-import-position


black = "#292724"
Expand All @@ -37,8 +36,8 @@ def _html_color_swatches(colors, css_class, margin=0):
xml.SubElement(
root_xml,
"div",
style="float:left;width:20px;height:20px;margin-right:%spx;background-color:%s" % (margin, toyplot.color.to_css(color)))
return toyplot.compatibility.unicode_type(xml.tostring(root_xml, encoding="utf-8", method="html"), encoding="utf-8")
style="float:left;width:20px;height:20px;margin-right:%spx;background-color:%s" % (margin, to_css(color)))
return six.text_type(xml.tostring(root_xml, encoding="utf-8", method="html"), encoding="utf-8")


def _jupyter_color_swatches(colors):
Expand Down Expand Up @@ -148,7 +147,7 @@ def pivot(n):


def _require_color(color):
if isinstance(color, toyplot.compatibility.string_type):
if isinstance(color, six.string_types):
return css(color)
elif isinstance(color, (numpy.void, numpy.ndarray)) and color.dtype == dtype:
return color
Expand Down Expand Up @@ -192,12 +191,12 @@ def broadcast(colors, shape, default=None):

# Next, extract the user's choice of custom palette / colormap.
colormap = None
if isinstance(colors, toyplot.color.Map):
if isinstance(colors, Map):
colormap = colors
colors = numpy.arange(shape[0]) # By default, generate [0, M) per-datum values
if per_datum and shape[1] > 1:
colors = numpy.arange(shape[1]) # More than one series, so generate [0, N) per-series values
elif isinstance(colors, tuple) and len(colors) == 2 and isinstance(colors[1], toyplot.color.Map):
elif isinstance(colors, tuple) and len(colors) == 2 and isinstance(colors[1], Map):
colors, colormap = colors

# Next, convert the supplied colors into a toyplot color array.
Expand All @@ -209,7 +208,7 @@ def broadcast(colors, shape, default=None):
pass
elif issubclass(colors.dtype.type, numpy.number): # Array of numeric values, so map values to colors
if colormap is None:
colormap = toyplot.color.brewer.map("BlueRed", domain_min=colors.min(), domain_max=colors.max())
colormap = brewer.map("BlueRed", domain_min=colors.min(), domain_max=colors.max())
colors = colormap.colors(colors)
elif issubclass(colors.dtype.type, numpy.character): # Convert CSS strings to colors.
colors = numpy.array([_require_color(color) for color in colors.flat], dtype=dtype).reshape(colors.shape)
Expand Down Expand Up @@ -353,7 +352,7 @@ def max(self, value):
self._max = value

def __init__(self, domain_min, domain_max):
self._domain = toyplot.color.Map.DomainHelper(domain_min, domain_max)
self._domain = Map.DomainHelper(domain_min, domain_max)

def _finalize(self):
return self
Expand Down Expand Up @@ -381,7 +380,7 @@ def __init__(self, palette=None):
if palette is None:
palette = Palette()
self._palette = palette
toyplot.color.Map.__init__(self, domain_min=0, domain_max=len(palette))
super(CategoricalMap, self).__init__(domain_min=0, domain_max=len(palette))

def colors(self, values, domain_min=None, domain_max=None):
"""Convert a sequence of categorical (nonnegative integer) values to colors.
Expand Down Expand Up @@ -470,7 +469,7 @@ class DivergingMap(Map):
"""

def __init__(self, low=None, high=None, domain_min=None, domain_max=None):
toyplot.color.Map.__init__(self, domain_min=domain_min, domain_max=domain_max)
super(DivergingMap, self).__init__(domain_min=domain_min, domain_max=domain_max)

def _lab_to_msh(L, a, b):
M = numpy.sqrt(L * L + a * a + b * b)
Expand Down Expand Up @@ -595,7 +594,7 @@ def _repr_html_(self):
"div",
style="float:left;width:200px;height:20px;background:linear-gradient(to right,%s)" % (gradient_stops),
)
return toyplot.compatibility.unicode_type(xml.tostring(root_xml, encoding="utf-8", method="html"), encoding="utf-8")
return six.text_type(xml.tostring(root_xml, encoding="utf-8", method="html"), encoding="utf-8")


class LinearMap(Map):
Expand Down Expand Up @@ -627,7 +626,7 @@ class LinearMap(Map):
"""

def __init__(self, palette=None, stops=None, domain_min=None, domain_max=None):
toyplot.color.Map.__init__(self, domain_min=domain_min, domain_max=domain_max)
super(LinearMap, self).__init__(domain_min=domain_min, domain_max=domain_max)

if palette is None:
palette = brewer.palette("BlueRed")
Expand Down Expand Up @@ -711,7 +710,7 @@ def _repr_html_(self):
"div",
style="float:left;width:200px;height:20px;background:linear-gradient(to right,%s)" % (gradient_stops),
)
return toyplot.compatibility.unicode_type(xml.tostring(root_xml, encoding="utf-8", method="html"), encoding="utf-8")
return six.text_type(xml.tostring(root_xml, encoding="utf-8", method="html"), encoding="utf-8")


class BrewerFactory(object):
Expand Down
29 changes: 0 additions & 29 deletions toyplot/compatibility.py

This file was deleted.

5 changes: 3 additions & 2 deletions toyplot/coordinates.py
Expand Up @@ -11,6 +11,7 @@
import itertools

import numpy
import six

import toyplot.broadcast
import toyplot.color
Expand Down Expand Up @@ -1268,7 +1269,7 @@ def bars(
toyplot.style.require(style, allowed=toyplot.style.allowed.fill),
)

if isinstance(baseline, toyplot.compatibility.string_type):
if isinstance(baseline, six.string_types):
baseline = toyplot.require.value_in(baseline, ["stacked", "symmetric", "wiggle"])
if baseline == "stacked":
baseline = numpy.zeros(series.shape[0])
Expand Down Expand Up @@ -1507,7 +1508,7 @@ def fill(
toyplot.style.require(style, allowed=toyplot.style.allowed.fill),
)

if isinstance(baseline, toyplot.compatibility.string_type):
if isinstance(baseline, six.string_types):
baseline = toyplot.require.value_in(baseline, ["stacked", "symmetric", "wiggle"])
if baseline == "stacked":
baseline = numpy.ma.zeros(series.shape[0])
Expand Down
22 changes: 11 additions & 11 deletions toyplot/data.py
Expand Up @@ -17,9 +17,9 @@
import pandas
except: # pragma: no cover
pass
import six

import toyplot.color
import toyplot.compatibility


def minimax(items):
Expand Down Expand Up @@ -203,11 +203,11 @@ def __getitem__(self, index):
# Cases that return a column (array):

# table["a"]
if isinstance(index, toyplot.compatibility.string_type):
if isinstance(index, six.string_types):
column = index
column_slice = slice(None, None, None)
# table["a", 10], table["a", 10:20], table["a", [10, 12, 18]], etc.
elif isinstance(index, tuple) and isinstance(index[0], toyplot.compatibility.string_type):
elif isinstance(index, tuple) and isinstance(index[0], six.string_types):
column = index[0]
column_slice = index[1]

Expand Down Expand Up @@ -236,7 +236,7 @@ def __getitem__(self, index):
row_slice = index[0]

# table[, "a"]
if isinstance(index[1], toyplot.compatibility.string_type):
if isinstance(index[1], six.string_types):
columns = [index[1]]
# table[, ["a", "b", "c"]], etc.
else:
Expand All @@ -255,19 +255,19 @@ def __getitem__(self, index):


def __setitem__(self, index, value):
if isinstance(index, toyplot.compatibility.string_type):
if isinstance(index, six.string_types):
value = numpy.ma.array(value)
if value.ndim != 1:
raise ValueError("Can't assign %s-dimensional array to the '%s' column." % (value.ndim, index))
for column in self._columns.values():
if column.shape != value.shape:
raise ValueError("Expected %s values, received %s." % (column.shape[0], value.shape[0]))
column = toyplot.compatibility.unicode_type(index)
column = six.text_type(index)
self._columns[column] = value
return

if isinstance(index, tuple):
if isinstance(index[0], toyplot.compatibility.string_type) and isinstance(index[1], (int, slice)):
if isinstance(index[0], six.string_types) and isinstance(index[1], (int, slice)):
column, column_slice = index
self._columns[column][column_slice] = value
return
Expand Down Expand Up @@ -299,7 +299,7 @@ def _repr_html_(self):
xml.SubElement(
header_xml,
"th",
style="text-align:left;border:none;padding-right:1em;").text = toyplot.compatibility.unicode_type(name)
style="text-align:left;border:none;padding-right:1em;").text = six.text_type(name)

iterators = [iter(column) for column in self._columns.values()]
for _ in numpy.arange(len(self)):
Expand All @@ -311,9 +311,9 @@ def _repr_html_(self):
xml.SubElement(
row_xml,
"td",
style="border:none;padding-right:1em;").text = toyplot.compatibility.unicode_type(value)
style="border:none;padding-right:1em;").text = six.text_type(value)

return toyplot.compatibility.unicode_type(xml.tostring(root_xml, encoding="utf-8", method="html"), encoding="utf-8")
return six.text_type(xml.tostring(root_xml, encoding="utf-8", method="html"), encoding="utf-8")

@property
def shape(self):
Expand Down Expand Up @@ -411,7 +411,7 @@ def read_csv(fobj, convert=False):
Python standard library, or functionality provided by `numpy` or `Pandas`.
"""
import csv
if isinstance(fobj, toyplot.compatibility.string_type):
if isinstance(fobj, six.string_types):
fobj = open(fobj, "r")
rows = [row for row in csv.reader(fobj)]
columns = zip(*rows)
Expand Down
4 changes: 2 additions & 2 deletions toyplot/format.py
Expand Up @@ -5,7 +5,7 @@
from __future__ import division

import numpy
import toyplot.compatibility
import six


class Formatter(object):
Expand Down Expand Up @@ -57,7 +57,7 @@ def __init__(self, format="{:.6g}", nanshow=True):
self._nanshow = nanshow

def format(self, value):
if isinstance(value, toyplot.compatibility.string_type):
if isinstance(value, six.string_types):
return value, "", ""

if numpy.isnan(value) and not self._nanshow:
Expand Down

0 comments on commit 835840f

Please sign in to comment.