Skip to content

Functions

awaiting(awaitable: Awaitable[T]) -> T async

Wraps an awaitable into a coroutine.

This function is useful in, for example, asyncio, where some functions expect coroutines only.

Parameters:

Name Type Description Default
awaitable Awaitable[T]

The awaitable to wrap.

required

Returns:

Type Description
T

The result of await awaitable.

Source code in src/funcs/functions.py
20
21
22
23
24
25
26
27
28
29
30
31
32
async def awaiting(awaitable: Awaitable[T]) -> T:
    """Wraps an awaitable into a coroutine.

    This function is useful in, for example, [`asyncio`][asyncio],
    where some functions expect coroutines only.

    Arguments:
        awaitable: The awaitable to wrap.

    Returns:
        The result of `await awaitable`.
    """
    return await awaitable

asyncify(function: Callable[P, R]) -> AsyncCallable[P, R]

Wraps the synchronous function to be called asynchronously.

Parameters:

Name Type Description Default
function Callable[P, R]

The synchronous function to wrap.

required

Returns:

Type Description
AsyncCallable[P, R]

The wrapped asynchronous function.

Source code in src/funcs/functions.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def asyncify(function: Callable[P, R]) -> AsyncCallable[P, R]:
    """Wraps the synchronous function to be called asynchronously.

    Arguments:
        function: The synchronous function to wrap.

    Returns:
        The wrapped asynchronous function.
    """

    @wraps(function)
    async def async_function(*args: P.args, **kwargs: P.kwargs) -> R:
        return function(*args, **kwargs)

    return async_function

identity(item: T) -> T

Returns the given item.

Parameters:

Name Type Description Default
item T

The item to return.

required

Returns:

Type Description
T

The given item.

Source code in src/funcs/functions.py
52
53
54
55
56
57
58
59
60
61
def identity(item: T) -> T:
    """Returns the given `item`.

    Arguments:
        item: The item to return.

    Returns:
        The given item.
    """
    return item

returns(item: T) -> Nullary[T]

Creates a function that returns the given item when called.

Parameters:

Name Type Description Default
item T

The item to return.

required

Returns:

Type Description
Nullary[T]

The function that returns the given item.

Source code in src/funcs/functions.py
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def returns(item: T) -> Nullary[T]:
    """Creates a function that returns the given `item` when called.

    Arguments:
        item: The item to return.

    Returns:
        The function that returns the given item.
    """

    def return_item() -> T:
        return item

    return return_item

raises(error: AnyError) -> Nullary[Never]

Creates a function that always raises the given error when called.

Parameters:

Name Type Description Default
error AnyError

The error to raise.

required

Returns:

Type Description
Nullary[Never]

The function that always raises the given error.

Source code in src/funcs/functions.py
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def raises(error: AnyError) -> Nullary[Never]:
    """Creates a function that always raises the given `error` when called.

    Arguments:
        error: The error to raise.

    Returns:
        The function that always raises the given error.
    """

    def raise_error() -> Never:
        raise error

    return raise_error

flip(binary: Binary[T, U, R]) -> Binary[U, T, R]

Creates a function that flips arguments of the given binary function.

Parameters:

Name Type Description Default
binary Binary[T, U, R]

The binary function to flip.

required

Returns:

Type Description
Binary[U, T, R]

The flipped binary function.

Source code in src/funcs/functions.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
def flip(binary: Binary[T, U, R]) -> Binary[U, T, R]:
    """Creates a function that flips arguments of the given `binary` function.

    Arguments:
        binary: The binary function to flip.

    Returns:
        The flipped binary function.
    """

    def flipped(u: U, t: T) -> R:
        return binary(t, u)

    return flipped

complement(predicate: GenericPredicate[P]) -> GenericPredicate[P]

Wraps the given predicate to return the negated result.

Parameters:

Name Type Description Default
predicate GenericPredicate[P]

The predicate to wrap.

required

Returns:

Type Description
GenericPredicate[P]

The wrapped predicate.

Source code in src/funcs/functions.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
def complement(predicate: GenericPredicate[P]) -> GenericPredicate[P]:
    """Wraps the given `predicate` to return the negated result.

    Arguments:
        predicate: The predicate to wrap.

    Returns:
        The wrapped predicate.
    """

    def negate(*args: P.args, **kwargs: P.kwargs) -> bool:
        return not predicate(*args, **kwargs)

    return negate