Public » Miscellaneous » Twenty
Clone URL:  
Pushed to one repository · View In Graph Contained in tip

refactor for better student friendliness (hide things)

Changeset 59f8a41baee7

Parent 774aaedfda3c

by Benjamin Pollack

Changes to 2 files · Browse files at 59f8a41baee7 Showing diff from parent 774aaedfda3c Diff from another changeset...

Change 1 of 1 Show Entire File brain.py Stacked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
109
110
111
112
@@ -0,0 +1,112 @@
+import os +import pickle + +def ask(question): + while True: + print question, + answer = raw_input().strip().lower() + if answer in ('y', 'yes', 'yeah', 'si'): + return True + elif answer in ('n', 'no', 'nope'): + return False + else: + print 'Please type "yes" or "no"' + +class Determiner(object): + def __init__(self, questioner, question, true_question, false_question): + self.questioner = questioner + self._question = question + self.true_question = true_question + self.false_question = false_question + + def question(self): + return self._question + + def ask(self): + if ask(self.question()): + self.questioner.advance_yes() + else: + self.questioner.advance_no() + + +class Thing(object): + def __init__(self, questioner, what): + self.questioner = questioner + self._what = what + + def question(self): + return 'Are you thinking of %s?' % self._what + + def ask(self): + if ask(self.question()): + self.questioner.mark_succeeded() + else: + self.questioner.mark_failed() + + +class Questioner(object): + def __init__(self, initial_question): + self.root = Determiner(self, initial_question, None, None) + + def start(self): + self.current_question = self.root + self._solved = False + self._failed = False + + def next_question(self): + return self.current_question + + def advance_yes(self): + self.last_branch = True + self.last_question = self.current_question + self.current_question = self.current_question.true_question + + def advance_no(self): + self.last_branch = False + self.last_question = self.current_question + self.current_question = self.current_question.false_question + + def out_of_ideas(self): + return self.current_question is None or self._failed + + def solved(self): + return self._solved + + def learn_thing_and_question(self, question, thing): + true_answer = Thing(self, thing) + false_answer = self.last_question.true_question + new_question = Determiner(self, question, true_answer, false_answer) + if self.last_branch == True: + self.last_question.true_question = new_question + else: + self.last_question.false_question = new_question + + def learn_thing(self, thing): + new_thing = Thing(self, thing) + if self.last_branch == True: + self.last_question.true_question = new_thing + else: + self.last_question.false_question = new_thing + + def needs_clarification(self): + return self.current_question is not None + + def mark_succeeded(self): + self._solved = True + + def mark_failed(self): + self._failed = True + + def best_guess(self): + return self.current_question.what + +def load(path): + if os.path.exists(path): + with open(path, 'rb') as f: + return pickle.load(f) + else: + return Questioner('Does it have four legs?') + +def save(path, questioner): + with open(path, 'wb') as f: + pickle.dump(questioner, f, pickle.HIGHEST_PROTOCOL)
Change 1 of 4 Show Entire File twenty.py Stacked
 
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
 
94
95
96
97
 
98
99
100
 
104
105
106
 
107
108
109
 
118
119
120
 
121
122
123
 
1
2
 
 
 
 
 
 
3
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
6
7
8
 
16
17
18
 
19
20
21
22
 
26
27
28
29
30
31
32
 
41
42
43
44
45
46
47
@@ -1,86 +1,8 @@
 #!/usr/bin/env python   -class Determiner(object): - def __init__(self, questioner, question, true_question, false_question): - self.questioner = questioner - self.question = question - self.true_question = true_question - self.false_question = false_question +from brain import load, save   - def ask(self): - if ask(self.question): - self.questioner.advance_yes() - else: - self.questioner.advance_no() - - -class Thing(object): - def __init__(self, questioner, what): - self.questioner = questioner - self.what = what - - def ask(self): - if ask('Are you thinking of a %s?' % self.what): - self.questioner.mark_succeeded() - else: - self.questioner.mark_failed() - - -class Questioner(object): - def __init__(self, database): - self.root = Determiner(self, 'Does it have four legs?', None, None) - - def start(self): - self.current_question = self.root - self._solved = False - self._failed = False - - def next_question(self): - return self.current_question - - def advance_yes(self): - self.last_branch = True - self.last_question = self.current_question - self.current_question = self.current_question.true_question - - def advance_no(self): - self.last_branch = False - self.last_question = self.current_question - self.current_question = self.current_question.false_question - - def out_of_ideas(self): - return self.current_question is None or self._failed - - def solved(self): - return self._solved - - def learn_thing_and_question(self, question, thing): - true_answer = Thing(self, thing) - false_answer = self.last_question.true_question - new_question = Determiner(self, question, true_answer, false_answer) - if self.last_branch == True: - self.last_question.true_question = new_question - else: - self.last_question.false_question = new_question - - def learn_thing(self, thing): - new_thing = Thing(self, thing) - if self.last_branch == True: - self.last_question.true_question = new_thing - else: - self.last_question.false_question = new_thing - - def needs_clarification(self): - return self.current_question is not None - - def mark_succeeded(self): - self._solved = True - - def mark_failed(self): - self._failed = True - - def best_guess(self): - return self.current_question.what +BRAIN_DB = 'brain.db'    def ask(question):   while True: @@ -94,7 +16,7 @@
  print 'Please type "yes" or "no"'    def play(): - questioner = Questioner('questions.db') + questioner = load(BRAIN_DB)   questioner.start()   while True:   question = questioner.next_question() @@ -104,6 +26,7 @@
  if ask('Play again? '):   questioner.start()   else: + save(BRAIN_DB, questioner)   exit()   else:   if questioner.out_of_ideas(): @@ -118,6 +41,7 @@
  if ask('Play again?'):   questioner.start()   else: + save(BRAIN_DB, questioner)   exit()    if __name__ == '__main__':