我正在使用麻省理工学院的开放式课件学习python,其中一个问题是学习如何使用类来搜索感兴趣的主题的RSS源。我不知道为什么我收到下面显示的错误。我试图将搜索单词字符串和字符串从新闻故事对象转换为全部小写,然后尝试查找单词 - 如果搜索单词是“软”那么就会触发,那么触发器应该触发在诸如“考拉”之类的字符串上,它们是柔软而可爱的。而且“软砂纸”也没有效果。
import string
class NewsStory(object):
#news story is defined by 5 strings: guid, title, subject, summary, and link
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):
#abstract class
def evaluate(self, story):
"""
Returns True if an alert should be generated
for the given news item, or False otherwise.
"""
raise NotImplementedError
# Whole Word Triggers
# Problems 2-5
# TODO: WordTrigger
class WordTrigger(Trigger):
def __init__(self, word):
#make search word all lowercase to remove case sensitivity
self.word = string.lower(word)
def is_word_in(self, text):
#make text string from news story all lowercase to remove case sensitivity
self.text = string.lower(text)
#use the find method of the string class
#it returns the lowest index where substring s is found in string; returns -1 if substring not found
return string.find(self.text, self.word) > -1
# TODO: TitleTrigger, this subclass of WordTrigger will trigger on search term that is found in the title string of the news story object
class TitleTrigger(WordTrigger):
def __init__(self, word):
self.word = string.lower(word)
def evaluate(self, story):
#story is an instance of the NewsStory class
title = story.get_title
return super(TitleTrigger, self).is_word_in(title)
#make news story object
news1 = NewsStory('1234','it is raining dogs and cats','animals falling from sky','3 dogs and 2 cats fell from sky','www.cat.com')
#make TitleTrigger object
testTrig = TitleTrigger('cats')
testTrig.evaluate(news1)
我正在摸不着头脑的错误如下所示:
>>>
Traceback (most recent call last):
File "C:/Users/David/Documents/Python2_7/Unit 2/postonline.py", line 61, in <module>
testTrig.evaluate(news1)
File "C:/Users/David/Documents/Python2_7/Unit 2/postonline.py", line 55, in evaluate
return super(TitleTrigger, self).is_word_in(title)
File "C:/Users/David/Documents/Python2_7/Unit 2/postonline.py", line 42, in is_word_in
self.text = string.lower(text)
File "C:\WinPython-64bit-2.7.10.3\python-2.7.10.amd64\lib\string.py", line 228, in lower
return s.lower()
AttributeError: 'function' object has no attribute 'lower'