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

refactor brain.py to be a little more tell, a little less ask

Changeset 247374662bf2

Parent 169848b05f89

by Profile picture of Benjamin PollackBenjamin Pollack

Changes to one file · Browse files at 247374662bf2 Showing diff from parent 169848b05f89 Diff from another changeset...

Change 1 of 4 Show Entire File brain.py Stacked
 
45
46
47
 
 
 
48
49
50
 
68
69
70
71
72
 
 
73
74
75
76
 
77
78
 
79
 
 
80
81
82
 
96
97
98
99
100
 
 
101
102
103
104
105
106
107
108
109
110
111
112
 
 
 
 
 
 
 
113
114
115
116
117
118
119
120
121
122
 
 
 
 
 
 
 
123
124
125
 
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
 
197
198
199
 
45
46
47
48
49
50
51
52
53
 
71
72
73
 
 
74
75
76
77
78
 
79
80
 
81
82
83
84
85
86
87
 
101
102
103
 
 
104
105
106
107
108
109
 
 
 
 
 
 
 
 
110
111
112
113
114
115
116
117
118
 
 
 
 
 
 
 
 
119
120
121
122
123
124
125
126
127
128
 
172
173
174
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
176
177
178
179
180
 
181
182
183
184
@@ -45,6 +45,9 @@
  else:   self.questioner.advance_no()   + def is_possible_answer(self): + False +    class Thing(object):   """represents a concrete thing that might be the right answer @@ -68,15 +71,17 @@
  """asks the user on the command line     You can use Determiner.question() to grab the question text, - and manually call Questioner.mark_failed() or - Questioner.mark_succeeded(), if you want to ask users + and manually call Questioner.advance_yes() or + Questioner.advance_no(), if you want to ask users   questions in other ways. This is here just for convenience.   """   if ask(self.question()): - self.questioner.mark_succeeded() + self.questioner.advance_yes()   else: - self.questioner.mark_failed() + self.questioner.advance_no()   + def is_possible_answer(self): + return True    class Questioner(object):   """holds all knowledge in the universe as a form of yes/no questions @@ -96,30 +101,28 @@
  """returns the next question that you should ask     This will actually keep returning the same question until you - tell the questioner to continue by calling advance_yes, - advance_no, mark_succeeded, or mark_failed, as appropriate. + tell the questioner to continue by calling advance_yes or + advance_no, as appropriate.   """   return self.current_question     def advance_yes(self): - """tell the Questioner that his last Determiner was answered yes - - If the last question asked was actually a Thing, use - mark_succeeded instead - """ - self.last_branch = True - self.last_question = self.current_question - self.current_question = self.current_question.true_question + """tell the Questioner that his last Determiner was answered yes""" + if self.current_question.is_possible_answer(): + self._solved = True + else: + self.last_branch = True + self.last_question = self.current_question + self.current_question = self.current_question.true_question     def advance_no(self): - """tell the Questioner that his last Determiner was answered no - - If the last question asked was actually a Thing, use - mark_failed instead - """ - self.last_branch = False - self.last_question = self.current_question - self.current_question = self.current_question.false_question + """tell the Questioner that his last Determiner was answered no""" + if self.current_question.is_possible_answer(): + self._failed = True + else: + self.last_branch = False + self.last_question = self.current_question + self.current_question = self.current_question.false_question     def out_of_ideas(self):   """tell the user if we don't have any idea what they were thinking of""" @@ -169,31 +172,13 @@
  """   return self.current_question is not None   - def mark_succeeded(self): - """tell the Questioner they succeeded - - Once you call this, the Questioner assumes they got it, even - if they didn't, so don't call this unless the user's really - gotten the right answer. - """ - self._solved = True - - def mark_failed(self): - """tell the Questioner they failed - - Once you call this, the Questioner assumes they messed up, - even if they secretly actually really got it. Don't lie. - It's not nice. - """ - self._failed = True -   def best_guess(self):   """return our best guess, if we had one     This will only return something if we tried to guess a Thing.   Otherwise, it returns None.   """ - if isinstance(self.current_question, Thing): + if self.current_question.is_possible_answer():   return self.current_question.what     def dump(self):