class Tasks(object):
def __init__(self, container=None):
if container is None:
container = []
self.container = container
def add(self,name,date,priority):
self.container.append([name,date,priority])
def __str__(self):
return str(self.container)
def __repr__(self):
return str(self.container)
def __getitem__(self, key):
return Tasks(self.container[key])
def __len__(self):
return len(self.container)
class management(Tasks):
def save(self):
outfile = open ("tasks.txt","w")
outfile.write(("\n".join(map(lambda x: str(x), task))))
print task
outfile.close ()
def load(self):
load_file = open("tasks.txt","r")
task = load_file.readlines()
print task
#this line is the attempt to convert back into the original format
Tasks(add(task))
task = Tasks()
if __name__== "__main__":
p = management(Tasks)
#task.add("birthday","27092012","high")
#task.add("christmas","20062000","medium")
#task.add("easter","26011992","low")
print task
#print len(task)
#p.save()
p.load()
print "test",task
print len(task)
the ultimate aim of my code is to generate a task manager(to do list)
the code above generates a list of [name,date,priority], it then saves it in a text file called tasks.txt – as far as im aware this works perfectly(as long as i comment out p.load).
however… the load function loads the file but I need to be able to print the list it loads as print task as I did when I had commented out p.load().
this will enable me to be able to eventually, delete,sort etc. tasks
thanks in advance
I apologise for the bad question I didn’t know how to word it on1 line
edit:
I thought about pickling which would preserve the list format, but i dont think it would solve my problem of being able to pass the arguments back into the Tasks() class in order to be able to print them as print task
edit 2
the load function now reads
def load(self):
with open("tasks.txt", "r") as load_file:
tasks = [ast.literal_eval(ln) for ln in load_file]
print tasks
for t in tasks:
todo.add(t)
obviously (or at least I think ) I get the error NameError: global name ‘todo’ is not defined
I have tried with task.add(t) and get TypeError: add() takes exactly 4 arguments (2 given)
I also tried with Tasks.add(t) and got the error TypeError: unbound method add() must be called with Tasks instance as first argument (got list instance instead)
I clearly dont understand the code, could you clarify, thanks.
edit 3
while True:
menu_choice = int(input(“Select a number from the menu”))
try:
if menu_choice == 1:
task = raw_input ("task")
date = raw_input ("date")
priority = raw_input ("priority")
tasks = Tasks([(task,date,priority)])
print tasks
elif menu_choice == 2:
print tasks
elif menu_choice == 3:
tasks.save()
elif menu_choice == 4:
tasks.load()
except:
print sys.exc_info()
this over writes the task each time instead of appending it, any ideas? also menu choice 2,3,4 dont work because tasks isnt defined globally, not sure how I can get round this? maybe returning?
Trying a different, more Pythonic approach. Not so much an answer, but more an alternative. Edited thrice to take into account a number of requests for new features.
IMPORTANT NOTE: This is code produced as an exercise to demonstrate many of the features that you are seeking to implement and to show how a proper use of object oriented design allows you to encapsulate each task and task-list and to manipulate them with ease. It also demonstrates the use of pickle as a Pythonic alternative to the text file implementation that you were using in the original question. It also demonstrates searching and sorting by field, implementing two simple methods.
If you are serious about this project, you need to look at a proper database such as SQLite that will allow man more features for searching, sorting, updating, adding, and deleting records than you could reliably implement by hand.