[Python-ideas] Re: Ampersand operator for strings

2023-03-06 Thread Stephen J. Turnbull
Rob Cliffe writes:

 > Perhaps where you're not laying out a table,

I'm an economist, laying out tables is what I do. :-)  Getting
serious:

 > but constructing a  human-readable string?  So
 >      s1 + ' ' + s2 + ' ' + s3
 > or
 >      ' '.join((s1, s3, s3))
 > would become
 >      s1 & s2 & s3
 > saving you a bit of typing.  Just sayin'.

str '+' has long been quite rare in my coding.  str concatenation is
almost never in an inner loop, or slighly more complex formatting is
the point.  f-strings and .format save you the type conversion to str.
So I don't find that occasional saving at all interesting.  A
vanishingly small number of my str constructions involve only strs
with trivial formatting.

What interests me about the proposal is the space-collapsing part,
which a naive f-string would do incorrectly if, say, s2 == '' or s3 ==
'\t\t\ttabs to the left of me'.  But where does this space-surrounded
str data come from?

Steve
___
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/HTEPBAI6EKXTUSU5SYZDZJUCOWHQN6C7/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Ampersand operator for strings

2023-03-06 Thread Rob Cliffe via Python-ideas



On 06/03/2023 15:49, Stephen J. Turnbull wrote:

Steven D'Aprano writes:
  > I like the look of the & operator for concatenation, so I want to like
  > this proposal. But I think I will need to see real world code to
  > understand when it would be useful.

I have to second that motion.  Pretty much any time I'm constructing
lines containing variable text, each string value arrives stripped and
I far more often want padding of variable width values rather than
space compression.
Understood.  But perhaps in (the tiny minority? of) cases where you 
*don't* want padding, you would like single space separators? 
Perhaps where you're not laying out a table, but constructing a 
human-readable string?  So

    s1 + ' ' + s2 + ' ' + s3
or
    ' '.join((s1, s3, s3))
would become
    s1 & s2 & s3
saving you a bit of typing.  Just sayin'.
Best wishes
Rob Cliffe


I admit that I use M-SPC (aka just-one-space) lot in Emacsen, but I
can't recall wanting it in a program in any language.
___
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/DZXQJ4GQTDEEMOO6VX4VJE45ZN7XNMR4/
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/5W6E6JU7Y7BWYG26YFVKUXU2RCCMGQWJ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Ampersand operator for strings

2023-03-06 Thread Stephen J. Turnbull
Steven D'Aprano writes:
 > I like the look of the & operator for concatenation, so I want to like 
 > this proposal. But I think I will need to see real world code to 
 > understand when it would be useful.

I have to second that motion.  Pretty much any time I'm constructing
lines containing variable text, each string value arrives stripped and
I far more often want padding of variable width values rather than
space compression.

I admit that I use M-SPC (aka just-one-space) lot in Emacsen, but I
can't recall wanting it in a program in any language.
___
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/DZXQJ4GQTDEEMOO6VX4VJE45ZN7XNMR4/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Ampersand operator for strings

2023-03-06 Thread Joao S. O. Bueno
On Mon, Mar 6, 2023 at 7:37 AM Steven D'Aprano  wrote:

> (...)


> I like the look of the & operator for concatenation, so I want to like
> this proposal. But I think I will need to see real world code to
> understand when it would be useful.
>
I'd say we paint the shed blue.
I mean - maybe "|" is more pleasant when thinking about concatenation.


>
>
> --
> Steve
>
>
___
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/LYZ3ZAQNIUP3CCNWDGZWYAGBFLUFPHK2/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Ampersand operator for strings

2023-03-06 Thread Joao S. O. Bueno
On Mon, Mar 6, 2023 at 12:51 AM David Mertz, Ph.D. 
wrote:

> Is it really that much longer to write `f"{s1} {s2}"` when you want that?
>

As for being that much longer: yes it is.

The more important factor is, I think, the increase in complexity +
readabiity for default strings is worth it in this case.

One nice thing to think about might be how to make string subclassing to be
more useful - so this kind of thing could be done for whoever needs it in
one more idiomatic project. The drawback is how cumbersome it is to
instantiate a string subclass compared to a string literal. (I just got an
idea, but it would be too offtopic here - if I think it is worth, I will
post it in a new thread later)




>
> Maybe a couple characters more total, but once you are in an f-string, you
> can also do a zillion other things at the same time.
>
> On Sun, Mar 5, 2023 at 10:42 PM Rob Cliffe via Python-ideas <
> python-ideas@python.org> wrote:
>
>> Tl;dr: Join strings together with exactly one space between non-blank
>> text where they join.
>>
>> I propose a meaning for
>>  s1 & s2
>> where s1 and s2 are strings.
>> Namely, that it should be equivalent to
>>  s1.rstrip() + (' ' if (s1.strip() and s2.strip()) else '') +
>> s2.lstrip()
>> Informally, this will join the two strings together with exactly one space
>> between the last non-blank text in s1 and the first non-blank text in s2.
>> Example:  " bar " & "foo"==" bar
>> foo"
>>
>> This operator is associative, so there is no ambiguity in expressions
>> such as
>>  s1 & s2 & s3
>> There *is* a possible ambiguity in expressions such as
>>  s1 & s2 + s3
>> where the relative precedence of `&` and `+` matters when s2 consists
>> solely of white space.  E.g.
>>  " A " & "  " + " C" would evaluate
>> to " A C"
>> not to " A C"
>> because `+` has a higher precedence than '&'.
>>
>> Utility:
>>  In decades of experience with another language which had such an
>> operator
>>  (spelt differently) I have frequently found it useful for
>> constructing
>>  human-readable output (e.g. log output, debug/error messages,
>> on-screen labels).
>>
>> Cognitive burden:
>>  This would of course be one more thing to learn.
>>  But I suggest that it is fairly intuitive that
>>  s1 + s2
>>  s1 & s2
>>  both suggest that two strings are being combined in some way.
>>
>> Bars to overcome:
>>  This change would require no fundamental change to Python;
>>  just adding an `__and__ function to the str class.
>>
>> Backward compatibility:
>>  Given that `str1 & str2` currently raises TypeError,
>>  this change would be close to 100% backward-compatible.
>>
>> Alternative meanings:
>>  As far as I know nobody has ever suggested an alternative meaning
>> for `&` on strings.
>>
>> Bikeshedding:
>> (1) I don't think it is important for the utility of this change
>>  whether `&` strips off all whitespace, or just spaces.
>>  I think it is better if it strips off all whitespace,
>>  so that it can be understood as working similarly to strip().
>> (2) The definition could be simplified to
>>  s1.rstrip() + ' ' + s2.lstrip()
>>  (leave an initial/final space when one string is whitespace
>> only).
>>  Again the operator would be associative.
>>  Again I don't think this is important.
>>
>> Rob Cliffe
>> ___
>> 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/66XP7GY56XU7H3P52IJENLSWJFW53XIN/
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>
>
> --
> The dead increasingly dominate and strangle both the living and the
> not-yet born.  Vampiric capital and undead corporate persons abuse
> the lives and control the thoughts of homo faber. Ideas, once born,
> become abortifacients against new conceptions.
> ___
> 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/M7H3VZFEUJFZDO3BAAVUGXNKBH6WF4NA/
> 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/2XXY57LCSNNS6SVRUIYBSA6QT2P7RH4F/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Ampersand operator for strings

2023-03-06 Thread Marc-Andre Lemburg

On 06.03.2023 11:33, Steven D'Aprano wrote:

On Mon, Mar 06, 2023 at 10:33:26AM +0100, Marc-Andre Lemburg wrote:


def join_words(list_of_words)
 return ' '.join([x.strip() for x in list_of_words])


That's not Rob's suggestion either.


I know, but as I mentioned, I use the above often, whereas I find Rob's 
definition not very intuitive or useful.



Rob's suggestion is an operator which concats two substrings with
exactly one space between them, without stripping leading or trailing
whitespace of the result.

Examples:

 a = "\nHeading:"
 b = "Result\n\n"
 a & b

would give "\nHeading: Result\n\n"

 s = "my hovercraft\n"
 t = "is full of eels\n"
 s & t

would give "my hovercraft is full of eels\n"

I find the concept is very easy to understand: "concat with exactly one
space between the operands".  But I must admit I'm struggling to think
of cases where I would use it.

I like the look of the & operator for concatenation, so I want to like
this proposal. But I think I will need to see real world code to
understand when it would be useful.


--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Experts (#1, Mar 06 2023)
>>> Python Projects, Coaching and Support ...https://www.egenix.com/
>>> Python Product Development ...https://consulting.egenix.com/


::: We implement business ideas - efficiently in both time and costs :::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
   Registered at Amtsgericht Duesseldorf: HRB 46611
   https://www.egenix.com/company/contact/
 https://www.malemburg.com/

___
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/E7DCRBG5I7RJPIHWSNDZPEBG7UVJEGMR/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Ampersand operator for strings

2023-03-06 Thread Steven D'Aprano
On Mon, Mar 06, 2023 at 10:33:26AM +0100, Marc-Andre Lemburg wrote:

> def join_words(list_of_words)
> return ' '.join([x.strip() for x in list_of_words])

That's not Rob's suggestion either.

Rob's suggestion is an operator which concats two substrings with 
exactly one space between them, without stripping leading or trailing 
whitespace of the result.

Examples:

a = "\nHeading:"
b = "Result\n\n"
a & b

would give "\nHeading: Result\n\n"

s = "my hovercraft\n"
t = "is full of eels\n"
s & t

would give "my hovercraft is full of eels\n"

I find the concept is very easy to understand: "concat with exactly one 
space between the operands".  But I must admit I'm struggling to think 
of cases where I would use it.

I like the look of the & operator for concatenation, so I want to like 
this proposal. But I think I will need to see real world code to 
understand when it would be useful.


-- 
Steve
___
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/2OZXSXETHQLWJQYAM2S3SJVACPEIPDSZ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Ampersand operator for strings

2023-03-06 Thread Marc-Andre Lemburg

On 02.03.2023 18:27, Rob Cliffe via Python-ideas wrote:
Tl;dr: Join strings together with exactly one space between non-blank 
text where they join.


I propose a meaning for
     s1 & s2
where s1 and s2 are strings.
Namely, that it should be equivalent to
     s1.rstrip() + (' ' if (s1.strip() and s2.strip()) else '') + 
s2.lstrip()

Informally, this will join the two strings together with exactly one space
between the last non-blank text in s1 and the first non-blank text in s2.
Example:  " bar " & "    foo    "    ==    " bar 
foo    "


I don't find these semantics particularly intuitive. Python already has 
the + operator for concatenating strings and this doesn't apply any 
stripping.


If you're emphasizing on joining words with single space delimiters, 
then the usual:


def join_words(list_of_words)
return ' '.join([x.strip() for x in list_of_words])

works much better. You can also apply this recursively, if needed, or 
add support for list_of_phrases (first splitting these into a 
list_of_words).


The advantage of join_words() is that it's easy to understand and 
applies stripping in a concise way. I use such helpers all the time.


--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Experts (#1, Mar 06 2023)
>>> Python Projects, Coaching and Support ...https://www.egenix.com/
>>> Python Product Development ...https://consulting.egenix.com/


::: We implement business ideas - efficiently in both time and costs :::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
   Registered at Amtsgericht Duesseldorf: HRB 46611
   https://www.egenix.com/company/contact/
 https://www.malemburg.com/

___
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/IQWVHHNXNFFVWCSSLHWVFF5LCAPVSR2J/
Code of Conduct: http://python.org/psf/codeofconduct/