Skip to content

parser_formula_rule

Module for Parser FormulaRule class

!!! classes FormulaRuleParser

ParserFormulaRule (IParserRuleBase)

Class for creating a FormulaRuleData

Source code in parsers/parser_formula_rule.py
class ParserFormulaRule(IParserRuleBase):
    """Class for creating a FormulaRuleData"""

    @property
    def rule_type_name(self) -> str:
        """Type name for the rule"""
        return "formula_rule"

    def parse_dict(self, dictionary: Dict[str, Any], logger: ILogger) -> IRuleData:
        """Parses the provided dictionary to an IRuleData
        Args:
            dictionary (Dict[str, Any]): Dictionary holding the values
                                         for making the rule
        Returns:
            RuleBase: Rule based on the provided data
        """
        name = get_dict_element("name", dictionary, True)
        input_variable_names = get_dict_element("input_variables", dictionary, True)
        formula: str = get_dict_element("formula", dictionary, True)
        self._validate_formula(formula)
        output_variable_name = get_dict_element("output_variable", dictionary)
        description = get_dict_element("description", dictionary, False)
        if not description:
            description = ""

        rule_data = FormulaRuleData(name, input_variable_names, formula)
        rule_data.output_variable = output_variable_name
        rule_data.description = description

        return rule_data

    def _validate_formula(self, formula: str):
        """
        Validates if the formula is well formed (a string)."""
        if not isinstance(formula, str):
            message = f"""Formula must be a string, \
                received: {formula} (type: {type(formula)})"""
            raise ValueError(message)

rule_type_name: str property readonly

Type name for the rule

parse_dict(self, dictionary, logger)

Parses the provided dictionary to an IRuleData

Parameters:

Name Type Description Default
dictionary Dict[str, Any]

Dictionary holding the values for making the rule

required

Returns:

Type Description
RuleBase

Rule based on the provided data

Source code in parsers/parser_formula_rule.py
def parse_dict(self, dictionary: Dict[str, Any], logger: ILogger) -> IRuleData:
    """Parses the provided dictionary to an IRuleData
    Args:
        dictionary (Dict[str, Any]): Dictionary holding the values
                                     for making the rule
    Returns:
        RuleBase: Rule based on the provided data
    """
    name = get_dict_element("name", dictionary, True)
    input_variable_names = get_dict_element("input_variables", dictionary, True)
    formula: str = get_dict_element("formula", dictionary, True)
    self._validate_formula(formula)
    output_variable_name = get_dict_element("output_variable", dictionary)
    description = get_dict_element("description", dictionary, False)
    if not description:
        description = ""

    rule_data = FormulaRuleData(name, input_variable_names, formula)
    rule_data.output_variable = output_variable_name
    rule_data.description = description

    return rule_data