[Python-ideas] Re: Argumenting in favor of first()

2019-12-14 Thread Random832
On Fri, Dec 13, 2019, at 19:24, Steven D'Aprano wrote: > `__builtins__` is a private CPython implementation detail, so the above > is always wrong in user code. Better: Wait, it is? Is there then no portable way to do the things like: - providing an alternate __builtins__ to evaluated code, with

[Python-ideas] Segmentation of string

2019-12-14 Thread smfiles
I think it's necessary to add a segment() method to str type or string module. This method is used to split a string into m parts and return all cases. With segment(), you can avoid tedious calculation and indexing if you want to segment a string. For example: segment('1234', m=3) -> [('1', '2

[Python-ideas] Re: Segmentation of string

2019-12-14 Thread Anders Hovmöller
What are the practical uses for this? I don't recall having done this in my soon 20 years career. > On 14 Dec 2019, at 15:42, smfiles <1668151...@qq.com> wrote: > > I think it's necessary to add a segment() method to str type or string > module. This method is used to split a string into m p

[Python-ideas] Re: Segmentation of string

2019-12-14 Thread Richard Damon
On 12/14/19 4:06 AM, smfiles wrote: > I think it's necessary to add a segment() method to str type or string > module. This method is used to split a string into m parts and return all > cases. With segment(), you can avoid tedious calculation and indexing if you > want to segment a string. > >

[Python-ideas] Re: Segmentation of string

2019-12-14 Thread David Mertz
This feels much too special purpose for a string method, and probably for anything in the standard library. I'm not sure when someone would want this. But it's an only very sightly special case of integer composition ( https://en.wikipedia.org/wiki/Composition_(combinatorics)). And related to that

[Python-ideas] Re: Segmentation of string

2019-12-14 Thread David Mertz
Here's a discussion of both a conceptually simple and a convoluted but fast version (the latter, naturally, by Tim Peters). This is for integer partitions, but compositions are simply the permutations on the full length of the list of each partition. http://code.activestate.com/recipes/218332-gene

[Python-ideas] Re: Argumenting in favor of first()

2019-12-14 Thread Stephen J. Turnbull
Tim Peters writes: > But hasn't it already been settled by experience? There is nothing > unique about `first(set)` implicitly appealing to iteration order. I'm not sure. I wouldn't think to use "first" on a set in my own code (and in others' code I'd consider it a code smell in many contexts

[Python-ideas] Segmentation of string

2019-12-14 Thread Stephen J. Turnbull
smfiles writes: > I think it's necessary to add a segment() method to str type or > string module. This method is used to split a string into m parts > and return all cases. With segment(), you can avoid tedious > calculation and indexing if you want to segment a string. In addition to David

[Python-ideas] Re: Segmentation of string

2019-12-14 Thread Mark Dickinson
On Sat, Dec 14, 2019 at 3:56 PM David Mertz wrote: > [...] compositions are simply the permutations on the full length of the > list of each partition. > Using permutations of partitions would be overkill. For compositions of a given fixed length, it's much easier to compute them directly using

[Python-ideas] Re: Segmentation of string

2019-12-14 Thread Mark Dickinson
Apologies for the bad formatting. Here are the relevant bits, better formatted (I hope): >>> segment = lambda s, m: (tuple(s[i:j] for i, j in zip((0,)+c, c+(len(s),))) ... for c in itertools.combinations(range(1, len(s)), m-1)) >>> >>> list(segment("12345", m=3)) [('1',

[Python-ideas] Re: Segmentation of string

2019-12-14 Thread Tim Peters
[David Mertz ] > Here's a discussion of both a conceptually simple and a convoluted but > fast version (the latter, naturally, by Tim Peters). Not really convoluted - it's the natural thing you'd do "by hand" to move from one partition to the next: look at the current partition, throw out the 1s,

[Python-ideas] Re: Segmentation of string

2019-12-14 Thread David Mertz
I had not known about math.comb() and math.perm() being added in 3.8. Those kinda feel to me like "not every one line function needs to be in the standard library." But I guess wiser people than me saw a reason they are needed. On Sat, Dec 14, 2019, 3:00 PM Tim Peters wrote: > Not really convolu

[Python-ideas] Re: Argumenting in favor of first()

2019-12-14 Thread Christopher Barker
On Sat, Dec 14, 2019 at 2:50 AM Stephen J. Turnbull < turnbull.stephen...@u.tsukuba.ac.jp> wrote: > Did you intend to reply only to me? Nope - I hate list that don’t default to replying to the list (I know that’s an unpopular opinion). I was wondering why no one comment on it. > I’m quite con

[Python-ideas] Re: Argumenting in favor of first()

2019-12-14 Thread Christopher Barker
> > I think we all agree that it does not belong in > > __builtins__, > > Do we? I'm not convinced. We already have all() and any() > in builtins, which are similar in that they operate on > iterators or iterables. Good point — I was assuming with all the hostility (OK, skepticism) to the idea, t

[Python-ideas] Re: Segmentation of string

2019-12-14 Thread Random832
On Sat, Dec 14, 2019, at 04:06, smfiles wrote: > I think it's necessary to add a segment() method to str type or string > module. This method is used to split a string into m parts and return > all cases. With segment(), you can avoid tedious calculation and > indexing if you want to segment a

[Python-ideas] Re: Argumenting in favor of first()

2019-12-14 Thread Juancarlo Añez
> The argument that first(it) and next(it) "look the same" doesn't convince > me; if these look the same then all function applications look the same, > and that can certainly not have been Meertens' intention. But if everyone > thinks that first() should raise, fine, this thread is way too long al

[Python-ideas] Add "elif" to "for_stmt" and "while_stmt"

2019-12-14 Thread komissar . off . andrey
Hello! I think it will be useful in Python syntax if we can use "elif" in "for" and "while" statements besides "else" Example for i in range(j): ... elif i > 5: ... else: ... What you think about this change? ___ Python-ideas mailing list -

[Python-ideas] Re: Add "elif" to "for_stmt" and "while_stmt"

2019-12-14 Thread Chris Angelico
On Sun, Dec 15, 2019 at 10:16 AM wrote: > > Hello! > I think it will be useful in Python syntax if we can use "elif" in "for" and > "while" statements besides "else" > > Example > for i in range(j): > ... > elif i > 5: > ... > else: > ... > > What you think about this change? Can you

[Python-ideas] Re: Add "elif" to "for_stmt" and "while_stmt"

2019-12-14 Thread Ned Batchelder
On 12/14/19 4:37 PM, komissar.off.and...@gmail.com wrote: Hello! I think it will be useful in Python syntax if we can use "elif" in "for" and "while" statements besides "else" Example for i in range(j): ... elif i > 5: ... else: ... What you think about this change? Can you sa

[Python-ideas] Re: Argumenting in favor of first()

2019-12-14 Thread Andrew Barnert via Python-ideas
On Dec 14, 2019, at 12:36, Christopher Barker wrote: > > Of the top of my head, I can’t think of a single non-contrived example of > using a bare iterator in case that is not specifically doing something > “special”. Calling iter on a container is hardly the only way to get an Iterator. You al

[Python-ideas] Re: Argumenting in favor of first()

2019-12-14 Thread David Mertz
A pattern I've written a number of times is roughly: lines = open(fname) header = next(lines) for line in lines: process (line, header) That's not so artificial, I think. Of course, first() would also work here. But I'm not sure it's any particular advantage in this case. On Sun, Dec 15, 201

[Python-ideas] Re: Argumenting in favor of first()

2019-12-14 Thread Christopher Barker
On Sat, Dec 14, 2019 at 9:46 PM Andrew Barnert wrote: > > Of the top of my head, I can’t think of a single non-contrived example > of using a bare iterator in case that is not specifically doing something > “special”. > > Calling iter on a container is hardly the only way to get an Iterator. You