From 3b642348c7240849439a5e0901e854ffad7898f5 Mon Sep 17 00:00:00 2001 From: CindyvdVries Date: Mon, 1 Jul 2024 16:21:54 +0200 Subject: [PATCH] reduce branches (pylint) --- decoimpact/business/workflow/model_builder.py | 159 +++++++++--------- 1 file changed, 84 insertions(+), 75 deletions(-) diff --git a/decoimpact/business/workflow/model_builder.py b/decoimpact/business/workflow/model_builder.py index fc9deb2e..a174344c 100644 --- a/decoimpact/business/workflow/model_builder.py +++ b/decoimpact/business/workflow/model_builder.py @@ -92,82 +92,91 @@ def _set_default_fields(rule_data: IRuleData, rule: RuleBase): @staticmethod def _create_rule(rule_data: IRuleData) -> IRule: - if isinstance(rule_data, IMultiplyRuleData): - rule = MultiplyRule( - rule_data.name, - [rule_data.input_variable], - rule_data.multipliers, - rule_data.date_range, - ) - elif isinstance(rule_data, IDepthAverageRuleData): - rule = DepthAverageRule( - rule_data.name, - rule_data.input_variables, - ) - elif isinstance(rule_data, ILayerFilterRuleData): - rule = LayerFilterRule( - rule_data.name, - [rule_data.input_variable], - rule_data.layer_number, - ) - elif isinstance(rule_data, IAxisFilterRuleData): - rule = AxisFilterRule( - rule_data.name, - [rule_data.input_variable], - rule_data.element_index, - rule_data.axis_name, - ) - elif isinstance(rule_data, IStepFunctionRuleData): - rule = StepFunctionRule( - rule_data.name, - rule_data.input_variable, - rule_data.limits, - rule_data.responses, - ) - elif isinstance(rule_data, ITimeAggregationRuleData): - rule = TimeAggregationRule( - rule_data.name, [rule_data.input_variable], rule_data.operation - ) - rule.settings.percentile_value = rule_data.percentile_value - rule.settings.time_scale = rule_data.time_scale - elif isinstance(rule_data, IRollingStatisticsRuleData): - rule = RollingStatisticsRule( - rule_data.name, [rule_data.input_variable], rule_data.operation - ) - rule.settings.percentile_value = rule_data.percentile_value - rule.settings.time_scale = rule_data.time_scale - rule.period = rule_data.period - elif isinstance(rule_data, ICombineResultsRuleData): - rule = CombineResultsRule( - rule_data.name, - rule_data.input_variable_names, - MultiArrayOperationType[rule_data.operation_type], - ) - elif isinstance(rule_data, IResponseCurveRuleData): - rule = ResponseCurveRule( - rule_data.name, - rule_data.input_variable, - rule_data.input_values, - rule_data.output_values, - ) - elif isinstance(rule_data, IFormulaRuleData): - rule = FormulaRule( - rule_data.name, - rule_data.input_variable_names, - rule_data.formula, - ) - elif isinstance(rule_data, IClassificationRuleData): - rule = ClassificationRule( - rule_data.name, rule_data.input_variable_names, rule_data.criteria_table - ) - else: - error_str = ( - f"The rule type of rule '{rule_data.name}' is currently " - "not implemented" - ) - raise NotImplementedError(error_str) + + match rule_data: + case IMultiplyRuleData(): + rule = MultiplyRule( + rule_data.name, + [rule_data.input_variable], + rule_data.multipliers, + rule_data.date_range, + ) + case IDepthAverageRuleData(): + rule = DepthAverageRule( + rule_data.name, + rule_data.input_variables, + ) + case ILayerFilterRuleData(): + rule = LayerFilterRule( + rule_data.name, + [rule_data.input_variable], + rule_data.layer_number, + ) + case IAxisFilterRuleData(): + rule = AxisFilterRule( + rule_data.name, + [rule_data.input_variable], + rule_data.element_index, + rule_data.axis_name, + ) + case IStepFunctionRuleData(): + rule = StepFunctionRule( + rule_data.name, + rule_data.input_variable, + rule_data.limits, + rule_data.responses, + ) + case ITimeAggregationRuleData(): + rule = TimeAggregationRule( + rule_data.name, [rule_data.input_variable], rule_data.operation + ) + rule.settings.percentile_value = rule_data.percentile_value + rule.settings.time_scale = rule_data.time_scale + + case IRollingStatisticsRuleData(): + rule = RollingStatisticsRule( + rule_data.name, [rule_data.input_variable], rule_data.operation + ) + rule.settings.percentile_value = rule_data.percentile_value + rule.settings.time_scale = rule_data.time_scale + rule.period = rule_data.period + + case ICombineResultsRuleData(): + rule = CombineResultsRule( + rule_data.name, + rule_data.input_variable_names, + MultiArrayOperationType[rule_data.operation_type], + ) + + case IResponseCurveRuleData(): + rule = ResponseCurveRule( + rule_data.name, + rule_data.input_variable, + rule_data.input_values, + rule_data.output_values, + ) + + case IFormulaRuleData(): + rule = FormulaRule( + rule_data.name, + rule_data.input_variable_names, + rule_data.formula, + ) + + case IClassificationRuleData(): + rule = ClassificationRule( + rule_data.name, + rule_data.input_variable_names, + rule_data.criteria_table + ) + + case _: + error_str = ( + f"The rule type of rule '{rule_data.name}' is currently " + "not implemented" + ) + raise NotImplementedError(error_str) if isinstance(rule, RuleBase): ModelBuilder._set_default_fields(rule_data, rule) - return rule