-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdebug.py
49 lines (37 loc) · 1.68 KB
/
debug.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
''' Some debug helpers. '''
import inspect, os, re
from typing import Any
def dprint(*args: Any) -> None:
frame = inspect.currentframe().f_back # type: ignore
name = os.path.basename(inspect.getframeinfo(frame).filename)
with open(inspect.getframeinfo(frame).filename) as f:
line = f.readlines()[frame.f_lineno - 1] # type: ignore
m = re.match(r'\s*dprint\((.*)\)\s*', line)
if m:
print(f'{name}:{frame.f_lineno}', m.group(1), *args)
else:
print(f'{name}:{frame.f_lineno} dprint parse error', *args)
def assert_eq(*args: Any) -> None:
assert len(args) == 2
if args[0] != args[1]:
frame = inspect.currentframe().f_back # type: ignore
name = os.path.basename(inspect.getframeinfo(frame).filename)
with open(inspect.getframeinfo(frame).filename) as f:
line = f.readlines()[frame.f_lineno - 1] # type: ignore
m = re.match(r'\s*(assert_eq\(.*\))\s*', line)
if m:
print(f'{name}:{frame.f_lineno} assertion failed:', m.group(1))
print(f'{args[0]} != {args[1]}')
assert False
def assert_ne(*args: Any) -> None:
assert len(args) == 2
if args[0] == args[1]:
frame = inspect.currentframe().f_back # type: ignore
name = os.path.basename(inspect.getframeinfo(frame).filename)
with open(inspect.getframeinfo(frame).filename) as f:
line = f.readlines()[frame.f_lineno - 1] # type: ignore
m = re.match(r'\s*(assert_ne\(.*\))\s*', line)
if m:
print(f'{name}:{frame.f_lineno} assertion failed:', m.group(1))
print(f'{args[0]} == {args[1]}')
assert False