-
Notifications
You must be signed in to change notification settings - Fork 0
/
ensure.py
50 lines (45 loc) · 1.52 KB
/
ensure.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
import inspect, sys
def choice(prompt, allowed, default=None):
for i in range(10):
try: ret = input('\007'+prompt+' ('+', '.join(allowed)+'): ')
except EOFError:
if default != None:
assert default in allowed
ret = default
print('[default: '+repr(default)+']')
else: raise
if ret in allowed:
return ret
raise
error_count = 0
def error(*msg, frames_back=1):
global error_count
back = inspect.currentframe()
for i in range(frames_back):
back = back.f_back
msg = [back.f_code.co_filename.rsplit('/')[-1]+':'+str(back.f_lineno)] + list(msg)
print('\nERROR:',*msg)
if not sys.stdout.isatty() or not sys.stderr.isatty():
sys.stderr.write('ERROR: '+' '.join(str(i) for i in msg)+'\n')
error_count += 1
def check(test, *msg, frames_back=2):
if not test:
error(*msg, frames_back=frames_back)
return test
def ensure(test, *msg, frames_back=2):
if not test:
error(*msg, frames_back=frames_back)
choice('Continue despite above error? ', ['continue'])
return test
class isused_dict(dict):
__slots__ = ['__used']
def __init__(self, init):
dict.__init__(self, init)
self.__used = set()
def __getitem__(self, i):
self.__used.add(i)
return dict.__getitem__(self, i)
def __setitem__(self, i, n):
self.__used.add(i)
return dict.__setitem__(self, i, n)
def unused(self): return self.__used ^ set(self.keys())