-
Notifications
You must be signed in to change notification settings - Fork 2
/
idea_selection.py
102 lines (92 loc) · 2.75 KB
/
idea_selection.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
import os
import sys
persons = []
ideas = []
votes = []
data_filename = sys.argv[1]
def save():
print 'saving to', data_filename
f = open(data_filename, 'w+')
f.write(str( (persons, ideas, votes) ))
f.close()
def load():
if os.path.exists(data_filename):
print 'loading', data_filename
global persons, ideas, votes
f = open(data_filename, 'r')
(persons, ideas, votes) = eval(f.read())
f.close()
def manage(collection_name, parse_f=str, cmd_f=None):
coll = globals()[collection_name]
while True:
cmd = raw_input(collection_name+'> ')
if cmd_f:
if cmd_f(cmd, coll):
continue
if cmd in ['a', 'add']:
coll.append(parse_f(raw_input('unesi: ')))
elif cmd in ['d', 'del']:
coll.remove(parse_f(raw_input('unesi: ')))
elif cmd in ['p', 'print']:
for s in coll:
print s
elif cmd in ['q', 'quit']:
break
elif cmd in ['l', 'load']:
load()
elif cmd in ['s', 'save']:
save()
elif cmd in ['h', 'help']:
print 'commands: add del print quit load save help'
def handle_votes_cmds(cmd, coll):
if cmd in ['a', 'add']:
for person in persons:
for idea in ideas:
coll.append((person, idea, raw_input('unesi za %s, %s: ' % (person, idea))))
return True
elif cmd in ['d', 'del']:
coll[:] = []
print 'votes are empty!'
return True
elif cmd in ['p', 'print']:
d = {}
for person, idea, score in coll:
if idea not in d: d[idea] = 0
d[idea] += int(score)
for idea, total_score in sorted(d.items(), key=lambda pair: -pair[1]):
print idea, ':', total_score
return True
else:
return False
def main():
print 'hello everybody! how are you doing today?!'
print 'for help enter help or h'
while True:
cmd = raw_input('> ')
if cmd in ['p', 'persons']:
manage('persons')
elif cmd in ['i', 'ideas']:
manage('ideas')
elif cmd in ['v', 'votes']:
manage('votes', parse_f=lambda s: tuple(s.split()),
cmd_f=handle_votes_cmds)
elif cmd in ['q', 'quit']:
break
elif cmd in ['l', 'load']:
load()
elif cmd in ['s', 'save']:
save()
elif cmd in ['h', 'help']:
print 'commands: persons ideas votes quit load save help'
load()
try:
main()
finally:
if raw_input('do you want to save data? ').lower() in ['y', 'yes']:
save()
'''
{
'dule': [('ulepsavanje ovoga programa', 5),...]
}
[('dule', 'ulepsavanje ovoga programa', 5),...]
'''