Define functions from existing MaskedArray methods. Parameters ---------- methodname : str Name of the method to transform. reversed : bool, optional Whether to reverse the first two arguments of the method. Default is False.
(methodname: str, reversed: bool = False)
| 7051 | |
| 7052 | |
| 7053 | def _frommethod(methodname: str, reversed: bool = False): |
| 7054 | """ |
| 7055 | Define functions from existing MaskedArray methods. |
| 7056 | |
| 7057 | Parameters |
| 7058 | ---------- |
| 7059 | methodname : str |
| 7060 | Name of the method to transform. |
| 7061 | reversed : bool, optional |
| 7062 | Whether to reverse the first two arguments of the method. Default is False. |
| 7063 | """ |
| 7064 | method = getattr(MaskedArray, methodname) |
| 7065 | assert callable(method) |
| 7066 | |
| 7067 | signature = inspect.signature(method) |
| 7068 | params = list(signature.parameters.values()) |
| 7069 | params[0] = params[0].replace(name="a") # rename 'self' to 'a' |
| 7070 | |
| 7071 | if reversed: |
| 7072 | assert len(params) >= 2 |
| 7073 | params[0], params[1] = params[1], params[0] |
| 7074 | |
| 7075 | def wrapper(a, b, *args, **params): |
| 7076 | return getattr(asanyarray(b), methodname)(a, *args, **params) |
| 7077 | |
| 7078 | else: |
| 7079 | def wrapper(a, *args, **params): |
| 7080 | return getattr(asanyarray(a), methodname)(*args, **params) |
| 7081 | |
| 7082 | wrapper.__signature__ = signature.replace(parameters=params) |
| 7083 | wrapper.__name__ = wrapper.__qualname__ = methodname |
| 7084 | |
| 7085 | # __doc__ is None when using `python -OO ...` |
| 7086 | if method.__doc__ is not None: |
| 7087 | str_signature = f"{methodname}{signature}" |
| 7088 | # TODO: For methods with a docstring "Parameters" section, that do not already |
| 7089 | # mention `a` (see e.g. `MaskedArray.var.__doc__`), it should be inserted there. |
| 7090 | wrapper.__doc__ = f" {str_signature}\n{method.__doc__}" |
| 7091 | |
| 7092 | return wrapper |
| 7093 | |
| 7094 | |
| 7095 | all = _frommethod('all') |