-1

This is code I made last year that takes data from google sheets, and uses it to make a CSV and upload assessment comments and grades to an online portal. I am sure this is not the best way to do this, but it works.

How can I take the following code and put it into a class setting?

orgid = wks1.col_values(1)   # student ID in  1 = col A
names = wks1.col_values(2)   # student concat names 2= col B
mark = wks1.col_values(4)   # student mark   4 = col D
commentList = wks1.col_values(15)   # Student comments  15 = col O *****
commentList =[x.strip(' ')  for x in commentList] #removes spaces at the end of comments 

del orgid[0]
del orgid[0]
del mark[0]
del mark[0]
marklist = list(zip(orgid, mark)) #combines student ID and marks for CSV
del names[0]
del names[0]
del commentList[0]
del commentList[0]
toplist = list(zip(names, commentList)) #combines student names and comments 
sorting = lambda toplist: toplist[0]
toplist.sort(key=sorting)

I have started making a class, but this is as far as I have gotten. I don't know how to take that info and put it into the class system. The youtube videos I have found deal with the creation of classes, but not so much how to put a larger amount of data into it.

class Student:
    def __init__(self, name, orgid, mark, comment):
        self.name=name
        self.orgid=orgid
        self.mark=mark
        self.comment=comment

I am sure this is rather basic, but I think that is where I'm at in my learning so far.

grolschie
  • 2,163
  • 2
  • 12
  • 29
Calvin Hobbes
  • 77
  • 1
  • 7

1 Answers1

1

Before looking at instantiating the class, you do not need all the del calls. Instead, you can get all the values after the first item by calling names[1:] - see get everything except the first element for details.

As for instantiating the class, for a single object, run:

student = Student(name, orgid, mark, comment)`

For multiple objects, you can use the zip function in a for loop:

for n, o, m, c in zip(orgid[1:], names[1:], marks[1:], comments[1:]):
    Student(n, o, m, c)

and to assign the result of this for loop to a variable, use a list comprehension:

students = [Student(n, o, m, c) for n, o, m, c in zip(orgid[1:], names[1:], marks[1:], comments[1:])]
David Whitlock
  • 312
  • 1
  • 4