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(("
".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)
我的代码的最终目的 是要产生一个任务经理( 做列表)
上面的代码产生一个列表[名称、日期、优先,然后保存在名为任务.txt - 的文本文件中,只要我意识到这一点是完全有效的(只要我对 p.load 进行评论)。
然而... 负载函数会加载文件, 但我需要能够打印列表 它载载载的打印任务, 就像我评论 p.load () 时一样 。
这将让我最终能够删除任务等等。
提前感谢
我为这个坏问题道歉 我不知道该怎么在1线上说
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)
我还尝试过任务.add(t), 并得到了错误 TypeError: 必须将无约束方法添加 () 调用任务实例作为第一个参数( 而不是列表实例)
我显然不明白代码, 你能澄清一下吗,谢谢。
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()
而不是附加任何想法? 菜单选择 2,3,不起作用, 因为任务不是全球性定义,