Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/numpy/distutils/log.py
7757 views
1
# Colored log
2
import sys
3
from distutils.log import * # noqa: F403
4
from distutils.log import Log as old_Log
5
from distutils.log import _global_log
6
7
from numpy.distutils.misc_util import (red_text, default_text, cyan_text,
8
green_text, is_sequence, is_string)
9
10
11
def _fix_args(args,flag=1):
12
if is_string(args):
13
return args.replace('%', '%%')
14
if flag and is_sequence(args):
15
return tuple([_fix_args(a, flag=0) for a in args])
16
return args
17
18
19
class Log(old_Log):
20
def _log(self, level, msg, args):
21
if level >= self.threshold:
22
if args:
23
msg = msg % _fix_args(args)
24
if 0:
25
if msg.startswith('copying ') and msg.find(' -> ') != -1:
26
return
27
if msg.startswith('byte-compiling '):
28
return
29
print(_global_color_map[level](msg))
30
sys.stdout.flush()
31
32
def good(self, msg, *args):
33
"""
34
If we log WARN messages, log this message as a 'nice' anti-warn
35
message.
36
37
"""
38
if WARN >= self.threshold:
39
if args:
40
print(green_text(msg % _fix_args(args)))
41
else:
42
print(green_text(msg))
43
sys.stdout.flush()
44
45
46
_global_log.__class__ = Log
47
48
good = _global_log.good
49
50
def set_threshold(level, force=False):
51
prev_level = _global_log.threshold
52
if prev_level > DEBUG or force:
53
# If we're running at DEBUG, don't change the threshold, as there's
54
# likely a good reason why we're running at this level.
55
_global_log.threshold = level
56
if level <= DEBUG:
57
info('set_threshold: setting threshold to DEBUG level,'
58
' it can be changed only with force argument')
59
else:
60
info('set_threshold: not changing threshold from DEBUG level'
61
' %s to %s' % (prev_level, level))
62
return prev_level
63
64
def get_threshold():
65
return _global_log.threshold
66
67
def set_verbosity(v, force=False):
68
prev_level = _global_log.threshold
69
if v < 0:
70
set_threshold(ERROR, force)
71
elif v == 0:
72
set_threshold(WARN, force)
73
elif v == 1:
74
set_threshold(INFO, force)
75
elif v >= 2:
76
set_threshold(DEBUG, force)
77
return {FATAL:-2,ERROR:-1,WARN:0,INFO:1,DEBUG:2}.get(prev_level, 1)
78
79
80
_global_color_map = {
81
DEBUG:cyan_text,
82
INFO:default_text,
83
WARN:red_text,
84
ERROR:red_text,
85
FATAL:red_text
86
}
87
88
# don't use INFO,.. flags in set_verbosity, these flags are for set_threshold.
89
set_verbosity(0, force=True)
90
91
92
_error = error
93
_warn = warn
94
_info = info
95
_debug = debug
96
97
98
def error(msg, *a, **kw):
99
_error(f"ERROR: {msg}", *a, **kw)
100
101
102
def warn(msg, *a, **kw):
103
_warn(f"WARN: {msg}", *a, **kw)
104
105
106
def info(msg, *a, **kw):
107
_info(f"INFO: {msg}", *a, **kw)
108
109
110
def debug(msg, *a, **kw):
111
_debug(f"DEBUG: {msg}", *a, **kw)
112
113