I got a error message in Python:
AttributeError: 'str' object has no attribute 'compute_mark_list' in my code.
This code suppose to read though a XML file and get the content inside its children:
import libxml2 import sys ''' purpose store the information from an answer element ''' class Answer: def __init__(self, index, path, result, answer, time): self.index = index self.path = path self.result = result self.answer = answer self.time = time ''' purpose Store the information from a display element. ''' class Display: def __init__(self, index, path, time): self.index = index self.path = path self.time = time ''' purpose Extract the information from log_file and return it as a list of answer and display objects. preconditions log_file is the name of a legal, readable quiz log XML file ''' def load_quiz_log(log_file): # parse the XML file in argv[2] and load it into memory parse_tree = libxml2.parseFile(sys.argv[1]) # create a context for tree traversal context = parse_tree.xpathNewContext() root = parse_tree.getRootElement() log_file = root.children #create a list to hold the objects L = [ ] while log_file is not None: if log_file.name == 'answer': answer = log_file.content if log_file.name == 'display': display = log_file.content log_file = log_file.next L = [answer, display] return L ''' purpose Return the number of distinct questions in log_list. preconditions log_list was returned by load_quiz_log ''' def compute_question_count(log_list): while log_list is not None: if log_list.name == 'answer': count += 1 else: break log_list = log_list.next return count ''' purpose Extract the list of marks. For each index value, use the result from the last non-empty answer, or 0 if there are no non-empty results. preconditions log_list was returned by load_quiz_log ''' def compute_mark_list(log_list): marks = [ ] while log_list is not None: if log_list.name == 'answer': log_list_answer = log_list.children while log_list_answer is not None: if log_list_answer.name == 'index': index = 0 if log_list_answer.content == index: result = log_list_answer.next.next.content index = log_list_answer.content marks.append(result) else: continue log_list_answer = log_list_answer.next log_list = log_list.next return marks When I run this code I get the error message in the title
import libxml2 import sys import quiz_library print '-------------------- test load_quiz_log' log_list = quiz_library.load_quiz_log(sys.argv[1]) for x in log_list: if isinstance(x, quiz_library.Answer): print 'Answer:', x.index, x.path, x.result, x.answer, x.time else: print 'Display:', x.index, x.path, x.time print '-------------------- test compute_question_count' question_count = quiz_library.compute_question_count(log_list) print question_count print '-------------------- test compute_mark_list' mark_list = quiz_library.compute_mark_list(log_list) print mark_list The XML file looks like:
No comments:
Post a Comment