Skip to content

add default in __str__ when default is given  #280

Description

@sdementen

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})"

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions