[issue45093] Add method to compare dicts accounting for order

2021-09-03 Thread Michael Rans


Michael Rans  added the comment:

Thanks for all your help and advice.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45093] Add method to compare dicts accounting for order

2021-09-03 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

> Another case is in round trip processing of JSON or YML.

Sorry for my ignorance, but I don't see how or why an unordered 
comparison would help you with round-tripping JSON or YAML.

The order of key:value pairs in JSON is not guaranteed to be preserved, 
so if you round-trip a dict to JSON back to a dict, and then use an 
ordered comparison, you might wrongly think that they are unequal.

(I think that Python's JSON does preserve order, by default. But other 
JSON encoders might not.)

> Other cases are where you would prefer an OrderedDict over a dict. 

Then use an OrderedDict.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45093] Add method to compare dicts accounting for order

2021-09-03 Thread Terry J. Reedy


Terry J. Reedy  added the comment:

-1 also for the reasons given.

Michael, a question asking how would have made a good python-list post. So I 
posted the answers in "How to include insertion order in dict equality"

--
nosy: +terry.reedy
resolution:  -> rejected
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45093] Add method to compare dicts accounting for order

2021-09-03 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
Removed message: https://bugs.python.org/msg401022

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45093] Add method to compare dicts accounting for order

2021-09-03 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

-1 The dict API is too important to be burdened with a mostly useless and 
rarely needed method.  Besides we already have simple ways to do it, for 
example:

assert list(d.items()) == list(e.items())

or:

assert OrderedDict(d) == OrderedDict(e)

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45093] Add method to compare dicts accounting for order

2021-09-03 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

-1 The dict API is to important to be burdened with a mostly useless and rarely 
needed method.  Besides we already have simple ways to do it, for example:

assert list(d.items()) == list(e.items())

or:

assert OrderedDict(d) == OrderedDict(e)

--
nosy: +rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45093] Add method to compare dicts accounting for order

2021-09-03 Thread Michael Rans


Michael Rans  added the comment:

Thank you. Another case is in round trip processing of JSON or YML. Other cases 
are where you would prefer an OrderedDict over a dict. 

I think the method would help clarify things because it would make it obvious 
that it is for ordered comparisons while the existing == does not do that.

eg. something like:
d1.compare_ordered(d2)
or:
d1.compare(d2, ordered=True)  # ordered could be by default True

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45093] Add method to compare dicts accounting for order

2021-09-02 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

For tests it can be more convenient to use

list(d1.items()) == list(d2.items())

because it can produce better report on failure.

You can add a simple helper function if you use it multiple times in tests.

--
nosy: +serhiy.storchaka

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45093] Add method to compare dicts accounting for order

2021-09-02 Thread Michael Rans


Michael Rans  added the comment:

My use case is in testing. Currently I use an OrderedDict, but since dict 
introduced ordering in 3.7, I was considering switching. For my test, I need to 
check that the dictionary is the same both in content and order. I can indeed 
use your one liner, but just thought it would be useful to have a method 
available.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45093] Add method to compare dicts accounting for order

2021-09-02 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

What is your use-case for having dicts that differ only in order compare 
unequal?

I can't think of any reason why I would want such a comparison, but if I did, 
it's a trivial one-liner:

d1 == d2 and all(k1 == k2 for k1, k2 in zip(d1, d2))

--
nosy: +steven.daprano
type: behavior -> enhancement

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45093] Add method to compare dicts accounting for order

2021-09-02 Thread Michael Rans


Change by Michael Rans :


--
type:  -> behavior

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45093] Add method to compare dicts accounting for order

2021-09-02 Thread Michael Rans


New submission from Michael Rans :

I suggest adding a method that allows comparing dicts taking into account the 
order - an ordered compare (since == does not do that).

(Also for my other issue (https://bugs.python.org/issue45092) where I suggested 
that set be ordered to reduce confusion, the set could also have an ordered 
compare).

--
messages: 400975
nosy: mcarans
priority: normal
severity: normal
status: open
title: Add method to compare dicts accounting for order
versions: Python 3.11

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com