CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
_user_interface.py58 linesDownload Raw Back to utils
1# Authors: The scikit-learn developers
2# SPDX-License-Identifier: BSD-3-Clause
3
4import timeit
5from contextlib import contextmanager
6
7
8def _message_with_time(source, message, time):
9    """Create one line message for logging purposes.
10
11    Parameters
12    ----------
13    source : str
14        String indicating the source or the reference of the message.
15
16    message : str
17        Short message.
18
19    time : int
20        Time in seconds.
21    """
22    start_message = "[%s] " % source
23
24    # adapted from joblib.logger.short_format_time without the Windows -.1s
25    # adjustment
26    if time > 60:
27        time_str = "%4.1fmin" % (time / 60)
28    else:
29        time_str = " %5.1fs" % time
30    end_message = " %s, total=%s" % (message, time_str)
31    dots_len = 70 - len(start_message) - len(end_message)
32    return "%s%s%s" % (start_message, dots_len * ".", end_message)
33
34
35@contextmanager
36def _print_elapsed_time(source, message=None):
37    """Log elapsed time to stdout when the context is exited.
38
39    Parameters
40    ----------
41    source : str
42        String indicating the source or the reference of the message.
43
44    message : str, default=None
45        Short message. If None, nothing will be printed.
46
47    Returns
48    -------
49    context_manager
50        Prints elapsed time upon exit if verbose.
51    """
52    if message is None:
53        yield
54    else:
55        start = timeit.default_timer()
56        yield
57        print(_message_with_time(source, message, timeit.default_timer() - start))
58 
Aluode/PerceptionLabPortable · CoolFace