Kent Johnson wrote:
On Wed, Apr 8, 2009 at 8:38 PM, David <da...@abbottdavid.com> wrote:
Hi Everyone,
I have a simple todo list program with functions to add, delete, edit and
print out a list of todo's. I need to understand how to remove the same
blocks of code I use in each function. Should I create a class for the same
block of code I am including in each function?
No, you can make functions to contain common code. You are sort of on
the right track with your load_todo() function, though it should
really be called save_todo().
Here are two of the
functions;
def main():
print '\nYour current Todo list is: \n'
if os.path.exists('todo.dat'):
try:
fname = open('todo.dat', 'rb')
data = cPickle.Unpickler(fname)
todo = data.load()
load_todo(todo)
Not sure why you call load_todo() here, it writes back to the file you
just read.
You could have a real load_todo() that looks like this:
def load_todo():
if (os.path.exists('todo.dat')):
with open('todo.dat', 'rb') as fname: # with statement is simpler
than try/finally
data = cPickle.Unpickler(fname)
todo = data.load()
else:
todo = {}
return todo
Then your main() could look like this:
def main():
print '\nYour current Todo list is: \n'
todo = load_todo()
if todo:
for k, v in todo.iteritems():
print k, v[0], v[1]
else:
print "You have 0 todo's"
menu()
Kent
Ok, I got it now, rename load_todo to save_todo, then create the load_todo.
thanks kent
--
Powered by Gentoo GNU/Linux
http://linuxcrazy.com
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor