Passed
Push — main ( a79885...50e5ea )
by Douglas
02:25
created

GlobalWarningUtils.common_never_substrings()   A

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
from __future__ import annotations
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
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__:
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable '__qualname__'
Loading history...
25
        """
26
        Common initialization, including setting a better formatter that doesn't say "WARNING:py.warnings:".
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (108/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
27
        """
28
29
        def new_formatter(message, category, filename, lineno, line=None):
0 ignored issues
show
Unused Code introduced by
The argument line seems to be unused.
Loading history...
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():
0 ignored issues
show
Coding Style Naming introduced by
Variable name "v" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
63
            cls.substring_never(v)
64
        for v in cls.common_once_substrings():
0 ignored issues
show
Coding Style Naming introduced by
Variable name "v" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
65
            cls.substring_once(v)
66
        return cls
67
68
    @classmethod
69
    def common_never_substrings(cls):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
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",
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (118/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
76
        ]
77
78
    @classmethod
79
    def common_once_substrings(cls):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
80
        return [
81
            "Trying to unpickle estimator",
82
        ]
83
84
85
__all__ = ["GlobalWarningUtils"]
86