FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

BUG: Re-enable overriding functions in the `np.strings` module. by roytsmart · Pull Request #28741 · numpy/numpy · GitHub

/ numpy Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
1 change: 1 addition & 0 deletions doc/release/upcoming_changes/28741.change.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Re-enable overriding functions in the :mod:`numpy.strings` module.
80 changes: 79 additions & 1 deletion numpy/_core/strings.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
"""

import sys
import functools
import numpy as np
from numpy import (
equal, not_equal, less, less_equal, greater, greater_equal,
add, multiply as _multiply_ufunc,
)
from numpy._core.multiarray import _vec_string
from numpy._core.overrides import set_module
from numpy._core.overrides import set_module, array_function_dispatch
from numpy._core.umath import (
isalpha,
isdigit,
Expand Down Expand Up @@ -84,6 +85,9 @@ def _override___module__():

MAX = np.iinfo(np.int64).max

array_function_dispatch = functools.partial(
array_function_dispatch, module='numpy.strings')


def _get_num_chars(a):
"""
Expand Down Expand Up @@ -130,7 +134,12 @@ def _clean_args(*args):
return newargs


def _multiply_dispatcher(a, i):
return (a,)


@set_module("numpy.strings")
@array_function_dispatch(_multiply_dispatcher)
def multiply(a, i):
"""
Return (a * i), that is string multiple concatenation,
Expand Down Expand Up @@ -194,7 +203,12 @@ def multiply(a, i):
return _multiply_ufunc(a, i, out=out)


def _mod_dispatcher(a, values):
return (a, values)


@set_module("numpy.strings")
@array_function_dispatch(_mod_dispatcher)
def mod(a, values):
"""
Return (a % i), that is pre-Python 2.6 string formatting
Expand Down Expand Up @@ -507,7 +521,12 @@ def endswith(a, suffix, start=0, end=None):
return _endswith_ufunc(a, suffix, start, end)


def _code_dispatcher(a, encoding=None, errors=None):
return (a,)


@set_module("numpy.strings")
@array_function_dispatch(_code_dispatcher)
def decode(a, encoding=None, errors=None):
r"""
Calls :meth:`bytes.decode` element-wise.
Expand Down Expand Up @@ -556,6 +575,7 @@ def decode(a, encoding=None, errors=None):


@set_module("numpy.strings")
@array_function_dispatch(_code_dispatcher)
def encode(a, encoding=None, errors=None):
"""
Calls :meth:`str.encode` element-wise.
Expand Down Expand Up @@ -600,7 +620,12 @@ def encode(a, encoding=None, errors=None):
np.bytes_(b''))


def _expandtabs_dispatcher(a, tabsize=None):
return (a,)


@set_module("numpy.strings")
@array_function_dispatch(_expandtabs_dispatcher)
def expandtabs(a, tabsize=8):
"""
Return a copy of each string element where all tab characters are
Expand Down Expand Up @@ -652,7 +677,12 @@ def expandtabs(a, tabsize=8):
return _expandtabs(a, tabsize, out=out)


def _just_dispatcher(a, width, fillchar=None):
return (a,)


@set_module("numpy.strings")
@array_function_dispatch(_just_dispatcher)
def center(a, width, fillchar=' '):
"""
Return a copy of `a` with its elements centered in a string of
Expand Down Expand Up @@ -721,6 +751,7 @@ def center(a, width, fillchar=' '):


@set_module("numpy.strings")
@array_function_dispatch(_just_dispatcher)
def ljust(a, width, fillchar=' '):
"""
Return an array with the elements of `a` left-justified in a
Expand Down Expand Up @@ -785,6 +816,7 @@ def ljust(a, width, fillchar=' '):


@set_module("numpy.strings")
@array_function_dispatch(_just_dispatcher)
def rjust(a, width, fillchar=' '):
"""
Return an array with the elements of `a` right-justified in a
Expand Down Expand Up @@ -848,7 +880,12 @@ def rjust(a, width, fillchar=' '):
return _rjust(a, width, fillchar, out=out)


def _zfill_dispatcher(a, width):
return (a,)


@set_module("numpy.strings")
@array_function_dispatch(_zfill_dispatcher)
def zfill(a, width):
"""
Return the numeric string left-filled with zeros. A leading
Expand Down Expand Up @@ -1033,7 +1070,12 @@ def strip(a, chars=None):
return _strip_chars(a, chars)


def _unary_op_dispatcher(a):
return (a,)


@set_module("numpy.strings")
@array_function_dispatch(_unary_op_dispatcher)
def upper(a):
"""
Return an array with the elements converted to uppercase.
Expand Down Expand Up @@ -1071,6 +1113,7 @@ def upper(a):


@set_module("numpy.strings")
@array_function_dispatch(_unary_op_dispatcher)
def lower(a):
"""
Return an array with the elements converted to lowercase.
Expand Down Expand Up @@ -1108,6 +1151,7 @@ def lower(a):


@set_module("numpy.strings")
@array_function_dispatch(_unary_op_dispatcher)
def swapcase(a):
"""
Return element-wise a copy of the string with
Expand Down Expand Up @@ -1148,6 +1192,7 @@ def swapcase(a):


@set_module("numpy.strings")
@array_function_dispatch(_unary_op_dispatcher)
def capitalize(a):
"""
Return a copy of ``a`` with only the first character of each element
Expand Down Expand Up @@ -1188,6 +1233,7 @@ def capitalize(a):


@set_module("numpy.strings")
@array_function_dispatch(_unary_op_dispatcher)
def title(a):
"""
Return element-wise title cased version of string or unicode.
Expand Down Expand Up @@ -1229,7 +1275,12 @@ def title(a):
return _vec_string(a_arr, a_arr.dtype, 'title')


def _replace_dispatcher(a, old, new, count=None):
return (a,)


@set_module("numpy.strings")
@array_function_dispatch(_replace_dispatcher)
def replace(a, old, new, count=-1):
"""
For each element in ``a``, return a copy of the string with
Expand Down Expand Up @@ -1293,6 +1344,11 @@ def replace(a, old, new, count=-1):
return _replace(arr, old, new, counts, out=out)


def _join_dispatcher(sep, seq):
return (sep, seq)


@array_function_dispatch(_join_dispatcher)
def _join(sep, seq):
"""
Return a string which is the concatenation of the strings in the
Expand Down Expand Up @@ -1329,6 +1385,11 @@ def _join(sep, seq):
_vec_string(sep, np.object_, 'join', (seq,)), seq)


def _split_dispatcher(a, sep=None, maxsplit=None):
return (a,)


@array_function_dispatch(_split_dispatcher)
def _split(a, sep=None, maxsplit=None):
"""
For each element in `a`, return a list of the words in the
Expand Down Expand Up @@ -1373,6 +1434,7 @@ def _split(a, sep=None, maxsplit=None):
a, np.object_, 'split', [sep] + _clean_args(maxsplit))


@array_function_dispatch(_split_dispatcher)
def _rsplit(a, sep=None, maxsplit=None):
"""
For each element in `a`, return a list of the words in the
Expand Down Expand Up @@ -1418,6 +1480,11 @@ def _rsplit(a, sep=None, maxsplit=None):
a, np.object_, 'rsplit', [sep] + _clean_args(maxsplit))


def _splitlines_dispatcher(a, keepends=None):
return (a,)


@array_function_dispatch(_splitlines_dispatcher)
def _splitlines(a, keepends=None):
"""
For each element in `a`, return a list of the lines in the
Expand Down Expand Up @@ -1455,7 +1522,12 @@ def _splitlines(a, keepends=None):
a, np.object_, 'splitlines', _clean_args(keepends))


def _partition_dispatcher(a, sep):
return (a,)


@set_module("numpy.strings")
@array_function_dispatch(_partition_dispatcher)
def partition(a, sep):
"""
Partition each element in ``a`` around ``sep``.
Expand Down Expand Up @@ -1524,6 +1596,7 @@ def partition(a, sep):


@set_module("numpy.strings")
@array_function_dispatch(_partition_dispatcher)
def rpartition(a, sep):
"""
Partition (split) each element around the right-most separator.
Expand Down Expand Up @@ -1592,7 +1665,12 @@ def rpartition(a, sep):
a, sep, pos, out=(out["f0"], out["f1"], out["f2"]))


def _translate_dispatcher(a, table, deletechars=None):
return (a,)


@set_module("numpy.strings")
@array_function_dispatch(_translate_dispatcher)
def translate(a, table, deletechars=None):
"""
For each element in `a`, return a copy of the string where all
Expand Down
68 changes: 68 additions & 0 deletions numpy/_core/tests/test_strings.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1367,3 +1367,71 @@ def test_replace_broadcasting(self, dt):
dtype=dt))
r3 = np.strings.replace(a, ["0", "0,0", "0,0,0"], "X")
assert_array_equal(r3, np.array(["X,X,X", "X,0", "X"], dtype=dt))


class TestOverride:
@classmethod
def setup_class(cls):
class Override:

def __array_function__(self, *args, **kwargs):
return "function"

def __array_ufunc__(self, *args, **kwargs):
return "ufunc"

cls.override = Override()

@pytest.mark.parametrize("func, kwargs", [
(np.strings.center, dict(width=10)),
(np.strings.capitalize, {}),
(np.strings.decode, {}),
(np.strings.encode, {}),
(np.strings.expandtabs, {}),
(np.strings.ljust, dict(width=10)),
(np.strings.lower, {}),
(np.strings.mod, dict(values=2)),
(np.strings.multiply, dict(i=2)),
(np.strings.partition, dict(sep="foo")),
(np.strings.rjust, dict(width=10)),
(np.strings.rpartition, dict(sep="foo")),
(np.strings.swapcase, {}),
(np.strings.title, {}),
(np.strings.translate, dict(table=None)),
(np.strings.upper, {}),
(np.strings.zfill, dict(width=10)),
])
def test_override_function(self, func, kwargs):
assert func(self.override, **kwargs) == "function"

@pytest.mark.parametrize("func, args, kwargs", [
(np.strings.add, (None, ), {}),
(np.strings.lstrip, (), {}),
(np.strings.rstrip, (), {}),
(np.strings.strip, (), {}),
(np.strings.equal, (None, ), {}),
(np.strings.not_equal, (None, ), {}),
(np.strings.greater_equal, (None, ), {}),
(np.strings.less_equal, (None, ), {}),
(np.strings.greater, (None, ), {}),
(np.strings.less, (None, ), {}),
(np.strings.count, ("foo", ), {}),
(np.strings.endswith, ("foo", ), {}),
(np.strings.find, ("foo", ), {}),
(np.strings.index, ("foo", ), {}),
(np.strings.isalnum, (), {}),
(np.strings.isalpha, (), {}),
(np.strings.isdecimal, (), {}),
(np.strings.isdigit, (), {}),
(np.strings.islower, (), {}),
(np.strings.isnumeric, (), {}),
(np.strings.isspace, (), {}),
(np.strings.istitle, (), {}),
(np.strings.isupper, (), {}),
(np.strings.rfind, ("foo", ), {}),
(np.strings.rindex, ("foo", ), {}),
(np.strings.startswith, ("foo", ), {}),
(np.strings.str_len, (), {}),
])
def test_override_ufunc(self, func, args, kwargs):
assert func(self.override, *args, **kwargs) == "ufunc"

Back | FazBrowse Home | New Git URL