[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-01 Thread Steven D'Aprano
On Sat, Aug 01, 2020 at 03:57:34PM +1000, Chris Angelico wrote: > Ahh, okay. So it's safe in the sense that it can't accidentally leak a > confusing exception. Unfortunately a lot of people are going to assume > that it means "will always give a useful return value". Definitely > worth being very

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-01 Thread Marco Sulla
On Sat, 1 Aug 2020 at 03:00, Inada Naoki wrote: > Please teach me if you know any algorithm which has no hole, O(1) > deletion, preserving insertion order, and efficient and fast as array. > :) About the hole, I was thinking that in theory the problem can be circumvented using a modified versio

[Python-ideas] Re: How to prevent shared memory from being corrupted ?

2020-08-01 Thread Vinay Sharma via Python-ideas
> On 01-Aug-2020, at 1:31 AM, Marco Sulla wrote: > > On Thu, 30 Jul 2020 at 12:57, Vinay Sharma via Python-ideas > mailto:[email protected]>> wrote: > Python has support for atomic types, I guess: > Atomic Int: > https://github.com/python/cpython/blob/master/Include/internal/pycore_ato

[Python-ideas] Re: How to prevent shared memory from being corrupted ?

2020-08-01 Thread Marco Sulla
You don't need locks with immutable objects. Since they're immutable, any operation that usually will mutate the object, generate another immutable instead. The most common example is str: the sum of two strings in Python (and in many other languages) produces a new string. This is usually slower

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-01 Thread Christopher Barker
On Sat, Aug 1, 2020 at 2:28 AM Marco Sulla wrote: > On Sat, 1 Aug 2020 at 03:00, Inada Naoki wrote: > >> Please teach me if you know any algorithm which has no hole, O(1) >> deletion, preserving insertion order, and efficient and fast as array. >> > I would think the goal here would be to re-ord

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-01 Thread Tim Peters
[Steven D'Aprano ] >> >> The other simple solution is `next(iter(mydict.items()))`. [Guido] > That one always makes me uncomfortable, because the StopIteration it > raises when the dict is empty might be misinterpreted. Basically I never > want to call next() unless there's a try...except Sto

[Python-ideas] Default behavior for random.sample when no k

2020-08-01 Thread Ram Rachum
When writing some code now, I needed to produce a shuffled version of `range(10, 10 ** 5)`. This is one way to do it: shuffled_numbers = list(range(10, 10 ** 5)) random.shuffle(shuffled_numbers) I don't like it because (1) it's too imperative and (2) I'm calling the list "shuffled" even before

[Python-ideas] Re: Default behavior for random.sample when no k

2020-08-01 Thread Alex Hall
I agree that calling random.shuffle imperatively is annoying. But I don't think your proposed solution is readable. You're not taking a sample. A sample generally implies a strict subset, usually quite a small one. I've often thought there should just be a `random.shuffled()` function which return

[Python-ideas] Re: Default behavior for random.sample when no k

2020-08-01 Thread Ram Rachum
I would also prefer a `random.shuffled` function. The reason I didn't propose it is because there's usually more resistance for adding new functions. But in my view that'll be the best solution. On Sat, Aug 1, 2020 at 9:17 PM Alex Hall wrote: > I agree that calling random.shuffle imperatively is

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-01 Thread Stestagg
I wrote some (better than the previously shared) benchmarks for this change a while ago. They are run on cPython with a patch applied that implements dict_views __getitem__() using a method similar to `lookdict` to perform indexing on keys/values/etc. Irrespective of where in the api this logic s

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-01 Thread Christopher Barker
On Fri, Jul 31, 2020 at 7:34 AM Guido van Rossum wrote: > So maybe we need to add dict.ordered() which returns a view on the items > that is a Sequence rather than a set? Or ordereditems(), orderedkeys() and > orderedvalues()? > I'm still confused as to when "ordered" became synonymous with "Seq

[Python-ideas] Re: How to prevent shared memory from being corrupted ?

2020-08-01 Thread Eric V. Smith
On 8/1/2020 1:25 PM, Marco Sulla wrote: You don't need locks with immutable objects. Since they're immutable, any operation that usually will mutate the object, generate another immutable instead. The most common example is str: the sum of two strings in Python (and in many other languages) pro

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-01 Thread Guido van Rossum
Yeah, it is totally doable to refactor the collection ABCs to have something in between `Collection` and `Sequence` that just supports `__getitem__`. But I would take Marco's research (and Inada's musings) seriously -- we don't actually want to support `__getitem__`, because of the unpredictable p

[Python-ideas] Re: Default behavior for random.sample when no k

2020-08-01 Thread Neil Girdhar
Can you not just use https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.random_permutation ? On Saturday, August 1, 2020 at 2:26:23 PM UTC-4 Ram Rachum wrote: > I would also prefer a `random.shuffled` function. The reason I didn't > propose it is because there's usually mo

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-01 Thread Wes Turner
first() https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.first https://toolz.readthedocs.io/en/latest/api.html#toolz.itertoolz.first last() https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.last https://toolz.readthedocs.io/en/latest/api.html#toolz.iter

[Python-ideas] Re: How to prevent shared memory from being corrupted ?

2020-08-01 Thread Wes Turner
PyArrow Plasma object ids, "sealing" makes an object immutable, pyristent https://arrow.apache.org/docs/python/plasma.html#object-ids https://arrow.apache.org/docs/python/plasma.html#creating-an-object-buffer > Objects are created in Plasma in two stages. First, they are created, which allocates

[Python-ideas] Re: How to prevent shared memory from being corrupted ?

2020-08-01 Thread Wes Turner
https://docs.dask.org/en/latest/shared.html#known-limitations : > Known Limitations > The shared memory scheduler has some notable limitations: > > - It works on a single machine > - The threaded scheduler is limited by the GIL on Python code, so if your operations are pure python functions, you s

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-01 Thread Inada Naoki
On Sun, Aug 2, 2020 at 2:34 AM Christopher Barker wrote: > > On Sat, Aug 1, 2020 at 2:28 AM Marco Sulla > wrote: >> >> On Sat, 1 Aug 2020 at 03:00, Inada Naoki wrote: >>> >>> Please teach me if you know any algorithm which has no hole, O(1) >>> deletion, preserving insertion order, and efficien

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-01 Thread Wes Turner
Is there any reason that these features couldn't be added to OrderedDict (which is a linked list)? https://github.com/python/cpython/blob/master/Objects/odictobject.c On Sat, Aug 1, 2020, 9:13 PM Inada Naoki wrote: > On Sun, Aug 2, 2020 at 2:34 AM Christopher Barker > wrote: > > > > On Sat, Aug

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-01 Thread Steven D'Aprano
On Sat, Aug 01, 2020 at 07:25:42PM +0100, Stestagg wrote: > Irrespective of where in the api this logic should exist, the > implementation won't be algorithmically different, (I think, even with a > `.ordered` view, as the view would have to cope with changes to the > underlying dictionary over it

[Python-ideas] Re: Default behavior for random.sample when no k

2020-08-01 Thread Steven D'Aprano
On Sat, Aug 01, 2020 at 08:54:16PM +0300, Ram Rachum wrote: > When writing some code now, I needed to produce a shuffled version of > `range(10, 10 ** 5)`. > > This is one way to do it: > > shuffled_numbers = list(range(10, 10 ** 5)) > random.shuffle(shuffled_numbers) > > > I don't like it bec