Skip to content

Commit fec5eee

Browse files
authored
refactor: ak._util (#1848)
* refactor: simplify `isint`, remove `isnum` * refactor: rename `isint` to `is_integer` * refactor: remove `isstr` * chore: correct pylint complaints
1 parent fc25886 commit fec5eee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+135
-143
lines changed

src/awkward/_slicing.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ def prepare_advanced_indexing(items):
3838
list, # of strings
3939
ak.contents.ListOffsetArray,
4040
ak.contents.IndexedOptionArray,
41+
str,
4142
),
4243
)
43-
or ak._util.isstr(item)
4444
or item is np.newaxis
4545
or item is Ellipsis
4646
):
@@ -126,13 +126,13 @@ def prepare_advanced_indexing(items):
126126

127127

128128
def normalise_item(item, nplike):
129-
if ak._util.isint(item):
129+
if ak._util.is_integer(item):
130130
return int(item)
131131

132132
elif isinstance(item, slice):
133133
return item
134134

135-
elif ak._util.isstr(item):
135+
elif isinstance(item, str):
136136
return item
137137

138138
elif item is np.newaxis:
@@ -160,7 +160,7 @@ def normalise_item(item, nplike):
160160
elif ak._util.is_sized_iterable(item) and len(item) == 0:
161161
return nplike.empty(0, dtype=np.int64)
162162

163-
elif ak._util.is_sized_iterable(item) and all(ak._util.isstr(x) for x in item):
163+
elif ak._util.is_sized_iterable(item) and all(isinstance(x, str) for x in item):
164164
return list(item)
165165

166166
elif ak._util.is_sized_iterable(item):

src/awkward/_typetracer.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def shape(self):
254254

255255
@shape.setter
256256
def shape(self, value):
257-
if ak._util.isint(value):
257+
if ak._util.is_integer(value):
258258
value = (value,)
259259
elif value is None or isinstance(value, (UnknownLengthType, UnknownScalar)):
260260
value = (UnknownLength,)
@@ -347,7 +347,7 @@ def __getitem__(self, where):
347347
missing = max(0, len(self._shape) - (len(before) + len(after)))
348348
where = before + (slice(None, None, None),) * missing + after
349349

350-
if ak._util.isint(where):
350+
if ak._util.is_integer(where):
351351
if len(self._shape) == 1:
352352
if where == 0:
353353
return UnknownScalar(self._dtype)
@@ -391,7 +391,7 @@ def __getitem__(self, where):
391391
shapes = []
392392
for j in range(num_basic, len(where)):
393393
wh = where[j]
394-
if ak._util.isint(wh):
394+
if ak._util.is_integer(wh):
395395
shapes.append(numpy.array(0))
396396
elif hasattr(wh, "dtype") and hasattr(wh, "shape"):
397397
sh = [
@@ -416,7 +416,7 @@ def __getitem__(self, where):
416416
elif (
417417
isinstance(where, tuple)
418418
and len(where) > 0
419-
and (ak._util.isint(where[0]) or isinstance(where[0], slice))
419+
and (ak._util.is_integer(where[0]) or isinstance(where[0], slice))
420420
):
421421
head, tail = where[0], where[1:]
422422
next = self.__getitem__(head)
@@ -466,8 +466,8 @@ def reshape(self, *args):
466466
args = args[0]
467467

468468
assert len(args) != 0
469-
assert ak._util.isint(args[0]) or isinstance(args[0], UnknownLengthType)
470-
assert all(ak._util.isint(x) for x in args[1:])
469+
assert ak._util.is_integer(args[0]) or isinstance(args[0], UnknownLengthType)
470+
assert all(ak._util.is_integer(x) for x in args[1:])
471471
assert all(x >= 0 for x in args[1:])
472472

473473
return TypeTracerArray(self._dtype, (UnknownLength,) + args[1:])
@@ -587,7 +587,11 @@ def arange(self, *args, **kwargs):
587587
elif len(args) == 3:
588588
start, stop, step = args[0], args[1], args[2]
589589

590-
if ak._util.isint(start) and ak._util.isint(stop) and ak._util.isint(step):
590+
if (
591+
ak._util.is_integer(start)
592+
and ak._util.is_integer(stop)
593+
and ak._util.is_integer(step)
594+
):
591595
length = max(0, (stop - start + (step - (1 if step > 0 else -1))) // step)
592596

593597
return TypeTracerArray(kwargs["dtype"], (length,))

src/awkward/_util.py

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,8 @@ def is_sized_iterable(obj):
6868
return isinstance(obj, Iterable) and isinstance(obj, Sized)
6969

7070

71-
def isint(x):
72-
return isinstance(x, (int, numbers.Integral, np.integer)) and not isinstance(
73-
x, (bool, np.bool_)
74-
)
75-
76-
77-
def isnum(x):
78-
return isinstance(x, (int, float, numbers.Real, np.number)) and not isinstance(
79-
x, (bool, np.bool_)
80-
)
81-
82-
83-
def isstr(x):
84-
return isinstance(x, str)
71+
def is_integer(x):
72+
return isinstance(x, numbers.Integral) and not isinstance(x, bool)
8573

8674

8775
def tobytes(array):
@@ -153,12 +141,12 @@ def overlay_behavior(behavior: dict | None) -> collections.abc.Mapping:
153141
def arrayclass(layout, behavior):
154142
behavior = overlay_behavior(behavior)
155143
arr = layout.parameter("__array__")
156-
if isstr(arr):
144+
if isinstance(arr, str):
157145
cls = behavior.get(arr)
158146
if isinstance(cls, type) and issubclass(cls, ak.highlevel.Array):
159147
return cls
160148
deeprec = layout.purelist_parameter("__record__")
161-
if isstr(deeprec):
149+
if isinstance(deeprec, str):
162150
cls = behavior.get(("*", deeprec))
163151
if isinstance(cls, type) and issubclass(cls, ak.highlevel.Array):
164152
return cls
@@ -181,11 +169,11 @@ def custom_cast(obj, behavior):
181169
def custom_broadcast(layout, behavior):
182170
behavior = overlay_behavior(behavior)
183171
custom = layout.parameter("__array__")
184-
if not isstr(custom):
172+
if not isinstance(custom, str):
185173
custom = layout.parameter("__record__")
186-
if not isstr(custom):
174+
if not isinstance(custom, str):
187175
custom = layout.purelist_parameter("__record__")
188-
if isstr(custom):
176+
if isinstance(custom, str):
189177
for key, fcn in behavior.items():
190178
if (
191179
isinstance(key, tuple)
@@ -202,9 +190,9 @@ def custom_ufunc(ufunc, layout, behavior):
202190

203191
behavior = overlay_behavior(behavior)
204192
custom = layout.parameter("__array__")
205-
if not isstr(custom):
193+
if not isinstance(custom, str):
206194
custom = layout.parameter("__record__")
207-
if isstr(custom):
195+
if isinstance(custom, str):
208196
for key, fcn in behavior.items():
209197
if (
210198
isinstance(key, tuple)
@@ -219,12 +207,12 @@ def custom_ufunc(ufunc, layout, behavior):
219207
def numba_array_typer(layouttype, behavior):
220208
behavior = overlay_behavior(behavior)
221209
arr = layouttype.parameters.get("__array__")
222-
if isstr(arr):
210+
if isinstance(arr, str):
223211
typer = behavior.get(("__numba_typer__", arr))
224212
if callable(typer):
225213
return typer
226214
deeprec = layouttype.parameters.get("__record__")
227-
if isstr(deeprec):
215+
if isinstance(deeprec, str):
228216
typer = behavior.get(("__numba_typer__", "*", deeprec))
229217
if callable(typer):
230218
return typer
@@ -234,12 +222,12 @@ def numba_array_typer(layouttype, behavior):
234222
def numba_array_lower(layouttype, behavior):
235223
behavior = overlay_behavior(behavior)
236224
arr = layouttype.parameters.get("__array__")
237-
if isstr(arr):
225+
if isinstance(arr, str):
238226
lower = behavior.get(("__numba_lower__", arr))
239227
if callable(lower):
240228
return lower
241229
deeprec = layouttype.parameters.get("__record__")
242-
if isstr(deeprec):
230+
if isinstance(deeprec, str):
243231
lower = behavior.get(("__numba_lower__", "*", deeprec))
244232
if callable(lower):
245233
return lower
@@ -249,7 +237,7 @@ def numba_array_lower(layouttype, behavior):
249237
def recordclass(layout, behavior):
250238
behavior = overlay_behavior(behavior)
251239
rec = layout.parameter("__record__")
252-
if isstr(rec):
240+
if isinstance(rec, str):
253241
cls = behavior.get(rec)
254242
if isinstance(cls, type) and issubclass(cls, ak.highlevel.Record):
255243
return cls
@@ -259,7 +247,7 @@ def recordclass(layout, behavior):
259247
def reducer_recordclass(reducer, layout, behavior):
260248
behavior = overlay_behavior(behavior)
261249
rec = layout.parameter("__record__")
262-
if isstr(rec):
250+
if isinstance(rec, str):
263251
return behavior.get((reducer.highlevel_function(), rec))
264252

265253

@@ -271,8 +259,8 @@ def typestrs(behavior):
271259
isinstance(key, tuple)
272260
and len(key) == 2
273261
and key[0] == "__typestr__"
274-
and isstr(key[1])
275-
and isstr(typestr)
262+
and isinstance(key[1], str)
263+
and isinstance(typestr, str)
276264
):
277265
out[key[1]] = typestr
278266
return out
@@ -296,7 +284,7 @@ def gettypestr(parameters, typestrs):
296284
def numba_record_typer(layouttype, behavior):
297285
behavior = overlay_behavior(behavior)
298286
rec = layouttype.parameters.get("__record__")
299-
if isstr(rec):
287+
if isinstance(rec, str):
300288
typer = behavior.get(("__numba_typer__", rec))
301289
if callable(typer):
302290
return typer
@@ -306,7 +294,7 @@ def numba_record_typer(layouttype, behavior):
306294
def numba_record_lower(layouttype, behavior):
307295
behavior = overlay_behavior(behavior)
308296
rec = layouttype.parameters.get("__record__")
309-
if isstr(rec):
297+
if isinstance(rec, str):
310298
lower = behavior.get(("__numba_lower__", rec))
311299
if callable(lower):
312300
return lower
@@ -335,7 +323,7 @@ def overload(behavior, signature):
335323
def numba_attrs(layouttype, behavior):
336324
behavior = overlay_behavior(behavior)
337325
rec = layouttype.parameters.get("__record__")
338-
if isstr(rec):
326+
if isinstance(rec, str):
339327
for key, typer in behavior.items():
340328
if (
341329
isinstance(key, tuple)
@@ -350,7 +338,7 @@ def numba_attrs(layouttype, behavior):
350338
def numba_methods(layouttype, behavior):
351339
behavior = overlay_behavior(behavior)
352340
rec = layouttype.parameters.get("__record__")
353-
if isstr(rec):
341+
if isinstance(rec, str):
354342
for key, typer in behavior.items():
355343
if (
356344
isinstance(key, tuple)
@@ -369,7 +357,7 @@ def numba_unaryops(unaryop, left, behavior):
369357

370358
if isinstance(left, ak._connect.numba.layout.ContentType):
371359
left = left.parameters.get("__record__")
372-
if not isstr(left):
360+
if not isinstance(left, str):
373361
done = True
374362

375363
if not done:
@@ -391,12 +379,12 @@ def numba_binops(binop, left, right, behavior):
391379

392380
if isinstance(left, ak._connect.numba.layout.ContentType):
393381
left = left.parameters.get("__record__")
394-
if not isstr(left):
382+
if not isinstance(left, str):
395383
done = True
396384

397385
if isinstance(right, ak._connect.numba.layout.ContentType):
398386
right = right.parameters.get("__record__")
399-
if not isstr(right):
387+
if not isinstance(right, str):
400388
done = True
401389

402390
if not done:

src/awkward/contents/bitmaskedarray.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def __init__(
8282
)
8383
)
8484
if not isinstance(length, ak._typetracer.UnknownLengthType):
85-
if not (ak._util.isint(length) and length >= 0):
85+
if not (ak._util.is_integer(length) and length >= 0):
8686
raise ak._errors.wrap_error(
8787
TypeError(
8888
"{} 'length' must be a non-negative integer, not {}".format(
@@ -388,7 +388,7 @@ def _getitem_next(self, head, tail, advanced):
388388
):
389389
return self.toByteMaskedArray()._getitem_next(head, tail, advanced)
390390

391-
elif ak._util.isstr(head):
391+
elif isinstance(head, str):
392392
return self._getitem_next_field(head, tail, advanced)
393393

394394
elif isinstance(head, list):

src/awkward/contents/bytemaskedarray.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ def _getitem_next(self, head, tail, advanced):
427427
)
428428
return out2.simplify_optiontype()
429429

430-
elif ak._util.isstr(head):
430+
elif isinstance(head, str):
431431
return self._getitem_next_field(head, tail, advanced)
432432

433433
elif isinstance(head, list):
@@ -541,7 +541,7 @@ def num(self, axis, depth=0):
541541
posaxis = self.axis_wrap_if_negative(axis)
542542
if posaxis == depth:
543543
out = self.length
544-
if ak._util.isint(out):
544+
if ak._util.is_integer(out):
545545
return np.int64(out)
546546
else:
547547
return out

src/awkward/contents/content.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def form_with_key(self, form_key="node{id}", id_start=0):
101101
def getkey(layout):
102102
return None
103103

104-
elif ak._util.isstr(form_key):
104+
elif isinstance(form_key, str):
105105

106106
def getkey(layout):
107107
out = form_key.format(id=hold_id[0])
@@ -170,7 +170,7 @@ def to_buffers(
170170
TypeError("cannot call 'to_buffers' on an array without concrete data")
171171
)
172172

173-
if ak._util.isstr(buffer_key):
173+
if isinstance(buffer_key, str):
174174

175175
def getkey(layout, form, attribute):
176176
return buffer_key.format(form_key=form.form_key, attribute=attribute)
@@ -318,7 +318,7 @@ def _getitem_next_field(self, head, tail, advanced: ak.index.Index | None):
318318
def _getitem_next_fields(self, head, tail, advanced: ak.index.Index | None):
319319
only_fields, not_fields = [], []
320320
for x in tail:
321-
if ak._util.isstr(x) or isinstance(x, list):
321+
if isinstance(x, (str, list)):
322322
only_fields.append(x)
323323
else:
324324
not_fields.append(x)
@@ -527,7 +527,7 @@ def __getitem__(self, where):
527527
return self._getitem(where)
528528

529529
def _getitem(self, where):
530-
if ak._util.isint(where):
530+
if ak._util.is_integer(where):
531531
return self._getitem_at(where)
532532

533533
elif isinstance(where, slice) and where.step is None:
@@ -536,7 +536,7 @@ def _getitem(self, where):
536536
elif isinstance(where, slice):
537537
return self._getitem((where,))
538538

539-
elif ak._util.isstr(where):
539+
elif isinstance(where, str):
540540
return self._getitem_field(where)
541541

542542
elif where is np.newaxis:
@@ -626,7 +626,7 @@ def _getitem(self, where):
626626
return self._carry(ak.index.Index64.empty(0, self._nplike), allow_lazy=True)
627627

628628
elif ak._util.is_sized_iterable(where) and all(
629-
ak._util.isstr(x) for x in where
629+
isinstance(x, str) for x in where
630630
):
631631
return self._getitem_fields(where)
632632

@@ -1627,8 +1627,8 @@ def to_json(
16271627
isinstance(complex_record_fields, Sized)
16281628
and isinstance(complex_record_fields, Iterable)
16291629
and len(complex_record_fields) == 2
1630-
and ak._util.isstr(complex_record_fields[0])
1631-
and ak._util.isstr(complex_record_fields[1])
1630+
and isinstance(complex_record_fields[0], str)
1631+
and isinstance(complex_record_fields[1], str)
16321632
):
16331633
complex_real_string, complex_imag_string = complex_record_fields
16341634
else:

src/awkward/contents/emptyarray.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def _getitem_next(self, head, tail, advanced):
131131
elif isinstance(head, slice):
132132
raise ak._errors.index_error(self, head, "array is empty")
133133

134-
elif ak._util.isstr(head):
134+
elif isinstance(head, str):
135135
return self._getitem_next_field(head, tail, advanced)
136136

137137
elif isinstance(head, list):
@@ -163,7 +163,7 @@ def num(self, axis, depth=0):
163163

164164
if posaxis == depth:
165165
out = self.length
166-
if ak._util.isint(out):
166+
if ak._util.is_integer(out):
167167
return np.int64(out)
168168
else:
169169
return out

0 commit comments

Comments
 (0)