Skip to content

Commit

Permalink
Remove redundant py2 helper code
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-beedie committed Feb 6, 2024
1 parent 662a0d6 commit 63e6795
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 59 deletions.
52 changes: 24 additions & 28 deletions datashader/datashape/coretypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@

import numpy as np

from .py2help import (
_inttypes,
_strtypes,
)
from .internal_utils import IndexCallable, isidentifier


Expand Down Expand Up @@ -133,21 +129,21 @@ def subarray(self, leading):
return self

def __mul__(self, other):
if isinstance(other, _strtypes):
if isinstance(other, str):
from datashader import datashape
return datashape.dshape(other).__rmul__(self)
if isinstance(other, _inttypes):
if isinstance(other, int):
other = Fixed(other)
if isinstance(other, DataShape):
elif isinstance(other, DataShape):
return other.__rmul__(self)

return DataShape(self, other)

def __rmul__(self, other):
if isinstance(other, _strtypes):
if isinstance(other, str):
from datashader import datashape
return self * datashape.dshape(other)
if isinstance(other, _inttypes):
if isinstance(other, int):
other = Fixed(other)

return DataShape(other, self)
Expand Down Expand Up @@ -221,7 +217,7 @@ class Time(Unit):
__slots__ = 'tz',

def __init__(self, tz=None):
if tz is not None and not isinstance(tz, _strtypes):
if tz is not None and not isinstance(tz, str):
raise TypeError('tz parameter to time datashape must be a string')
# TODO validate against Olson tz database
self.tz = tz
Expand All @@ -241,7 +237,7 @@ class DateTime(Unit):
__slots__ = 'tz',

def __init__(self, tz=None):
if tz is not None and not isinstance(tz, _strtypes):
if tz is not None and not isinstance(tz, str):
raise TypeError('tz parameter to datetime datashape must be a '
'string')
# TODO validate against Olson tz database
Expand Down Expand Up @@ -318,7 +314,7 @@ class Units(Unit):
__slots__ = 'unit', 'tp'

def __init__(self, unit, tp=None):
if not isinstance(unit, _strtypes):
if not isinstance(unit, str):
raise TypeError('unit parameter to units datashape must be a '
'string')
if tp is None:
Expand Down Expand Up @@ -376,12 +372,12 @@ class String(Unit):
def __init__(self, *args):
if len(args) == 0:
fixlen, encoding = None, None
if len(args) == 1:
if isinstance(args[0], _strtypes):
elif len(args) == 1:
if isinstance(args[0], str):
fixlen, encoding = None, args[0]
if isinstance(args[0], _inttypes):
elif isinstance(args[0], int):
fixlen, encoding = args[0], None
if len(args) == 2:
elif len(args) == 2:
fixlen, encoding = args

encoding = encoding or 'U8'
Expand Down Expand Up @@ -532,7 +528,7 @@ class DataShape(Mono):
composite = False

def __init__(self, *parameters, **kwds):
if len(parameters) == 1 and isinstance(parameters[0], _strtypes):
if len(parameters) == 1 and isinstance(parameters[0], str):
raise TypeError("DataShape constructor for internal use.\n"
"Use dshape function to convert strings into "
"datashapes.\nTry:\n\tdshape('%s')"
Expand Down Expand Up @@ -600,7 +596,7 @@ def subarray(self, leading):
return DataShape(*self.parameters[leading:])

def __rmul__(self, other):
if isinstance(other, _inttypes):
if isinstance(other, int):
other = Fixed(other)
return DataShape(other, *self)

Expand Down Expand Up @@ -646,16 +642,16 @@ def _subshape(self, index):
{amount: int32, id: int32}
"""
from .predicates import isdimension
if isinstance(index, _inttypes) and isdimension(self[0]):
if isinstance(index, int) and isdimension(self[0]):
return self.subarray(1)
if isinstance(self[0], Record) and isinstance(index, _strtypes):
if isinstance(self[0], Record) and isinstance(index, str):
return self[0][index]
if isinstance(self[0], Record) and isinstance(index, _inttypes):
if isinstance(self[0], Record) and isinstance(index, int):
return self[0].parameters[0][index][1]
if isinstance(self[0], Record) and isinstance(index, list):
rec = self[0]
# Translate strings to corresponding integers
index = [self[0].names.index(i) if isinstance(i, _strtypes) else i
index = [self[0].names.index(i) if isinstance(i, str) else i
for i in index]
return DataShape(Record([rec.parameters[0][i] for i in index]))
if isinstance(self[0], Record) and isinstance(index, slice):
Expand Down Expand Up @@ -839,7 +835,7 @@ def __int__(self):

def __eq__(self, other):
return (type(other) is Fixed and self.val == other.val or
isinstance(other, _inttypes) and self.val == other)
isinstance(other, int) and self.val == other)

__hash__ = Mono.__hash__

Expand Down Expand Up @@ -918,13 +914,13 @@ def _launder(x):
>>> _launder(Fixed(5)) # No-op on valid parameters
Fixed(val=5)
"""
if isinstance(x, _inttypes):
if isinstance(x, int):
x = Fixed(x)
if isinstance(x, _strtypes):
elif isinstance(x, str):
x = datashape.dshape(x)
if isinstance(x, DataShape) and len(x) == 1:
elif isinstance(x, DataShape) and len(x) == 1:
return x[0]
if isinstance(x, Mono):
elif isinstance(x, Mono):
return x
return x

Expand Down Expand Up @@ -1008,7 +1004,7 @@ def __init__(self, fields):
fields = fields.items()
fields = list(fields)
names = [
str(name) if not isinstance(name, _strtypes) else name
str(name) if not isinstance(name, str) else name
for name, _ in fields
]
types = [_launder(v) for _, v in fields]
Expand Down
7 changes: 3 additions & 4 deletions datashader/datashape/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
Record, string, Null, DataShape, real, date_, time_,
Unit, timedelta_, TimeDelta, object_, String)
from .predicates import isdimension, isrecord
from .py2help import _strtypes, _inttypes
from .internal_utils import _toposort, groupby
from .util import subclasses

Expand Down Expand Up @@ -68,7 +67,7 @@ def discover(obj, **kwargs):
raise NotImplementedError("Don't know how to discover type %r" % type_name)


@dispatch(_inttypes)
@dispatch(int)
def discover(i): # noqa: F811
return int64

Expand Down Expand Up @@ -172,7 +171,7 @@ def is_zero_time(t):
return not (t.hour or t.minute or t.second or t.microsecond)


@dispatch(_strtypes)
@dispatch(str)
def discover(s): # noqa: F811
if not s:
return null
Expand Down Expand Up @@ -394,7 +393,7 @@ def is_string_array(x):
>>> is_string_array(np.array(['Hello', None], dtype='O'))
False
"""
return all(isinstance(i, _strtypes) for i in x.flat[:5].tolist())
return all(isinstance(i, str) for i in x.flat[:5].tolist())


@dispatch(np.ndarray)
Expand Down
2 changes: 1 addition & 1 deletion datashader/datashape/predicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def isscalar(ds):
"""
if isinstance(ds, str):
ds = dshape(ds)
if isinstance(ds, DataShape) and len(ds) == 1:
elif isinstance(ds, DataShape) and len(ds) == 1:
ds = ds[0]
return isinstance(getattr(ds, 'ty', ds), (Unit, Categorical))

Expand Down
24 changes: 0 additions & 24 deletions datashader/datashape/py2help.py

This file was deleted.

3 changes: 1 addition & 2 deletions datashader/datashape/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from itertools import chain
import operator

from .. import py2help
from .. import parser
from .. import type_symbol_table
from ..validation import validate
Expand Down Expand Up @@ -38,7 +37,7 @@ def dshape(o):
"""
if isinstance(o, coretypes.DataShape):
return o
if isinstance(o, py2help._strtypes):
if isinstance(o, str):
ds = parser.parse(o, type_symbol_table.sym)
elif isinstance(o, (coretypes.CType, coretypes.String,
coretypes.Record, coretypes.JSON,
Expand Down

0 comments on commit 63e6795

Please sign in to comment.