I am making an autograder that takes in an +100 excel sheets and automatically marks them. A pretty basic automation task.
I’m doing this by building a class called Marking, a simplified version is shown below:
class Marking():
# Get the list of all excel files
def __init__(self, directory_data):
self.files_data = os.listdir(directory_data)
self.questions = []
# Get a single workbook
def load_workbook(self, order):
self.workbook = load_workbook(self.files_data[order])
# Set the questions and their answers
def set_question(self, question):
self.questions.append(question)
m = Marking('../data/')
# Input question #1 as a dictionary
m.set_question({
...
})
# Input question #2 as a dictionary
m.set_question({
...
})
My issue is that I have 29 questions, and I feel like the way I’m initializing the questions is unwieldy and unpythonic. The other option that I have is just putting those 29 questions directly into the init method, but that seems even worse to me.
I’ve tried looking online for how to do what I’m doing in a better and more scalable way, but I couldn’t find an answer.
Edited:
Here is examples of how the questions look like:
m.set_question({
'type': 'range',
'range0': 0.05,
'range1': 0.2,
'location': 'J241',
'unit_location': 'K241',
'marks_available': 1
})
m.set_question({
'type': 'simple',
'answer': 6,
'location': 'J254',
'marks_available': 1
})
m.set_question({
'type': 'error',
'theo': 'E254',
'meas': 'J190',
'location': 'J254',
'marks_available': 1
})
>Solution :
Given that it’s going to be very ugly to do this 29 times, or to call your constructor with 29 dictionaries as arguments, I would suggest you serialize these dictionaries in a file, and then have your __init__ read them in. You could pass the name of the file to the constructor to allow for flexibility.