-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
53 lines (40 loc) · 1.19 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
DURATION_UNITS = ('days', 'hours', 'minutes', 'seconds')
def iter_deconstruct_duration(duration):
"""
Deconstructs the duration into days, hours, minutes, seconds.
This function is an iterable generator.
Parameters
----------
duration : `TimeDelta`
The duration to deconstruct.
Yields
------
unit : `int`
"""
yield duration.days
hours, seconds = divmod(duration.seconds, 60 * 60)
yield hours
yield from divmod(seconds, 60)
def build_duration_string(duration):
"""
Converts the mute duration to string.
Parameters
----------
duration : `TimeDelta`
Mute duration.
Returns
-------
duration_string : `str`
"""
string_parts = []
field_added = False
for unit_value, unit_name in zip(iter_deconstruct_duration(duration), DURATION_UNITS):
if unit_value:
if field_added:
string_parts.append(', ')
else:
field_added = True
string_parts.append(str(unit_value))
string_parts.append(' ')
string_parts.append(unit_name)
return ''.join(string_parts)