Thank you for your suggestion. I did not create the original script, so it
will stay as is and my addition for the menu has been adjusted.
Now that I can make a clear distinction of what I am returning, I am getting a
new error that leads me that I am trying to call a function that cannot be seen
when instantiated:
To add a name, please input he following information:
Name: Bryan
Number: 1234567890
What type of number is this? (choose one):
1. Home:
2. Work:
3. Cell:
: 1
Traceback (most recent call last):
File "menu_modified.py", line 95, in <module>
menu.addName()
File "menu_modified.py", line 73, in addName
enter(name, number, returnType)
AttributeError: phoneentry instance has no __call__ method
[code]
class MenuInput:
# ask user to input data and store to be passed to manageable variables.
def addName(self):
print "To add a name, please input he following information: "
name = raw_input("Name: ")
number = raw_input("Number: ")
get = int(raw_input("What type of number is this? (choose one):
\n \
1. Home:\n \
2. Work:\n \
3. Cell:\n \
: "))
returnType = self.numberType(get)
#print returnType
enter = phoneentry()
enter(name, number, returnType)
[/code]
I googled the __call__ method, but I am not getting to the meat of what all of
the leads have to say. I know it has to deal with when I instantiated
phoneentry() within addName(), I just don't know how to fix it or "call" it
properly.
Again, thanks in advance.
----- Original Message ----
From: Evert Rol <[EMAIL PROTECTED]>
To: Bryan Magalski <[EMAIL PROTECTED]>
Cc: tutor@python.org
Sent: Wednesday, November 14, 2007 1:28:45 PM
Subject: Re: [Tutor] Interactive Menu Woes
> I am trying to build a menu for the following script to make it
> more "user friendly". Nothing fancy, just a simple add data and
> look up the entered results.
>
> The problem is that when I run my modified version with this
> snippet (please see attachment for original and my modified
versions):
>
> [code]
> class MenuInput:
>
> # ask user to input data and store to be passed to manageable
> variables.
> def addName(self):
> print "To add a name, please input he following
> information: "
> self.enter = phoneentry()
> self.name = raw_input("Name: ")
> self.number = raw_input("Number: ")
> self.get = int(raw_input("What type of number is
> this? (choose one): \n 1. Home:\n 2. Work:\n 3. Cell:\n : "))
> def
numberType(self, get = 1):
> if self.get == 1:
> self.returnType = HOME
> #return self.getType
> elif self.gete == 2:
> self.returnType = WORK
> #return self.getType
> elif self.get == 3:
>
self.returnType = FAX
> #return self.getType
> return self.returnType
>
> self.type = numberType(self.get)
> self.enter(self.name, self.number, self.returnType)
>
Bypassing the whole 'function within function' problem, consider
using a dictionary instead: somedict = {1: HOME, 2: WORK, 3: FAX},
and self.type = somedict[self.get]. This also avoids endless if-elif-
elif-elif sequences (looking at showtype() in your original script,
where you can
apply the same technique); it's often clearer, and
certainly shorter.
____________________________________________________________________________________
Never miss a thing. Make Yahoo your home page.
http://www.yahoo.com/r/hs
import shelve
import string
UNKNOWN = 0
HOME = 1
WORK = 2
FAX = 3
CELL = 4
class phoneentry:
def __init__(self, name = 'Unknown', number = 'Unknown', types = UNKNOWN):
self.name = name
self.number = number
self.type = types
# create string representation
def __repr__(self):
return('%s:%d' % ( self.name, self.type ))
# fuzzy compare or two items
def __cmp__(self, that):
this = string.lower(str(self))
that = string.lower(that)
if string.find(this, that) >= 0:
return(0)
return(cmp(this, that))
def showtype(self):
if self.type == UNKNOWN: return('Unknown')
if self.type == HOME: return('Home')
if self.type == WORK: return('Work')
if self.type == FAX: return('Fax')
if self.type == CELL: return('Cellular')
class phonedb:
def __init__(self, dbname = 'phonedata'):
self.dbname = dbname;
self.shelve = shelve.open(self.dbname);
def __del__(self):
self.shelve.close()
self.shelve = None
def add(self, name, number, types = HOME):
e = phoneentry(name, number, types)
self.shelve[str(e)] = e
def lookup(self, string):
list = []
for key in self.shelve.keys():
e = self.shelve[key]
if cmp(e, string) == 0:
list.append(e)
return(list)
class MenuInput:
# ask user to input data and store to be passed to manageable variables.
def addName(self):
print "To add a name, please input he following information: "
name = raw_input("Name: ")
number = raw_input("Number: ")
get = int(raw_input("What type of number is this? (choose one): \n \
1. Home:\n \
2. Work:\n \
3. Cell:\n \
: "))
returnType = self.numberType(get)
#print returnType
enter = phoneentry()
enter(name, number, returnType)
def numberType(self, n):
typeDict = {1: HOME, 2: WORK, 3: FAX}
if n in typeDict:
return typeDict[n]
def display(self):
print "Enter a name to look up: (leave blank to exit)"
self.Name = str(raw_input("Name: "))
print "%s has the following information: " % self.Name
if self.Name != "":
foo = phonedb()
for entry in foo.lookup(self.Name):
print '%-40s %s (%s)' % (entry.name, entry.number, entry.showtype())
print
menu = MenuInput()
menu.addName()
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor