Пример #1
0
    def _process_local_results(self, results, targets):
        """Figure out what to return on the Client.

        Parameters
        ----------
        key : string
            Key corresponding to wrapped function's return value.

        Returns
        -------
        Varied
            A DistArray (if locally all values are LocalArray), a None (if
            locally all values are None), or else, pull the result back to the
            client and return it.  If all but one of the pulled values is None,
            return that non-None value only.
        """
        def is_NoneType(pxy):
            return pxy.type_str == str(type(None))

        def is_LocalArray(pxy):
            return (isinstance(pxy, Proxy) and 
                    pxy.type_str == "<class 'distarray.localapi.localarray.LocalArray'>")

        if all(is_LocalArray(r) for r in results):
            result = DistArray.from_localarrays(results[0], context=self, targets=targets)
        elif all(r is None for r in results):
            result = None
        else:
            if has_exactly_one(results):
                result = next(x for x in results if x is not None)
            else:
                result = results

        return result
Пример #2
0
    def process_return_value(self, context, result_key):
        """Figure out what to return on the Client.

        Parameters
        ----------
        key : string
            Key corresponding to wrapped function's return value.

        Returns
        -------
        Varied
            A DistArray (if locally all values are DistArray), a None (if
            locally all values are None), or else, pull the result back to the
            client and return it.  If all but one of the pulled values is None,
            return that non-None value only.
        """
        type_key = context._generate_key()
        type_statement = "{} = str(type({}))".format(type_key, result_key)
        context._execute(type_statement)
        result_type_str = context._pull(type_key)

        def is_NoneType(typestring):
            return (typestring == "<type 'NoneType'>" or
                    typestring == "<class 'NoneType'>")

        def is_LocalArray(typestring):
            return (typestring == "<class 'distarray.local.localarray."
                                  "LocalArray'>")

        if all(is_LocalArray(r) for r in result_type_str):
            result = DistArray.from_localarrays(result_key, context=context)
        elif all(is_NoneType(r) for r in result_type_str):
            result = None
        else:
            result = context._pull(result_key)
            if has_exactly_one(result):
                result = next(x for x in result if x is not None)

        return result
Пример #3
0
    def _process_local_results(self, results, targets):
        """Figure out what to return on the Client.

        Parameters
        ----------
        key : string
            Key corresponding to wrapped function's return value.

        Returns
        -------
        Varied
            A DistArray (if locally all values are LocalArray), a None (if
            locally all values are None), or else, pull the result back to the
            client and return it.  If all but one of the pulled values is None,
            return that non-None value only.
        """
        def is_NoneType(pxy):
            return pxy.type_str == str(type(None))

        def is_LocalArray(pxy):
            return (isinstance(pxy, Proxy) and pxy.type_str
                    == "<class 'distarray.localapi.localarray.LocalArray'>")

        if all(is_LocalArray(r) for r in results):
            result = DistArray.from_localarrays(results[0],
                                                context=self,
                                                targets=targets)
        elif all(r is None for r in results):
            result = None
        else:
            if has_exactly_one(results):
                result = next(x for x in results if x is not None)
            else:
                result = results

        return result
Пример #4
0
 def test_has_exactly_one_multi_non_none(self):
     iterable = [None, 5, 'abc', None, None]
     self.assertFalse(utils.has_exactly_one(iterable))
Пример #5
0
 def test_has_exactly_one_all_none(self):
     iterable = [None, None, None, None, None]
     self.assertFalse(utils.has_exactly_one(iterable))
Пример #6
0
 def test_has_exactly_one_true(self):
     iterable = [None, None, None, 55, None]
     self.assertTrue(utils.has_exactly_one(iterable))
Пример #7
0
 def test_has_exactly_one_multi_non_none(self):
     iterable = [None, 5, 'abc', None, None]
     self.assertFalse(utils.has_exactly_one(iterable))
Пример #8
0
 def test_has_exactly_one_all_none(self):
     iterable = [None, None, None, None, None]
     self.assertFalse(utils.has_exactly_one(iterable))
Пример #9
0
 def test_has_exactly_one_true(self):
     iterable = [None, None, None, 55, None]
     self.assertTrue(utils.has_exactly_one(iterable))