我正在研究麻省理工学院开放式课程课程的问题(没有任何信用),并遇到了这个问题。我正在尝试创建RSS Feed过滤器。
我首先创建了一个定义新闻故事特征的对象。
class NewsStory(object):
def __init__(self, guid, title, subject, summary, link):
self.guid = guid
self.title = title
self.subject = subject
self.summary = summary
self.link = link
def get_guid(self):
return self.guid
def get_title(self):
return self.title
def get_subject(self):
return self.subject
def get_summary(self):
return self.summary
def get_link(self):
return self.link
接下来,这是触发器的给定类。
class Trigger(object):
def evaluate(self, story):
"""
Returns True if an alert should be generated
for the given news item, or False otherwise.
"""
raise NotImplementedError
然后我尝试创建一个Trigger,WordTrigger的子类,以检查一个单词是否在我要检查的文本中。
class WordTrigger(Trigger):
def __init__(self, word):
self.word = word
def is_word_in(self, text):
word = self.word.lower()
text = text.lower()
for p in string.punctuation:
if p in text:
text.replace(p, ' ')
print self.word in text
要调用is_word_in
,我使用了WordTrigger的另一个子类TitleTrigger。
class TitleTrigger(WordTrigger):
def evaluate(self, story):
return self.is_word_in(story.get_title())
但是,为此运行单元测试会给我一个新错误(编辑代码后):
AssertionError: TitleTrigger failed to fire when the word appeared in the title
现在我不确定错误是因为从低到大的转换有什么问题?
以下是给我的单元测试:(我包括了我认为的相关部分)
class ProblemSet5NewsStory(unittest.TestCase):
def setUp(self):
pass
def testNewsStoryConstructor(self):
add_potential_points(1)
story = NewsStory('', '', '', '', '')
add_points(1)
class ProblemSet5(unittest.TestCase):
def setUp(self):
class TrueTrigger:
def evaluate(self, story): return True
class FalseTrigger:
def evaluate(self, story): return False
self.tt = TrueTrigger()
self.tt2 = TrueTrigger()
self.ft = FalseTrigger()
self.ft2 = FalseTrigger()
def test1TitleTrigger(self):
add_potential_points(5)
koala = NewsStory('', 'Koala bears are soft and cuddly', '', '', '')
pillow = NewsStory('', 'I prefer pillows that are soft.', '', '', '')
soda = NewsStory('', 'Soft drinks are great', '', '', '')
pink = NewsStory('', "Soft's the new pink!", '', '', '')
football = NewsStory('', '"Soft!" he exclaimed as he threw the football', '', '', '')
microsoft = NewsStory('', 'Microsoft announced today that pillows are bad', '', '', '')
nothing = NewsStory('', 'Reuters reports something really boring', '', '' ,'')
caps = NewsStory('', 'soft things are soft', '', '', '')
s1 = TitleTrigger('SOFT')
s2 = TitleTrigger('soft')
for trig in [s1, s2]:
self.assertTrue(trig.evaluate(koala), "TitleTrigger failed to fire when the word appeared in the title")
self.assertTrue(trig.evaluate(pillow), "TitleTrigger failed to fire when the word had punctuation on it")
self.assertTrue(trig.evaluate(soda), "TitleTrigger failed to fire when the case was different")
self.assertTrue(trig.evaluate(pink), "TitleTrigger failed to fire when the word had an apostrophe on it")
self.assertTrue(trig.evaluate(football), "TitleTrigger failed to fire in the presence of lots of punctuation")
self.assertTrue(trig.evaluate(caps), "TitleTrigger is case-sensitive and shouldn't be")
self.assertFalse(trig.evaluate(microsoft), "TitleTrigger fired when the word was present, but not as its own word (e.g. 'soft' and 'Microsoft)'")
self.assertFalse(trig.evaluate(nothing), "TitleTrigger fired when the word wasn't really present in the title")
我对如何在类中使用属性感到困惑,尤其是在继承发挥作用时。非常感谢任何帮助!