On 07/03/2023 09:54, Valentin Berlier wrote:
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.
As Steven d'Aprano pointed out re a similar suggestion, this is not the same as my proposal, where
    " foo " & " bar " & " something "
would evaluated to
    "   foo bar something   "
Far from stripping the left side over and over, it doesn't strip it at all!  (Or the right side.) This is trickier to write using join.  And if the first or last string can be blank, or all whitespace, it is trickier still. As it is so easy to get these things wrong, perhaps having it built in is not such a terrible idea?😁
Best wishes
Rob Cliffe

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 -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/A7RPR3FSBXEMRYAUXJVYYROCHVHL7DVP/
Code of Conduct: http://python.org/psf/codeofconduct/

_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/J356SW4PG3KUDYFBLXAWDS723VCV3EVE/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to