diff options
author | Johannes Löthberg <johannes@kyriasis.com> | 2014-12-17 00:18:31 +0100 |
---|---|---|
committer | Johannes Löthberg <johannes@kyriasis.com> | 2014-12-17 00:18:31 +0100 |
commit | 0208e64ae8df331cc1b5e2e9c7e3b98544f5b15b (patch) | |
tree | ca50f77f681e6abf0c35154d1db8cb2cac7d880a | |
parent | b27b96fcfd3630e433cfad3f016916d6e059746d (diff) | |
download | todo-0208e64ae8df331cc1b5e2e9c7e3b98544f5b15b.tar.xz |
todo: replace sort_toml with more readable sort_dict
-rwxr-xr-x[-rw-r--r--] | todo | 26 |
1 files changed, 20 insertions, 6 deletions
@@ -18,12 +18,26 @@ from os import getenv, path from docopt import docopt import toml +def dict_sort_key(sort_key): + '''Return a function for sorted() to sort a dict by the given key''' + def key(item): + return (sort_key not in item[1], item[1].get(sort_key, None)) + return key -def sort_toml(toml_dict, sort_key, rev=False): - return sorted( - list(toml_dict.items()), - key=lambda d: (sort_key not in d[1], d[1].get(sort_key, None)), - reverse=rev) +def sort_dict_by_int(d, rev): + '''Return dict sorted by int key''' + sorted_dict = [ (key, d[key]) for key in sorted(d, key=int, reverse=rev) ] + return OrderedDict(sorted_dict) + +def sort_dict(todo_dict, sort_key, rev=False): + todo_dict = sort_dict_by_int(todo_dict, rev) + + if sort_key != 'id': + todo_list = list(todo_dict.items()) + todo_dict = sorted(todo_list, reverse=rev, + key=dict_sort_key(sort_key)) + + return OrderedDict(todo_dict) class Config(): def __init__(self): @@ -79,7 +93,7 @@ def main(): todo_dict = toml.load(config.todo_file) unsorted = OrderedDict(sorted(todo_dict.items())) - todo_dict = OrderedDict(sort_toml(unsorted, config.sort_key, config.reverse)) + todo_dict = sort_dict(unsorted, config.sort_key, config.reverse) print(todo_dict) |