Newbie question 2

Rob talon2lm at yahoo.com
Sat Nov 8 20:59:36 EST 2003


Here's the code, I don't think my attachment made it..

import shelve
import string

UNKNOWN = 0
HOME = 1
WORK = 2
FAX = 3
CELL = 4

class phoneentry:
 def __init__(self, name = 'Unknown', number = 'Unknown',
   type = UNKNOWN):
  self.name = name
  self.number = number
  self.type = type

 #  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 = 'C:/phonedata'):
  self.dbname = dbname;
  self.shelve = shelve.open(self.dbname);

 def __del__(self):
  self.shelve.close()
  self.shelve = None

 def add(self, name, number, type = HOME):
  e = phoneentry(name, number, type)
  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)


def dele(name,ptype):
        e = shelve.open('C:/phonedata')
        print e
        f = name + ':' + ptype
        print
        try:
            del e[str(f)]
            e.close()
            print 'Entry deleted.'
        except (NameError):
            print 'No Entries with '+ptype+' exist!'

b = 1
foo = phonedb()
while b != 4:
    print
    print "Welcome to the Phone Database"
    print "Please choose from the following"
    print
    print "If you would like to display Phone Number(s) select 1"
    print
    print "If you would like to add an entry select 2"
    print
    print "If you would like to delete for an entry select 3"
    print
    print "To quit select 4 "
    b = input(':')
    if b == 1:
        print 'Enter the lookup up key to find (For example for all names
with ''Rob'' in them type Rob)'
        print
        a = raw_input(':')
        for entry in foo.lookup(a):
            print '%-40s %s (%s)' % ( entry.name, entry.number,
entry.showtype() )
            print

    if b == 2:
        print "Please enter the full name: (Example: John Smith)"
        n = raw_input(':')
        print "Please enter the phone number: (Example: 970-432-5432)"
        p = raw_input(':')
        print "Please enter the phone type: (0 = Unkown, 1 = Home, 2 = Work,
3 = Fax, 4 = Cell)"
        t = raw_input(':')
        if t == '0':
            foo.add(n, p, UNKNOWN)
        if t == '1':
            foo.add(n, p, HOME)
        if t == '2':
            foo.add(n, p, WORK)
        if t == '3':
            foo.add(n, p, FAX)
        if t == '4':
            foo.add(n, p, CELL)
    if b == 3:
        print "Enter the name of the entry to delete"
        dn = raw_input(':')
        print "Enter the phone type: (0 = Unkown, 1 = Home, 2 = Work, 3 =
Fax, 4 = Cell)"
        dp = raw_input(':')
        dele(dn,dp)




"Rob" <talon2lm at yahoo.com> wrote in message
news:Bf6dnT_Ctq5FCTCiRVn-hA at comcast.com...
> Ok, my two problems for this program that I can't seem to get are...
>
> 1)Why won't the program let me delete an entry after just entering it? If
I
> run the program, add an entry then quit, then restart the program it will
> let me delete it. Otherwise it won't see it...Am I missing something???
>
> 2)In the dele function I'm trying to add exceptions. The problem is if I
try
> to delete an entry that doesn't exist it will give me this error:
>
> Traceback (most recent call last):
> File "C:\Python23\phone.py", line 118, in -toplevel-
>     dele(dn,dp)
>   File "C:\Python23\phone.py", line 67, in dele
>     del e[str(f)]
>   File "C:\Python23\lib\shelve.py", line 133, in __delitem__
>     del self.dict[key]
>   File "C:\Python23\lib\bsddb\__init__.py", line 94, in __delitem__
>     del self.db[key]
> DBNotFoundError: (-30991, 'DB_NOTFOUND: No matching key/data pair found')
>
> Ok great...so I try to add the DBNotFoundError in my Except() only it
tells
> me:
>
> Traceback (most recent call last):
>   File "C:\Python23\phone.py", line 118, in -toplevel-
>     dele(dn,dp)
>   File "C:\Python23\phone.py", line 70, in dele
>     except (DBNotFoundError, NameError):
> NameError: global name 'DBNotFoundError' is not defined
>
> How can it give me an error and then not allow me to enter that error in
my
> Except()?
>
> Thanks for your help!
>
>
>






More information about the Python-list mailing list