Open anntzer opened 5 years ago
I think this sounds good. I believe we'll need to extract logic from pretty_dict
to a utility function that takes an iterable of key-value pairs instead of a dict
, called something like pretty_dict_from_pairs
so that we can use that function both here and in the implementation of pretty_dict
. This will ensure Python 3.5 can correctly render the output in the most common order. Then the actual pretty printer for Counter can be implemented as
build_fncall(
ctx,
type(counter),
argdocs=(
pretty_dict_from_pairs(ctx, counter.most_common()),
),
hug_sole_arg=True
)
Description
PrettyPrinter does not sort Counter()s entries by count, unlike
Counter.__str__
which explicitly does so.What I Did
I think the sorted form is clearly more useful (well, at least, it matches the intent of whoever wrote the stdlib's
Counter.__str__
).On Py3.6+ I think it's just a matter of
(well the stdlib's version has additionally some handling when most_common() fails due to unorderable values, but you get the idea) but I'm not sure how to make this work on Py3.5.