本文整理汇总了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
示例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
示例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
示例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
)
示例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))
)
示例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
示例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
示例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
示例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
示例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
示例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
示例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))
示例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
示例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))
示例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))