blob: fb9971a64f66c8b9f9415476a0fa9b2cf23e6c23 [file] [log] [blame]
Victor Stinner4ffe9c22018-06-14 14:58:13 +02001import math
Victor Stinner4d299832019-04-26 04:08:53 +02002import os.path
3import sys
Victor Stinner7ad16eb2018-06-01 11:04:45 +02004import textwrap
5
6
7def format_duration(seconds):
Victor Stinner4ffe9c22018-06-14 14:58:13 +02008 ms = math.ceil(seconds * 1e3)
9 seconds, ms = divmod(ms, 1000)
10 minutes, seconds = divmod(seconds, 60)
11 hours, minutes = divmod(minutes, 60)
Victor Stinner7ad16eb2018-06-01 11:04:45 +020012
Victor Stinner4ffe9c22018-06-14 14:58:13 +020013 parts = []
Victor Stinner7ad16eb2018-06-01 11:04:45 +020014 if hours:
Victor Stinner4ffe9c22018-06-14 14:58:13 +020015 parts.append('%s hour' % hours)
16 if minutes:
17 parts.append('%s min' % minutes)
18 if seconds:
19 parts.append('%s sec' % seconds)
20 if ms:
21 parts.append('%s ms' % ms)
22 if not parts:
23 return '0 ms'
24
25 parts = parts[:2]
26 return ' '.join(parts)
Victor Stinner7ad16eb2018-06-01 11:04:45 +020027
28
29def removepy(names):
30 if not names:
31 return
32 for idx, name in enumerate(names):
33 basename, ext = os.path.splitext(name)
34 if ext == '.py':
35 names[idx] = basename
36
37
38def count(n, word):
39 if n == 1:
40 return "%d %s" % (n, word)
41 else:
42 return "%d %ss" % (n, word)
43
44
45def printlist(x, width=70, indent=4, file=None):
46 """Print the elements of iterable x to stdout.
47
48 Optional arg width (default 70) is the maximum line length.
49 Optional arg indent (default 4) is the number of blanks with which to
50 begin each line.
51 """
52
53 blanks = ' ' * indent
54 # Print the sorted list: 'x' may be a '--random' list or a set()
55 print(textwrap.fill(' '.join(str(elt) for elt in sorted(x)), width,
56 initial_indent=blanks, subsequent_indent=blanks),
57 file=file)
Victor Stinner4d299832019-04-26 04:08:53 +020058
59
60def print_warning(msg):
61 print(f"Warning -- {msg}", file=sys.stderr, flush=True)