I'm -1 on this. You can easily make a helper that achieves the desired syntax.
Presenting "human readable data" isn't just about collapsing spaces, and having
your own helper means that you can adjust the formatting to your specific use
case if needed (for example with a different separator).
from typing import Self
class StripJoin:
def __init__(self, value: str = "") -> None:
self.value = value
def __and__(self, other: str) -> Self:
other = other.strip()
separator = bool(self.value and other) * " "
return StripJoin(f"{self.value}{separator}{other}")
def __str__(self) -> str:
return self.value
j = StripJoin()
print(j & " foo " & " bar " & " something ")
# Output: "foo bar something"
The example above is more efficient than a possible implementation directly on
the str builtin as it doesn't strip the left side over and over. However it
still incurs repeated allocations and encourages a pattern that performs badly
in loops. With a lot of input you should probably accumulate the stripped
strings in a list and join them all at once.
In any case I recommend reaching out for a library like Rich
(https://github.com/Textualize/rich) if you care about formatting the output of
your program nicely.
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/A7RPR3FSBXEMRYAUXJVYYROCHVHL7DVP/
Code of Conduct: http://python.org/psf/codeofconduct/