summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohannes Löthberg <johannes@kyriasis.com>2014-12-17 00:18:31 +0100
committerJohannes Löthberg <johannes@kyriasis.com>2014-12-17 00:18:31 +0100
commit0208e64ae8df331cc1b5e2e9c7e3b98544f5b15b (patch)
treeca50f77f681e6abf0c35154d1db8cb2cac7d880a
parentb27b96fcfd3630e433cfad3f016916d6e059746d (diff)
downloadtodo-0208e64ae8df331cc1b5e2e9c7e3b98544f5b15b.tar.xz
todo: replace sort_toml with more readable sort_dict
-rwxr-xr-x[-rw-r--r--]todo26
1 files changed, 20 insertions, 6 deletions
diff --git a/todo b/todo
index 153bda6..289f5e5 100644..100755
--- a/todo
+++ b/todo
@@ -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)