-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlist_example.py
42 lines (34 loc) · 1.48 KB
/
list_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from itertools import groupby
from app import get_db_connection
conn = get_db_connection()
todos = conn.execute('SELECT i.id, i.done, i.content, l.title \
FROM items i JOIN lists l \
ON i.list_id = l.id ORDER BY l.title;').fetchall()
lists = {}
for k, g in groupby(todos, key=lambda t: t['title']):
# Create an empty list for items
items = []
# Go through each to-do item row in the groupby() grouper object
for item in g:
# Get the assignees of the current to-do item
assignees = conn.execute('SELECT a.id, a.name FROM assignees a \
JOIN item_assignees i_a \
ON a.id = i_a.assignee_id \
WHERE i_a.item_id = ?',
(item['id'],)).fetchall()
# Convert the item row into a dictionary to add assignees
item = dict(item)
item['assignees'] = assignees
items.append(item)
# Build the list of dictionaries
# the list's name (ex: Home/Study/Work) as the key
# and a list of dictionaries of to-do items
# belonging to that list as the value
lists[k] = list(items)
for list_, items in lists.items():
print(list_)
for item in items:
assignee_names = ', '.join(a['name'] for a in item['assignees'])
print(' ', item['content'], '| id:',
item['id'], '| done:', item['done'],
'| assignees:', assignee_names)