- Notifications
You must be signed in to change notification settings - Fork 31.8k
/
Copy pathutils.py
64 lines (50 loc) · 1.56 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
importmath
importos.path
importsys
importtextwrap
defformat_duration(seconds):
ms=math.ceil(seconds*1e3)
seconds, ms=divmod(ms, 1000)
minutes, seconds=divmod(seconds, 60)
hours, minutes=divmod(minutes, 60)
parts= []
ifhours:
parts.append('%s hour'%hours)
ifminutes:
parts.append('%s min'%minutes)
ifseconds:
ifparts:
# 2 min 1 sec
parts.append('%s sec'%seconds)
else:
# 1.0 sec
parts.append('%.1f sec'% (seconds+ms/1000))
ifnotparts:
return'%s ms'%ms
parts=parts[:2]
return' '.join(parts)
defremovepy(names):
ifnotnames:
return
foridx, nameinenumerate(names):
basename, ext=os.path.splitext(name)
ifext=='.py':
names[idx] =basename
defcount(n, word):
ifn==1:
return"%d %s"% (n, word)
else:
return"%d %ss"% (n, word)
defprintlist(x, width=70, indent=4, file=None):
"""Print the elements of iterable x to stdout.
Optional arg width (default 70) is the maximum line length.
Optional arg indent (default 4) is the number of blanks with which to
begin each line.
"""
blanks=' '*indent
# Print the sorted list: 'x' may be a '--random' list or a set()
print(textwrap.fill(' '.join(str(elt) foreltinsorted(x)), width,
initial_indent=blanks, subsequent_indent=blanks),
file=file)
defprint_warning(msg):
print(f"Warning -- {msg}", file=sys.stderr, flush=True)