def handle_average(argv, Class, class_str, call_print_averages=True):
    '''Pass the appropriate data to get the corresponding averages.

    Keyword arguments:
    argv -- An array of command line arguments.
    Class -- The class in which to find the averages.
    class_str -- The stringified Classname (e.g., a Student class would be
    "Student").
    call_print_averages -- Boolean value to optionally print the averages
    '''
    arr = []
    for i in mean:
        i["pts_total"] = 0.0
        i["mean"] = 0.0
        i["num"] = 0
    for exercise in Exercise.select():
        compare = {"Student": exercise.student.id,
                   "Batch": exercise.student.batch.id,
                   "School": exercise.student.batch.school.id}
        if str(compare.get(class_str)) == str(argv[2]):
            get_average(exercise, argv)
    for i in range(0, len(mean)):
        if call_print_averages is True:
            print_averages(i)
        else:
            if print_averages(i, False) is not None:
                arr.append(print_averages(i, False))
    for student in Class.select():
        if str(student.id) == str(argv[2]):
            return arr
    print class_str + " not found"
def export_json(argv):
    '''Export all data in JSON format.

    Keyword arguments:
    argv -- An array of command line arguments.
    '''
    data = []
    for school in School.select():
        dict = {"name": school.name}
        data.append(dict)

    '''Add the batches.'''
    for dict in data:
        batches = []
        for batch in Batch.select():
            if batch.school.name == dict["name"]:
                batch_dict = {}
                dict["batches"] = batches
                batch_dict["name"] = batch.name
                batches.append(batch_dict)

    '''Add the students in their batch.'''
    for dict in data:
        if dict.get("batches") is not None:
            for batch in dict.get("batches"):
                students = []
                for student in Student.select():
                    if str(student.batch.name) == str(batch["name"]):
                        student_dict = {}
                        batch["students"] = students
                        student_dict["first_name"] = student.first_name
                        student_dict["last_name"] = student.last_name
                        student_dict["age"] = student.age
                        students.append(student_dict)

    '''Add the exercises to the corresponding student.'''
    for dict in data:
        if dict.get("batches") is not None:
            for batch in dict.get("batches"):
                for student in batch["students"]:
                    exercises = []
                    for e in Exercise.select():
                        if e.student.first_name == student.get("first_name"):
                            exercsie_dict = {}
                            student["exercises"] = exercises
                            exercsie_dict["note"] = e.note
                            exercsie_dict["subject"] = e.subject
                            exercises.append(exercsie_dict)

    print json.dumps(data)
def print_all(argv):
    '''Print all data in database, ordered with tab heirarchy.

    Keyword arguments:
    argv -- An array of command line arguments passed to the program.
    '''
    for school in School.select():
        print school
        for batch in Batch.select():
            if batch.school.id == school.id:
                print "\t" + str(batch)
                for student in Student.select():
                    if student.batch.id == batch.id:
                        print "\t\t" + str(student)
                        for exercise in Exercise.select():
                            if exercise.student.id == student.id:
                                print "\t\t\t" + str(exercise)
	elif sys.argv[1] == 'print':
		if len(sys.argv) >= 3:
			if sys.argv[2] == 'school':
				for school in School.select():	
					print school
			elif sys.argv[2] == 'batch':
				for batch in Batch.select():
					print batch
			elif sys.argv[2] == 'user':
				for user in User.select():
					print user
			elif sys.argv[2] == 'student':
				for student in Student.select():
					print student
			elif sys.argv[2] == 'exercise':
				for exercise in Exercise.select():
					print exercise					
	elif sys.argv[1] == 'insert':
		if sys.argv[2] == 'school':
			school = School.insert(name=sys.argv[3]).execute()
			print "New school: " + str(School.select().where(School.id==school).get())
		elif sys.argv[2] == 'batch':
			batch = Batch.insert(school=sys.argv[3], name=sys.argv[4]).execute()
			print "New batch: "	+ str(Batch.select().where(Batch.id==batch).get())
		elif sys.argv[2] == 'student':
			if len(sys.argv) == 6:
				student = Student.insert(batch=sys.argv[3], age=sys.argv[4], last_name='', first_name=sys.argv[5]).execute()
			else:		
				student = Student.insert(batch=sys.argv[3], age=sys.argv[4], last_name=sys.argv[5], first_name=sys.argv[6]).execute()
			print "New student: " + str(Student.select().where(Student.id==student).get())
		elif sys.argv[2] == 'exercise':