-
Notifications
You must be signed in to change notification settings - Fork 0
/
cc_str.py
124 lines (104 loc) · 2.64 KB
/
cc_str.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
'字符串相关'
import re,sys,time,datetime,traceback
from pprint import pprint
UNITS = 'KMGTPEZYBND'
def strsize(x):
unit = 'B'
for du in UNITS:
if x < 10:
return '%.2f %s' % (x,unit)
if x < 1000:
return '%.1f %s' % (x,unit)
unit = du + 'B'
x /= 1024.0
return '%g %s' % (x,unit)
year_len = 365.2422 #一年的天数
mon_len = year_len / 12
def strtime(a):
a *= 1e15
us = ['fs','ps','ns','us','ms','s','min','h', 'days','months','years','centuries']
ks = [1000,1000,1000,1000,1000, 60, 60, 24,mon_len, 12, 100,]
n = len(ks)
for i in range(n):
if a < ks[i]:
du = us[i]
break
a /= ks[i]
else:
du = us[n]
return '%.8g %s' % (a,du)
def strcount(n:int):
s = str(n)
ans = []
while s:
ans.append(s[-3:])
s = s[:-3]
ans.reverse()
return ','.join(ans)
def log_info(s:str,f=sys.stdout):
dt = datetime.datetime.now()
ts = dt.isoformat(' ')
msg = f'[{ts}]{s}\n'
f.write(msg)
f.flush()
def get_time_fn() -> str:
'根据时间产生文件名'
dt = datetime.datetime.now()
ts = dt.isoformat('_')
ts = re.sub(r'[/\-:]','',ts)
ts = ts.replace('.','_')
dt,_ = ts.split('_',1)
return ts
class prog_logger:
'进度提示器'
def __init__(self):
self.start()
def start(self):
self.t = time.time()
def finish(self):
log_info('finish')
def log(self,i,n,delay=300,tip=''):
dt = time.time()
if dt < self.t + delay:return
self.t = dt
log_info(f'{i}/{n} {tip}')
class prog_logger2(prog_logger):
def __init__(self):
super().__init__()
self.n = 0
def log(self,dn,delay=300):
self.n += dn
dt = time.time()
if dt < self.t + delay:return
self.t = dt
log_info(str(self.n))
def finish(self):
log_info(f'finish {self.n}')
def join_re_pts(pts,sep='|',add_bra=True,escape=False):
if escape:
pts = map(re.escape,pts)
if add_bra:
pts = (f'({t})' for t in pts)
return sep.join(pts)
def show_exc():
log_info('Exception')
traceback.print_exc(file=sys.stdout)
log_info('Stack')
traceback.print_stack(file=sys.stdout)
def call_with_exc(func,*a,**k):
try:
func(*a,**k)
except SystemExit:
raise
except:
show_exc()
raise
def _test():
xs = (0,1,100,10**7,123<<30,907<<30,1008<<30,234<<180)
for x in xs:
log_info(strsize(x))
for x in xs:
log_info(strtime(x))
print(1/0)
if __name__=='__main__':
call_with_exc(_test)