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