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 string representation #3

Open
arthur-tacca opened this issue Feb 15, 2024 · 0 comments
Open

Add string representation #3

arthur-tacca opened this issue Feb 15, 2024 · 0 comments

Comments

@arthur-tacca
Copy link
Owner

At the moment, the classes in aioresult do not override the __str__() and __repr__() methods. That means that the string representation is, to put it mildly, not helpful:

<aioresult._src.ResultCapture object at 0x0000014DA6513B80>

You can't just log out the ResultBase.result() return value because it can raise an exception if the task is not done or has raised, and even ResultBase.exception() will throw if the task is not done. I think those are good design choices but they're annoying here. I suggest that the string representation could look like this:

ResultCapture(is_done=False)
ResultCapture(result="elephant")
ResultCapture(exception=KeyError(3))

These aren't real ways that you could construct a ResultCapture object but that's OK. I don't think there's any point including the other two fields (e.g. ResultCapture(is_done=True, result="elephant", exception=None)).

Another consideration is that ResultCapture equality is not overridden so is based only on the ID of the object. This is for the best because even two ResultCapture for the same routine and arguments that finish in the same way still represent two different runs of that routine. But it means that, from a purity point of view, the string representation should include the ... at 0x0000014DA6513B80 part. I think that, in practice, this would be more annoying than helpful.

All the above works for ResultBase including Future. But for ResultCapture we may want to include the routine and arguments, but not always. So I suggest allowing an alternative format string to include it; we could use "#" to match the character for alternative format of numbers. The output would look like this:

ResultCapture(routine=get_animal, args=(3,), result="elephant")

This code would do it:

class ResultBase:
    # ... current code ...

    def _routine_str(self):
        return ""  # Overridden in ResultCapture

    def __format__(self, format_spec):
        if format_spec not in ("", "#"):
            raise ValueError("ResultBase format must be '' or '#'")
        routine_str = self._routine_str() if format_spec == "#" else ""
        if not self.is_done():
            result_str = "is_done=False"
        elif self._exception is not None:
            result_str = "exception=" + repr(self._exception)
        else:
            result_str = "result=" + str(self._result)
        return f"{type(self).__name__}({routine_str}{result_str})"

    def __repr__(self):
        return self.__format__("")


class ResultCapture:
    # ... current code ...

    def _routine_str(self):
        """Allows the alternative format to include routine and arguments; used in __format__."""
        return f"routine={self.routine.__qualname__}, args={self.args}, "
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

No branches or pull requests

1 participant