@@ -2467,25 +2467,18 @@ def flatten_sequence(iterable):
2467
2467
return out
2468
2468
2469
2469
2470
- class _arraymethod (object ):
2470
+ def _arraymethod (funcname , onmask = True ):
2471
2471
"""
2472
- Define a wrapper for basic array methods .
2472
+ Return a class method wrapper around a basic array method .
2473
2473
2474
- Upon call, returns a masked array, where the new ``_data`` array is
2475
- the output of the corresponding method called on the original
2476
- ``_data``.
2474
+ Creates a class method which returns a masked array, where the new
2475
+ ``_data`` array is the output of the corresponding basic method called
2476
+ on the original ``_data``.
2477
2477
2478
2478
If `onmask` is True, the new mask is the output of the method called
2479
2479
on the initial mask. Otherwise, the new mask is just a reference
2480
2480
to the initial mask.
2481
2481
2482
- Attributes
2483
- ----------
2484
- _onmask : bool
2485
- Holds the `onmask` parameter.
2486
- obj : object
2487
- The object calling `_arraymethod`.
2488
-
2489
2482
Parameters
2490
2483
----------
2491
2484
funcname : str
@@ -2495,47 +2488,31 @@ class _arraymethod(object):
2495
2488
alone (False). Default is True. Make available as `_onmask`
2496
2489
attribute.
2497
2490
2498
- """
2499
-
2500
- def __init__ (self , funcname , onmask = True ):
2501
- self .__name__ = funcname
2502
- self ._onmask = onmask
2503
- self .obj = None
2504
- self .__doc__ = self .getdoc ()
2505
-
2506
- def getdoc (self ):
2507
- "Return the doc of the function (from the doc of the method)."
2508
- methdoc = getattr (ndarray , self .__name__ , None ) or \
2509
- getattr (np , self .__name__ , None )
2510
- if methdoc is not None :
2511
- return methdoc .__doc__
2512
-
2513
- def __get__ (self , obj , objtype = None ):
2514
- self .obj = obj
2515
- return self
2491
+ Returns
2492
+ -------
2493
+ method : instancemethod
2494
+ Class method wrapper of the specified basic array method.
2516
2495
2517
- def __call__ (self , * args , ** params ):
2518
- methodname = self .__name__
2519
- instance = self .obj
2520
- # Fallback : if the instance has not been initialized, use the first
2521
- # arg
2522
- if instance is None :
2523
- args = list (args )
2524
- instance = args .pop (0 )
2525
- data = instance ._data
2526
- mask = instance ._mask
2527
- cls = type (instance )
2528
- result = getattr (data , methodname )(* args , ** params ).view (cls )
2529
- result ._update_from (instance )
2496
+ """
2497
+ def wrapped_method (self , * args , ** params ):
2498
+ result = getattr (self ._data , funcname )(* args , ** params )
2499
+ result = result .view (type (self ))
2500
+ result ._update_from (self )
2501
+ mask = self ._mask
2530
2502
if result .ndim :
2531
- if not self . _onmask :
2503
+ if not onmask :
2532
2504
result .__setmask__ (mask )
2533
2505
elif mask is not nomask :
2534
- result .__setmask__ (getattr (mask , methodname )(* args , ** params ))
2506
+ result .__setmask__ (getattr (mask , funcname )(* args , ** params ))
2535
2507
else :
2536
2508
if mask .ndim and (not mask .dtype .names and mask .all ()):
2537
2509
return masked
2538
2510
return result
2511
+ methdoc = getattr (ndarray , funcname , None ) or getattr (np , funcname , None )
2512
+ if methdoc is not None :
2513
+ wrapped_method .__doc__ = methdoc .__doc__
2514
+ wrapped_method .__name__ = funcname
2515
+ return wrapped_method
2539
2516
2540
2517
2541
2518
class MaskedIterator (object ):
0 commit comments