當前位置: 首頁>>代碼示例>>Python>>正文


Python typing.Callable方法代碼示例

本文整理匯總了Python中typing.Callable方法的典型用法代碼示例。如果您正苦於以下問題:Python typing.Callable方法的具體用法?Python typing.Callable怎麽用?Python typing.Callable使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在typing的用法示例。


在下文中一共展示了typing.Callable方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: get_listeners

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Callable [as 別名]
def get_listeners(
        self, event_name=None
    ):  # type: (str) -> Union[List[Callable], Dict[str, Callable]]
        if event_name is not None:
            if event_name not in self._listeners:
                return []

            if event_name not in self._sorted:
                self._sort_listeners(event_name)

            return self._sorted[event_name]

        for event_name, event_listeners in self._listeners.items():
            if event_name not in self._sorted:
                self._sort_listeners(event_name)

        return self._sorted 
開發者ID:sdispater,項目名稱:clikit,代碼行數:19,代碼來源:event_dispatcher.py

示例2: builder

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Callable [as 別名]
def builder(func: Callable) -> Callable:
    """
    Decorator for wrapper "builder" functions.  These are functions on the Query class or other classes used for
    building queries which mutate the query and return self.  To make the build functions immutable, this decorator is
    used which will deepcopy the current instance.  This decorator will return the return value of the inner function
    or the new copy of the instance.  The inner function does not need to return self.
    """
    import copy

    def _copy(self, *args, **kwargs):
        self_copy = copy.copy(self) if getattr(self, "immutable", True) else self
        result = func(self_copy, *args, **kwargs)

        # Return self if the inner function returns None.  This way the inner function can return something
        # different (for example when creating joins, a different builder is returned).
        if result is None:
            return self_copy

        return result

    return _copy 
開發者ID:kayak,項目名稱:pypika,代碼行數:23,代碼來源:utils.py

示例3: s3_request

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Callable [as 別名]
def s3_request(func: Callable):
    """
    Wrapper function for s3 requests in order to create more helpful error
    messages.
    """

    @wraps(func)
    def wrapper(url: str, *args, **kwargs):
        try:
            return func(url, *args, **kwargs)
        except ClientError as exc:
            if int(exc.response["Error"]["Code"]) == 404:
                raise FileNotFoundError("file {} not found".format(url))
            else:
                raise

    return wrapper 
開發者ID:ymcui,項目名稱:cmrc2019,代碼行數:19,代碼來源:file_utils.py

示例4: _build_predicate

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Callable [as 別名]
def _build_predicate(
            self,
            test: t.Callable,
            operation: Operation,
            args: t.Iterable
            ) -> Predicate:
        """
        Generate a Predicate object based on a test function.

        :param test: The test the Predicate executes.
        :param operation: An `Operation` instance for the Predicate.
        :return: A `Predicate` object
        """
        if not self._path:
            raise ValueError('Var has no path')
        return Predicate(
            check_path(test, self._path),
            operation,
            args,
            self._name
        ) 
開發者ID:pcah,項目名稱:python-clean-architecture,代碼行數:23,代碼來源:predicate.py

示例5: test

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Callable [as 別名]
def test(self, func: t.Callable[..., bool], *args, **kwargs) -> Predicate:
        """
        Run a user-defined test function against the value.
        >>> def test_func(val):
        ...     return val == 42
        ...
        >>> var('f1').test(test_func)

        :param func: The function to call, passing the dict as the first
            argument
        :param args:
        :param kwargs:
            Additional arguments to pass to the test function
        """
        return self._build_predicate(
            lambda lhs, value: func(lhs, *args, **kwargs),
            Operation.TEST,
            (self._path, func, args, freeze(kwargs))
        ) 
開發者ID:pcah,項目名稱:python-clean-architecture,代碼行數:21,代碼來源:predicate.py

示例6: check_path

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Callable [as 別名]
def check_path(
    test: t.Callable[[t.Any, t.Any], bool],
    path: t.Iterable[str]
) -> t.Callable[..., bool]:
    def check_path_curried(value):
        orig_value = value
        for part in path:
            try:
                value = getattr(value, part)
            except AttributeError:
                try:
                    value = value[part]
                except (KeyError, TypeError):
                    return False
        return test(value, orig_value)

    return check_path_curried 
開發者ID:pcah,項目名稱:python-clean-architecture,代碼行數:19,代碼來源:operators.py

示例7: throttle

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Callable [as 別名]
def throttle(interval: Union[float, int]):
    """Decorator ensures function that can only be called once every `s` seconds.
    """

    def decorate(fn: Callable) -> Callable:
        t = None

        def wrapped(*args, **kwargs):
            nonlocal t
            t_ = time()
            if t is None or t_ - t >= interval:
                result = fn(*args, **kwargs)
                t = time()
                return result

        return wrapped

    return decorate 
開發者ID:PhilipTrauner,項目名稱:cmus-osx,代碼行數:20,代碼來源:util.py

示例8: deprecated_test

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Callable [as 別名]
def deprecated_test(test: Callable) -> Callable:
    """Marks a test as using deprecated functionality.

    Ensures the test is executed within the `pytest.deprecated_call()` context.

    Args:
        test: The test.

    Returns:
        The decorated test.
    """

    @functools.wraps(test)
    def decorated_test(*args, **kwargs) -> Any:
        with pytest.deprecated_call():
            test(*args, **kwargs)

    return decorated_test 
開發者ID:quantumlib,項目名稱:OpenFermion-Cirq,代碼行數:20,代碼來源:_compat_test.py

示例9: ignore_copy

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Callable [as 別名]
def ignore_copy(func: Callable) -> Callable:
    """
    Decorator for wrapping the __getattr__ function for classes that are copied via deepcopy.  This prevents infinite
    recursion caused by deepcopy looking for magic functions in the class. Any class implementing __getattr__ that is
    meant to be deepcopy'd should use this decorator.

    deepcopy is used by pypika in builder functions (decorated by @builder) to make the results immutable.  Any data
    model type class (stored in the Query instance) is copied.
    """

    def _getattr(self, name):
        if name in [
            "__copy__",
            "__deepcopy__",
            "__getstate__",
            "__setstate__",
            "__getnewargs__",
        ]:
            raise AttributeError(
                  "'%s' object has no attribute '%s'" % (self.__class__.__name__, name)
            )

        return func(self, name)

    return _getattr 
開發者ID:kayak,項目名稱:pypika,代碼行數:27,代碼來源:utils.py

示例10: endpoint

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Callable [as 別名]
def endpoint(self, endpoint: str) -> Callable:
        """Add an endpoint to the blueprint.

        This is designed to be used as a decorator, and has the same arguments
        as :meth:`~quart.Quart.endpoint`. An example usage,

        .. code-block:: python

            blueprint = Blueprint(__name__)
            @blueprint.endpoint('index')
            def index():
                ...
        """

        def decorator(func: Callable) -> Callable:
            self.record_once(lambda state: state.register_endpoint(endpoint, func))
            return func

        return decorator 
開發者ID:pgjones,項目名稱:quart,代碼行數:21,代碼來源:blueprints.py

示例11: app_template_filter

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Callable [as 別名]
def app_template_filter(self, name: Optional[str] = None) -> Callable:
        """Add an application wide template filter.

        This is designed to be used as a decorator, and has the same arguments
        as :meth:`~quart.Quart.template_filter`. An example usage,

        .. code-block:: python

            blueprint = Blueprint(__name__)
            @blueprint.app_template_filter()
            def filter(value):
                ...
        """

        def decorator(func: Callable) -> Callable:
            self.add_app_template_filter(func, name=name)
            return func

        return decorator 
開發者ID:pgjones,項目名稱:quart,代碼行數:21,代碼來源:blueprints.py

示例12: add_app_template_filter

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Callable [as 別名]
def add_app_template_filter(self, func: Callable, name: Optional[str] = None) -> None:
        """Add an application wide template filter.

        This is designed to be used on the blueprint directly, and
        has the same arguments as
        :meth:`~quart.Quart.add_template_filter`. An example usage,

        .. code-block:: python

            def filter():
                ...

            blueprint = Blueprint(__name__)
            blueprint.add_app_template_filter(filter)
        """
        self.record_once(lambda state: state.register_template_filter(func, name)) 
開發者ID:pgjones,項目名稱:quart,代碼行數:18,代碼來源:blueprints.py

示例13: app_template_test

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Callable [as 別名]
def app_template_test(self, name: Optional[str] = None) -> Callable:
        """Add an application wide template test.

        This is designed to be used as a decorator, and has the same arguments
        as :meth:`~quart.Quart.template_test`. An example usage,

        .. code-block:: python

            blueprint = Blueprint(__name__)
            @blueprint.app_template_test()
            def test(value):
                ...
        """

        def decorator(func: Callable) -> Callable:
            self.add_app_template_test(func, name=name)
            return func

        return decorator 
開發者ID:pgjones,項目名稱:quart,代碼行數:21,代碼來源:blueprints.py

示例14: add_app_template_test

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Callable [as 別名]
def add_app_template_test(self, func: Callable, name: Optional[str] = None) -> None:
        """Add an application wide template test.

        This is designed to be used on the blueprint directly, and
        has the same arguments as
        :meth:`~quart.Quart.add_template_test`. An example usage,

        .. code-block:: python

            def test():
                ...

            blueprint = Blueprint(__name__)
            blueprint.add_app_template_test(test)
        """
        self.record_once(lambda state: state.register_template_test(func, name)) 
開發者ID:pgjones,項目名稱:quart,代碼行數:18,代碼來源:blueprints.py

示例15: add_app_template_global

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Callable [as 別名]
def add_app_template_global(self, func: Callable, name: Optional[str] = None) -> None:
        """Add an application wide template global.

        This is designed to be used on the blueprint directly, and
        has the same arguments as
        :meth:`~quart.Quart.add_template_global`. An example usage,

        .. code-block:: python

            def global():
                ...

            blueprint = Blueprint(__name__)
            blueprint.add_app_template_global(global)
        """
        self.record_once(lambda state: state.register_template_global(func, name)) 
開發者ID:pgjones,項目名稱:quart,代碼行數:18,代碼來源:blueprints.py


注:本文中的typing.Callable方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。