Skip to content

Commit

Permalink
reduce branches (pylint)
Browse files Browse the repository at this point in the history
  • Loading branch information
CindyvdVries committed Jul 1, 2024
1 parent 4ee1bc5 commit 3b64234
Showing 1 changed file with 84 additions and 75 deletions.
159 changes: 84 additions & 75 deletions decoimpact/business/workflow/model_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3b64234

Please sign in to comment.