예제 #1
0
	def prepare_for_processing(self,verbose=True):
	
		if self.prepared:
			return self.code_list

		self.d = acedu.paths.corresponding()

		# I need a pretty printer for debugging purposes
		self.p = pprint.PrettyPrinter(indent=2)

		self.homeworks = []

		for homework_file in self.d:
			if verbose:
				print "parsing "+homework_file.path
			try:
				if homework_file.basename() != ".DS_Store":
					self.homeworks.append(parse(homework_file.path))
			except:
				print "parsing error in "+homework_file.path
				raise

		self.assignments = []

		for homework in self.homeworks:
			homework_assignments = homework.getElementsByTagName("assignment")
			if 0 == len(homework_assignments):
				print "Warning, encountered homework that had no assignment."
				print "outputing assignment"
				print homework.toprettyxml()
				sys.exit("Warning, encountered homework that had no assignment.")
			for assignment in homework_assignments:
				self.assignments.append(assignment)

		for assignment in self.assignments:
			attrs = attributesOf(assignment)
			try:
				assert(attrs["student"])
				print (attrs["student"])
				assert(attrs["book_edition"])
				assert(attrs["chapter"])
				problems = assignment.getElementsByTagName("problem")
				for problem in problems:
					attrs = attributesOf(problem)
					assert(attrs["number"])
					if verbose: print getText(problem.childNodes)
			except:
				print ("failed on "+str(attrs))
				raise

		self.assignments = map(Assignment,self.assignments)

		self.book_editions = defaultdict(list)
		for assignment in self.assignments:
			self.book_editions[assignment.book_edition].append(assignment)
		#print self.book_editions

		for assignment in self.assignments:
			if(assignment.chapter != int(sys.argv[1])):
				print assignment.student
				assert(assignment.chapter == int(sys.argv[1]))
			num_probs = len(assignment.problems)
			if verbose: print "num_probs was "+str(num_probs)+" for student "+assignment.student

		self.problems = []

		for assignment in self.assignments:
			for problem in assignment.problems:
				prob_id = (assignment.book_edition,assignment.chapter,problem.number)
				prob_data = (assignment.student,problem.text)
				prob = (prob_id,prob_data)
				self.problems.append(prob)

		if verbose: print len(self.problems)
		
		self.problem_groups = defaultdict(list)

		self.code_list = []
		for problem in self.problems:
			prob_id, prob_data = problem
			assignment_student,problem_text = prob_data
			prob_code_data = (assignment_student,prob_id)
			prob_code = str(hash(prob_code_data))
			self.code_list.append((prob_code,prob_code_data))
			self.problem_groups[prob_id].append(prob_code+"\n"+("="*50)+"\n"+problem_text)

		if verbose: self.p.pprint(self.code_list)

		#print "by groups"
		self.count_by_groups = 0
		for group in self.problem_groups.values():
			self.count_by_groups += len(group)
		if verbose: print self.count_by_groups
		
		self.prepared = True
		
		return self.code_list
예제 #2
0
파일: hwml.py 프로젝트: andrew-chen/actools
	def __init__(self,theNode):
		attrs = attributesOf(theNode)
		self.number = int(attrs["number"])
		self.text = getText(theNode.childNodes)