-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
62 lines (41 loc) · 1.36 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
import os
from datetime import datetime
def get_date_format():
return '%Y-%m-%d %H:%M:%S'
def get_current_datetime():
now = datetime.now()
return datetime.strptime(now.strftime(get_date_format()), get_date_format())
def get_current_datetime_w_us_str():
now = datetime.now()
return now.strftime('%Y%m%d_%H%M%S_%f')
def format_fsize(bytes_size):
if not isinstance(bytes_size, int):
return None
units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
threshold = 1024
if bytes_size == 0:
return '0B'
size = bytes_size
unit_index = 0
while size >= threshold and unit_index < len(units) - 1:
size /= threshold
unit_index += 1
formatted_size = f'{size:.1f}{units[unit_index]}'
if formatted_size.endswith('.0'):
formatted_size = formatted_size[:-2]
return formatted_size
def format_time_bin(minutes):
s = ''
if minutes < 60:
s = f'{round(minutes, 1)}m' if minutes != 1 else '1m'
elif minutes < 1440:
hours = minutes / 60
s = f'{round(hours, 1)}h' if hours != 1 else '1h'
else:
days = minutes / 1440
s = f'{int(days)}d' if days != 1 else '1d'
return s
def get_bcount_from_string(s):
return len(s.encode('utf-8'))
def make_path(*filepaths):
return os.path.join(os.path.abspath(os.path.dirname(__file__)), *filepaths)