[Python-ideas] Re: Addition to fnmatch.py

2022-06-07 Thread MRAB
On 2022-06-07 11:03, Chris Angelico wrote: On Tue, 7 Jun 2022 at 19:09, Ben Rudiak-Gould wrote: This calls the predicate once per element: def partition(pred, iterable): t1, t2 = tee((pred(x), x) for x in iterable) return (x for b, x in t1 if not b), (x for b, x in t2 if

[Python-ideas] Re: Addition to fnmatch.py

2022-06-07 Thread Chris Angelico
On Tue, 7 Jun 2022 at 19:09, Ben Rudiak-Gould wrote: > > This calls the predicate once per element: > > def partition(pred, iterable): > t1, t2 = tee((pred(x), x) for x in iterable) > return (x for b, x in t1 if not b), (x for b, x in t2 if b) > > It's kind of inefficient

[Python-ideas] Re: Addition to fnmatch.py

2022-06-07 Thread Ben Rudiak-Gould
This calls the predicate once per element: def partition(pred, iterable): t1, t2 = tee((pred(x), x) for x in iterable) return (x for b, x in t1 if not b), (x for b, x in t2 if b) It's kind of inefficient though. ___ Python-ideas

[Python-ideas] Re: Addition to fnmatch.py

2022-06-06 Thread Chris Angelico
On Tue, 7 Jun 2022 at 10:26, Steven D'Aprano wrote: > > Why don't you use the version from the itertools recipes? > > > ``` > from itertools import tee, filterfalse > def partition(pred, iterable): > "Use a predicate to partition entries into false entries and true entries" > #

[Python-ideas] Re: Addition to fnmatch.py

2022-06-06 Thread Steven D'Aprano
Why don't you use the version from the itertools recipes? ``` from itertools import tee, filterfalse def partition(pred, iterable): "Use a predicate to partition entries into false entries and true entries" # partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9 t1, t2 =

[Python-ideas] Re: Addition to fnmatch.py

2022-06-06 Thread Steven D'Aprano
On Mon, Jun 06, 2022 at 06:17:32PM +0200, Benedict Verhegghe wrote: > There still is something wrong. I get the second list twice: > > odd, even = partition(lambda i: i % 2, range(20)) > print(list(odd)) > [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] > print(list(even)) > [0, 2, 4, 6, 8, 10, 12, 14, 16,

[Python-ideas] Re: Addition to fnmatch.py

2022-06-06 Thread Chris Angelico
On Tue, 7 Jun 2022 at 01:58, David Mertz, Ph.D. wrote: > In [2]: def isEven(n): >...: return n/2 == n//2 >...: > Huh. That's a very strange way to write that predicate. >>> isEven(2) True >>> isEven(20001) True >>> isEven(20002) False >>>

[Python-ideas] Re: Addition to fnmatch.py

2022-06-06 Thread Benedict Verhegghe
Op 6/06/2022 om 18:07 schreef Mathew Elman: oops, you're right, I have ended up with an unnecessary "not" in there, should be: from collections import deque def partition(pred, iterable): results = deque([]), deque([]) def gen_split(only): for thing in iterable:

[Python-ideas] Re: Addition to fnmatch.py

2022-06-06 Thread Mathew Elman
oops, you're right, I have ended up with an unnecessary "not" in there, should be: from collections import deque def partition(pred, iterable): results = deque([]), deque([]) def gen_split(only): for thing in iterable: if results[only]: yield

[Python-ideas] Re: Addition to fnmatch.py

2022-06-06 Thread David Mertz, Ph.D.
This is really nice, but I think you have a negation wrong in there somewhere: In [1]: from collections import deque ...: ...: def partition(pred, iterable): ...: results = deque([]), deque([]) ...: ...: def gen_split(only): ...: for thing in iterable: ...:

[Python-ideas] Re: Addition to fnmatch.py

2022-06-06 Thread John Carter
Thanks for all the comments. I did consider using sets but order was important and when partitioning a list of general strings, not file names, there could be identical and important strings (strings at different positions could mean different things, especially when generating test and

[Python-ideas] Re: Addition to fnmatch.py

2022-06-06 Thread Mathew Elman
from collections import deque def partition(pred, iterable): results = deque([]), deque([]) def gen_split(only): for thing in iterable: if results[only]: yield results[only].popleft() results[not pred(thing)].append(thing) for thing

[Python-ideas] Re: Addition to fnmatch.py

2022-06-06 Thread David Mertz, Ph.D.
As Chris Angelico notes, that's only good for finite iterables. The recipe in the itertools docs is unbounded, but it's also unbounded in the cache needed if you get long strings of things the do (or don't) confirm the predicate. On Mon, Jun 6, 2022 at 7:06 AM Stephen J. Turnbull <

[Python-ideas] Re: Addition to fnmatch.py

2022-06-06 Thread Stephen J. Turnbull
Christopher Barker writes: > How do you divide a sequence into two sequences cleanly? I don't know how to do it in a one liner, but yes = [] no = [] for elem in seq: (yes if test(elem) else no).append(elem) seems pretty clean to me.

[Python-ideas] Re: Addition to fnmatch.py

2022-06-06 Thread Chris Angelico
On Mon, 6 Jun 2022 at 18:22, Paul Moore wrote: > > There’s an itertools recipe, “partition”. > Yes, there is (and plenty of equivalents in other languages). But it's still not what I'd call "particularly easy". What that recipe does is filter the same iterable with the same predicate twice, so

[Python-ideas] Re: Addition to fnmatch.py

2022-06-06 Thread Paul Moore
There’s an itertools recipe, “partition”. On Mon, 6 Jun 2022 at 08:28, Chris Angelico wrote: > On Mon, 6 Jun 2022 at 16:42, Christopher Barker > wrote: > > > > I think y’all have addressed the OP’s problem — at least the performance > issues. > > > > But I think this brings up a pattern that I

[Python-ideas] Re: Addition to fnmatch.py

2022-06-06 Thread Chris Angelico
On Mon, 6 Jun 2022 at 16:42, Christopher Barker wrote: > > I think y’all have addressed the OP’s problem — at least the performance > issues. > > But I think this brings up a pattern that I don’t have a nifty way to address: > > How do you divide a sequence into two sequences cleanly? > > The

[Python-ideas] Re: Addition to fnmatch.py

2022-06-06 Thread Christopher Barker
I think y’all have addressed the OP’s problem — at least the performance issues. But I think this brings up a pattern that I don’t have a nifty way to address: How do you divide a sequence into two sequences cleanly? The filter pattern: selectively remove items from a sequence, returning a new

[Python-ideas] Re: Addition to fnmatch.py

2022-06-05 Thread Eric V. Smith via Python-ideas
On 6/5/2022 1:22 PM, Benedict Verhegghe wrote: Op 5/06/2022 om 18:47 schreef David Mertz, Ph.D.: Sure, that's nice enough code and has the same big-O complexity. I suspect set difference is a little faster (by a constant multiple) because it hits C code more, but I haven't benchmarked. The

[Python-ideas] Re: Addition to fnmatch.py

2022-06-05 Thread Benedict Verhegghe
Op 5/06/2022 om 18:47 schreef David Mertz, Ph.D.: Sure, that's nice enough code and has the same big-O complexity. I suspect set difference is a little faster (by a constant multiple) because it hits C code more, but I haven't benchmarked. The OP said the elements were from fnmatch though,

[Python-ideas] Re: Addition to fnmatch.py

2022-06-05 Thread David Mertz, Ph.D.
On Sun, Jun 5, 2022, 12:08 PM MRAB wrote: > On 2022-06-05 16:12, David Mertz, Ph.D. wrote: > > This is exactly the problem solved by set difference. E.g. `{a, b, c} > - {a, c}`. > > > > This operation is linear on the size of the removed set. > > > You don't have to use a set difference, which

[Python-ideas] Re: Addition to fnmatch.py

2022-06-05 Thread MRAB
On 2022-06-05 16:12, David Mertz, Ph.D. wrote: This is exactly the problem solved by set difference. E.g. `{a, b, c} - {a, c}`. This operation is linear on the size of the removed set. You don't have to use a set difference, which might matter if the order was important, but just make a set

[Python-ideas] Re: Addition to fnmatch.py

2022-06-05 Thread Jean Abou Samra
Le 05/06/2022 à 16:52, John Carter a écrit : I’d like to propose a simple addition to the 'fnmatch' module. Specificity a function that returns a list of names that match a pattern AND a list of those that don't. In a recent project I found that I wished to split a list of files and move

[Python-ideas] Re: Addition to fnmatch.py

2022-06-05 Thread David Mertz, Ph.D.
This is exactly the problem solved by set difference. E.g. `{a, b, c} - {a, c}`. This operation is linear on the size of the removed set. On Sun, Jun 5, 2022, 11:01 AM John Carter wrote: > I’d like to propose a simple addition to the 'fnmatch' module. Specificity > a function that returns a