Skip to content
This repository has been archived by the owner on Oct 24, 2021. It is now read-only.

Commit

Permalink
current sorting isnt repeatable for tests, nor is it what we want. ne…
Browse files Browse the repository at this point in the history
…ed to rewrite filter_reversable_dictsort() to be something more special/specific that sorts a dict of tuple: int by the int in reverse order, then by the tuple. have some sample code, need to clean up, write tests, test, put into place
  • Loading branch information
jantman committed Jun 19, 2014
1 parent 8369b50 commit 8dfe6fb
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion pypuppetdb_daily_report/pypuppetdb_daily_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ def filter_reversable_dictsort(value, case_sensitive=False, by='key', reverse=Fa
"""
jinja2's dictsort patched to be reversable
This also sorts based on the combination of the key and value, to yield a
reproducable sort order.
Sort a dict and yield (key, value) pairs. Because python dicts are
unsorted you may want to use this function to order them by either
key or value:
Expand All @@ -176,11 +179,32 @@ def filter_reversable_dictsort(value, case_sensitive=False, by='key', reverse=Fa
raise FilterArgumentError('You can only sort by either '
'"key" or "value"')

"""
from collections import OrderedDict
def sort_dict(d):
items = list(d.iteritems())
keyfunc = lambda x: tuple([x[1]] + list(x[0]))
return OrderedDict(sorted(items, key=keyfunc))
x = {
('Service', 'dataeng'): 1,
('Package', 'libsmbios'): 1,
('Service', 'zookeeper-server'): 2,
}
print x
print sort_dict(x)
"""

def sort_func(item):
value = item[pos]
if isinstance(value, string_types) and not case_sensitive:
value = value.lower()
return value
return (value, item)

return sorted(value.items(), key=sort_func, reverse=reverse)

Expand Down

0 comments on commit 8dfe6fb

Please sign in to comment.