-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathutil.py
93 lines (68 loc) · 2.31 KB
/
util.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
from datetime import timedelta
import functools
import logging
from typing import Optional, TypeVar, Union, List, Any
TType = TypeVar("TType")
import os
import sys
import config
import tempfile
def flatten_list(lst):
return [item for sublist in lst for item in sublist]
def unzip(lst):
res = [[i for i, j in lst], [j for i, j in lst]]
return res
def pad(lst, index):
if index >= len(lst):
lst += [None] * (index + 1 - len(lst))
return lst
def print_time_delta(prefix, start_time, end_time):
## Always output time in the log.
time_difference = (end_time - start_time) / timedelta(milliseconds=1)
## If output_time flag is set, log the time
if config.OUTPUT_TIME:
log("{} time:".format(prefix), time_difference, " ms", level=0)
else:
log("{} time:".format(prefix), time_difference, " ms")
## This function decorates a function to add the logging prefix (without missing the old prefix)
def logging_prefix(logging_prefix):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
old_prefix = config.LOGGING_PREFIX
config.LOGGING_PREFIX = logging_prefix
result = func(*args, **kwargs)
config.LOGGING_PREFIX = old_prefix
return result
return wrapper
return decorator
## This is a wrapper for prints
def log(*args, end="\n", level=1):
## If the debug logging level is at least
## as high as this log message.
## TODO: Allow all levels
if level >= 1:
concatted_args = " ".join([str(a) for a in list(args)])
logging.info(f"{config.LOGGING_PREFIX} {concatted_args}")
def ptempfile():
fd, name = tempfile.mkstemp(dir=config.PASH_TMP_PREFIX)
## TODO: Get a name without opening the fd too if possible
os.close(fd)
return name
def return_empty_list_if_none_else_itself(
arg: Optional[TType],
) -> Union[TType, List[Any]]: # list always empty
if arg is None:
return []
else:
return arg
def return_default_if_none_else_itself(arg: Optional[TType], default: TType) -> TType:
if arg is None:
return default
else:
return arg
## This function gets a key and a value from the ast json format
def get_kv(dic):
return (dic[0], dic[1])
def make_kv(key, val):
return [key, val]