1 import os.path 2 import math 3 import textwrap 4 5 6 def format_duration(seconds): 7 ms = math.ceil(seconds * 1e3) 8 seconds, ms = divmod(ms, 1000) 9 minutes, seconds = divmod(seconds, 60) 10 hours, minutes = divmod(minutes, 60) 11 12 parts = [] 13 if hours: 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) 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) 57