|
1
|
|
|
from __future__ import annotations |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
import os |
|
4
|
|
|
import warnings |
|
5
|
|
|
|
|
6
|
|
|
from pocketutils.core.exceptions import NotConstructableError |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class GlobalWarningUtils: |
|
10
|
|
|
""" |
|
11
|
|
|
Convenient API to add warning filters. |
|
12
|
|
|
Also provides ``init``, which sets a less-verbose warning formatter. |
|
13
|
|
|
|
|
14
|
|
|
Example: |
|
15
|
|
|
>>> GlobalWarningUtils.init()\ |
|
16
|
|
|
.filter_common_numeric()\ |
|
17
|
|
|
.substring_once('Number of features differ') |
|
18
|
|
|
""" |
|
19
|
|
|
|
|
20
|
|
|
def __init__(self): |
|
21
|
|
|
raise NotConstructableError(f"Do not instantiate {self.__class__.__name__}") |
|
22
|
|
|
|
|
23
|
|
|
@classmethod |
|
24
|
|
|
def init(cls) -> __qualname__: |
|
|
|
|
|
|
25
|
|
|
""" |
|
26
|
|
|
Common initialization, including setting a better formatter that doesn't say "WARNING:py.warnings:". |
|
|
|
|
|
|
27
|
|
|
""" |
|
28
|
|
|
|
|
29
|
|
|
def new_formatter(message, category, filename, lineno, line=None): |
|
|
|
|
|
|
30
|
|
|
return ( |
|
31
|
|
|
"%s:%s: %s: %s\n" % (os.path.basename(filename), lineno, category.__name__, message) |
|
32
|
|
|
).replace("WARNING:py.warnings:", "") |
|
33
|
|
|
|
|
34
|
|
|
warnings.formatwarning = new_formatter |
|
35
|
|
|
return cls |
|
36
|
|
|
|
|
37
|
|
|
@classmethod |
|
38
|
|
|
def filter(cls, **kwargs) -> __qualname__: |
|
39
|
|
|
"""Same as warnings.filterwarnings.""" |
|
40
|
|
|
warnings.filterwarnings(**kwargs) |
|
41
|
|
|
return cls |
|
42
|
|
|
|
|
43
|
|
|
@classmethod |
|
44
|
|
|
def substring_never(cls, substring: str) -> __qualname__: |
|
45
|
|
|
"""Adds a filter containing this substring, never showing the warning.""" |
|
46
|
|
|
warnings.filterwarnings(message=".*" + substring + ".*", action="ignore") |
|
47
|
|
|
return cls |
|
48
|
|
|
|
|
49
|
|
|
@classmethod |
|
50
|
|
|
def substring_once(cls, substring: str) -> __qualname__: |
|
51
|
|
|
"""Adds a filter containing this substring, warning only once.""" |
|
52
|
|
|
warnings.filterwarnings(message=".*" + substring + ".*", action="once") |
|
53
|
|
|
return cls |
|
54
|
|
|
|
|
55
|
|
|
@classmethod |
|
56
|
|
|
def filter_common_numeric(cls) -> __qualname__: |
|
57
|
|
|
""" |
|
58
|
|
|
Adds filters for common unavoidable warnings from numpy, pandas, scikit-learn, etc. |
|
59
|
|
|
|
|
60
|
|
|
See ``common_never_substrings`` and ``common_once_substrings``. |
|
61
|
|
|
""" |
|
62
|
|
|
for v in cls.common_never_substrings(): |
|
|
|
|
|
|
63
|
|
|
cls.substring_never(v) |
|
64
|
|
|
for v in cls.common_once_substrings(): |
|
|
|
|
|
|
65
|
|
|
cls.substring_once(v) |
|
66
|
|
|
return cls |
|
67
|
|
|
|
|
68
|
|
|
@classmethod |
|
69
|
|
|
def common_never_substrings(cls): |
|
|
|
|
|
|
70
|
|
|
return [ |
|
71
|
|
|
"libuv only supports millisecond timer resolution", |
|
72
|
|
|
"or '1type' as a synonym of type is deprecated", |
|
73
|
|
|
"Series.nonzero() is deprecated and will be removed in a future version", |
|
74
|
|
|
"Monkey-patching ssl after ssl has already been imported may lead to errors", |
|
75
|
|
|
"your performance may suffer as PyTables will pickle object types that it cannot map directly to c-types", |
|
|
|
|
|
|
76
|
|
|
] |
|
77
|
|
|
|
|
78
|
|
|
@classmethod |
|
79
|
|
|
def common_once_substrings(cls): |
|
|
|
|
|
|
80
|
|
|
return [ |
|
81
|
|
|
"Trying to unpickle estimator", |
|
82
|
|
|
] |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
__all__ = ["GlobalWarningUtils"] |
|
86
|
|
|
|