blob: d36bf9196626db5c2230e550f3345e859f1208b9 [file] [log] [blame]
Victor Stinner3dd802d2018-06-01 12:29:39 +02001import os.path
Victor Stinnerd1f94812018-06-26 23:47:35 +02002import math
Victor Stinner3dd802d2018-06-01 12:29:39 +02003import textwrap
4
5
6def format_duration(seconds):
Victor Stinnerd1f94812018-06-26 23:47:35 +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 Stinner3dd802d2018-06-01 12:29:39 +020011
Victor Stinnerd1f94812018-06-26 23:47:35 +020012 parts = []
Victor Stinner3dd802d2018-06-01 12:29:39 +020013 if hours:
Victor Stinnerd1f94812018-06-26 23:47:35 +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 Stinner3dd802d2018-06-01 12:29:39 +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)