2013-04-01 115 views
0

代碼應該返回觸發器對象 - 觸發器映射的長度應該顯示實際觸發器的實際數量......如果至少有一個或多個觸發器,但它們都返回FALSE,它應該返回True ZERO觸發器......任何人都可以請幫忙嗎?代碼如下,測試人員的錯誤消息將打印在下面。測試人員出了什麼問題?

def makeTrigger(triggerMap, triggerType, params, name): 

    def readTriggerConfig(filename): 
     triggerfile = open(filename, "r") 
     all = [ line.rstrip() for line in triggerfile.readlines() ] 
     lines = [] 
     for line in all: 
      if len(line) == 0 or line[0] == '#': 
       continue 
      lines.append(line) 
      if len(line) >= 1: 
       return True 
     triggers = [] 
     triggerMap = {} 

    # Be sure you understand this code - we've written it for you, 
    # but it's code you should be able to write yourself 
     for line in lines: 

      linesplit = line.split(" ") 

     # Making a new trigger 
      if linesplit[0] != "ADD": 
       trigger = makeTrigger(triggerMap, linesplit[1], 
             linesplit[2:], linesplit[0]) 

     # Add the triggers to the list 
      else: 
       for name in linesplit[1:]: 
        triggers.append(triggerMap[name]) 

     return triggers 

import thread 

SLEEPTIME = 60 #seconds -- how often we poll 

def main_thread(master): 

    try: 
     # These will probably generate a few hits... 
     t1 = TitleTrigger("Obama") 
     t2 = SubjectTrigger("Romney") 
     t3 = PhraseTrigger("Election") 
     t4 = OrTrigger(t2, t3) 
     triggerlist = [t1, t4] 

     # TODO: Problem 11 
     # After implementing makeTrigger, uncomment the line below: 
     triggerlist = readTriggerConfig("triggers.txt") 

     # from here is about drawing 

     frame = Frame(master) 
     frame.pack(side=BOTTOM) 
     scrollbar = Scrollbar(master) 
     scrollbar.pack(side=RIGHT,fill=Y) 

     t = "Google & Yahoo Top News" 
     title = StringVar() 
     title.set(t) 
     ttl = Label(master, textvariable=title, font=("Helvetica", 18)) 
     ttl.pack(side=TOP) 
     cont = Text(master, font=("Helvetica",14), yscrollcommand=scrollbar.set) 
     cont.pack(side=BOTTOM) 
     cont.tag_config("title", justify='center') 
     button = Button(frame, text="Exit", command=root.destroy) 
     button.pack(side=BOTTOM) 
     guidShown = [] 
     def get_cont(newstory): 
      if newstory.getGuid() not in guidShown: 
       cont.insert(END, newstory.getTitle()+"\n", "title") 
       cont.insert(END, (80 * '-').join("\n\n"), "title") 
       cont.insert(END, newstory.getSummary()) 
       cont.insert(END, (120 * '*').join("\n\n"), "title") 
       guidShown.append(newstory.getGuid()) 

     while True: 

      print "Polling . . .", 
      Get stories from Google's Top Stories RSS news feed 
      stories = process("http://news.google.com/?output=rss") 

      Get stories from Yahoo's Top Stories RSS news feed 
      stories.extend(process("http://rss.news.yahoo.com/rss/topstories")) 

      stories = filterStories(stories, triggerlist) 

      map(get_cont, stories) 
      scrollbar.config(command=cont.yview) 


      print "Sleeping..." 
      time.sleep(SLEEPTIME) 

    except Exception as e: 
     print e 


if __name__ == '__main__': 

    root = Tk() 
    root.title("Some RSS parser") 
    thread.start_new_thread(main_thread, (root,)) 
    root.mainloop() 

現在,這裏是錯誤消息從測試 - 如果可能的話請大家幫幫忙......它擁有我的心搞得一團糟! Total的SEVEN測試正在給出這些錯誤信息,而且我仍然無能爲力,無法解決問題!請幫助,如果你可以... ciao!

Test: 1 make TitleTrigger 
Your output: 
Trigger file: 
t1 TITLE NASA 

**** RUNNING MAKETRIGGER ON TRIGGER FILE **** 
None 
**** ERROR: It looks like your makeTrigger function is not properly returning the  trigger; the trigger, rather than 'None', should not have printed on the previous line. 
--> Is trigger t1 in triggerMap? False 
**** ERROR: Length of triggerMap should be 1; it is: 0 
Correct output: 
Trigger file: 
t1 TITLE NASA 

**** RUNNING MAKETRIGGER ON TRIGGER FILE **** 
TitleTrigger made with word: NASA 
--> Is trigger t1 in triggerMap? True 

Test: 2 make basic triggers 
Your output: 
Trigger file: 
t1 TITLE NASA 
t2 SUBJECT Mars 
t3 SUMMARY rocket 

**** RUNNING MAKETRIGGER ON TRIGGER FILE **** 
None 
--> Is trigger t1 in triggerMap? False 
None 
--> Is trigger t2 in triggerMap? False 
None 
--> Is trigger t3 in triggerMap? False 
**** ERROR: Length of triggerMap should be 3; it is: 0 
Correct output: 
Trigger file: 
t1 TITLE NASA 
t2 SUBJECT Mars 
t3 SUMMARY rocket 

**** RUNNING MAKETRIGGER ON TRIGGER FILE **** 
TitleTrigger made with word: NASA 
--> Is trigger t1 in triggerMap? True 
SubjectTrigger made with word: Mars 
--> Is trigger t2 in triggerMap? True 
SummaryTrigger made with word: rocket 
--> Is trigger t3 in triggerMap? True 
Test: 3 make PhraseTriggers 
Your output: 
Trigger file: 
t1 PHRASE NASA 
t2 PHRASE Curiosity Mars rover 
t3 PHRASE Neil deGrasse Tyson 

**** RUNNING MAKETRIGGER ON TRIGGER FILE **** 
None 
--> Is trigger t1 in triggerMap? False 
None 
--> Is trigger t2 in triggerMap? False 
None 
--> Is trigger t3 in triggerMap? False 
**** ERROR: Length of triggerMap should be 3; it is: 0 
Correct output: 
Trigger file: 
t1 PHRASE NASA 
t2 PHRASE Curiosity Mars rover 
t3 PHRASE Neil deGrasse Tyson 

**** RUNNING MAKETRIGGER ON TRIGGER FILE **** 
PhraseTrigger made with phrase: NASA 
--> Is trigger t1 in triggerMap? True 
PhraseTrigger made with phrase: Curiosity Mars rover 
--> Is trigger t2 in triggerMap? True 
PhraseTrigger made with phrase: Neil deGrasse Tyson 
--> Is trigger t3 in triggerMap? True 
Test: 4 make NotTriggers 
Your output: 
Trigger file: 
t1 TITLE Manitoba 
t2 NOT t1 
t3 PHRASE British Colombia 
t4 NOT t3 

**** RUNNING MAKETRIGGER ON TRIGGER FILE **** 
None 
--> Is trigger t1 in triggerMap? False 
None 
--> Is trigger t2 in triggerMap? False 
None 
--> Is trigger t3 in triggerMap? False 
None 
--> Is trigger t4 in triggerMap? False 
**** ERROR: Length of triggerMap should be 4; it is: 0 
Correct output: 
Trigger file: 
t1 TITLE Manitoba 
t2 NOT t1 
t3 PHRASE British Colombia 
t4 NOT t3 

**** RUNNING MAKETRIGGER ON TRIGGER FILE **** 
TitleTrigger made with word: Manitoba 
--> Is trigger t1 in triggerMap? True 
NotTrigger made with trigger: TitleTrigger("Manitoba") 
--> Is trigger t2 in triggerMap? True 
PhraseTrigger made with phrase: British Colombia 
--> Is trigger t3 in triggerMap? True 
NotTrigger made with trigger: PhraseTrigger("British Colombia") 
--> Is trigger t4 in triggerMap? True 
Test: 5 make AndTriggers 
Your output: 
Trigger file: 
t1 TITLE Manitoba 
t2 PHRASE British Colombia 
t3 AND t1 t2 

**** RUNNING MAKETRIGGER ON TRIGGER FILE **** 
None 
--> Is trigger t1 in triggerMap? False 
None 
--> Is trigger t2 in triggerMap? False 
None 
--> Is trigger t3 in triggerMap? False 
**** ERROR: Length of triggerMap should be 3; it is: 0 
Correct output: 
Trigger file: 
t1 TITLE Manitoba 
t2 PHRASE British Colombia 
t3 AND t1 t2 

**** RUNNING MAKETRIGGER ON TRIGGER FILE **** 
TitleTrigger made with word: Manitoba 
--> Is trigger t1 in triggerMap? True 
PhraseTrigger made with phrase: British Colombia 
--> Is trigger t2 in triggerMap? True 
AndTrigger made with triggers: TitleTrigger("Manitoba") and PhraseTrigger("British Colombia") 
--> Is trigger t3 in triggerMap? True 
Test: 6 make OrTriggers 
Your output: 
Trigger file: 
t1 SUBJECT Harper 
t2 PHRASE Prime Minister 
t3 OR t1 t2 

**** RUNNING MAKETRIGGER ON TRIGGER FILE **** 
None 
--> Is trigger t1 in triggerMap? False 
None 
--> Is trigger t2 in triggerMap? False 
None 
--> Is trigger t3 in triggerMap? False 
**** ERROR: Length of triggerMap should be 3; it is: 0 
Correct output: 
Trigger file: 
t1 SUBJECT Harper 
t2 PHRASE Prime Minister 
t3 OR t1 t2 

**** RUNNING MAKETRIGGER ON TRIGGER FILE **** 
SubjectTrigger made with word: Harper 
--> Is trigger t1 in triggerMap? True 
PhraseTrigger made with phrase: Prime Minister 
--> Is trigger t2 in triggerMap? True 
OrTrigger made with triggers: SubjectTrigger("Harper") and PhraseTrigger("Prime Minister") 
--> Is trigger t3 in triggerMap? True 
Test: 7 make composite triggers 
Your output: 
Trigger file: 
t1 SUBJECT Cameron 
t2 SUBJECT Gillard 
t3 SUBJECT Harper 
t4 PHRASE Prime Minister 
t5 AND t1 t4 
t6 AND t2 t4 
t7 OR t3 t4 

**** RUNNING MAKETRIGGER ON TRIGGER FILE **** 
None 
--> Is trigger t1 in triggerMap? False 
None 
--> Is trigger t2 in triggerMap? False 
None 
--> Is trigger t3 in triggerMap? False 
None 
--> Is trigger t4 in triggerMap? False 
None 
--> Is trigger t5 in triggerMap? False 
None 
--> Is trigger t6 in triggerMap? False 
None 
--> Is trigger t7 in triggerMap? False 
**** ERROR: Length of triggerMap should be 7; it is: 0 
Correct output: 
Trigger file: 
t1 SUBJECT Cameron 
t2 SUBJECT Gillard 
t3 SUBJECT Harper 
t4 PHRASE Prime Minister 
t5 AND t1 t4 
t6 AND t2 t4 
t7 OR t3 t4 

**** RUNNING MAKETRIGGER ON TRIGGER FILE **** 
SubjectTrigger made with word: Cameron 
--> Is trigger t1 in triggerMap? True 
SubjectTrigger made with word: Gillard 
--> Is trigger t2 in triggerMap? True 
SubjectTrigger made with word: Harper 
--> Is trigger t3 in triggerMap? True 
PhraseTrigger made with phrase: Prime Minister 
--> Is trigger t4 in triggerMap? True 
AndTrigger made with triggers: SubjectTrigger("Cameron") and PhraseTrigger("Prime Minister") 
--> Is trigger t5 in triggerMap? True 
AndTrigger made with triggers: SubjectTrigger("Gillard") and PhraseTrigger("Prime Minister") 
--> Is trigger t6 in triggerMap? True 
OrTrigger made with triggers: SubjectTrigger("Harper") and PhraseTrigger("Prime Minister") 
--> Is trigger t7 in triggerMap? True 

回答

0

看起來你從來沒有在makeTrigger函數內執行任何代碼;你只是定義了另一個函數readTriggerConfig。

您是否試圖將makeTrigger函數與代碼的其餘部分隔離開來,並使用一小組已知輸入運行它以更好地瞭解其行爲?通過調試器來執行它的執行可能會幫助您瞭解發生的事情。