'''business_checker.py'''
from operator import lt, gt, le, ge
import numpy as np
import pandas as pd
from dqa.core.tools.declare.except_handler import except_handler
class BusinessChecker:
'''Class checks matches between attributes with
comparison operator.
Class attributes:
operators: dictionary of comparison operator
transformation from string to function
Object attributes:
_log: log for analysis
_case_id: name of the column with case IDs
_matching_attributes: Series, that stores matches
between attributes
'''
operators = {
'<': lt,
'>': gt,
'<=': le,
'>=': ge
}
@except_handler
def init(self, log: pd.DataFrame, case_id: str,
atribute1: str, atribute2: str, data_type: str,
operator: str, dayfirst: bool=True):
'''Initialization of a class object BusinessChecker.
:param log: log for analysis
:type log: pd.DatatFrame
:param case_id: column name
:type case_id: str
:param atribute1: column name
:type atribute1: str
:param atribute2: column name
:type atribute2: str
:param data_type: data type of columns attribute1
and attribute2
:type data_type: str
:param operator: comparison operator for attribute1
and attribute2
:type operator: str
:param dayfirst: position of the day in the date
:type dayfirst: bool
'''
self._log = log
self._case_id = case_id
self._matching_attributes = self.__check_matching(
atribute1, atribute2, data_type, operator, dayfirst
)
def __check_matching(self, atribute1: str, atribute2: str, data_type: str,
operator: str, dayfirst: bool=True) -> pd.Series:
'''Initialization of a class object BusinessChecker.
:param atribute1: column name
:type atribute1: str
:param atribute2: column name
:type atribute2: str
:param data_type: data type of columns attribute1
and attribute2
:type data_type: str
:param operator: comparison operator for attribute1
and attribute2
:type operator: str
:param dayfirst: position of the day in the date
:type dayfirst: bool
:return: matching series between attribute1
and attribute2
:rtype: pd.Series
'''
if operator in self.operators:
if data_type == 'numeric':
self._log['left_col'] = pd.to_numeric(self._log[atribute1],
errors='coerce')
self._log['right_col'] = pd.to_numeric(self._log[atribute2],
errors='coerce')
elif data_type == 'datetime':
self._log['left_col'] = pd.to_datetime(self._log[atribute1],
errors='coerce',
dayfirst=dayfirst)
self._log['right_col'] = pd.to_datetime(self._log[atribute2],
errors='coerce',
dayfirst=dayfirst)
else:
raise TypeError(f"Received wrong data type: {data_type}")
matching_attrs = self.operators[operator](self._log['left_col'],
self._log['right_col'])
self._log = self._log.drop(['left_col', 'right_col'], axis=1)
return matching_attrs
else:
raise ValueError(f"Invalid operator: {operator}")
@property
def matching_attributes(self) -> pd.Series:
'''Return matching attributes for BusinessChecker'''
return self._matching_attributes
@except_handler
def getattr(self, name):
'''Raises an exception if the object does not have the attribute'''
raise AttributeError(f"Object has no attribute {name}")
@except_handler
def percentage_matching(self) -> int:
'''Returns the percentage match between two attributes.
:return: percentage matching
:rtype: int
'''
sum_matching = self._matching_attributes.sum(skipna=True)
percentage_matching = round(sum_matching /
len(self._matching_attributes) * 100)
return percentage_matching
@except_handler
def no_matching_rows(self) -> pd.DataFrame:
'''Returns all rows (as DataFrame) that don't match the
operator between two attributes.
:return: log with mismatches
:rtype: pd.DataFrame
'''
return self._log[self._matching_attributes == False]
@except_handler
def no_matching_case(self) -> np.ndarray:
'''Returns list of cases in which at least one row don't match
the operator between two attributes.
:return: list of cases
:rtype: np.ndarray
'''
return self._log[self._matching_attributes == False]
[self._case_id].unique()
def delete_no_matching_cases(self) -> pd.DataFrame:
'''Returns fitered DataFrame without cases in which at least
one row don't match the operator between two attributes.
:return: log without mismatches
:rtype: pd.DataFrame
'''
id_to_delete = self._log[self._matching_attributes == False]
[self._case_id].unique()
return self._log[~self._log[self._case_id].isin(id_to_delete)]
есть ли какие нибудь ошибки в коде?