File size: 971 Bytes
c0e9d84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import collections

from .debug import aexec, shell  # noqa: F401


class UtilKit:
    """Small patching and time-formatting utilities.

    One instance (``util_kit``); the module-level functions below are
    backward-compatible aliases.
    """

    def patch(self, cls: type) -> collections.abc.Callable:
        def wrap(func: collections.abc.Callable) -> collections.abc.Callable:
            if hasattr(func, "_patch"):
                func._patch.append(cls)
            else:
                func._patch = [cls]

            return func

        return wrap

    def usecs(self, s: float) -> str:
        if not s:
            return "0 s"

        if s >= 1:
            return f"{s:.2f} s"

        if s >= 0.001:
            return f"{s * 1000:.2f} ms"

        return f"{s * 1000000:.2f} µs"


util_kit = UtilKit()


def patch(cls: type) -> collections.abc.Callable:
    return util_kit.patch(cls)


def usecs(s: float) -> str:
    return util_kit.usecs(s)