Define functions from existing MaskedArray methods. Parameters ---------- methodname : str Name of the method to transform.
| 6842 | |
| 6843 | |
| 6844 | class _frommethod: |
| 6845 | """ |
| 6846 | Define functions from existing MaskedArray methods. |
| 6847 | |
| 6848 | Parameters |
| 6849 | ---------- |
| 6850 | methodname : str |
| 6851 | Name of the method to transform. |
| 6852 | |
| 6853 | """ |
| 6854 | |
| 6855 | def __init__(self, methodname, reversed=False): |
| 6856 | self.__name__ = methodname |
| 6857 | self.__doc__ = self.getdoc() |
| 6858 | self.reversed = reversed |
| 6859 | |
| 6860 | def getdoc(self): |
| 6861 | "Return the doc of the function (from the doc of the method)." |
| 6862 | meth = getattr(MaskedArray, self.__name__, None) or\ |
| 6863 | getattr(np, self.__name__, None) |
| 6864 | signature = self.__name__ + get_object_signature(meth) |
| 6865 | if meth is not None: |
| 6866 | doc = """ %s\n%s""" % ( |
| 6867 | signature, getattr(meth, '__doc__', None)) |
| 6868 | return doc |
| 6869 | |
| 6870 | def __call__(self, a, *args, **params): |
| 6871 | if self.reversed: |
| 6872 | args = list(args) |
| 6873 | a, args[0] = args[0], a |
| 6874 | |
| 6875 | marr = asanyarray(a) |
| 6876 | method_name = self.__name__ |
| 6877 | method = getattr(type(marr), method_name, None) |
| 6878 | if method is None: |
| 6879 | # use the corresponding np function |
| 6880 | method = getattr(np, method_name) |
| 6881 | |
| 6882 | return method(marr, *args, **params) |
| 6883 | |
| 6884 | |
| 6885 | all = _frommethod('all') |