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

Custom annotations do not appear in Atom __repr__() #602

Closed
ljarosch opened this issue Jun 17, 2024 · 1 comment · Fixed by #604
Closed

Custom annotations do not appear in Atom __repr__() #602

ljarosch opened this issue Jun 17, 2024 · 1 comment · Fixed by #604
Labels

Comments

@ljarosch
Copy link
Contributor

ljarosch commented Jun 17, 2024

When adding custom annotations to an AtomArray, they are not displayed in the repr() of that array. I think this is because the annotations are hardcoded in the __repr__ method of Atom.

Showing all annotations would make it more straightforward to interactively do structural analysis, especially in a Notebook context where you frequently print atom arrays.

Below is a suggestion for a potential fix. If this would be an interesting feature to be added to Biotite I'm happy to make a PR.

def __repr__(self):
    # print out key-value pairs and format strings in quotation marks
    annot_parts = [
        f'{key}="{value}"' if isinstance(value, str) else f'{key}={value}'
        for key, value in self._annot.items()
    ]

    annot = ', '.join(annot_parts)
    return f'Atom(np.{np.array_repr(self.coord)}, {annot})'
@padix-key
Copy link
Member

Since repr() is intended to return actual code that recreates an object (https://docs.python.org/3/library/functions.html#repr), I would even consider the current behavior a bug, as information would be missing. Hence, your PR would be welcome.

@padix-key padix-key added the bug label Jun 17, 2024
ljarosch added a commit to ljarosch/Issue-602-biotite that referenced this issue Jun 17, 2024
This changes the hardcoded Atom __repr__() to a more general version that will additionally print custom added attributes.
ljarosch added a commit to ljarosch/Issue-602-biotite that referenced this issue Jun 19, 2024
padix-key pushed a commit that referenced this issue Jun 21, 2024
* Fix Atom __repr__() (#602)

This changes the hardcoded Atom __repr__() to a more general version that will additionally print custom added attributes.

* Add back docstring

* Add new contributor (PR for #602)
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 a pull request may close this issue.

2 participants