I'm storing the call to a class Task in an array in a .dat file. I'd like to read this file and reconstruct the class calls.
Here's the class that I'm using right now:
class Task:
def __init__(self, name, timespent):
self.name = name
self.timespent = timespent
def __repr__(self):
return repr('Task("%s",%s)'%(self.name, self.timespent))
Here's the reading from the file:
task_list = []
with open("task_list2.dat", "r") as file:
task_list = eval(file.readline())
Here's the writing to the file:
with open("task_list2.dat", "w") as outFile:
print(repr(task_list), file = outFile)
And here's the contents of the file:
['Task("class",20)']
Where "class" is the name of the task.
I understand that the problem has to do with the single quotes around 'Task("class",20)' but I have no clue how to get rid of them. The error message I get says something along the lines of: "str object has no attribute 'name'"
How can I remove those quotes so that I can reconstruct the classes the next time that I read the file?