/
condition.py
134 lines (107 loc) · 3.99 KB
/
condition.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# ----------------------------------------------------------------------------
# Copyright (C) 2021-2022 Deepchecks (https://www.deepchecks.com)
#
# This file is part of Deepchecks.
# Deepchecks is distributed under the terms of the GNU Affero General
# Public License (version 3 or later).
# You should have received a copy of the GNU Affero General Public License
# along with Deepchecks. If not, see <http://www.gnu.org/licenses/>.
# ----------------------------------------------------------------------------
#
"""Module containing all the base classes for checks."""
import enum
from typing import Callable, Dict, cast
from deepchecks.core.errors import DeepchecksValueError
__all__ = [
'Condition',
'ConditionResult',
'ConditionCategory'
]
class Condition:
"""Contain condition attributes.
Parameters
----------
name : str
function : Callable
params : Dict
"""
name: str
function: Callable
params: Dict
def __init__(self, name: str, function: Callable, params: Dict):
if not isinstance(function, Callable):
raise DeepchecksValueError(f'Condition must be a function `(Any) -> Union[ConditionResult, bool]`, '
f'but got: {type(function).__name__}')
if not isinstance(name, str):
raise DeepchecksValueError(f'Condition name must be of type str but got: {type(name).__name__}')
self.name = name
self.function = function
self.params = params
def __call__(self, *args, **kwargs) -> 'ConditionResult':
"""Run this condition."""
result = cast(ConditionResult, self.function(*args, **kwargs))
result.set_name(self.name)
return result
class ConditionCategory(enum.Enum):
"""Condition result category. indicates whether the result should fail the suite."""
FAIL = 'FAIL'
WARN = 'WARN'
PASS = 'PASS'
ERROR = 'ERROR'
class ConditionResult:
"""Contain result of a condition function.
Parameters
----------
category : ConditionCategory
The category to which the condition result belongs.
details : str
What actually happened in the condition.
"""
is_pass: bool
category: ConditionCategory
details: str
name: str
def __init__(self, category: ConditionCategory, details: str = ''):
self.details = details
self.category = category
def set_name(self, name: str):
"""Set name to be displayed in table.
Parameters
----------
name : str
Description of the condition to be displayed.
"""
self.name = name
@property
def priority(self) -> int:
"""Return priority of the current condition.
This value is primarily used to determine the order in which
conditions should be displayed.
Returns
-------
int
condition priority value.
"""
if self.category == ConditionCategory.PASS:
return 4
elif self.category == ConditionCategory.FAIL:
return 1
elif self.category == ConditionCategory.WARN:
return 2
return 3 # if error
@property
def is_pass(self) -> bool:
"""Return true if the category is PASS."""
return self.category == ConditionCategory.PASS
def get_icon(self):
"""Return icon of the result to display."""
if self.category == ConditionCategory.PASS:
return '<div style="color: green;text-align: center">\U00002713</div>'
elif self.category == ConditionCategory.FAIL:
return '<div style="color: red;text-align: center">\U00002716</div>'
elif self.category == ConditionCategory.WARN:
return '<div style="color: orange;text-align: center;font-weight:bold">\U00000021</div>'
return '<div style="color: firebrick;text-align: center;font-weight:bold">\U00002048</div>'
def __repr__(self):
"""Return string representation for printing."""
return str(vars(self))