0

代码应该返回触发器对象 - 触发器映射的长度应该显示实际存在的触发器的实际数量......如果至少有一个或多个触发器,它应该返回 True 但它们都返回 FALSE 和零触发器...有人可以帮忙吗?代码在下面,测试人员的错误消息将打印在下面。

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()

现在这里是来自测试人员的错误信息——如果可能的话请帮忙……我的脑子都糊涂了!!!总共有七个测试给出了这些错误消息,我仍然不知道如何修复它们!如果可以的话,请帮忙... 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
4

1 回答 1

0

看起来您从未在 makeTrigger 函数中执行任何代码;您只是在定义另一个函数 readTriggerConfig。

您是否尝试过将 makeTrigger 函数与其余代码隔离开来,并使用一小组已知输入来运行它以更好地了解其行为?使用调试器逐步执行它可能会帮助您了解正在发生的事情。

于 2013-04-01T08:57:34.427 回答