On Mon, 6 Dec 2004, Liam Clarke wrote:
hey thanks a lot Liam! didnt tried it yet but later i will. appreciate you all good people.
Just tested the setout thing. It works. Prolly a hack, but it works.
On Mon, 6 Dec 2004 19:05:58 +1300, Liam Clarke <[EMAIL PROTECTED]> wrote:[quote]
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() [/quote]
if file_in_disk in os.listdir('/home/jerimed'): -
if os.path.exists('/home/jerimed/file_in_disk'):
Oh, and if it's in a subdir off the current dir -
if os.path.exists('./home/jerimed/file_in_disk'):
"./' means current
or you could use - path = os.path.join(os.getcwd(), 'home','jerimed','filename')
[quote]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[/quote]
try this -
index = 0 for (key, item) in myDict.items(): index += 1 print "%d. %s \t %s" % (index, key, item)
Although you may find that the length of key will vary, making it look messy.
So, find the max length of the keys (names) first -
highLength=0 for element in myDict.keys(): if len(element) > highLength: highLength = len(element)
index = 0 minimumSpaces= 5 for (key, item) in myDict.items(): index += 1 spaceMult=(highLength+minimumSpaces)-len(key) outString=str(index)+". "+key+(spaceMult * " ") + item print outString
What this line spaceMult=(highLength+minimumSpaces)-len(key) does -
So, say you have two names -
Bob Bobalicious
obviously one tab(which Python usually counts as four spaces) separating will be
Bob Bob's email Bobalicious Bobalicious' email
spaceMult=(highLength+minimumSpaces)-len(key)
highLength is 11, the length of Bob. The minimum separation between key and item is 5 spaces, so we're looking for the item to be 16 chars away from the start of the line.
so spaceMult=(11+5)-len('bob') spaceMult = 13
So, the function will pad 13 spaces between 'bob' and 'bob's email' whereas only the minimum 5 between Bobalicious and his email.
Which should equal nicely laid out.
Haven't tested this though...
Standard disclaimer -
There's probably an easier way to do it, and a more elegant way. Which someone will post shortly.
Cheers,
Liam Clarke
On Mon, 6 Dec 2004 07:55:11 +0300 (Arab Standard Time), Eri Mendz <[EMAIL PROTECTED]> wrote: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
--
'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences.
-- Regards, Eri Mendz
_______________________________________________ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor