Skip to content

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

Closed
wants to merge 3 commits into from
Closed

Conversation

flomnes
Copy link
Member

@flomnes flomnes commented Aug 13, 2024

No description provided.

@flomnes flomnes requested a review from sylvlecl August 13, 2024 09:39
Comment on lines 121 to 156
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",
}
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 ?

@tbittar tbittar force-pushed the fix/remove-if-elif branch from bbd79fb to 2cbe4d8 Compare April 11, 2025 10:04
@tbittar
Copy link
Collaborator

tbittar commented Apr 11, 2025

@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 = {
Copy link
Member

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+

Copy link
Collaborator

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

@tbittar
Copy link
Collaborator

tbittar commented Apr 11, 2025

After reviews of the different proposals in this PR, it seems that the original code is in fact the best for now

@tbittar tbittar closed this Apr 11, 2025
@tbittar tbittar deleted the fix/remove-if-elif branch April 11, 2025 14:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants