bpo-32360: unittest.util: Use Counter instead of custom count function (GH-4994)
This commit is contained in:
parent
b7a80d543e
commit
018e1b7aad
|
@ -1,6 +1,6 @@
|
||||||
"""Various utility functions."""
|
"""Various utility functions."""
|
||||||
|
|
||||||
from collections import namedtuple, OrderedDict
|
from collections import namedtuple, Counter
|
||||||
from os.path import commonprefix
|
from os.path import commonprefix
|
||||||
|
|
||||||
__unittest = True
|
__unittest = True
|
||||||
|
@ -153,17 +153,10 @@ def _count_diff_all_purpose(actual, expected):
|
||||||
result.append(diff)
|
result.append(diff)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def _ordered_count(iterable):
|
|
||||||
'Return dict of element counts, in the order they were first seen'
|
|
||||||
c = OrderedDict()
|
|
||||||
for elem in iterable:
|
|
||||||
c[elem] = c.get(elem, 0) + 1
|
|
||||||
return c
|
|
||||||
|
|
||||||
def _count_diff_hashable(actual, expected):
|
def _count_diff_hashable(actual, expected):
|
||||||
'Returns list of (cnt_act, cnt_exp, elem) triples where the counts differ'
|
'Returns list of (cnt_act, cnt_exp, elem) triples where the counts differ'
|
||||||
# elements must be hashable
|
# elements must be hashable
|
||||||
s, t = _ordered_count(actual), _ordered_count(expected)
|
s, t = Counter(actual), Counter(expected)
|
||||||
result = []
|
result = []
|
||||||
for elem, cnt_s in s.items():
|
for elem, cnt_s in s.items():
|
||||||
cnt_t = t.get(elem, 0)
|
cnt_t = t.get(elem, 0)
|
||||||
|
|
Loading…
Reference in New Issue