blob: 85049cb06b36498147edb126cf13e1bed58380b3 [file] [log] [blame]
Victor Stinner3dd802d2018-06-01 12:29:39 +02001import os.path
2import textwrap
3
4
5def format_duration(seconds):
6 if seconds < 1.0:
7 return '%.0f ms' % (seconds * 1e3)
8 if seconds < 60.0:
9 return '%.0f sec' % seconds
10
11 minutes, seconds = divmod(seconds, 60.0)
12 hours, minutes = divmod(minutes, 60.0)
13 if hours:
14 return '%.0f hour %.0f min' % (hours, minutes)
15 else:
16 return '%.0f min %.0f sec' % (minutes, seconds)
17
18
19def removepy(names):
20 if not names:
21 return
22 for idx, name in enumerate(names):
23 basename, ext = os.path.splitext(name)
24 if ext == '.py':
25 names[idx] = basename
26
27
28def count(n, word):
29 if n == 1:
30 return "%d %s" % (n, word)
31 else:
32 return "%d %ss" % (n, word)
33
34
35def printlist(x, width=70, indent=4, file=None):
36 """Print the elements of iterable x to stdout.
37
38 Optional arg width (default 70) is the maximum line length.
39 Optional arg indent (default 4) is the number of blanks with which to
40 begin each line.
41 """
42
43 blanks = ' ' * indent
44 # Print the sorted list: 'x' may be a '--random' list or a set()
45 print(textwrap.fill(' '.join(str(elt) for elt in sorted(x)), width,
46 initial_indent=blanks, subsequent_indent=blanks),
47 file=file)