-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
62 lines (51 loc) · 1.68 KB
/
util.py
File metadata and controls
62 lines (51 loc) · 1.68 KB
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
from typing import Any
def printt(s: str) -> None:
print(s)
def make_error_string(e: Exception) -> str:
"""
Standard way for the app to display exceptions
"""
return f"{type(e).__name__}: {e}"
def clamp(value, min, max) -> Any:
if value < min:
return min
if value > max:
return max
return value
def make_position_string(position: float, whole_seconds: bool=False) -> str:
"""
Convert a position in seconds to a formatted timestamp string.
"H:MM:SS" or "H:MM:SS.cc", etc
"""
total_seconds = int(position)
centiseconds = int((position - total_seconds) * 100)
hours = total_seconds // 3600
minutes = (total_seconds % 3600) // 60
seconds = total_seconds % 60
if hours > 0:
s = f"{hours}:{minutes:02d}:{seconds:02d}"
elif minutes > 0:
s = f"{minutes}:{seconds:02d}"
else:
s = f"0:{seconds}" if whole_seconds else f"{seconds}"
if not whole_seconds:
s += f".{centiseconds:02d}"
return s
def make_position_range_string(start: float, end: float, whole_seconds: bool=False) -> str:
return f"({make_position_string(start, whole_seconds)}-{make_position_string(end, whole_seconds)})"
def make_duration_string(duration: float) -> str:
"""
Make a string using format "1h5m3s", etc
"""
total_seconds = int(duration)
centiseconds = int((duration - total_seconds) * 100)
hours = total_seconds // 3600
minutes = (total_seconds % 3600) // 60
seconds = total_seconds % 60
if hours > 0:
s = f"{hours}h{minutes}m:{seconds}s"
elif minutes > 0:
s = f"{minutes}m{seconds}s"
else:
s = f"{seconds}"
return s