This repository has been archived by the owner on Jun 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathingredients_classify.py
108 lines (77 loc) · 2.88 KB
/
ingredients_classify.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import curses
from pony.orm import *
db = Database()
class Ingredient(db.Entity):
name = Required(str)
tags = Required(Json)
db.bind('sqlite', 'data/ingredients.sqlite', create_db=True)
db.generate_mapping(create_tables=True)
class Tagger:
def __init__(self, stdscr):
self.screen = stdscr
self.current_ingredient = 0
self.next_ingredient()
@db_session
def next_ingredient(self):
if self.current_ingredient + 1 <= select(i for i in Ingredient).count():
self.current_ingredient += 1
self.ingredient = Ingredient[self.current_ingredient].name
self.tags = Ingredient[self.current_ingredient].tags
@db_session
def prev_ingredient(self):
if self.current_ingredient > 1:
self.current_ingredient -= 1
self.ingredient = Ingredient[self.current_ingredient].name
self.tags = Ingredient[self.current_ingredient].tags
@db_session
def update_ingredient(self):
self.ingredient = Ingredient[self.current_ingredient].name
self.tags = Ingredient[self.current_ingredient].tags
@db_session
def toggle_tag(self, tag):
if tag in Ingredient[self.current_ingredient].tags:
i = Ingredient[self.current_ingredient].tags.index(tag)
Ingredient[self.current_ingredient].tags.pop(i)
else:
Ingredient[self.current_ingredient].tags.append(tag)
def render(self):
self.screen.clear()
self.screen.addstr(1, 1,
'[n]ext [p]revious ' +
'[v]egan [c]arnic ' +
'[a]nimal origin',
curses.color_pair(1))
self.screen.addstr(2, 1,
"%s: %s" % (str(self.current_ingredient), self.ingredient),
curses.color_pair(2))
self.screen.addstr(3, 1,
str(self.tags),
curses.color_pair(2))
self.screen.refresh()
def main(stdscr):
# initialize curses environment
curses.curs_set(0)
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_WHITE)
curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_WHITE, curses.COLOR_BLUE)
t = Tagger(stdscr)
while True:
t.render()
c = stdscr.getch()
if c == ord('n'):
t.next_ingredient()
elif c == ord('p'):
t.prev_ingredient()
t.screen.clear()
elif c == ord('c'):
t.toggle_tag('carnic')
t.next_ingredient()
elif c == ord('v'):
t.toggle_tag('vegan')
t.next_ingredient()
elif c == ord('a'):
t.toggle_tag('animal origin')
t.next_ingredient()
elif c == ord('q'):
break # Exit the while()
curses.wrapper(main)