[Python-ideas] Re: Allowing `str.format` to format one or more parameters instead of all parameters

2023-04-21 Thread Matthias Görgens
What's the use-case for this?

Have you looked into using functools.partial instead?

On Sat, 22 Apr 2023, 06:23 Samuel Muldoon,  wrote:

> Consider the following string:
>
> x = r"\mathjax{{color}}{{text}}"
>
> string `x` contains two parameters named `color` and the other named `text
> `.
>
> Currently, python requires that the string class method `str.format` contain
> key-word arguments for *all *parameters, not just *one* parameter
>
> result = r"\mathjax{{color}}{{text}}".format(color = "blue" , text = 
> "Spanish")
>
> result = "\mathjax{blue}{Spanish}"
>
> Can we change str.format so that it is possible to change only one string 
> parameter, but leave the other parameters alone?
>
> # pfr is partially_formatted_result
>
> pfr= r"\mathjax{{color}}{{text}}".format(color = "blue")   # ERROR! 
> missing parameter `text`
>
> result = r"\mathjax{{color}}{{text}}".format(text = "Spanish") # ERROR! 
> missing parameter `color`
>
> The implementation requires fewer than ten lines of code in python, probably 
> less than fifty lines in C, or a different implementation language.
>
> class str:
> def format(self, **kwargs):
> for key in kwargs:
> x = "{"+key+"}"
> ostr   = kwargs[key].join(self.split(x))
> return ostr # output string
>
> As an example, everywhere a string contained `{text}` it will now say 
> `*Spanish*` .
>
>
> *Samuel Muldoon*
>
> *(720) 653 -2408*
>
> *muldoonsam...@gmail.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/2A6RTXKQ3LCMKDGDIPOX525O52KYQJS7/
> 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/UQV4S22X4X7SF5GAFK4MBRMEPPHJ6P3S/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Allowing `str.format` to format one or more parameters instead of all parameters

2023-04-21 Thread Greg Ewing

On 22/04/23 10:20 am, Samuel Muldoon wrote:
Can we change str.formatso that it is possible to change only one string 
parameter, but leave the other parameters alone?


That would have the effect that every use of str.format for everyone
would start producing partially-formatted strings if an argument is
accidentally omitted instead of raising an error. Some people might
not like that.

It would be better to write a separate function for doing partial
formatting.

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


[Python-ideas] Allowing `str.format` to format one or more parameters instead of all parameters

2023-04-21 Thread Samuel Muldoon
Consider the following string:

x = r"\mathjax{{color}}{{text}}"

string `x` contains two parameters named `color` and the other named `text`.

Currently, python requires that the string class method `str.format` contain
key-word arguments for *all *parameters, not just *one* parameter

result = r"\mathjax{{color}}{{text}}".format(color = "blue" , text = "Spanish")

result = "\mathjax{blue}{Spanish}"

Can we change str.format so that it is possible to change only one
string parameter, but leave the other parameters alone?

# pfr is partially_formatted_result

pfr= r"\mathjax{{color}}{{text}}".format(color = "blue")   #
ERROR! missing parameter `text`

result = r"\mathjax{{color}}{{text}}".format(text = "Spanish") #
ERROR! missing parameter `color`

The implementation requires fewer than ten lines of code in python,
probably less than fifty lines in C, or a different implementation
language.

class str:
def format(self, **kwargs):
for key in kwargs:
x = "{"+key+"}"
ostr   = kwargs[key].join(self.split(x))
return ostr # output string

As an example, everywhere a string contained `{text}` it will now say
`*Spanish*` .


*Samuel Muldoon*

*(720) 653 -2408*

*muldoonsam...@gmail.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/2A6RTXKQ3LCMKDGDIPOX525O52KYQJS7/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Protocols Subclassing Normal Classes

2023-04-21 Thread Chris Angelico
On Fri, 21 Apr 2023 at 22:57, Jordan Macdonald  wrote:
> However, I then encountered an issue: I could define a Protocol that 
> specified the 'stop()' method easily enough, but if I annotated the manager 
> as taking that, it would accept any class which implemented a method named 
> 'stop()', which was not correct; the manager should only accept threads which 
> implement such a method.
>

To what extent is that actually a problem? Does it need any other
features of the thread? My guess is that, after stopping the thread,
it may want to join() it; in that case, what you could do is add join
to the Protocol. Or whatever else is needed.

You're trying to hybridize duck typing and inheritance typing, which
seems odd. It should be possible to pick one or the other here.

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


[Python-ideas] Protocols Subclassing Normal Classes

2023-04-21 Thread Jordan Macdonald
We have a large codebase which uses threads. Many - but not all - of these
threads implement a method named 'stop()' which sets a flag, triggers an
event, closes a connection, or what-have-you in order to command the thread
in question to terminate.

I was writing a thread manager, intended to automatically terminate threads
in an organized way at shutdown. It could accept any thread which
implemented a 'stop()' method, so how could I type-hint it correctly?
'Aha!' said I, 'This is what those newfangled Protocol things are for! I
shall use one of them!' (We only recently updated from 3.7 to 3.11, so
quite a lot of features are still 'newfangled' to me.)

However, I then encountered an issue: I could define a Protocol that
specified the 'stop()' method easily enough, but if I annotated the manager
as taking that, it would accept *any* class which implemented a method
named 'stop()', which was not correct; the manager should only accept
*threads* which implement such a method. I couldn't add 'threading.Thread'
as a parent of the protocol; protocols aren't allowed to inherit from
normal classes. And there's no syntax for marking an argument as needing to
be *both* a given type and a given protocol.

My proposal is this: Currently, a Protocol is forbidden from inheriting
from a normal class, on the basis that it would break transitivity of
subtyping.

Instead, allow Protocols to inherit normal classes, with the rule that a
class is only considered to implement that protocol if it also inherits the
same normal classes. E.g.:

```python
import typing as _tp

class Base:
...

class MyProtocol(Base, _tp.Protocol):
def proto_method(self, string: str) -> bool:
raise NotImplementedError()

class Foo:
def proto_method(self, string: str) -> bool:
...

class Bar(Base):
def proto_method(self, string: str) -> str:
...

class Baz(Base):
def proto_method(self, string: str) -> bool:
...

class Zap(MyProtocol):
def proto_method(self, string: str) -> bool:
...

def my_func(proto: MyProtocol):
...

my_func(Foo())  # Invalid; `Foo` does not inherit `Base` and therefore does
not implement `MyProtocol` despite having the necessary method
my_func(Bar())  # Invalid; `Bar` does not implement the method with the
correct signature for `MyProtocol`
my_func(Baz())  # Valid; `baz` inherits `Base` explicitly
my_func(Zap())  # Valid; `Zap` inherits `Base` indirectly, via inheriting
`MyProtocol` explicitly
```

-- 
So many books, so little time... - Anon.

You haven't *lived*
'Till you've heard the floor ring
To the whoop and the call
Of 'Balance and swing!'
___
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/7YCLH2CUZAZEXSZXUKP2LQLG7P4RHMIV/
Code of Conduct: http://python.org/psf/codeofconduct/