Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1fe19c6

Browse files
committed
Add *_impl file
1 parent fe8f1c7 commit 1fe19c6

File tree

4 files changed

+89
-79
lines changed

4 files changed

+89
-79
lines changed

numpy/lib/_array_utils_impl.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from numpy.core import asarray
2+
from numpy.core.numeric import normalize_axis_tuple, normalize_axis_index
3+
4+
5+
__all__ = ["byte_bounds", "normalize_axis_tuple", "normalize_axis_index"]
6+
7+
8+
def byte_bounds(a):
9+
"""
10+
Returns pointers to the end-points of an array.
11+
12+
Parameters
13+
----------
14+
a : ndarray
15+
Input array. It must conform to the Python-side of the array
16+
interface.
17+
18+
Returns
19+
-------
20+
(low, high) : tuple of 2 integers
21+
The first integer is the first byte of the array, the second
22+
integer is just past the last byte of the array. If `a` is not
23+
contiguous it will not use every byte between the (`low`, `high`)
24+
values.
25+
26+
Examples
27+
--------
28+
>>> I = np.eye(2, dtype='f'); I.dtype
29+
dtype('float32')
30+
>>> low, high = np.byte_bounds(I)
31+
>>> high - low == I.size*I.itemsize
32+
True
33+
>>> I = np.eye(2); I.dtype
34+
dtype('float64')
35+
>>> low, high = np.byte_bounds(I)
36+
>>> high - low == I.size*I.itemsize
37+
True
38+
39+
"""
40+
ai = a.__array_interface__
41+
a_data = ai['data'][0]
42+
astrides = ai['strides']
43+
ashape = ai['shape']
44+
bytes_a = asarray(a).dtype.itemsize
45+
46+
a_low = a_high = a_data
47+
if astrides is None:
48+
# contiguous case
49+
a_high += a.size * bytes_a
50+
else:
51+
for shape, stride in zip(ashape, astrides):
52+
if stride < 0:
53+
a_low += (shape-1)*stride
54+
else:
55+
a_high += (shape-1)*stride
56+
a_high += bytes_a
57+
return a_low, a_high

numpy/lib/_array_utils_impl.pyi

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import Any, Iterable, Tuple
2+
3+
from numpy import ndarray, generic
4+
5+
# NOTE: In practice `byte_bounds` can (potentially) take any object
6+
# implementing the `__array_interface__` protocol. The caveat is
7+
# that certain keys, marked as optional in the spec, must be present for
8+
# `byte_bounds`. This concerns `"strides"` and `"data"`.
9+
def byte_bounds(a: generic | ndarray[Any, Any]) -> tuple[int, int]: ...
10+
11+
def normalize_axis_tuple(
12+
axis: int | Iterable[int],
13+
ndim: int = ...,
14+
argname: None | str = ...,
15+
allow_duplicate: None | bool = ...,
16+
) -> Tuple[int, int]: ...
17+
18+
def normalize_axis_index(
19+
axis: int = ...,
20+
ndim: int = ...,
21+
msg_prefix: None | str = ...,
22+
) -> int: ...

numpy/lib/array_utils.py

Lines changed: 5 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,5 @@
1-
from numpy.core import asarray
2-
from numpy.core.numeric import normalize_axis_tuple, normalize_axis_index
3-
4-
5-
__all__ = ["byte_bounds", "normalize_axis_tuple", "normalize_axis_index"]
6-
7-
8-
def byte_bounds(a):
9-
"""
10-
Returns pointers to the end-points of an array.
11-
12-
Parameters
13-
----------
14-
a : ndarray
15-
Input array. It must conform to the Python-side of the array
16-
interface.
17-
18-
Returns
19-
-------
20-
(low, high) : tuple of 2 integers
21-
The first integer is the first byte of the array, the second
22-
integer is just past the last byte of the array. If `a` is not
23-
contiguous it will not use every byte between the (`low`, `high`)
24-
values.
25-
26-
Examples
27-
--------
28-
>>> I = np.eye(2, dtype='f'); I.dtype
29-
dtype('float32')
30-
>>> low, high = np.byte_bounds(I)
31-
>>> high - low == I.size*I.itemsize
32-
True
33-
>>> I = np.eye(2); I.dtype
34-
dtype('float64')
35-
>>> low, high = np.byte_bounds(I)
36-
>>> high - low == I.size*I.itemsize
37-
True
38-
39-
"""
40-
ai = a.__array_interface__
41-
a_data = ai['data'][0]
42-
astrides = ai['strides']
43-
ashape = ai['shape']
44-
bytes_a = asarray(a).dtype.itemsize
45-
46-
a_low = a_high = a_data
47-
if astrides is None:
48-
# contiguous case
49-
a_high += a.size * bytes_a
50-
else:
51-
for shape, stride in zip(ashape, astrides):
52-
if stride < 0:
53-
a_low += (shape-1)*stride
54-
else:
55-
a_high += (shape-1)*stride
56-
a_high += bytes_a
57-
return a_low, a_high
1+
from ._array_utils_impl import (
2+
byte_bounds,
3+
normalize_axis_index,
4+
normalize_axis_tuple,
5+
)

numpy/lib/array_utils.pyi

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,5 @@
1-
from typing import Any, Iterable, Tuple
2-
3-
from numpy import ndarray, generic
4-
5-
# NOTE: In practice `byte_bounds` can (potentially) take any object
6-
# implementing the `__array_interface__` protocol. The caveat is
7-
# that certain keys, marked as optional in the spec, must be present for
8-
# `byte_bounds`. This concerns `"strides"` and `"data"`.
9-
def byte_bounds(a: generic | ndarray[Any, Any]) -> tuple[int, int]: ...
10-
11-
def normalize_axis_tuple(
12-
axis: int | Iterable[int],
13-
ndim: int = ...,
14-
argname: None | str = ...,
15-
allow_duplicate: None | bool = ...,
16-
) -> Tuple[int, int]: ...
17-
18-
def normalize_axis_index(
19-
axis: int = ...,
20-
ndim: int = ...,
21-
msg_prefix: None | str = ...,
22-
) -> int: ...
1+
from ._array_utils_impl import (
2+
byte_bounds as byte_bounds,
3+
normalize_axis_index as normalize_axis_index,
4+
normalize_axis_tuple as normalize_axis_tuple,
5+
)

0 commit comments

Comments
 (0)