-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
48 lines (32 loc) · 1.11 KB
/
helpers.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
import logging
import sys
from itertools import chain, repeat, islice
import re
import enums
def pad_infinite(iterable, padding=None):
return chain(iterable, repeat(padding))
def pad(iterable, size, padding=None):
return list(islice(pad_infinite(iterable, padding), size))
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
def setup_logger(name, log_file, level=logging.INFO):
"""To setup as many loggers as you want"""
handler = logging.FileHandler(log_file)
handler.setFormatter(formatter)
logger = logging.getLogger(name)
logger.setLevel(level)
logger.addHandler(handler)
return logger
def is_relevant(item):
if re.match(r'_(?!.*di|.*log|_).*', item[0]):
return True
else:
return False
def process_intcode_exception(logger, pc, program, err):
logger.exception(err)
logger.error(f'Exception occurred at {pc}')
window_size = 7
start = pc - window_size
end = pc + window_size if len(program) > pc + window_size else len(program)
logger.error(f'{start}:{end}')
logger.error(f'{program[start:end]}')
sys.exit(-1)