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

use some subclassing because why not

Changeset 0d8f9ceaaf2c

Parent 59e0bd62d49a

by Profile picture of Benjamin PollackBenjamin Pollack

Changes to one file · Browse files at 0d8f9ceaaf2c Showing diff from parent 59e0bd62d49a Diff from another changeset...

Change 1 of 4 Show Entire File brain.py Stacked
 
14
15
16
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
19
20
 
22
23
24
 
25
26
27
 
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
 
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
 
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
 
46
47
48
49
50
51
52
 
54
55
56
 
57
58
 
 
 
 
 
 
 
 
 
 
 
 
 
59
60
61
62
 
63
64
65
66
 
71
72
73
 
 
 
 
 
74
75
 
 
 
 
 
 
 
 
 
 
 
 
 
76
77
78
@@ -14,7 +14,31 @@
  else:   print 'Please type "yes" or "no"'   -class Determiner(object): +class Question(object): + """abstract class for thing that can be asked""" + + def is_possible_answer(self): + """return whether I'm a final answer (e.g., a thing, person, etc.)""" + raise NotImplementedError + + def question(self): + """returns the question that I represent""" + raise NotImplementedError + + def ask(self): + """asks the question on the command line + + You can use Determiner.question() to grab the question text, + and manually call Questioner.advance_yes() or + Questioner.advance_no based on the answer, if you want to + ask users questions in some other way (see twivy.py) + """ + if ask(self.question()): + self.questioner.advance_yes() + else: + self.questioner.advance_no() + +class Determiner(Question):   """represents a question that helps us narrow down possible answers     Determiners need to know about their questioner, so they can let @@ -22,6 +46,7 @@
  true_question and a false_question property that stores the next   thing to ask if the user says yes/no, respectively.   """ +   def __init__(self, questioner, question, true_question, false_question):   self.questioner = questioner   self._question = question @@ -29,27 +54,13 @@
  self.false_question = false_question     def question(self): - """returns the question as a string"""   return self._question   - def ask(self): - """asks the user on the command line - - You can use Determiner.question() to grab the question text, - 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.advance_yes() - else: - self.questioner.advance_no() -   def is_possible_answer(self):   False     -class Thing(object): +class Thing(Question):   """represents a concrete thing that might be the right answer     For example, valid Things might be "a dog", "a cat", "Apollo 11", @@ -60,26 +71,8 @@
  self.what = what     def question(self): - """returns the question as a string - - This nicely formats things for you in the form of a question - so you don't have to. - """   return 'Are you thinking of %s?' % self.what   - def ask(self): - """asks the user on the command line - - You can use Determiner.question() to grab the question text, - 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.advance_yes() - else: - self.questioner.advance_no() -   def is_possible_answer(self):   return True