Skip to content

Commit

Permalink
Fixes #36 by replacing range with xrange in Python 2. (#88)
Browse files Browse the repository at this point in the history
* Fixes #36 by replacing range with xrange in Python 2.

* Black, flake8, and warnings to avoid breaking Python 2 syntax.
  • Loading branch information
jpivarski authored Aug 31, 2020
1 parent 62b09d3 commit 9ff06a4
Show file tree
Hide file tree
Showing 17 changed files with 67 additions and 49 deletions.
3 changes: 0 additions & 3 deletions tests/test_0053-parents-should-not-be-bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import uproot4


@pytest.mark.skipif(uproot4._util.py2, reason="Bug in Python 2")
def test_TRefArray():
with uproot4.open(skhep_testdata.data_path("uproot-issue513.root"))["Delphes"] as t:
array = t["GenJet.Particles"].array(entry_stop=1, library="np")[0]
Expand Down Expand Up @@ -85,7 +84,6 @@ def test_TRefArray():
]


@pytest.mark.skipif(uproot4._util.py2, reason="Bug in Python 2")
def test_awkward_TRefArray():
awkward1 = pytest.importorskip("awkward1")
with uproot4.open(skhep_testdata.data_path("uproot-issue513.root"))["Delphes"] as t:
Expand Down Expand Up @@ -336,7 +334,6 @@ def test_awkward_TRefArray():
]


@pytest.mark.skipif(uproot4._util.py2, reason="Bug in Python 2")
def test_same_names():
with uproot4.open(skhep_testdata.data_path("uproot-issue513.root"))["Delphes"] as t:
one, two = t.values(filter_name="Particle_size")
Expand Down
4 changes: 3 additions & 1 deletion uproot4/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
win = os.name == "nt"


# to silence flake8 F821 errors
if py2:
# to silence flake8 F821 errors
unicode = eval("unicode")
range = eval("xrange")
else:
unicode = None
range = eval("range")


def isint(x):
Expand Down
14 changes: 8 additions & 6 deletions uproot4/behaviors/TBranch.py
Original file line number Diff line number Diff line change
Expand Up @@ -1098,7 +1098,9 @@ def iterate(
tree = self.tree

previous_baskets = {}
for sub_entry_start in range(entry_start, entry_stop, entry_step):
for sub_entry_start in uproot4._util.range(
entry_start, entry_stop, entry_step
):
sub_entry_stop = min(sub_entry_start + entry_step, entry_stop)
if sub_entry_stop - sub_entry_start == 0:
continue
Expand Down Expand Up @@ -1728,7 +1730,7 @@ def _regularize_object_path(
object_cache=None,
array_cache=None,
custom_classes=custom_classes,
**options,
**options # NOTE: a comma after **options breaks Python 2
).root_directory
if object_path is None:
trees = [k for k, v in file.classnames().items() if v == "TTree"]
Expand Down Expand Up @@ -1791,7 +1793,7 @@ def iterate(
report=False,
custom_classes=None,
allow_missing=False,
**options
**options # NOTE: a comma after **options breaks Python 2
):
files = _regularize_files(files)
decompression_executor, interpretation_executor = _regularize_executors(
Expand Down Expand Up @@ -1861,7 +1863,7 @@ def concatenate(
report=False,
custom_classes=None,
allow_missing=False,
**options
**options # NOTE: a comma after **options breaks Python 2
):
files = _regularize_files(files)
decompression_executor, interpretation_executor = _regularize_executors(
Expand Down Expand Up @@ -1915,7 +1917,7 @@ def lazy(
report=False,
custom_classes=None,
allow_missing=False,
**options
**options # NOTE: a comma after **options breaks Python 2
):
files = _regularize_files(files)
decompression_executor, interpretation_executor = _regularize_executors(
Expand Down Expand Up @@ -2022,7 +2024,7 @@ def real_filter_branch(branch):
obj, step_size, entry_start, entry_stop, branchid_interpretation
)

for start in range(entry_start, entry_stop, entry_step):
for start in uproot4._util.range(entry_start, entry_stop, entry_step):
stop = min(start + entry_step, entry_stop)
length = stop - start

Expand Down
6 changes: 3 additions & 3 deletions uproot4/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ def _read_nested(
else:
values = numpy.empty(length, dtype=_stl_object_type)
if isinstance(model, AsContainer):
for i in range(length):
for i in uproot4._util.range(length):
values[i] = model.read(
chunk, cursor, context, file, selffile, parent, header=header
)
else:
for i in range(length):
for i in uproot4._util.range(length):
values[i] = model.read(chunk, cursor, context, file, selffile, parent)
return values

Expand Down Expand Up @@ -1121,7 +1121,7 @@ def __eq__(self, other):

def tolist(self):
out = {}
for i in range(len(self)):
for i in uproot4._util.range(len(self)):
x = self._values[i]
if isinstance(x, (Container, numpy.ndarray)):
out[self._keys[i]] = x.tolist()
Expand Down
6 changes: 3 additions & 3 deletions uproot4/interpretation/identify.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,15 +726,15 @@ def _parse_node(tokens, i, typename, file, quote, header, inner_header):

if quote:
cls = "c({0})".format(repr(classname))
for x in range(pointers):
for x in uproot4._util.range(pointers):
cls = "uproot4.containers.AsPointer({0})".format(cls)
elif file is None:
cls = uproot4.classes[classname]
for x in range(pointers):
for x in uproot4._util.range(pointers):
cls = uproot4.containers.AsPointer(cls)
else:
cls = file.class_named(classname)
for x in range(pointers):
for x in uproot4._util.range(pointers):
cls = uproot4.containers.AsPointer(cls)

return i + 1, cls
Expand Down
18 changes: 11 additions & 7 deletions uproot4/interpretation/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def concatenate(self, all_arrays):
return all_arrays

if isinstance(all_arrays[0], (tuple, list)):
keys = range(len(all_arrays[0]))
keys = uproot4._util.range(len(all_arrays[0]))
elif isinstance(all_arrays[0], dict):
keys = list(all_arrays[0])
else:
Expand Down Expand Up @@ -450,7 +450,7 @@ def concatenate(self, all_arrays):
return all_arrays

if isinstance(all_arrays[0], (tuple, list)):
keys = range(len(all_arrays[0]))
keys = uproot4._util.range(len(all_arrays[0]))
elif isinstance(all_arrays[0], dict):
keys = list(all_arrays[0])
else:
Expand Down Expand Up @@ -496,7 +496,7 @@ def _pandas_basic_index(pandas, entry_start, entry_stop):
if hasattr(pandas, "RangeIndex"):
return pandas.RangeIndex(entry_start, entry_stop)
else:
return pandas.Int64Index(range(entry_start, entry_stop))
return pandas.Int64Index(uproot4._util.range(entry_start, entry_stop))


class Pandas(Library):
Expand Down Expand Up @@ -578,7 +578,9 @@ def finalize(self, array, branch, interpretation, entry_start, entry_stop):
names = []
arrays = {}
for n in array.dtype.names:
for tup in itertools.product(*[range(d) for d in array.shape[1:]]):
for tup in itertools.product(
*[uproot4._util.range(d) for d in array.shape[1:]]
):
name = (n + "".join("[" + str(x) + "]" for x in tup),)
names.append(name)
arrays[name] = array[n][(slice(None),) + tup]
Expand All @@ -598,7 +600,9 @@ def finalize(self, array, branch, interpretation, entry_start, entry_stop):
elif len(array.shape) != 1:
names = []
arrays = {}
for tup in itertools.product(*[range(d) for d in array.shape[1:]]):
for tup in itertools.product(
*[uproot4._util.range(d) for d in array.shape[1:]]
):
name = "".join("[" + str(x) + "]" for x in tup)
names.append(name)
arrays[name] = array[(slice(None),) + tup]
Expand Down Expand Up @@ -789,7 +793,7 @@ def concatenate(self, all_arrays):
return all_arrays

if isinstance(all_arrays[0], (tuple, list)):
keys = range(len(all_arrays[0]))
keys = uproot4._util.range(len(all_arrays[0]))
elif isinstance(all_arrays[0], dict):
keys = list(all_arrays[0])
else:
Expand Down Expand Up @@ -845,7 +849,7 @@ def concatenate(self, all_arrays):
return all_arrays

if isinstance(all_arrays[0], (tuple, list)):
keys = range(len(all_arrays[0]))
keys = uproot4._util.range(len(all_arrays[0]))
elif isinstance(all_arrays[0], dict):
keys = list(all_arrays[0])
else:
Expand Down
8 changes: 4 additions & 4 deletions uproot4/interpretation/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,25 +205,25 @@ def final_array(
for basket_num, stop in enumerate(entry_offsets[1:]):
if start <= entry_start and entry_stop <= stop:
basket_array = basket_arrays[basket_num]
for global_i in range(entry_start, entry_stop):
for global_i in uproot4._util.range(entry_start, entry_stop):
local_i = global_i - start
output[global_i - entry_start] = basket_array[local_i]

elif start <= entry_start < stop:
basket_array = basket_arrays[basket_num]
for global_i in range(entry_start, stop):
for global_i in uproot4._util.range(entry_start, stop):
local_i = global_i - start
output[global_i - entry_start] = basket_array[local_i]

elif start <= entry_stop <= stop:
basket_array = basket_arrays[basket_num]
for global_i in range(start, entry_stop):
for global_i in uproot4._util.range(start, entry_stop):
local_i = global_i - start
output[global_i - entry_start] = basket_array[local_i]

elif entry_start < stop and start <= entry_stop:
basket_array = basket_arrays[basket_num]
for global_i in range(start, stop):
for global_i in uproot4._util.range(start, stop):
local_i = global_i - start
output[global_i - entry_start] = basket_array[local_i]

Expand Down
6 changes: 4 additions & 2 deletions uproot4/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,9 @@ def _classname_encode_convert(bad_characters):

def _classname_decode_convert(hex_characters):
g = hex_characters.group(1)
return b"".join(chr(int(g[i : i + 2], 16)) for i in range(0, len(g), 2))
return b"".join(
chr(int(g[i : i + 2], 16)) for i in uproot4._util.range(0, len(g), 2)
)


else:
Expand All @@ -604,7 +606,7 @@ def _classname_encode_convert(bad_characters):

def _classname_decode_convert(hex_characters):
g = hex_characters.group(1)
return bytes(int(g[i : i + 2], 16) for i in range(0, len(g), 2))
return bytes(int(g[i : i + 2], 16) for i in uproot4._util.range(0, len(g), 2))


def classname_encode(classname, version=None, unknown=False):
Expand Down
2 changes: 1 addition & 1 deletion uproot4/models/TList.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def read_members(self, chunk, cursor, context, file):
self._members["fSize"] = cursor.field(chunk, _tlist_format1, context)

self._data = []
for i in range(self._members["fSize"]):
for i in uproot4._util.range(self._members["fSize"]):
item = uproot4.deserialization.read_object_any(
chunk, cursor, context, file, self._file, self._parent
)
Expand Down
5 changes: 2 additions & 3 deletions uproot4/models/TObjArray.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ def read_members(self, chunk, cursor, context, file):
self._members["fSize"], self._members["fLowerBound"] = cursor.fields(
chunk, _tobjarray_format1, context
)

self._data = []
for i in range(self._members["fSize"]):
for i in uproot4._util.range(self._members["fSize"]):
item = uproot4.deserialization.read_object_any(
chunk, cursor, context, file, self._file, self._parent
)
Expand Down Expand Up @@ -102,7 +101,7 @@ def read_members(self, chunk, cursor, context, file):
)

self._data = []
for i in range(self._members["fSize"]):
for i in uproot4._util.range(self._members["fSize"]):
item = uproot4.deserialization.read_object_any(
chunk,
cursor,
Expand Down
13 changes: 8 additions & 5 deletions uproot4/reading.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def open(
object_cache=100,
array_cache="100 MB",
custom_classes=None,
**options
**options # NOTE: a comma after **options breaks Python 2
):
"""
Args:
Expand Down Expand Up @@ -100,7 +100,7 @@ def open(
object_cache=object_cache,
array_cache=array_cache,
custom_classes=custom_classes,
**options
**options # NOTE: a comma after **options breaks Python 2
)

if object_path is None:
Expand Down Expand Up @@ -262,7 +262,7 @@ def __init__(
object_cache=100,
array_cache="100 MB",
custom_classes=None,
**options
**options # NOTE: a comma after **options breaks Python 2
):
self._file_path = file_path
self.object_cache = object_cache
Expand All @@ -282,7 +282,10 @@ def __init__(
Source, file_path = uproot4._util.file_path_to_source_class(
file_path, self._options
)
self._source = Source(file_path, **self._options)
self._source = Source(
file_path,
**self._options # NOTE: a comma after **options breaks Python 2
)

self.hook_before_get_chunks()

Expand Down Expand Up @@ -1059,7 +1062,7 @@ def __init__(self, path, cursor, context, file, parent):
)

self._keys = []
for i in range(num_keys):
for i in uproot4._util.range(num_keys):
key = ReadOnlyKey(
keys_chunk, keys_cursor, {}, file, self, read_strings=True
)
Expand Down
4 changes: 3 additions & 1 deletion uproot4/source/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,9 @@ def debug(

formatter = u"{{0:>{0}.{0}s}}".format(dtype.itemsize * 4 - 1)

for line_start in range(0, int(numpy.ceil(len(data) / 20.0)) * 20, 20):
for line_start in uproot4._util.range(
0, int(numpy.ceil(len(data) / 20.0)) * 20, 20
):
line_data = data[line_start : line_start + 20]

prefix = u""
Expand Down
5 changes: 3 additions & 2 deletions uproot4/source/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def __init__(self, file_path, **options):

self._file_path = file_path
self._executor = uproot4.source.futures.ResourceThreadPoolExecutor(
[FileResource(file_path) for x in range(num_workers)]
[FileResource(file_path) for x in uproot4._util.range(num_workers)]
)
self._num_bytes = os.path.getsize(self._file_path)

Expand All @@ -80,7 +80,8 @@ def __init__(self, file_path, **options):
opts = dict(options)
opts["num_workers"] = num_fallback_workers
self._fallback = uproot4.source.file.MultithreadedFileSource(
file_path, **opts
file_path,
**opts # NOTE: a comma after **opts breaks Python 2
)

def __repr__(self):
Expand Down
2 changes: 1 addition & 1 deletion uproot4/source/futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def __init__(self, num_workers=None):

self._work_queue = queue.Queue()
self._workers = []
for x in range(num_workers):
for x in uproot4._util.range(num_workers):
self._workers.append(Worker(self._work_queue))
for worker in self._workers:
worker.start()
Expand Down
Loading

0 comments on commit 9ff06a4

Please sign in to comment.