[Python-ideas] Re: New explicit methods to trim strings

2020-03-22 Thread Kyle Stanley
Stephen J. Turnbull wrote:
> The only cases I can remember are files named things like
> "thesis.doc.doc" in GUI environments. ;-)

For edge cases like that, something like
`"thesis.doc.doc".removesuffix(".doc").removesuffix(".doc")` should
suffice, no? It may not be the cleanest looking solution, but IMO, it's
better than over-complicating the method for something that would be used
rarely at best.

On Mon, Mar 23, 2020 at 1:34 AM Stephen J. Turnbull <
turnbull.stephen...@u.tsukuba.ac.jp> wrote:

> Paul Moore writes:
>
>  > I've needed to remove one prefix/suffix. I've never needed to remove
>  > more than one.
>
> The only cases I can remember are files named things like
> "thesis.doc.doc" in GUI environments. ;-)
> ___
> 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/NT3ORR6E54EW3CKJGXGCN7VJWPAG5PBX/
> 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/3ALBB4MJG3ENCNCZ3VWHYYCOHP75O7WS/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: New explicit methods to trim strings

2020-03-22 Thread Stephen J. Turnbull
Paul Moore writes:

 > I've needed to remove one prefix/suffix. I've never needed to remove
 > more than one.

The only cases I can remember are files named things like
"thesis.doc.doc" in GUI environments. ;-)
___
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/NT3ORR6E54EW3CKJGXGCN7VJWPAG5PBX/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Developer messages on install (pip, poetry, etc.)

2020-03-22 Thread Ronie Martinez
Good day!

I have been developing in Node for a few months now for non-Python projects
(legacy project) and I found a NPM feature which could be helpful to
developers of Python libraries.

When you install a NPM package, some show a message from the author after
installation (see example below).

--

Thank you for using core-js ( https://github.com/zloirock/core-js ) for
polyfilling JavaScript standard library!


The project needs your help! Please consider supporting of core-js on Open
Collective or Patreon:

> https://opencollective.com/core-js

> https://www.patreon.com/zloirock


Also, the author of core-js ( https://github.com/zloirock ) is looking for
a good job -)
--

Is it possible to adopt this? My proposal is to add an entry in the package
metadata which I believe is safer and easier to implement compared to
running a post-installation script. It is up for developers of package
managers (pip, poetry, etc.) to actually print these on the terminal.

I appreciate comments and feedback.

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


[Python-ideas] Re: Instance method to test equivalence between set and iterable

2020-03-22 Thread Guido van Rossum
On Sun, Mar 22, 2020 at 6:51 PM Steven D'Aprano  wrote:

> We might have a terminology issue here, since according to Wikipedia
> there is some dispute over whether or not to include the equality case
> in subset/superset:
>
> https://en.wikipedia.org/wiki/Subset
>
> For what it is worth, I'm in the school that subset implies proper subset
> [...]
>

Wikipedia's pedantry notwithstanding, I don't think this is a useful
position *when talking about Python sets*, since Python's set's .issubset()
method returns True when the argument is the same set:

>>> {1}.issubset({1})
True

Or did I miss a wink?

-- 
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*

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


[Python-ideas] Re: Instance method to test equivalence between set and iterable

2020-03-22 Thread Andrew Barnert via Python-ideas
> On Mar 22, 2020, at 18:54, Steven D'Aprano  wrote:
> 
> Do you have an example of `A <= B and B <= A` aside from the `A == B` 
> case?

For mathematical sets, this is either impossible by definition, or impossible 
by 2-line proof, depending on which definitions you like. For Python sets, 
presumably you could come up with something pathological involving some kind of 
“anti-NaN” value that’s equal to everything?

But I don’t think that’s relevant here. You claimed that 'the "arbitrary 
iterables" part is a distraction', but I think it’s actually the whole point of 
the proposal. The initial suggestion is that there are lots of iterables that 
are both a subset and a superset of some set, and only the ones that are sets 
are equal to the set, and not having a way to test for the ones that aren’t 
sets is the “missing functionality” that needs to be added for completeness.

And that’s what you’re missing at the end of your previous message: you don’t 
see “how `.issubset(other) and .issuperset(other)` can be true” if 
the set and other aren’t equal: it’s true whenever other isn’t a set. And if 
you need it more concrete:

>>> {1.2}.issubset((1,2))
True
>>> {1.2}.issuperset((1,2))
True
>>> {1,2} == (1,2)
False

So, asking for an example of ones that are sets but still can’t be tested for 
equality is non sequitur. The OP didn’t expect there to be any, and isn’t 
trying to solve that problem even if there are.

As far as I can tell, they’re just trying to add a method isequivalent or 
iscoextensive or whatever that extends beyond == to handle non-set iterables in 
the exact same way issubset and issuperset extend beyond <= and >= to handle 
non-set iterables.

That’s a perfectly coherent request, and of course arbitrary iterables are 
relevant to it. The question is why anyone would ever need that method. If 
someone has a use case for it, they should be able to post it easily.
___
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/L7SNOAGXR3ARHXSJTG4FQ6U56GVAN2UD/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Syntax for loop invariants

2020-03-22 Thread Cameron Simpson

On 21Mar2020 12:15, Stephen J. Turnbull  
wrote:

Steven D'Aprano writes:
> So each loop will need its own context manager, meaning its own
> class. The *actual* invariant will be buried far away, in a CM
> class, rather than right there next to the loop.

I see no reason why that will be true for many invariants.  The
context manager class would be simple, in principle: __entry__,
__exit__, and invariants.  The expression in the with statement would
be a constructor.

> Worse: the condition will likely need to be written as a string, or a
> lambda, in order to delay evaluation.

Or a local def.  This is correct, as far as I know.  I would expect
the example from Wikipedia
(https://en.wikipedia.org/wiki/Loop_invariant#Informal_example)
translated to Python would be written in the "condition as string"
implementation as

def max(n, a):

   m = a[0]

   # m equals the maximum value in a[0...0]
   i = 1
   while (i != n) {
   with Invariants('m == max(a[0:i])'):

[... and hacks with globals, locals ...]

Twio things:

1:
Might I point out to everyone the icontract PyPI module (maybe I've 
missing this in the discussion?), which does all this kind of thing for 
functions (programming by contract, thus the function level focus).


The important point here is that it uses lambda to express the 
assertions:


   @require(lambda foo: foo > 1)
   def func(foo):
   ...

There's a post condition decorator, and invariants etc. It does funky 
function signature inspection to match up the lambda parameter names to 
the function paraemeter names. very cool.


2:
Anyway, a salient point here is that you can write _arbitrary_ 
invariants as context managers using closures to get the locals/globals 
stuff (icontract isn't doing this, but it is heavy on the lambdas). For 
example, maybe:


   from ... import invariant_cm

   def max(n, a):
   m = a[0]
   # m equals the maximum value in a[0...0]
   i = 1
   while (i != n) {
   with invariant_cm(lambda: m == max(a[0:i])):

where invariant_cm came out of your library for checking before and 
after the suite. The closure gets you the locals/globals directly 
without cumbersomeness.


Cheers,
Cameron Simpson 
___
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/K5A6SMIU6QCGQIAADTR2SQHKO5YX47LN/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Instance method to test equivalence between set and iterable

2020-03-22 Thread Kyle Stanley
Steve Jorgensen wrote:
> Basically, it is for a sense of completeness. It feels weird that there
is a way to check whether an iterable is a subset of a set or a superset
> of a set but no way to directly ask whether it is equivalent to the set.

I can't say this has never happened historically, but I've yet to ever see
a change proposal accepted simply on the basis of "completeness". There
typically has to be some form of concrete, practical use case for the
feature. Otherwise, it's quite simply not going to be worth the review and
long-term maintenance cost to the limited core CPython development
resources.

> Even though the need for it might not be common, I think that the
collection of methods makes more sense if a method like this is present.

It's okay if the need isn't very common, but it still typically requires
some real use case to be clearly defined. Even if it might be fairly niche,
are there any?

(Note that if it's too niche, it's likely more suitable as a 3rd party
package.)

On Sun, Mar 22, 2020 at 8:11 PM Steve Jorgensen  wrote:

> Paul Moore wrote:
> > On Sun, 22 Mar 2020 at 20:01, Steve Jorgensen ste...@stevej.name wrote:
> > >
> > > Currently, the issubset and
> > > issuperset methods of set objects accept arbitrary iterables as
> arguments. An
> > > iterable that is both a subset and superset is, in a sense, "equal" to
> the set. It would
> > > be inappropriate for == to return True for such a comparison,
> > > however, since that would break the Hashable contract.
> > > Should sets have an additional method, something like like(other),
> > > issimilar(other), or isequivalent(other), that returns
> > > True for any iterable that contains the all of the items in the set
> and no
> > > items that are not in the set? It would therefore be true in the same
> cases where
> > >  = set(other) or .issubset(other) and
> > > .issuperset(other) is true.
> > > What is the practical use case for this? It seems like it would be a
> > pretty rare need, at best.
> > Paul
>
> Basically, it is for a sense of completeness. It feels weird that there is
> a way to check whether an iterable is a subset of a set or a superset of a
> set but no way to directly ask whether it is equivalent to the set.
>
> Even though the need for it might not be common, I think that the
> collection of methods makes more sense if a method like this is present.
> ___
> 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/MRCHHRVCXEUAB3HBV4WRMZ56O3HUJQYL/
> 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/F63RTRE56JNDUJ3P2ZKIIE5VISKACV3N/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Instance method to test equivalence between set and iterable

2020-03-22 Thread Christopher Barker
> there is a way to check whether an iterable is a subset of a set or a

> > superset of a set but no way to directly ask whether it is equivalent
> > to the set.


A_set == set(an_iterable)

Seems straightforward to me :-)

I see that subset will accept an arbitrary iterable, whereas __eq__ does
not— but I think that’s more because there’s no reason for subser and
friends NOT to work on an arbitrary iterable than because there’s a
compelling reason the should.

The same is not true for __eq__.


-CHB





>
> I still don't see what you consider "equivalent" aside from the
> equality case.
>
> Your request would be much more clear and easy to understand if you had
> started with concrete examples, especially since the terminology you are
> using is ambiguous.
>
>
> --
> Steven
> ___
> 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/ZWO36MYVIEMBDLXIOU4F77JZDRXWRZOI/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-- 
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
___
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/3L6KDTSUZZYC2RYBLFBQUH6FQHCX5ODX/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Instance method to test equivalence between set and iterable

2020-03-22 Thread Steven D'Aprano
On Mon, Mar 23, 2020 at 12:08:23AM -, Steve Jorgensen wrote:

> Basically, it is for a sense of completeness. It feels weird that 
> there is a way to check whether an iterable is a subset of a set or a 
> superset of a set but no way to directly ask whether it is equivalent 
> to the set.

I still don't see what you consider "equivalent" aside from the 
equality case.

Your request would be much more clear and easy to understand if you had 
started with concrete examples, especially since the terminology you are 
using is ambiguous.


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


[Python-ideas] Re: Instance method to test equivalence between set and iterable

2020-03-22 Thread Steven D'Aprano
On Mon, Mar 23, 2020 at 12:03:50AM -, Steve Jorgensen wrote:

> Every set is a superset of itself and a subset of itself. A set may 
> not be a "formal" subset or a "formal" superset of itself. `issubset` 
> and `issuperset` refer to standard subsets and supersets, not formal 
> subsets and supersets.

Sorry, I don't understand your terminology "formal" and "standard". I 
think you might mean "proper" rather than formal? But I don't know what 
you mean by "standard".

We might have a terminology issue here, since according to Wikipedia 
there is some dispute over whether or not to include the equality case 
in subset/superset:

https://en.wikipedia.org/wiki/Subset

For what it is worth, I'm in the school that subset implies proper 
subset, i.e. A ⊂ B implies that A ≠ B and that sets are *not* subsets 
(or supersets) of themselves.

To be explicit, A ⊂ B means the same as A ⊊ B not A ⊆ B.

But I can see that people's usage on this varies, so I won't argue that 
one way or the other is "wrong". So long as we agree on which convention 
we are using.

So to be clear, you are referring to the improper subset, which in 
Python we write as the first comparion, not the second:

py> {1} <= {1}  # like {1}.issubset({1})
True
py> {1} < {1}
False


> In Python, you can trivially check that…
> ```
> In [1]: {1, 2, 3}.issubset({1, 2, 3})
> Out[1]: True

Okay, that's an *improper* subset, since the two sets are equal. That's 
documented as more-or-less equivalent to `A <= B`:

https://docs.python.org/3/library/stdtypes.html#frozenset.issubset

(The only difference is that the operator version requires actual sets, 
while the method version accepts any iterable.)

Do you have an example of `A <= B and B <= A` aside from the `A == B` 
case?


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


[Python-ideas] Re: dunder methods for encoding & prettiness aware formal & informal representations

2020-03-22 Thread Kyle Stanley
I ended up opening an issue for it at https://bugs.python.org/issue40045.


On Sun, Mar 22, 2020 at 8:33 PM Kyle Stanley  wrote:

> Chistopher Barker wrote:
> > I'd suggest you make a PR on the docs.
>
> Yeah I was planning on either doing that, or opening it as a "newcomer
> friendly"/"easy" issue on bugs.python.org. IMO, it could make for a
> decent first PR.
>
> On Sun, Mar 22, 2020 at 1:28 PM Christopher Barker 
> wrote:
>
>> On Fri, Mar 20, 2020 at 8:52 PM Kyle Stanley  wrote:
>>
>>> >
>>> https://docs.python.org/3/reference/lexical_analysis.html#reserved-classes-of-identifiers
>>>
>>> I remembered the existence of this rule and tried to locate it recently
>>> (prior to this discussion), but was unable to because it doesn't explicitly
>>> mention "dunder". IMO, it would make that section much easier to find if it
>>> were to say either of:
>>>
>>> 1) System-defined names, also known as "dunder" names.
>>> 2) System-defined names, informally known as "dunder" names.
>>> 3) System-defined "dunder" names.
>>>
>>
>> The word "dunder" was coined (relatively) recently. It was not in common
>> use when those docs were written. Another common nickname is "magic
>> methods".
>>
>> So that explains why the word "dunder" isn't in those docs. And those
>> docs are pretty "formal" / "technical" anyway, not designed for the casual
>> user to read.
>>
>> But yes, it would be good to add a bit of that text to make it more
>> findable.
>>
>> I'd suggest you make a PR on the docs.
>>
>> -CHB
>>
>>
>> Python Language Consulting
>>   - Teaching
>>   - Scientific Software Development
>>   - Desktop GUI and Web Development
>>   - wxPython, numpy, scipy, Cython
>>
>
___
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/I4GRJAO37DYY5ZJDBU4XWIEZQT4WRXR4/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: dunder methods for encoding & prettiness aware formal & informal representations

2020-03-22 Thread Kyle Stanley
Chistopher Barker wrote:
> I'd suggest you make a PR on the docs.

Yeah I was planning on either doing that, or opening it as a "newcomer
friendly"/"easy" issue on bugs.python.org. IMO, it could make for a decent
first PR.

On Sun, Mar 22, 2020 at 1:28 PM Christopher Barker 
wrote:

> On Fri, Mar 20, 2020 at 8:52 PM Kyle Stanley  wrote:
>
>> >
>> https://docs.python.org/3/reference/lexical_analysis.html#reserved-classes-of-identifiers
>>
>> I remembered the existence of this rule and tried to locate it recently
>> (prior to this discussion), but was unable to because it doesn't explicitly
>> mention "dunder". IMO, it would make that section much easier to find if it
>> were to say either of:
>>
>> 1) System-defined names, also known as "dunder" names.
>> 2) System-defined names, informally known as "dunder" names.
>> 3) System-defined "dunder" names.
>>
>
> The word "dunder" was coined (relatively) recently. It was not in common
> use when those docs were written. Another common nickname is "magic
> methods".
>
> So that explains why the word "dunder" isn't in those docs. And those docs
> are pretty "formal" / "technical" anyway, not designed for the casual user
> to read.
>
> But yes, it would be good to add a bit of that text to make it more
> findable.
>
> I'd suggest you make a PR on the docs.
>
> -CHB
>
>
> Python Language Consulting
>   - Teaching
>   - Scientific Software Development
>   - Desktop GUI and Web Development
>   - wxPython, numpy, scipy, Cython
>
___
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/SAD4FIMQHP5PQI2X6X6KTZCF2L5AZMXG/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Instance method to test equivalence between set and iterable

2020-03-22 Thread Steve Jorgensen
Paul Moore wrote:
> On Sun, 22 Mar 2020 at 20:01, Steve Jorgensen ste...@stevej.name wrote:
> >
> > Currently, the issubset and
> > issuperset methods of set objects accept arbitrary iterables as arguments. 
> > An
> > iterable that is both a subset and superset is, in a sense, "equal" to the 
> > set. It would
> > be inappropriate for == to return True for such a comparison,
> > however, since that would break the Hashable contract.
> > Should sets have an additional method, something like like(other),
> > issimilar(other), or isequivalent(other), that returns
> > True for any iterable that contains the all of the items in the set and no
> > items that are not in the set? It would therefore be true in the same cases 
> > where
> >  = set(other) or .issubset(other) and
> > .issuperset(other) is true.
> > What is the practical use case for this? It seems like it would be a
> pretty rare need, at best.
> Paul

Basically, it is for a sense of completeness. It feels weird that there is a 
way to check whether an iterable is a subset of a set or a superset of a set 
but no way to directly ask whether it is equivalent to the set.

Even though the need for it might not be common, I think that the collection of 
methods makes more sense if a method like this is present.
___
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/MRCHHRVCXEUAB3HBV4WRMZ56O3HUJQYL/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Instance method to test equivalence between set and iterable

2020-03-22 Thread Steve Jorgensen
Steven D'Aprano wrote:
> On Sun, Mar 22, 2020 at 07:59:59PM -, Steve Jorgensen wrote:
> > Currently, the issubset and
> > issuperset methods of set objects 
> > accept arbitrary iterables as arguments. An iterable that is both a 
> > subset and superset is, in a sense, "equal" to the set. It would be 
> > inappropriate for == to return True for such a comparison, 
> > however, since that would break the Hashable contract.
> > I think the "arbitrary iterables" part is a distraction. We are 
> fundamentally talking about a comparison on sets, even if Python relaxes 
> the requirements and also allows one operand to be a arbitrary iterable.
> I don't believe that a set A can be both a superset and subset of 
> another set B at the same time. On a Venn Diagram, that would require A 
> to be both completely surrounded by B and B to be completely surrounded 
> by A at the same time, which is impossible.
> I think you might be talking about sets which partially overlap:
> A = {1, 2, 3, 4}
> B = {2, 3, 4, 5}

Every set is a superset of itself and a subset of itself. A set may not be a 
"formal" subset or a "formal" superset of itself. `issubset` and `issuperset` 
refer to standard subsets and supersets, not formal subsets and supersets.

In Python, you can trivially check that…
```
In [1]: {1, 2, 3}.issubset({1, 2, 3})
Out[1]: True

In [2]: {1, 2, 3}.issuperset({1, 2, 3})
Out[2]: True

In [3]: {1, 2, 3}.issubset((1, 2, 3))
Out[3]: True

In [4]: {1, 2, 3}.issuperset((1, 2, 3))
Out[4]: True
```
___
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/QRI7LQAR7TZXSWOVYY5KLS52HK2GU7IK/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Instance method to test equivalence between set and iterable

2020-03-22 Thread Steven D'Aprano
On Sun, Mar 22, 2020 at 07:59:59PM -, Steve Jorgensen wrote:

> Currently, the `issubset` and `issuperset` methods of set objects 
> accept arbitrary iterables as arguments. An iterable that is both a 
> subset and superset is, in a sense, "equal" to the set. It would be 
> inappropriate for `==` to return `True` for such a comparison, 
> however, since that would break the `Hashable` contract.

I think the "arbitrary iterables" part is a distraction. We are 
fundamentally talking about a comparison on sets, even if Python relaxes 
the requirements and also allows one operand to be a arbitrary iterable.

I don't believe that a set A can be both a superset and subset of 
another set B at the same time. On a Venn Diagram, that would require A 
to be both completely surrounded by B and B to be completely surrounded 
by A at the same time, which is impossible.

I think you might be talking about sets which partially overlap:

A = {1, 2, 3, 4}
B = {2, 3, 4, 5}

but neither the issubset nor issuperset methods return True in that 
case:

* A is not a subset of B because 1 is not in B;
* A is not a superset of B because it lacks 5;
* B is not a subset of A because 5 is not in A;
* and B is not a superset of A because it lacks 1.

You are right that it would be inappropriate to return equal, but 
nothing to do with Hashable since sets aren't hashable. They are not 
equal because, well, they ain't equal :-)


> Should sets have an additional method, something like `like(other)`, 
> `issimilar(other)`, or `isequivalent(other)`, that returns `True` for 
> any iterable that contains the all of the items in the set and no 
> items that are not in the set?

That would be equality :-)

A = {1, 2, 3, 4}
B = {1, 2, 3, 4}

B contains all of the items in A and no items which are not in A; 
likewise A contains all the items in B and no items not in B. That makes 
them equal.

I might be missing something obvious, but I really don't think that
`.issubset(other) and .issuperset(other)` can be true.


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


[Python-ideas] Re: Instance method to test equivalence between set and iterable

2020-03-22 Thread Paul Moore
On Sun, 22 Mar 2020 at 20:01, Steve Jorgensen  wrote:
>
> Currently, the `issubset` and `issuperset` methods of set objects accept 
> arbitrary iterables as arguments. An iterable that is both a subset and 
> superset is, in a sense, "equal" to the set. It would be inappropriate for 
> `==` to return `True` for such a comparison, however, since that would break 
> the `Hashable` contract.
>
> Should sets have an additional method, something like `like(other)`, 
> `issimilar(other)`, or `isequivalent(other)`, that returns `True` for any 
> iterable that contains the all of the items in the set and no items that are 
> not in the set? It would therefore be true in the same cases where ` = 
> set(other)` or `.issubset(other) and .issuperset(other)` is true.

What is the practical use case for this? It seems like it would be a
pretty rare need, at best.
Paul
___
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/OJKLFPGZWL2MN5AZJOA6GAYCG32COIX6/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Instance method to test equivalence between set and iterable

2020-03-22 Thread Rob Cliffe via Python-ideas



On 22/03/2020 19:59, Steve Jorgensen wrote:

Currently, the `issubset` and `issuperset` methods of set objects accept arbitrary 
iterables as arguments. An iterable that is both a subset and superset is, in a sense, 
"equal" to the set. It would be inappropriate for `==` to return `True` for 
such a comparison, however, since that would break the `Hashable` contract.

Should sets have an additional method, something like `like(other)`, `issimilar(other)`, or 
`isequivalent(other)`, that returns `True` for any iterable that contains the all of the items in 
the set and no items that are not in the set? It would therefore be true in the same cases where 
` = set(other)` or `.issubset(other) and .issuperset(other)` is 
true.

You worded the above carefully, but it may not be universally obvious that
    .isequivalent(other) == True
does not imply
    len() == len(other)
(assuming `other` has a len() method), e.g.
     = set("a")
    other = list("aa")
I think this point should be documented, lest someone interpret 
`isequivalent` as "equals".


A similar point holds with the existing `issuperset` method (a set can 
be shorter than something it is a superset of),

but I think there would be more danger of confusion with this new method.

+0 on the proposal
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/DQQXOB25HDDBUROL4SF2XRKZPWS4VKQ4/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Instance method to test equivalence between set and iterable

2020-03-22 Thread Steve Jorgensen
Bar Harel wrote:
> Hey Steve,
> How about set.symmetric_difference()?
> Does it not do what you want?
> Best regards,
> Bar Harel
> On Sun, Mar 22, 2020, 10:03 PM Steve Jorgensen ste...@stevej.name wrote:
> > Currently, the issubset and
> > issuperset methods of set objects accept
> > arbitrary iterables as arguments. An iterable that is both a subset and
> > superset is, in a sense, "equal" to the set. It would be inappropriate for
> > == to return True for such a comparison, however, since that
> > would
> > break the Hashable contract.
> > Should sets have an additional method, something like like(other),
> > issimilar(other), or isequivalent(other), that returns
> > True for any
> > iterable that contains the all of the items in the set and no items that
> > are not in the set? It would therefore be true in the same cases where
> >  = set(other) or .issubset(other) and
> > .issuperset(other)
> > is true.
> > 
> > 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/ULQQ7T...
> > Code of Conduct: http://python.org/psf/codeofconduct/
> >
Indirectly, it does, but that returns a set, not a `bool`. It would also, 
therefore, do more work than necessary to determine the result in many cases.

A python implementation for what I'm talking about would be something like the 
following.

```
def like(self, other):
found = set()
for item in other:
if item not in self:
return False
found.add(item)
return len(found) == len(self)
```
___
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/XURB3B3RVM23ECR7BZZFFW7ISLLR63NQ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Instance method to test equivalence between set and iterable

2020-03-22 Thread Bar Harel
Hey Steve,

How about set.symmetric_difference()?
Does it not do what you want?

Best regards,
Bar Harel

On Sun, Mar 22, 2020, 10:03 PM Steve Jorgensen  wrote:

> Currently, the `issubset` and `issuperset` methods of set objects accept
> arbitrary iterables as arguments. An iterable that is both a subset and
> superset is, in a sense, "equal" to the set. It would be inappropriate for
> `==` to return `True` for such a comparison, however, since that would
> break the `Hashable` contract.
>
> Should sets have an additional method, something like `like(other)`,
> `issimilar(other)`, or `isequivalent(other)`, that returns `True` for any
> iterable that contains the all of the items in the set and no items that
> are not in the set? It would therefore be true in the same cases where
> ` = set(other)` or `.issubset(other) and .issuperset(other)`
> is true.
> ___
> 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/ULQQ7TZBPQN3RAGKIP52XHFD6LR4HIB4/
> 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/O6PEO3FZ35SRNE7HZNW6OTHDWM3ZEZ4V/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Instance method to test equivalence between set and iterable

2020-03-22 Thread Steve Jorgensen
Currently, the `issubset` and `issuperset` methods of set objects accept 
arbitrary iterables as arguments. An iterable that is both a subset and 
superset is, in a sense, "equal" to the set. It would be inappropriate for `==` 
to return `True` for such a comparison, however, since that would break the 
`Hashable` contract.

Should sets have an additional method, something like `like(other)`, 
`issimilar(other)`, or `isequivalent(other)`, that returns `True` for any 
iterable that contains the all of the items in the set and no items that are 
not in the set? It would therefore be true in the same cases where ` = 
set(other)` or `.issubset(other) and .issuperset(other)` is true.
___
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/ULQQ7TZBPQN3RAGKIP52XHFD6LR4HIB4/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: New explicit methods to trim strings

2020-03-22 Thread Paul Moore
On Sun, 22 Mar 2020 at 17:58, Guido van Rossum  wrote:
>
> On Sun, Mar 22, 2020 at 9:54 AM Christopher Barker  
> wrote:
>>
>> On Sun, Mar 22, 2020 at 2:08 AM Barry Scott  wrote:
>>>
>>> >>> Should `-+-+-+Spam'.stripprefix('-+')  remove just the first occurence? 
>>> >>>  All of them?  Does it need a 'count' parameter?
>>> >> The only ways to use this function without counting is remove 1 prefix 
>>> >> or remove all.
>
>
> Please, please. removeprefix/removesuffix do not need a count. The use case 
> is quite different from that of replace. And they should only remove (at 
> most) one prefix or suffix.

+1 from me. These should be simple functions to remove a prefix/suffix
(note "a prefix" = "one prefix"). Let's not over-engineer them.

I've needed to remove one prefix/suffix. I've never needed to remove
more than one.
Paul
___
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/PCXX2E77MJTXKMTZDNUMMRV6BMZMUY3N/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Add the imath module

2020-03-22 Thread Chris Angelico
On Mon, Mar 23, 2020 at 5:13 AM Neil Girdhar  wrote:
>
> I mean:
>
> def binom(n, *ks):
> # Check that there is at least one ki, and that their sum is less than n, 
> and that they are all nonnegative.
> # Returns n! / (prod(ki! for ki in ks) * (n-sum(ks))!)
>
> This would still work for binom(n, k), but would also work for the mulinomial 
> case.
>

Thanks for pulling this up again. I actually would have very much
liked to have an efficient and accurate binom(n,k) function available
- everything I could find was either floating-point (with the
limitations thereof) or too inefficient to be used for ridiculously
large values of n and/or k.

+1 on moving forward with adding an imath module.

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


[Python-ideas] Re: Add the imath module

2020-03-22 Thread Neil Girdhar
I mean:

def binom(n, *ks):
# Check that there is at least one ki, and that their sum is less than 
n, and that they are all nonnegative.
# Returns n! / (prod(ki! for ki in ks) * (n-sum(ks))!)

This would still work for binom(n, k), but would also work for the 
mulinomial case.

On Sunday, March 22, 2020 at 2:01:18 PM UTC-4, Neil Girdhar wrote:
>
> I really like this idea.  It fixes the weirdness whereby cmath is for 
> complex numbers, and math is for real numbers and integers.  I like 
> separating them into three categories.
>
> One suggestion: Consider generalizing binom to the multinomial coefficent.
>
> def binom(n, *ks):
> # Returns n! / prod(ki! for ki in ks)
>
> Best,
>
> Neil 
>
> On Thursday, July 12, 2018 at 7:57:08 AM UTC-4, Serhiy Storchaka wrote:
>>
>> What are your thoughts about adding a new imath module for integer 
>> mathematics? It could contain the following functions: 
>>
>> * factorial(n) 
>>
>> Is just moved from the math module, but non-integer types are rejected. 
>> Currently math.factorial() accepts also integer floats like 3.0. It 
>> looks to me, the rationale was that at the time when math.factorial() 
>> was added, all function in the math module worked with floats. But now 
>> we can revise this decision. 
>>
>> * gcd(n, m) 
>>
>> Is just moved from the math module. 
>>
>> * as_integer_ration(x) 
>>
>> Equivalents to: 
>>
>> def as_integer_ration(x): 
>>  if hasattr(x, 'as_integer_ration'): 
>>  return x.as_integer_ration() 
>>  else: 
>>  return (x.numerator, x.denominator) 
>>
>> * binom(n, k) 
>>
>> Returns factorial(n) // (factorial(k) * factorial(n-k)), but uses more 
>> efficient algorithm. 
>>
>> * sqrt(n) 
>>
>> Returns the largest integer r such that r**2 <= n and (r+1)**2 > n. 
>>
>> * isprime(n) 
>>
>> Tests if n is a prime number. 
>>
>> * primes() 
>>
>> Returns an iterator of prime numbers: 2, 3, 5, 7, 11, 13,... 
>>
>> Are there more ideas? 
>>
>> ___ 
>> Python-ideas mailing list 
>> python...@python.org 
>> https://mail.python.org/mailman/listinfo/python-ideas 
>> 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/AB6FYCQ6B3BKMJRV5C7BOPIVGON3MWTQ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Add the imath module

2020-03-22 Thread Neil Girdhar
I really like this idea.  It fixes the weirdness whereby cmath is for 
complex numbers, and math is for real numbers and integers.  I like 
separating them into three categories.

One suggestion: Consider generalizing binom to the multinomial coefficent.

def binom(n, *ks):
# Returns n! / prod(ki! for ki in ks)

Best,

Neil 

On Thursday, July 12, 2018 at 7:57:08 AM UTC-4, Serhiy Storchaka wrote:
>
> What are your thoughts about adding a new imath module for integer 
> mathematics? It could contain the following functions: 
>
> * factorial(n) 
>
> Is just moved from the math module, but non-integer types are rejected. 
> Currently math.factorial() accepts also integer floats like 3.0. It 
> looks to me, the rationale was that at the time when math.factorial() 
> was added, all function in the math module worked with floats. But now 
> we can revise this decision. 
>
> * gcd(n, m) 
>
> Is just moved from the math module. 
>
> * as_integer_ration(x) 
>
> Equivalents to: 
>
> def as_integer_ration(x): 
>  if hasattr(x, 'as_integer_ration'): 
>  return x.as_integer_ration() 
>  else: 
>  return (x.numerator, x.denominator) 
>
> * binom(n, k) 
>
> Returns factorial(n) // (factorial(k) * factorial(n-k)), but uses more 
> efficient algorithm. 
>
> * sqrt(n) 
>
> Returns the largest integer r such that r**2 <= n and (r+1)**2 > n. 
>
> * isprime(n) 
>
> Tests if n is a prime number. 
>
> * primes() 
>
> Returns an iterator of prime numbers: 2, 3, 5, 7, 11, 13,... 
>
> Are there more ideas? 
>
> ___ 
> Python-ideas mailing list 
> python...@python.org  
> https://mail.python.org/mailman/listinfo/python-ideas 
> 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/NTSXZQAIC2OLSA2K4HP5N7HSFUNW7ZYF/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: New explicit methods to trim strings

2020-03-22 Thread Guido van Rossum
On Sun, Mar 22, 2020 at 9:54 AM Christopher Barker 
wrote:

> On Sun, Mar 22, 2020 at 2:08 AM Barry Scott 
> wrote:
>
>> >>> Should `-+-+-+Spam'.stripprefix('-+')  remove just the first
>> occurence?  All of them?  Does it need a 'count' parameter?
>> >> The only ways to use this function without counting is remove 1 prefix
>> or remove all.
>>
>
Please, please. removeprefix/removesuffix do not need a count. The use case
is quite different from that of replace. And they should only remove (at
most) one prefix or suffix.

-- 
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*

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


[Python-ideas] Re: dunder methods for encoding & prettiness aware formal & informal representations

2020-03-22 Thread Christopher Barker
On Fri, Mar 20, 2020 at 8:24 PM Stephen J. Turnbull <
turnbull.stephen...@u.tsukuba.ac.jp> wrote:

> Single leading underscore is reserved for class-private use, so you
> could more safely use "sunders" (_pretty_) or "splunders" (_pretty__).
>

Though this use case really isn't "class-private" --  it's more "package
private", but I don't think it's even that. The idea is to allow anyone to
make a class that plugs into this system -- so very much like a dunder, but
without the blessing of the standard library.

Maybe a semi-convention of "trunders" would make sense?

___pretty___

-CHB

-- 
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
___
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/SBYUIS43HI2LINQ6JBPQH7T6JFCMUKQ4/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: New explicit methods to trim strings

2020-03-22 Thread Christopher Barker
On Sun, Mar 22, 2020 at 10:08 AM Chris Angelico  wrote:

> > I imagine that the count=1 is the most common use case for replace()
> anyway,
> >
>
> Do you mean "other than not specifying the count",


yes, that -- most common use case for using count at all.

I'm suggesting that if -1 and 1 were the only options, very few people
would notice :-)


>  Because in my
> experience, replacing all is *by far* the most common case


Agreed -- that's why it's a good default.  I don't know that I ever even
noticed that count was there before now :-)

-CHB

-- 
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
___
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/56T5CESUIYJIN67JJ25LGMIUJGH2GT6Z/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: dunder methods for encoding & prettiness aware formal & informal representations

2020-03-22 Thread Christopher Barker
On Fri, Mar 20, 2020 at 8:52 PM Kyle Stanley  wrote:

> >
> https://docs.python.org/3/reference/lexical_analysis.html#reserved-classes-of-identifiers
>
> I remembered the existence of this rule and tried to locate it recently
> (prior to this discussion), but was unable to because it doesn't explicitly
> mention "dunder". IMO, it would make that section much easier to find if it
> were to say either of:
>
> 1) System-defined names, also known as "dunder" names.
> 2) System-defined names, informally known as "dunder" names.
> 3) System-defined "dunder" names.
>

The word "dunder" was coined (relatively) recently. It was not in common
use when those docs were written. Another common nickname is "magic
methods".

So that explains why the word "dunder" isn't in those docs. And those docs
are pretty "formal" / "technical" anyway, not designed for the casual user
to read.

But yes, it would be good to add a bit of that text to make it more
findable.

I'd suggest you make a PR on the docs.

-CHB


Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
___
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/KDBSSAEU7UII2K56YS74LRZI4MX73IA7/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: New explicit methods to trim strings

2020-03-22 Thread Chris Angelico
On Mon, Mar 23, 2020 at 3:53 AM Christopher Barker  wrote:
>
> On Sun, Mar 22, 2020 at 2:08 AM Barry Scott  wrote:
>>
>> >>> Should `-+-+-+Spam'.stripprefix('-+')  remove just the first occurence?  
>> >>> All of them?  Does it need a 'count' parameter?
>> >> The only ways to use this function without counting is remove 1 prefix or 
>> >> remove all.
>
>
> I imagine that the count=1 is the most common use case for replace() anyway,
>

Do you mean "other than not specifying the count", or do you actually
mean that it's more common than replacing all? Because in my
experience, replacing all is *by far* the most common case - but yes,
replacing just one would be the next most common.

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


[Python-ideas] Re: New explicit methods to trim strings

2020-03-22 Thread Christopher Barker
On Sun, Mar 22, 2020 at 2:08 AM Barry Scott  wrote:

> >>> Should `-+-+-+Spam'.stripprefix('-+')  remove just the first
> occurence?  All of them?  Does it need a 'count' parameter?
> >> The only ways to use this function without counting is remove 1 prefix
> or remove all.
>

I imagine that the count=1 is the most common use case for replace()
anyway,

So it seems it would be useful to have a way to select either "one" or
"all". Once we have that, why not "count", and -1 means "all", just like it
does for .replace() -- after all, why introduce yet another API?

That being said, I'd be just as happy with only one.

On a related note, I just noticed:

In [8]: s.replace('a', 'x', count=2)

---
TypeError Traceback (most recent call last)
 in 
> 1 s.replace('a', 'x', count=2)

TypeError: replace() takes no keyword arguments

having count be a keyword parameter seems like the natural API to me. Is is
just legacy that it's not? Is there a good reason not to make it a keyword
parameter? (it is optional).

Frankly, that's always been confusing -- particularly as until 3.8 you
couldn't make a function with a default and not a keyword at all.

-CHB

-- 
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
___
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/QVTFWNBWCPYPTH5FBAIVU4MIHBHAHPT4/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: New explicit methods to trim strings

2020-03-22 Thread Barry Scott



> On 19 Mar 2020, at 22:12, Rob Cliffe  wrote:
> 
> 
> On 18/03/2020 20:16, Barry Scott wrote:
>> 
>>> On 18 Mar 2020, at 18:03, Rob Cliffe via Python-ideas 
>>>  wrote:
>>> 
>>> Consider that the start or end of a string may contain repetitions of an 
>>> affix.
>>> 
>>> Should `-+-+-+Spam'.stripprefix('-+')  remove just the first occurence?  
>>> All of them?  Does it need a 'count' parameter?
>> The only ways to use this function without counting is remove 1 prefix or 
>> remove all.
>> As Alex said 1 prefix is the common case. For the all case there are 
>> existing ways to do it.
>> 
>> If you are counting the number of prefix occurrences that exist you can 
>> simple slice the answer
>> without the strip prefix function.



>> 
>> Barry
> 
> I don't understand the last sentence.  I had in mind a case where you might 
> want to remove repetitions of an affix without knowning how many there were 
> (possibly none).

Yep could have worded better. I was wonder what example would need the count 
and if when you have the count its
easy to solve another way. I'm less sure have thought more about it.

As you noted later str.replace() has a count. So why by prefix/suffix striping?

Barry

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