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

add default in __str__ when default is given #280

Open
sdementen opened this issue Mar 15, 2024 · 0 comments · May be fixed by #281
Open

add default in __str__ when default is given #280

sdementen opened this issue Mar 15, 2024 · 0 comments · May be fixed by #281

Comments

@sdementen
Copy link
Contributor

the default of a trace does not appear in the string representation.
Could adapt str with

    def __str__(self):
        """A human-readable string representation (truncated if it gets too
        long).

        """

        def format_item(item):
            return "{!s}: {!s}".format(*item)

        MAX_LENGTH = 20
        half = MAX_LENGTH // 2
        if len(self) > MAX_LENGTH:
            one = ", ".join(format_item(_) for _ in self._d.items()[:half])
            two = ", ".join(format_item(_) for _ in self._d.items()[half:-half])
            three = ", ".join(format_item(_) for _ in self._d.items()[-half:])
            truncate_string = f"<...{len(self) - MAX_LENGTH} items...>"
            if len(truncate_string) < len(two):
                two = truncate_string
            items = ", ".join([one, two, three])
        else:
            items = ", ".join(format_item(_) for _ in self._d.items())
        default = f", default={self.default!r}".format(self.default) if self.default else ""
        return f"{type(self).__name__}({{{items}}}{default})"
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 a pull request may close this issue.

1 participant