On Sun, 5 Dec 2004, Jacob S. wrote:
I did something like this about three or four months ago...
This is what I did. Notice the use of the built-in str() and eval()
functions to write and receive data to and from Telephone.cfg...
Thanks a lot Jacob, and to all who replied. I'll go through the code
definitely. I started building that address book last night and its
pretty crude. I hit a snag though: i was able to save the name/email
address pairs and write to disk. But i cant get it to load on startup. My
location is several dirs down my home directory. Of course the pickled
file is in same directory as the code. Its something like:
if select == '1' or select == 'v' or select == 'V':
if file_in_disk in os.listdir('/home/jerimed'): # change???
fhandle = open(file_in_disk, 'r') # read mode
cPickle.load(fhandle) # restore saved data
fhandle.close()
show_contacts()
elif len(data_holder) > 0:
show_contacts()
else:
is_empty()
/home/jerimed should be changed and should be dynamic to match wherever
the python script is. Can you guyz advise? And is that first
if-statement right? I like to know if im doing the right thing.
How do i pretty print output of dictionary container? Sort of tabular
form or something, e.g.,
1. name1 email address1
2. name2 email address2
Just for my learning experience :-). Thanks!
--
Regards,
Eri Mendz
from __future__ import division
tel = {}
try:
file = open('Telephone.cfg', 'r')
except:
file = open('Telephone.cfg','w')
file.close()
file = open('Telephone.cfg','r')
try:
tel = eval(file.read())
a = 0
except:
a = 1
print "No entries on file."
pass
print """\
Commands are:
add
get
save
delete
quit
all is a wildcard
"""
while 1:
ask = raw_input('Tell me what you wish to do. ')
if ask == "quit":
break
ask = ask.split(" ")
command = ask[0]
entity = ask[1:]
entity = " ".join(entity)
if entity == '':
entity = raw_input("Who do you want to %s? " % command)
if command == 'add':
person = entity
if tel.has_key(person):
print "That person is already in there. If you wish to edit the
file, please delete the record first."
else:
tel[person] = raw_input("What is their phone number? ")
if command == 'get':
if a == 1:
print "Sorry, there are no entries available."
else:
person = entity
if person == 'all':
key = tel.keys()
key.sort()
print
for x in key:
print "%s\n%s\n" % (x,tel[x])
elif tel.has_key(person):
print "\n%s\n%s\n" % (person,tel[person])
else:
print "%s is not in your records." % person
if command == 'save':
file=open('Telephone.cfg', 'w')
file.write(str(tel))
file.close()
print 'Saved in Telephone.cfg'
if command == 'delete':
if a == 1:
print "Sorry, there are no entries available."
else:
person = entity
if person == 'all':
tel={}
newfile=open('Telephone.cfg', 'w')
newfile.close()
else:
if tel.has_key(person):
del tel[person]
else:
print "%s is not in your records." % person
file.close()
file = open('Telephone.cfg', 'w')
file.write(str(tel))
file.close()
As always, feel free to modify, use, and otherwise tear apart my code and
give me suggests on how to improve it.
Jacob Schmidt
Dear Tutor,
I like to know what is the proper procedure (is algorithmn the right
term?) in creating data in a program, write it to file, close the app
then retrieve the data when run again. Basically, I'm trying to simulate
a simple address book (well not really for the datas are just names for
now) and so far have created the basic menu interface. It is console
base so forget gui. I ask user input and store it in a list. There are
menus to change, delete the data, and to save the data list in file. I
use cPickle for this and have verified the file is created by checking
in my $PWD. I want to retrieve that data when program is run again. What
to add in my code? I thought not to post the code but explain it as
above.
What i want: when program is run again, the saved data is loaded when user
selects option 1 below. Of course the first time it is run, the list is
empty.
def print_options():
print '''
Options:
[1] - Print content of list
[2] - Add name to list
[3] - Delete name from list
[4] - Change name in list
[5] - Save list to file
[P] - Print this menu
[Q] - Quit
'''
--
Regards,
Eri Mendz
Using PC-Pine 4.61
--
Using PC-Pine 4.61
_______________________________________________
Tutor maillist - [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor