Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove long list of if/elif, use dict #48

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 22 additions & 33 deletions src/andromede/expression/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,39 +118,28 @@ def visit(root: ExpressionNode, visitor: ExpressionVisitor[T]) -> T:
"""
Utility method to dispatch calls to the right method of a visitor.
"""
if isinstance(root, LiteralNode):
return visitor.literal(root)
elif isinstance(root, NegationNode):
return visitor.negation(root)
elif isinstance(root, VariableNode):
return visitor.variable(root)
elif isinstance(root, ParameterNode):
return visitor.parameter(root)
elif isinstance(root, ComponentParameterNode):
return visitor.comp_parameter(root)
elif isinstance(root, ComponentVariableNode):
return visitor.comp_variable(root)
elif isinstance(root, AdditionNode):
return visitor.addition(root)
elif isinstance(root, MultiplicationNode):
return visitor.multiplication(root)
elif isinstance(root, DivisionNode):
return visitor.division(root)
elif isinstance(root, SubstractionNode):
return visitor.substraction(root)
elif isinstance(root, ComparisonNode):
return visitor.comparison(root)
elif isinstance(root, TimeOperatorNode):
return visitor.time_operator(root)
elif isinstance(root, TimeAggregatorNode):
return visitor.time_aggregator(root)
elif isinstance(root, ScenarioOperatorNode):
return visitor.scenario_operator(root)
elif isinstance(root, PortFieldNode):
return visitor.port_field(root)
elif isinstance(root, PortFieldAggregatorNode):
return visitor.port_field_aggregator(root)
raise ValueError(f"Unknown expression node type {root.__class__}")
TYPES = {
LiteralNode: "literal",
NegationNode: "negation",
VariableNode: "variable",
ParameterNode: "parameter",
ComponentParameterNode: "comp_parameter",
ComponentVariableNode: "comp_variable",
AdditionNode: "addition",
MultiplicationNode: "multiplication",
DivisionNode: "division",
SubstractionNode: "substraction",
ComparisonNode: "comparison",
TimeOperatorNode: "time_operator",
TimeAggregatorNode: "time_aggregator",
ScenarioOperatorNode: "scenario_operator",
PortFieldNode: "port_field",
PortFieldAggregatorNode: "port_field_aggregator",
}
Comment on lines +121 to +138
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to define this object once and for all, and not evaluate it for each function call. However I don't remember how to achieve this in Python.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe using a single dispatch from functools ?
https://docs.python.org/3/library/functools.html#functools.singledispatch

Copy link
Contributor

@ianmnz ianmnz Aug 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, you already did what they proposed here:
https://docs.python.org/3/faq/programming.html#how-do-i-use-strings-to-call-functions-methods

You could change the key from a string to the function itself (or a lambda), maybe ?

if type(root) in TYPES:
return getattr(visitor, TYPES[type(root)])(root)
else:
raise ValueError(f"Unknown expression node type {root.__class__}")


class SupportsOperations(Protocol[T]):
Expand Down
Loading