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

Override __eq__ methods of Node and other UXsim object? #135

Closed
toruseo opened this issue Sep 16, 2024 · 3 comments
Closed

Override __eq__ methods of Node and other UXsim object? #135

toruseo opened this issue Sep 16, 2024 · 3 comments
Labels
enhancement New feature or request wontfix This will not be worked on

Comments

@toruseo
Copy link
Owner

toruseo commented Sep 16, 2024

Currently we can specify Node (or Link, Vehicle) by Node object itself or its name which is str. This is sometimes confusing and causes a bug. It may be possible to solve this issue by overriding __eq__ method (==) properly so that something like node == "node_name" holds true if node.name = "node_name".

Also, it will be great if we can use Node object and node name as a dictionary key interchangeably.

But I am not familiar to overriding __eq__ method, so this requires some study.

@toruseo toruseo added the enhancement New feature or request label Sep 16, 2024
@EwoutH
Copy link
Contributor

EwoutH commented Sep 16, 2024

I have found it confusing at times that the Node id and name are used in different situations. I would suggest always using the id in UXsim internally, and leaving the name just for something the user can use to track different nodes.

We recently had a similar discussion over at Mesa, it might be interesting:

@toruseo
Copy link
Owner Author

toruseo commented Sep 17, 2024

yes, that is also confusing. Refactoring the internal code will be difficult, but let me think if there are feasible solutions.

Thanks for the info too. It is interesting that many people are facing similar issues.

@toruseo
Copy link
Owner Author

toruseo commented Oct 3, 2024

I have tried the following functions. It worked well for most of the cases. However, it turned out that it causes unexpected, very counter intuitive errors in some cases. There must be workarounds, but I now believe modifying these fundamental functions just for convenience is not very wise. Therefore, I abandon this plan

    def __eq__(self, other):
        """
        Override `==` operator to compare objects by name as well as object itself.
        """
        if isinstance(other, str):
            return self.name == other
        elif isinstance(other, Vehicle):
            return self.name == other.name
        return False
    
    def __hash__(self):
        """
        Override hash function, in order to make dict of objects can be accessible by their name as well.
        """
        return hash(self.name)

@toruseo toruseo closed this as completed Oct 3, 2024
@toruseo toruseo added the wontfix This will not be worked on label Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants