Climbs_lika_Spyder
Climbs_lika_Spyder

Reputation: 6724

class or object instead of dictionaries in Python 2

I normally use nested dictionaries, but I'd like to move into classes and objects. I have a list of home work grades that look like:

assignment_name assignment_subject student_name due_date grade grade_pct rank

I'd like to make an object that holds all the students and each students holds all of their assignments details. My dictionary would look like:

homeworks['student_name']['assignment_name'] = {'assignment_subject': 'math', 'due_date': '11/12/13', 'grade': 15, 'grade_pct': 15/20.0, 'rank': 'B'}

Should this be a class within a class?

class Homeworks(object):
    def _init_(self):
        self.students

    class student(object):
        def _init_(self,student_name):
             self.student_name = student_name

        def add_assignment(line):
             student.assignment_name = line[0]
             student.assignment_subject = line[1]
             student.grade = line[4]

I know this isn't right. I don't know if I am on the right track or not. I do need to be able to access the grades for one student. (for assignment in Homeworks['Robbie Michaels'].keys(): print Homeworks['Robbie Michaels'][assignment]['grade']) I also like to access them using if assignment_subject == 'math' etc.

Upvotes: 0

Views: 182

Answers (2)

tmj
tmj

Reputation: 1858

class student:
    def __init__(self, student_name):
        self.name = student_name

class homework:

    def __init__(self,student_name,assignment_name,assignment_subject,grade):
        self.student = student(student_name)
        self.assignment_name = assignment_name
        self.assignment_subject = assignment_subject
        self.grade = grade

    @classmethod
    def new_hw(cls, line):
        return cls(line[0],line[1],line[2],line[3])

    @classmethod
    def get_all_grades(cls, student_name, homework_list):
        return [ x.grade for x in homework_list if student_name is x.student.name]

lines = [["abc","a","b","A"],["abc","c","d","B"],["ebc","c","d","B"]]
hw_list = [homework.new_hw(line) for line in lines]
print homework.get_all_grades("abc",hw_list)

What you need to understand is "How to design Object-Orientedly?". The above code, may not be the best of designs, hence, try this to learn where to start. How do I design a class in Python?

Upvotes: 1

qwertynl
qwertynl

Reputation: 3933

Don't do that.

Just make a Homework class and a Student class.

You can create a list of Homework classes and then just pass the students that are part of each "homework" to their respective object.

For example:

class Homework(object):
    def _init_(self, name, subject):
       self.assignment_name = name
       self.assignment_subject = name
       self.students = []

    def add_student(self, student):
       self.students.append(student)

class Student(object):
    def _init_(self, student_name):
         self.student_name = student_name

You can expand from there.


What I would really do is have a list of Classes that each have homework.

And each student could be in multiple Classes, etc, but it really all up to you

Have fun with it!

Upvotes: 0

Related Questions