Python collections.abc.Callable() Examples

The following are 30 code examples of collections.abc.Callable(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module collections.abc , or try the search function .
Example #1
Source File: unittest.py    From hutils with MIT License 7 votes vote down vote up
def assert_increases(self, delta: int, func: Callable, name=""):
        """ shortcuts to verify func change is equal to delta """
        test_case = self

        class Detector:
            def __init__(self):
                self.previous = None

            def __enter__(self):
                self.previous = func()

            def __exit__(self, exc_type, exc_val, exc_tb):
                if not exc_val:
                    test_case.assertEqual(
                        self.previous + delta, func(), "{} should change {}".format(name, delta).strip()
                    )

        return Detector() 
Example #2
Source File: transform_list.py    From aliyun-log-python-sdk with MIT License 6 votes vote down vote up
def _dict_transform_fn(tr):
    def _real_transform(event):
        result = {}
        for k, v in six.iteritems(tr):
            if isinstance(v, Callable):
                v = v(event)

            if isinstance(v, (six.text_type, six.binary_type)):
                result[k] = v
            elif v is None:
                if k in result:
                    del result[k]
            else:
                logger.warning(u"unknown type of transform value for key:{0} value:{1}".format(k, v))
        event.update(result)
        return event

    return _real_transform 
Example #3
Source File: test_collections.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def test_Callable(self):
        non_samples = [None, 42, 3.14, 1j,
                       "", b"", (), [], {}, set(),
                       (lambda: (yield))(),
                       (x for x in []),
                       ]
        for x in non_samples:
            self.assertNotIsInstance(x, Callable)
            self.assertFalse(issubclass(type(x), Callable), repr(type(x)))
        samples = [lambda: None,
                   type, int, object,
                   len,
                   list.append, [].append,
                   ]
        for x in samples:
            self.assertIsInstance(x, Callable)
            self.assertTrue(issubclass(type(x), Callable), repr(type(x)))
        self.validate_abstract_methods(Callable, '__call__')
        self.validate_isinstance(Callable, '__call__') 
Example #4
Source File: test_collections.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 6 votes vote down vote up
def test_Callable(self):
        non_samples = [None, 42, 3.14, 1j,
                       "", b"", (), [], {}, set(),
                       (lambda: (yield))(),
                       (x for x in []),
                       ]
        for x in non_samples:
            self.assertNotIsInstance(x, Callable)
            self.assertFalse(issubclass(type(x), Callable), repr(type(x)))
        samples = [lambda: None,
                   type, int, object,
                   len,
                   list.append, [].append,
                   ]
        for x in samples:
            self.assertIsInstance(x, Callable)
            self.assertTrue(issubclass(type(x), Callable), repr(type(x)))
        self.validate_abstract_methods(Callable, '__call__')
        self.validate_isinstance(Callable, '__call__') 
Example #5
Source File: test_api_service.py    From graphql-over-kafka with MIT License 6 votes vote down vote up
def test_can_find_service_auth_criteria(self):
        # the auth criteria of the mocked service
        auth_criteria = self.service().auth_criteria

        # and it's the only one
        assert len(auth_criteria) == 1, (
            "There is an incorrect number of entries in the auth criteria map"
        )
        # check that the target service is in the dictionary
        assert 'TestService' in auth_criteria, (
            "Could not find service auth criteria in service dictionary"
        )
        # and that it's callable
        assert isinstance(auth_criteria['TestService'], Callable), (
            "Auth criteria handler was not callable."
        ) 
Example #6
Source File: typing.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def __new__(cls, name, bases, namespace, _root=False,
                args=None, result=None):
        if args is None and result is None:
            pass  # Must be 'class Callable'.
        else:
            if args is not Ellipsis:
                if not isinstance(args, list):
                    raise TypeError("Callable[args, result]: "
                                    "args must be a list."
                                    " Got %.100r." % (args,))
                msg = "Callable[[arg, ...], result]: each arg must be a type."
                args = tuple(_type_check(arg, msg) for arg in args)
            msg = "Callable[args, result]: result must be a type."
            result = _type_check(result, msg)
        self = super().__new__(cls, name, bases, namespace, _root=_root)
        self.__args__ = args
        self.__result__ = result
        return self 
Example #7
Source File: callbacks.py    From scikit-optimize with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def check_callback(callback):
    """
    Check if callback is a callable or a list of callables.
    """
    if callback is not None:
        if isinstance(callback, Callable):
            return [callback]

        elif (isinstance(callback, list) and
              all([isinstance(c, Callable) for c in callback])):
            return callback

        else:
            raise ValueError("callback should be either a callable or "
                             "a list of callables.")
    else:
        return [] 
Example #8
Source File: box.py    From Box with MIT License 6 votes vote down vote up
def __get_default(self, item):
        default_value = self._box_config['default_box_attr']
        if default_value in (self.__class__, dict):
            value = self.__class__(**self.__box_config())
        elif isinstance(default_value, dict):
            value = self.__class__(**self.__box_config(), **default_value)
        elif isinstance(default_value, list):
            value = box.BoxList(**self.__box_config())
        elif isinstance(default_value, Callable):
            value = default_value()
        elif hasattr(default_value, 'copy'):
            value = default_value.copy()
        else:
            value = default_value
        self.__convert_and_store(item, value)
        return value 
Example #9
Source File: apiGateway.py    From graphql-over-kafka with MIT License 6 votes vote down vote up
def auth_criteria(self):
        """
            This attribute provides the mapping of services to their auth requirement

            Returns:
                (dict) : the mapping from services to their auth requirements.
        """
        # the dictionary we will return
        auth = {}

        # go over each attribute of the service
        for attr in dir(self):
            # make sure we could hit an infinite loop
            if attr != 'auth_criteria':
                # get the actual attribute
                attribute = getattr(self, attr)
                # if the service represents an auth criteria
                if isinstance(attribute, Callable) and hasattr(attribute, '_service_auth'):
                    # add the criteria to the final results
                    auth[getattr(self, attr)._service_auth] = attribute

        # return the auth mapping
        return auth 
Example #10
Source File: typing.py    From review-heatmap with GNU Affero General Public License v3.0 6 votes vote down vote up
def __getitem__(self, parameters):
        """A thin wrapper around __getitem_inner__ to provide the latter
        with hashable arguments to improve speed.
        """

        if self.__origin__ is not None or self._gorg is not Callable:
            return super().__getitem__(parameters)
        if not isinstance(parameters, tuple) or len(parameters) != 2:
            raise TypeError("Callable must be used as "
                            "Callable[[arg, ...], result].")
        args, result = parameters
        if args is Ellipsis:
            parameters = (Ellipsis, result)
        else:
            if not isinstance(args, list):
                raise TypeError("Callable[args, result]: args must be a list."
                                " Got %.100r." % (args,))
            parameters = (tuple(args), result)
        return self.__getitem_inner__(parameters) 
Example #11
Source File: test_typing.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 6 votes vote down vote up
def test_extended_generic_rules_eq(self):
        T = TypeVar('T')
        U = TypeVar('U')
        self.assertEqual(Tuple[T, T][int], Tuple[int, int])
        self.assertEqual(typing.Iterable[Tuple[T, T]][T], typing.Iterable[Tuple[T, T]])
        with self.assertRaises(TypeError):
            Tuple[T, int][()]
        with self.assertRaises(TypeError):
            Tuple[T, U][T, ...]

        self.assertEqual(Union[T, int][int], int)
        self.assertEqual(Union[T, U][int, Union[int, str]], Union[int, str])
        class Base: ...
        class Derived(Base): ...
        self.assertEqual(Union[T, Base][Derived], Base)
        with self.assertRaises(TypeError):
            Union[T, int][1]

        self.assertEqual(Callable[[T], T][KT], Callable[[KT], KT])
        self.assertEqual(Callable[..., List[T]][int], Callable[..., List[int]])
        with self.assertRaises(TypeError):
            Callable[[T], U][..., int]
        with self.assertRaises(TypeError):
            Callable[[T], U][[], int] 
Example #12
Source File: test_collections.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
def test_Callable(self):
        non_samples = [None, 42, 3.14, 1j,
                       "", b"", (), [], {}, set(),
                       (lambda: (yield))(),
                       (x for x in []),
                       ]
        for x in non_samples:
            self.assertNotIsInstance(x, Callable)
            self.assertFalse(issubclass(type(x), Callable), repr(type(x)))
        samples = [lambda: None,
                   type, int, object,
                   len,
                   list.append, [].append,
                   ]
        for x in samples:
            self.assertIsInstance(x, Callable)
            self.assertTrue(issubclass(type(x), Callable), repr(type(x)))
        self.validate_abstract_methods(Callable, '__call__')
        self.validate_isinstance(Callable, '__call__') 
Example #13
Source File: typing.py    From review-heatmap with GNU Affero General Public License v3.0 6 votes vote down vote up
def _replace_arg(arg, tvars, args):
    """An internal helper function: replace arg if it is a type variable
    found in tvars with corresponding substitution from args or
    with corresponding substitution sub-tree if arg is a generic type.
    """

    if tvars is None:
        tvars = []
    if hasattr(arg, '_subs_tree') and isinstance(arg, (GenericMeta, _TypingBase)):
        return arg._subs_tree(tvars, args)
    if isinstance(arg, TypeVar):
        for i, tvar in enumerate(tvars):
            if arg == tvar:
                return args[i]
    return arg


# Special typing constructs Union, Optional, Generic, Callable and Tuple
# use three special attributes for internal bookkeeping of generic types:
# * __parameters__ is a tuple of unique free type parameters of a generic
#   type, for example, Dict[T, T].__parameters__ == (T,);
# * __origin__ keeps a reference to a type that was subscripted,
#   e.g., Union[T, int].__origin__ == Union;
# * __args__ is a tuple of all arguments used in subscripting,
#   e.g., Dict[T, int].__args__ == (T, int). 
Example #14
Source File: test_typing.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 6 votes vote down vote up
def test_pickle(self):
        global C  # pickle wants to reference the class by name
        T = TypeVar('T')

        class B(Generic[T]):
            pass

        class C(B[int]):
            pass

        c = C()
        c.foo = 42
        c.bar = 'abc'
        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
            z = pickle.dumps(c, proto)
            x = pickle.loads(z)
            self.assertEqual(x.foo, 42)
            self.assertEqual(x.bar, 'abc')
            self.assertEqual(x.__dict__, {'foo': 42, 'bar': 'abc'})
        simples = [Any, Union, Tuple, Callable, ClassVar, List, typing.Iterable]
        for s in simples:
            for proto in range(pickle.HIGHEST_PROTOCOL + 1):
                z = pickle.dumps(s, proto)
                x = pickle.loads(z)
                self.assertEqual(s, x) 
Example #15
Source File: condition_list.py    From aliyun-log-python-sdk with MIT License 6 votes vote down vote up
def _get_check(c):
    if isinstance(c, bool):
        return lambda e: c
    elif isinstance(c, Callable):
        return c
    elif isinstance(c, (dict,)):
        def check(event):
            for k, v in six.iteritems(c):
                if k in event:
                    if (isinstance(v, bool) and v) \
                            or (isinstance(v, Callable) and v(event[k])) \
                            or (isinstance(v, (six.text_type, six.binary_type))
                                and re_full_match(v, event[k])):
                        continue

                    return False
                else:
                    if v is None:
                        continue

                    return False

            return True

        return check 
Example #16
Source File: condition_list.py    From aliyun-log-python-sdk with MIT License 6 votes vote down vote up
def __call__(self, entity):
        entity = u(entity)

        if isinstance(entity, (dict,)):
            return any(c(entity) for c in self.check_list)
        elif isinstance(entity, Callable):
            fn = entity

            @functools.wraps(fn)
            def _wrapped(event, *args, **kwargs):
                try:
                    if any(c(event) for c in self.check_list):
                        return self.call_processor(fn, event, *args, **kwargs)
                except Exception as ex:
                    logger.error(
                        u'fail to call hooked function "{0}" with event "{1}", error: {2}'.format(fn, event, ex))

                return event

            return _wrapped
        else:
            errors = u"condition: unsupported data type: {0}".format(entity)
            logger.error(errors)
            raise SettingError(settings=errors) 
Example #17
Source File: test_typing.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_generic_forward_ref(self):
        def foobar(x: List[List['CC']]): ...
        class CC: ...
        self.assertEqual(
            get_type_hints(foobar, globals(), locals()),
            {'x': List[List[CC]]}
        )
        T = TypeVar('T')
        AT = Tuple[T, ...]
        def barfoo(x: AT): ...
        self.assertIs(get_type_hints(barfoo, globals(), locals())['x'], AT)
        CT = Callable[..., List[T]]
        def barfoo2(x: CT): ...
        self.assertIs(get_type_hints(barfoo2, globals(), locals())['x'], CT) 
Example #18
Source File: test_typing.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_extended_generic_rules_subclassing(self):
        class T1(Tuple[T, KT]): ...
        class T2(Tuple[T, ...]): ...
        class C1(Callable[[T], T]): ...
        class C2(Callable[..., int]):
            def __call__(self):
                return None

        self.assertEqual(T1.__parameters__, (T, KT))
        self.assertEqual(T1[int, str].__args__, (int, str))
        self.assertEqual(T1[int, T].__origin__, T1)

        self.assertEqual(T2.__parameters__, (T,))
        with self.assertRaises(TypeError):
            T1[int]
        with self.assertRaises(TypeError):
            T2[int, str]

        self.assertEqual(repr(C1[int]).split('.')[-1], 'C1[int]')
        self.assertEqual(C2.__parameters__, ())
        self.assertIsInstance(C2(), collections_abc.Callable)
        self.assertIsSubclass(C2, collections_abc.Callable)
        self.assertIsSubclass(C1, collections_abc.Callable)
        self.assertIsInstance(T1(), tuple)
        self.assertIsSubclass(T2, tuple)
        self.assertIsSubclass(Tuple[int, ...], typing.Sequence)
        self.assertIsSubclass(Tuple[int, ...], typing.Iterable) 
Example #19
Source File: test_typing.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_extended_generic_rules_repr(self):
        T = TypeVar('T')
        self.assertEqual(repr(Union[Tuple, Callable]).replace('typing.', ''),
                         'Union[Tuple, Callable]')
        self.assertEqual(repr(Union[Tuple, Tuple[int]]).replace('typing.', ''),
                         'Tuple')
        self.assertEqual(repr(Callable[..., Optional[T]][int]).replace('typing.', ''),
                         'Callable[..., Union[int, NoneType]]')
        self.assertEqual(repr(Callable[[], List[T]][int]).replace('typing.', ''),
                         'Callable[[], List[int]]') 
Example #20
Source File: test_typing.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_copy_and_deepcopy(self):
        T = TypeVar('T')
        class Node(Generic[T]): ...
        things = [Union[T, int], Tuple[T, int], Callable[..., T], Callable[[int], int],
                  Tuple[Any, Any], Node[T], Node[int], Node[Any], typing.Iterable[T],
                  typing.Iterable[Any], typing.Iterable[int], typing.Dict[int, str],
                  typing.Dict[T, Any], ClassVar[int], ClassVar[List[T]], Tuple['T', 'T'],
                  Union['T', int], List['T'], typing.Mapping['T', int]]
        for t in things + [Any]:
            self.assertEqual(t, copy(t))
            self.assertEqual(t, deepcopy(t)) 
Example #21
Source File: test_collections.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_direct_subclassing(self):
        for B in Hashable, Iterable, Iterator, Sized, Container, Callable:
            class C(B):
                pass
            self.assertTrue(issubclass(C, B))
            self.assertFalse(issubclass(int, C)) 
Example #22
Source File: test_typing.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_subscript_meta(self):
        T = TypeVar('T')
        self.assertEqual(Type[GenericMeta], Type[GenericMeta])
        self.assertEqual(Union[T, int][GenericMeta], Union[GenericMeta, int])
        self.assertEqual(Callable[..., GenericMeta].__args__, (Ellipsis, GenericMeta)) 
Example #23
Source File: properties.py    From keyring with MIT License 5 votes vote down vote up
def __init__(self, fget):
        assert fget is not None, "fget cannot be none"
        assert isinstance(fget, abc.Callable), "fget must be callable"
        self.fget = fget 
Example #24
Source File: request_history.py    From django-debug-toolbar-request-history with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def content(self):
        """ Content of the panel when it's displayed in full screen. """
        toolbars = OrderedDict()
        for id, toolbar in DebugToolbar._store.items():
            content = {}
            for panel in toolbar.panels:
                panel_id = None
                nav_title = ''
                nav_subtitle = ''
                try:
                    panel_id = panel.panel_id
                    nav_title = panel.nav_title
                    nav_subtitle = panel.nav_subtitle() if isinstance(
                        panel.nav_subtitle, Callable) else panel.nav_subtitle
                except Exception:
                    logger.debug('Error parsing panel info:', exc_info=True)
                if panel_id is not None:
                    content.update({
                        panel_id: {
                            'panel_id': panel_id,
                            'nav_title': nav_title,
                            'nav_subtitle': nav_subtitle,
                        }
                    })
            toolbars[id] = {
                'toolbar': toolbar,
                'content': content
            }
        return get_template().render(Context({
            'toolbars': OrderedDict(reversed(list(toolbars.items()))),
            'trunc_length': get_config().get('RH_POST_TRUNC_LENGTH', 0)
        })) 
Example #25
Source File: _type_validation.py    From attrs-strict with Apache License 2.0 5 votes vote down vote up
def _type_matching(actual, expected):
    actual = actual.__supertype__ if is_newtype(actual) else actual
    expected = expected.__supertype__ if is_newtype(expected) else expected

    if expected == actual or expected == typing.Any:
        return True

    base_type = _get_base_type(expected)

    if base_type == typing.Union:  # type: ignore
        return any(
            _type_matching(actual, expected_candidate)
            for expected_candidate in expected.__args__
        )

    elif base_type in (
        SimilarTypes.Dict
        | SimilarTypes.List
        | SimilarTypes.Tuple
        | SimilarTypes.Callable
    ):
        return all(
            _type_matching(actual, expected)
            for actual, expected in zip_longest(
                actual.__args__, expected.__args__
            )
        )

    return False 
Example #26
Source File: _type_validation.py    From attrs-strict with Apache License 2.0 5 votes vote down vote up
def _validate_elements(attribute, value, expected_type):
    if expected_type is None:
        return

    base_type = _get_base_type(expected_type)

    if base_type == typing.Any:
        return

    if isinstance(base_type, (str, ForwardRef)):
        # These base_types happen when you have string annotations and cannot
        # be used in isinstance.
        raise _StringAnnotationError()

    if base_type != typing.Union and not isinstance(  # type: ignore
        value, base_type
    ):
        raise AttributeTypeError(value, attribute)

    if base_type == typing.Union:  # type: ignore
        _handle_union(attribute, value, expected_type)
    elif base_type in SimilarTypes.List:
        _handle_set_or_list(attribute, value, expected_type)
    elif base_type in SimilarTypes.Dict:
        _handle_dict(attribute, value, expected_type)
    elif base_type in SimilarTypes.Tuple:
        _handle_tuple(attribute, value, expected_type)
    elif base_type in SimilarTypes.Callable:  # type: ignore
        _handle_callable(attribute, value, expected_type) 
Example #27
Source File: typing.py    From review-heatmap with GNU Affero General Public License v3.0 5 votes vote down vote up
def __getitem_inner__(self, parameters):
        args, result = parameters
        msg = "Callable[args, result]: result must be a type."
        result = _type_check(result, msg)
        if args is Ellipsis:
            return super().__getitem__((_TypingEllipsis, result))
        msg = "Callable[[arg, ...], result]: each arg must be a type."
        args = tuple(_type_check(arg, msg) for arg in args)
        parameters = args + (result,)
        return super().__getitem__(parameters) 
Example #28
Source File: typing.py    From review-heatmap with GNU Affero General Public License v3.0 5 votes vote down vote up
def _tree_repr(self, tree):
        if self._gorg is not Callable:
            return super()._tree_repr(tree)
        # For actual Callable (not its subclass) we override
        # super()._tree_repr() for nice formatting.
        arg_list = []
        for arg in tree[1:]:
            if not isinstance(arg, tuple):
                arg_list.append(_type_repr(arg))
            else:
                arg_list.append(arg[0]._tree_repr(arg))
        if arg_list[0] == '...':
            return repr(tree[0]) + '[..., %s]' % arg_list[1]
        return (repr(tree[0]) +
                '[[%s], %s]' % (', '.join(arg_list[:-1]), arg_list[-1])) 
Example #29
Source File: pytest_plugin.py    From Galaxy_Plugin_Bethesda with MIT License 5 votes vote down vote up
def aiohttp_client(loop):  # type: ignore
    """Factory to create a TestClient instance.

    aiohttp_client(app, **kwargs)
    aiohttp_client(server, **kwargs)
    aiohttp_client(raw_server, **kwargs)
    """
    clients = []

    async def go(__param, *args, server_kwargs=None, **kwargs):  # type: ignore

        if (isinstance(__param, Callable) and  # type: ignore
                not isinstance(__param, (Application, BaseTestServer))):
            __param = __param(loop, *args, **kwargs)
            kwargs = {}
        else:
            assert not args, "args should be empty"

        if isinstance(__param, Application):
            server_kwargs = server_kwargs or {}
            server = TestServer(__param, loop=loop, **server_kwargs)
            client = TestClient(server, loop=loop, **kwargs)
        elif isinstance(__param, BaseTestServer):
            client = TestClient(__param, loop=loop, **kwargs)
        else:
            raise ValueError("Unknown argument type: %r" % type(__param))

        await client.start_server()
        clients.append(client)
        return client

    yield go

    async def finalize():  # type: ignore
        while clients:
            await clients.pop().close()

    loop.run_until_complete(finalize()) 
Example #30
Source File: qualityquery.py    From ctapipe with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, config=None, parent=None, **kwargs):
        super().__init__(config=config, parent=parent, **kwargs)

        # add a selection to count all entries and make it the first one
        self.quality_criteria.insert(0, ("TOTAL", "lambda x: True"))
        self.criteria_names = []
        self.selection_function_strings = []
        self._selectors = []

        for name, func_str in self.quality_criteria:
            try:  # generate real functions from the selection function strings
                self.criteria_names.append(name)
                self.selection_function_strings.append(func_str)

                func = eval(func_str, ALLOWED_GLOBALS)
                if not isinstance(func, Callable):
                    raise QualityCriteriaError(
                        f"Selection criterion '{name}' cannot be evaluated because "
                        f" '{func_str}' is not a callable function"
                    )
                self._selectors.append(func)

            except NameError as err:
                # catch functions that cannot be defined. Note that this cannot check
                # that the function can run, that only happens the first time it's
                # called.
                raise QualityCriteriaError(
                    f"Couldn't evaluate selection function '{name}' -> '{func_str}' "
                    f"because: {err}"
                )

        # arrays for recording overall statistics
        self._counts = np.zeros(len(self._selectors), dtype=np.int)
        self._cumulative_counts = np.zeros(len(self._selectors), dtype=np.int)