-
Notifications
You must be signed in to change notification settings - Fork 3
/
common.py
53 lines (39 loc) · 1.1 KB
/
common.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
# Common functions
import struct
def rreplace(s, old, new, occurrence=1) -> str:
li = s.rsplit(old, occurrence)
return new.join(li)
def hex_spaced(s, sep=' ') -> str:
return sep.join(["{:02x}".format(x) for x in s])
def ts2str(ts, sep=':') -> str:
res = []
for part in [1000, 60, 60]:
res.append(ts % part)
ts //= part
res = "{:02d}:{:02d}:{:02d}{}{:03d}".format(
ts, res[2], res[1], sep, res[0])
return res
def str2ts(*args) -> int:
args = [int(v) for v in reversed(args)]
factors = [1, 1000, 60, 60]
factor = 1
res = 0
for i in range(len(factors)):
factor *= factors[i]
res += args[i]*factor
return res
def read_float(src, size) -> float or None:
buf = src.read(size)
if not buf:
return None
return struct.unpack("<d", buf)[0]
def read_int(src, size) -> int or None:
buf = src.read(size)
if not buf:
return None
return int.from_bytes(buf, 'little', signed=True)
def lines_count(name):
with open(name) as f:
for i, l in enumerate(f):
pass
return i + 1