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

Prevent the same object being added more than once to a given list #1414

Merged
merged 3 commits into from
Feb 23, 2024

Conversation

apdavison
Copy link
Member

This relates to #1405 - in the scenario in that issue, a ValueError will be raised.

(note: the change of attribute name from contents to _items is because _handle_append is also used by SpikeTrainList, which uses the name _items).

Copy link
Collaborator

@zm711 zm711 left a comment

Choose a reason for hiding this comment

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

Just one tiny question/suggestion for the error, but otherwise this looks good to me. I want to test this locally this afternoon and then I'll sign off. But I agree that biologically it really doesn't make sense to allow someone to add the exact same object multiple times.

@@ -32,6 +32,10 @@ def _handle_append(self, obj):
)
):
raise TypeError(f"Object is a {type(obj)}. It should be one of {self.allowed_contents}.")

if self._contains(obj):
raise ValueError("Cannot add the same object twice")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a way to be more descriptive here. You can add the same type of object, but not the same object. I'm just a little worried someone might get confused.

Suggested change
raise ValueError("Cannot add the same object twice")
raise ValueError(f"Cannot add {obj} because it is already contained within {self}")

Not sure if that works or if you need something else.

Copy link
Member Author

Choose a reason for hiding this comment

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

The string representation of data objects can be very long, so I don't think it's a good idea to put it in exception messages. How about "Cannot add this object because it is already contained within the list"?

Copy link
Collaborator

Choose a reason for hiding this comment

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

yeah that sounds good!

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@zm711 zm711 merged commit 9892c95 into NeuralEnsemble:master Feb 23, 2024
41 checks passed
@apdavison apdavison deleted the prevent-duplicates branch April 4, 2024 12:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants