-
Notifications
You must be signed in to change notification settings - Fork 0
/
a_more_useful_print.py
53 lines (44 loc) · 1.46 KB
/
a_more_useful_print.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
from datetime import datetime
from inspect import currentframe, getframeinfo
def super_print(
active=True,
presentation="|#count@line - time|---> result",
start=0, step=1,
suffix="\n", endfix="\n"
):
count = start
last_time = datetime.now()
def format_presentation(line_number, input_string):
current_time = datetime.now()
time_diff = current_time - last_time
time_str = f"{time_diff.seconds}.{time_diff.microseconds}"
formatted_line = (
presentation
.replace("count", str(count))
.replace("line", str(line_number))
.replace("time", time_str)
.replace("result", input_string)
)
if 'result' not in presentation:
formatted_line += f': {input_string}'
return formatted_line
def print_logger(string="", active=active):
nonlocal count, last_time
if active:
caller_frame = currentframe().f_back
line_number = getframeinfo(caller_frame).lineno
formatted_string = ''.join([
suffix,
format_presentation(line_number, str(string)),
endfix
])
print(formatted_string)
count += step
last_time = datetime.now()
return print_logger
if __name__ == "__main__":
# Example usage
pp = super_print(active=True, presentation=">line@time: `result`")
pp('start')
a_dict = {'a': 1}
pp(a_dict)