[Python-Dev] Re: Should set objects maintain insertion order too?

2019-12-28 Thread Tim Peters
[Larry]
> Here is the original description of my problem, from the original email in
> this thread.  I considered this an adequate explanation of my problem
> at the time.

>> I do have a use case for this. In one project I maintain a "ready" list of
>> jobs; I need to iterate over it, but I also want fast lookup because I
>> soemtimes remove jobs when they're subsequently marked "not ready".

Which is a description not of "the problem", but of the operations you
want to perform.  It's the first time in my life I've heard of a
"ready list of jobs" where the programmer sometimes decides later "oh,
no - this one and that one aren't ready after all" ;-)

Not that it matters much - you want the operations you want.  The lack
of "oh - of course - that's a problem we all face at times"
motivation, though, works against it being _compelling_ on its own.

> ...
> In subsequent emails I've clarified that my workloads are small enough--and
> computers are fast enough--that almost any data structure would work fine
> for me here, e.g. a list.
>
>  I don't think my needs should drive the decision making process regardless.
>  I only described my problem to get the conversation rolling.

Which it did :-)  Others went on to, explicitly or implicitly, suggest
that an ordered set must also, e.g., support the entire
MutableSequence interface, and even define what happens if you mutate
the ordered set _while_ iterating over it (lists define the results in
such cases, but ordered dicts raise an exception).


> I opine that anybody who iterates over set objects and has bugs in their
> code would appreciate set objects maintaining insertion order.  I suspect
> it would make their debugging easier, because given identical inputs their
> set iteration would behave identically, thus making their bugs that much
> more stable.  That's as much use case as I have for the feature.

That's much stronger to me than some weird FIFO-only queue supporting
fast deletion of interior elements (which - sorry - I've never had a
use for).

And fine by me if such a thing is added.  All else being equal, I'd
prefer that the builtin set type be ordered, simply because it's less
surprising. and dicts are ordered now (although, as Inada Naoki said,
their current implementation isn't suitable for a FIFO queue, doe
primarily to O(N) time needed to find "the first" key+value record).

I believe we agree that adding _some_ flavor of OrderedSet to
`collections` would be a fine start.  I'm just not cool with replacing
the builtin set before there's an implementation fast and
memory-efficient enough that _current_ heavy set users won't feel
betrayed by major slowdowns or memory bloat when the switch is made.

Toward that end, my opinion - which I won't defend - is that
OrderedSet should not promise better than O(N)-time indexing (if it
supports indexing at all), and should - like current sets and dicts -
try to raise an exception if it can detect that the container has been
mutated during iteration.
___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/AH4LQB3OCMCE22FEWUALRND5P6AHUWE6/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: Should set objects maintain insertion order too?

2019-12-28 Thread Larry Hastings


On 12/27/19 7:44 PM, Tim Peters wrote:

[Nick Coghlan ]

I took Larry's request a slightly different way: he has a use case where
he wants order preservation (so built in sets aren't good), but combined
with low cost duplicate identification and elimination and removal of
arbitrary elements (so lists and collections.deque aren't good). Organising
a work queue that way seems common enough ...

Is it?  I confess I haven't thought of a plausible use case.  Larry
didn't really explain his problem, just suggested that an ordered set
would be "a solution" to it.



Here is the original description of my problem, from the original email 
in this thread.  I considered this an adequate explanation of my problem 
at the time.



On 12/15/19 6:48 PM, Larry Hastings wrote:


I do have a use case for this. In one project I maintain a "ready" 
list of jobs; I need to iterate over it, but I also want fast lookup 
because I soemtimes remove jobs when they're subsequently marked "not 
ready".  The existing set object would work fine here, except that it 
doesn't maintain insertion order.  That means multiple runs of the 
program with the same inputs may result in running jobs in different 
orders, and this instability makes debugging more difficult.  I've 
therefore switched from a set to a dict with all keys mapped to None, 
which provides all the set-like functionality I need.




In subsequent emails I've clarified that my workloads are small 
enough--and computers are fast enough--that almost any data structure 
would work fine for me here, e.g. a list.  I don't think my needs should 
drive the decision making process regardless.  I only described my 
problem to get the conversation rolling.


I opine that anybody who iterates over set objects and has bugs in their 
code would appreciate set objects maintaining insertion order.  I 
suspect it would make their debugging easier, because given identical 
inputs their set iteration would behave identically, thus making their 
bugs that much more stable.  That's as much use case as I have for the 
feature.



//arry/

___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/XWTP7OD4UMGL7DLVABM3S2CB26LN5RBP/
Code of Conduct: http://python.org/psf/codeofconduct/