Victor Stinner | 7ad16eb | 2018-06-01 11:04:45 +0200 | [diff] [blame] | 1 | import os.path |
Victor Stinner | 4ffe9c2 | 2018-06-14 14:58:13 +0200 | [diff] [blame] | 2 | import math |
Victor Stinner | 7ad16eb | 2018-06-01 11:04:45 +0200 | [diff] [blame] | 3 | import textwrap |
| 4 | |
| 5 | |
| 6 | def format_duration(seconds): |
Victor Stinner | 4ffe9c2 | 2018-06-14 14:58:13 +0200 | [diff] [blame] | 7 | ms = math.ceil(seconds * 1e3) |
| 8 | seconds, ms = divmod(ms, 1000) |
| 9 | minutes, seconds = divmod(seconds, 60) |
| 10 | hours, minutes = divmod(minutes, 60) |
Victor Stinner | 7ad16eb | 2018-06-01 11:04:45 +0200 | [diff] [blame] | 11 | |
Victor Stinner | 4ffe9c2 | 2018-06-14 14:58:13 +0200 | [diff] [blame] | 12 | parts = [] |
Victor Stinner | 7ad16eb | 2018-06-01 11:04:45 +0200 | [diff] [blame] | 13 | if hours: |
Victor Stinner | 4ffe9c2 | 2018-06-14 14:58:13 +0200 | [diff] [blame] | 14 | 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 Stinner | 7ad16eb | 2018-06-01 11:04:45 +0200 | [diff] [blame] | 26 | |
| 27 | |
| 28 | def 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 | |
| 37 | def count(n, word): |
| 38 | if n == 1: |
| 39 | return "%d %s" % (n, word) |
| 40 | else: |
| 41 | return "%d %ss" % (n, word) |
| 42 | |
| 43 | |
| 44 | def 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) |