File size: 2,274 Bytes
5e797a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""
Tools for Functional-Programming in Python.

From: https://github.com/joaomcteixeira/libfuncpy
"""
import time

from functools import wraps
from operator import is_not
from typing import Any, Callable, Literal


def chainf(init: Any, *funcs: Callable[..., Any], **common: Any) -> Any:
    """
    Apply a sequence of functions to an initial value.

    Example
    -------
    >>> chainf(2, *[str, int, float])
    2.0

    Parameters
    ----------
    init : anything
        The initial value.

    **common : keyword arguments
        Common key word arguments to all functions.

    Returns
    -------
    anything
        The result of the chain of functions; this is, the return value
        of the last function.
    """
    for func in funcs:
        init = func(init, **common)
    return init


def chainfs(*funcs: Callable[..., Any], **common: Any) -> Callable[..., Any]:
    """
    Store functions be executed on a value.

    Example
    -------
    >>> do = chainfs(str, int, float)
    >>> do(2)
    2.0

    See Also
    --------
    :py:func:`chainf`
    """
    def execute(value: Any) -> Any:
        return chainf(value, *funcs, **common)

    return execute


def give_same(value: Any) -> Any:
    """Return what is given."""
    return value


def true(*ignore: Any, **everything: Any) -> Literal[True]:
    """Give True regardless of the input."""
    return True


def false(*ignore: Any, **everything: Any) -> Literal[False]:
    """Give False regardless of the input."""
    return False


def none(*ignore: Any, **everything: Any) -> Literal[None]:
    """Give None regardless of the input."""
    return None


def nan(*ignore: Any, **everything: Any) -> float:
    """Give nan regardless of the input."""
    return float('nan')


def not_none(value: Any) -> bool:
    """Give True if value is not None, or False otherwise."""
    return is_not(value, None)


def exec_time(func):
    @wraps(func)
    def timeit_wrapper(*args, **kwargs):
        start_time = time.perf_counter()
        result = func(*args, **kwargs)
        end_time = time.perf_counter()
        total_time = end_time - start_time
        print(f'Function {func.__name__}{args} {kwargs} Took {total_time:.4f} seconds')
        return result
    return timeit_wrapper