-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
87 lines (71 loc) · 1.91 KB
/
util.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from datetime import datetime
import traceback
import time
import os
media_types = [
'audio',
'document',
'photo',
'sticker',
'animation',
'video',
'voice',
'video_note'
]
def time_us():
return int(time.time() * 1000000)
def time_ms():
return int(time_us() / 1000)
def format_duration_us(t_us):
t_us = int(t_us)
t_ms = t_us / 1000
t_s = t_ms / 1000
t_m = t_s / 60
t_h = t_m / 60
t_d = t_h / 24
if t_d >= 1:
rem_h = t_h % 24
rem_m = t_m % 60
rem_s = t_s % (24 * 60 * 60) % 60
return '%dd%dh%dm%ds' % (t_d, rem_h, rem_m, rem_s)
elif t_h >= 1:
rem_m = t_m % 60
rem_s = t_s % (60 * 60) % 60
return '%dh%dm%ds' % (t_h, rem_m, rem_s)
elif t_m >= 1:
rem_s = t_s % 60
return '%dm%ds' % (t_m, rem_s)
elif t_s >= 10:
return '%ds' % t_s
elif t_ms >= 10:
return '%d ms' % t_ms
else:
return '%d μs' % t_us
def find_prefixed_funcs(obj, prefix):
results = []
for sym in dir(obj):
if sym.startswith(prefix):
name = sym[len(prefix):]
func = getattr(obj, sym)
if not callable(func):
continue
results.append((name, func))
return results
def filter_input_block(inp):
if inp.startswith('```') and inp.endswith('```'):
inp = inp[3:][:-3]
elif inp.startswith('`') and inp.endswith('`'):
inp = inp[1:][:-1]
return inp
def format_exception(exp):
tb = traceback.extract_tb(exp.__traceback__)
# Replace absolute paths with relative paths
cwd = os.getcwd()
for frame in tb:
if cwd in frame.filename:
frame.filename = os.path.relpath(frame.filename)
stack = ''.join(traceback.format_list(tb))
msg = str(exp)
if msg:
msg = ': ' + msg
return f'Traceback (most recent call last):\n{stack}{type(exp).__name__}{msg}'