-
Notifications
You must be signed in to change notification settings - Fork 4
Remove long list of if/elif, use dict #48
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
Conversation
src/andromede/expression/visitor.py
Outdated
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", | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 ?
bbd79fb
to
2cbe4d8
Compare
@sylvlecl should we merge this ? |
elif isinstance(root, PortFieldAggregatorNode): | ||
return visitor.port_field_aggregator(root) | ||
raise ValueError(f"Unknown expression node type {root.__class__}") | ||
TYPES = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure of the added value:
it weakens the type checking and possibilities of refactoring, without bringing much clarity or brevity.
Would be better with a mapping to the actual method, probably:
methods = {
LiteralNode: visitor.literal,
NegationNode: visitor.negation,
...
}
return methods[type(root)](root)
We could also use a match
in python 3.10+
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mypy is not happy with methods[type(root)](root)
as it does not know the type of the function it calls
After reviews of the different proposals in this PR, it seems that the original code is in fact the best for now |
No description provided.