a question about flushing input buffer
Jacob A Hoehn
hoehn at cs.utk.edu
Wed Apr 12 14:01:53 EDT 2000
I am expanding on the rolodex example given in the learning python book,
as a project for me to better learn python. I have run into a problem. I
have a class named Rolodex that uses the cmd module. One of the methods
for the class is to add a user to the rolodex. In the book example it just
has you enter the person's name and phone number. I want to alter this
so that you can also enter in additional information. So, in the add
method I have the following statement:
print "Enter additional information (to stop, hit ^D):"
try:
self.info[name] = sys.stdin.readlines()
sys.stdin.flush()
except EOFError: return
The problem is, when I run the program I get the following:
ROLODEX> add foo
Enter phone number for foo: 911
Enter additional information (to stop, hit ^D):
hello
world
ROLODEX> list
=========================================
foo : 911
=========================================
ROLODEX> find foo
=========================================
The number for foo is 911.
Additional information:
hello
world
=========================================
ROLODEX> add guido
Enter phone number for guido: 111-1111
Enter additional information (to stop, hit ^D):
ROLODEX> list
=========================================
foo : 911
guido : 111-1111
=========================================
ROLODEX> find guido
=========================================
The number for guido is 111-1111.
Additional information:
=========================================
ROLODEX>
It is not letting me enter in the additional information. It skips it as
if I had typed cntrl D. So, is there a buffer that I need to be flushing?
I thought sys.stdin.flush() would do that. Below is all of the code.
Thanks for any help you can give me and I appologize for the length of
this email.
Jacob Hoehn
#!/usr/local/bin/python
# rolodex, a simpler version of this exists in the oreilly
# learning python book... i added some stuff
import string, sys, pickle, cmd
class Rolodex(cmd.Cmd):
def __init__(self):
cmd.Cmd.__init__(self)
self.prompt = "ROLODEX> "
self.people = {}
self.info = {}
def help_add(self):
print "Adds an entry (specifiy a name)"
def do_add(self, name):
if name == "": name = raw_input("Enter name: ")
name = string.lower(name)
if self.people.has_key(name):
option = ""
while 1:
if option == 'y': break
if option == 'n': return
option = raw_input("Name exists! " \
"Overwrite? Enter [y/n] ")
phone = raw_input("Enter phone number for "+ name+": ")
self.people[name] = phone
print "Enter additional information (to stop, hit ^D):"
try:
self.info[name] = sys.stdin.readlines()
sys.stdin.flush()
except EOFError: return
def help_delete(self):
print "Delete an entry (specify a name)"
def do_delete(self, name):
if name == "": name = raw_input("Enter name: ")
name = string.lower(name)
try:
del self.people[name]
del self.info[name]
except KeyError:
print "Name not found"
def help_find(self):
print "Find an entry (specifiy a name)"
def do_find(self, name):
if name == "": name = raw_input("Enter name: ")
name = string.lower(name)
if self.people.has_key(name):
print '='*41
print "The number for %s is %s." % \
(name, self.people[name])
print "\nAdditional information:\n"
for line in self.info[name]: print line,
print '='*41
else:
print "We have no record for %s." % (name,)
def help_list(self):
print "Prints the contents of the directory"
def do_list(self, line):
names = self.people.keys()
if names == []: return
names.sort()
print '='*41
for name in names:
print string.rjust(name, 20), ":", \
string.ljust(self.people[name], 20)
print '='*41
def help_EOF(self):
print "Quits the program"
def do_EOF(self, line):
sys.exit()
def help_save(self):
print "save the current state of affairs"
def do_save(self, filename):
if filename == "":
filename = raw_input("Enter filename: ")
saveFile = open(filename, 'w')
pickle.dump(self.people, saveFile)
pickle.dump(self.info, saveFile)
def help_load(self):
print "load a directory"
def do_load(self, filename):
if filename == "":
filename = raw_input("Enter filename: ")
saveFile = open(filename, 'r')
self.people = pickle.load(saveFile)
self.info = pickle.load(saveFile)
if __name__ == '__main__':
rolo = Rolodex()
rolo.cmdloop()
More information about the Python-list
mailing list