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

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 +138
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 ?

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.

2 participants