Skip to content

Commit 5a0091e

Browse files
authored
chore: no more Python2 support in source code (#10)
* chore: no more Python2 support in source code * Update src/cuda_histogram/hist_tools.py
1 parent 9e2a5c5 commit 5a0091e

File tree

1 file changed

+25
-38
lines changed

1 file changed

+25
-38
lines changed

src/cuda_histogram/hist_tools.py

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,12 @@
77
import warnings
88
from abc import ABCMeta, abstractmethod
99
from collections import namedtuple
10+
from collections.abc import Sequence
1011

1112
import awkward
1213
import cupy
1314
import numpy as np
1415

15-
# Python 2 and 3 compatibility
16-
_regex_pattern = re.compile("dummy").__class__
17-
try:
18-
basestring # noqa: B018
19-
except NameError:
20-
basestring = str
21-
22-
try:
23-
from collections.abc import Sequence
24-
except ImportError:
25-
from collections.abc import Sequence
26-
2716
MaybeSumSlice = namedtuple("MaybeSumSlice", ["start", "stop", "sum"])
2817

2918
_replace_nans = cupy.ElementwiseKernel("T v", "T x", "x = isnan(x)?v:x", "replace_nans")
@@ -115,9 +104,7 @@ def __str__(self):
115104
return self._label
116105
if self.nan():
117106
return "(nanflow)"
118-
# string representation of floats is apparently a touchy subject.. further reading:
119-
# https://stackoverflow.com/questions/25898733/why-does-strfloat-return-more-digits-in-python-3-than-python-2
120-
return "{}{:.12g}, {:.12g})".format(
107+
return "{}{}, {})".format(
121108
"(" if self._lo == -np.inf else "[",
122109
self._lo,
123110
self._hi,
@@ -193,7 +180,7 @@ class StringBin:
193180
"""
194181

195182
def __init__(self, name, label=None):
196-
if not isinstance(name, basestring):
183+
if not isinstance(name, str):
197184
raise TypeError(
198185
f"StringBin only supports string categories, received a {name!r}"
199186
)
@@ -273,7 +260,7 @@ def __eq__(self, other):
273260
return False
274261
# label doesn't matter
275262
return True
276-
elif isinstance(other, basestring):
263+
elif isinstance(other, str):
277264
# Convenient for testing axis in list by name
278265
return not self._name != other
279266
raise TypeError(f"Cannot compare an Axis with a {other!r}")
@@ -364,9 +351,9 @@ def _ireduce(self, the_slice):
364351
out = None
365352
if isinstance(the_slice, StringBin):
366353
out = [the_slice.name]
367-
elif isinstance(the_slice, _regex_pattern):
354+
elif isinstance(the_slice, re.Pattern):
368355
out = [k for k in self._sorted if the_slice.match(k)]
369-
elif isinstance(the_slice, basestring):
356+
elif isinstance(the_slice, str):
370357
pattern = "^" + re.escape(the_slice).replace(r"\*", ".*") + "$"
371358
m = re.compile(pattern)
372359
out = [k for k in self._sorted if m.match(k)]
@@ -380,11 +367,11 @@ def _ireduce(self, the_slice):
380367
if the_slice.step is not None:
381368
raise IndexError("Not sure how to use slice step for categories...")
382369
start, stop = 0, len(self._sorted)
383-
if isinstance(the_slice.start, basestring):
370+
if isinstance(the_slice.start, str):
384371
start = self._sorted.index(the_slice.start)
385372
else:
386373
start = the_slice.start
387-
if isinstance(the_slice.stop, basestring):
374+
if isinstance(the_slice.stop, str):
388375
stop = self._sorted.index(the_slice.stop)
389376
else:
390377
stop = the_slice.stop
@@ -775,7 +762,7 @@ class AccumulatorABC(metaclass=ABCMeta):
775762
After defining an accumulator::
776763
777764
from coffea.processor import dict_accumulator, column_accumulator, defaultdict_accumulator
778-
from coffea.hist import Hist, Bin
765+
from cuda_histogram import Hist, Bin
779766
import numpy as np
780767
781768
adef = dict_accumulator({
@@ -861,26 +848,28 @@ class Hist(AccumulatorABC):
861848
862849
Creating a histogram with a sparse axis, and two dense axes::
863850
864-
h = coffea.hist.Hist("Observed bird count",
865-
coffea.hist.Cat("species", "Bird species"),
866-
coffea.hist.Bin("x", "x coordinate [m]", 20, -5, 5),
867-
coffea.hist.Bin("y", "y coordinate [m]", 20, -5, 5),
851+
import cuda_histogram as chist
852+
853+
h = chist.Hist("Observed bird count",
854+
chist.Cat("species", "Bird species"),
855+
chist.Bin("x", "x coordinate [m]", 20, -5, 5),
856+
chist.Bin("y", "y coordinate [m]", 20, -5, 5),
868857
)
869858
870859
# or
871860
872-
h = coffea.hist.Hist(label="Observed bird count",
873-
axes=(coffea.hist.Cat("species", "Bird species"),
874-
coffea.hist.Bin("x", "x coordinate [m]", 20, -5, 5),
875-
coffea.hist.Bin("y", "y coordinate [m]", 20, -5, 5),
861+
h = chist.Hist(label="Observed bird count",
862+
axes=(chist.Cat("species", "Bird species"),
863+
chist.Bin("x", "x coordinate [m]", 20, -5, 5),
864+
chist.Bin("y", "y coordinate [m]", 20, -5, 5),
876865
)
877866
)
878867
879868
# or
880869
881-
h = coffea.hist.Hist(axes=[coffea.hist.Cat("species", "Bird species"),
882-
coffea.hist.Bin("x", "x coordinate [m]", 20, -5, 5),
883-
coffea.hist.Bin("y", "y coordinate [m]", 20, -5, 5),
870+
h = chist.Hist(axes=[chist.Cat("species", "Bird species"),
871+
chist.Bin("x", "x coordinate [m]", 20, -5, 5),
872+
chist.Bin("y", "y coordinate [m]", 20, -5, 5),
884873
],
885874
label="Observed bird count",
886875
)
@@ -896,12 +885,10 @@ class Hist(AccumulatorABC):
896885
DEFAULT_DTYPE = "d"
897886

898887
def __init__(self, label, *axes, **kwargs):
899-
if not isinstance(label, basestring):
888+
if not isinstance(label, str):
900889
raise TypeError("label must be a string")
901890
self._label = label
902-
self._dtype = kwargs.pop(
903-
"dtype", Hist.DEFAULT_DTYPE
904-
) # Much nicer in python3 :(
891+
self._dtype = kwargs.get("dtype", Hist.DEFAULT_DTYPE)
905892
self._axes = axes
906893
if len(axes) == 0 and "axes" in kwargs:
907894
if not isinstance(kwargs["axes"], Sequence):
@@ -1612,7 +1599,7 @@ def expandkey(key):
16121599
return out
16131600

16141601
def to_hist(self):
1615-
"""Convert this coffea.hist histogram to a hist object"""
1602+
"""Convert the cuda_histogram histogram to a hist object"""
16161603
import hist
16171604

16181605
return hist.Hist(self.to_boost())

0 commit comments

Comments
 (0)