Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing


  • 3 years ago

In the solution for PS5 (the newsfeed problem), the last problem -- user-specified triggers -- the solution uses a function 'makeTrigger' which modifies a trigger_map dict. Then, in the readTriggerConfig function, a makeTrigger call is assigned to a variable 'trigger'... but then, I noticed that 'trigger' is not used again. Is this just a construct to allow makeTrigger to be called.. or is there another way to do, without creating extra variables?

  • This Question is Closed
  1. shawnf
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    you don't need an extra class makeTrigger. Here are all my classes: class NewsStory: """description of class""" def __init__(self, guid, title, subject, summary, link): self.guid = guid self.title = title self.subject = subject self.summary = summary = 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 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 class WordTrigger(Trigger): def __init__(self,word): self.word = word.lower() def is_word_in(self,text): if self.word in text: return True else: return False def noPunct(text): intab = string.punctuation outtab = " "*32 trantab = maketrans(intab, outtab) return text.translate(trantab) class NotTrigger(Trigger): def __init__(self,obj): self.obj = obj def evaluate(self,obj): return not self.obj.evaluate(self) class AndTrigger(Trigger): def __init__(self,object1,object2): self.object1 = object1 self.object2 = object2 def evaluate(self,obj): return self.object1.evaluate(obj) and self.object2.evaluate(obj) class OrTrigger(Trigger): def __init__(self,object1,object2): self.object1 = object1 self.object2 = object2 def evaluate(self,obj): return self.object1.evaluate(obj) or self.object2.evaluate(obj) class PhraseTrigger(Trigger): def __init__(self,phrase): self.phrase = phrase def is_phrase_in(self,text): if self.phrase in text: return True else: return False def evaluate(self,object): #objects subject, title, or summary text = object.get_subject() + object.get_title() + object.get_summary() text = noPunct(text) #text = text.split() return self.is_phrase_in(text) class TitleTrigger(WordTrigger): def evaluate(self,object): text = noPunct(object.get_title().lower()) text = text.split() return self.is_word_in(text) class SubjectTrigger(WordTrigger): def evaluate(self,object): text = noPunct(object.get_subject().lower()) text = text.split() return self.is_word_in(text) class SummaryTrigger(WordTrigger): def evaluate(self,object): text = noPunct(object.get_summary().lower()) text = text.split() return self.is_word_in(text)

  2. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy