This is for part of my homework in an Object Oriented Programming class. I debugged the top part to make it workable. I need help on how to delete a searched item and also think I am missing closing the database file at the end. The search function #2 is not quite working correctly (when I add two entries with same name and search), although on option #3 it does search through contacts and ask if it is the correct entry for deletion. Kind of odd.

    #!/usr/bin/python
    #
    # An example from Sean Reifschneider's Python Tutorial at Linux Expo 98.
    #
    # Copyright (c) 1998 Sean Reifschneider, tummy.com, ltd.
    # This program is free software; you can redistribute it and/or
    # modify it under the terms of the GNU General Public License
    # as published by the Free Software Foundation; either version 2
    # of the License, or (at your option) any later version.

    # This program is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    # GNU General Public License for more details.

    # You should have received a copy of the GNU General Public License
    # along with this program; if not, write to the Free Software
    # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    #
    # You can contact Sean Reifschneider at
    # P.O. Box 270624,
    # Fort Collins, CO USA 80527-0624,
    # or at jafo-gpl@tummy.com
    #
    #  Simple phone-number database module
    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)
            else:
                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, 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 remove(self, name, number, type = HOME):
            #e = phoneentry(name, number, type)
            #self.remove[str(e)] = e


    if __name__ == '__main__':
        foo = phonedb()



    print'''
    ====================
    Running a small test
    ====================
    '''

    print "ADDING 'Sean Reifschneider', '970-555-1111', HOME\n"
    foo.add('Sean Reifschneider', '970-555-1111', HOME)
    print "ADDING 'Sean Reifschneider', '970-555-2222', CELL\n"
    foo.add('Sean Reifschneider', '970-555-2222', CELL)
    print "ADDING 'Evelyn Mitchell', '970-555-1111', HOME\n"
    foo.add('Evelyn Mitchell', '970-555-1111', HOME)

    print 'First lookup:'
    for entry in foo.lookup('reifsch'):
        print '%s %s (%s)' % ( entry.name, entry.number, entry.showtype() )

    print ''

    print 'Second lookup:'
    for entry in foo.lookup('e'):
        print '%s %s (%s)' % ( entry.name, entry.number, entry.showtype() )

    print'''


    '''

    def phonemenu(b):
        b = 1
        if b != 4:
            print "                    Phone Directory                    "
            print'If directory closes, type print phonemenu(b) to restart directory.'
            print
            print "Please choose from the following:"
            print "1: Add a contact, 2: Lookup a contact, 3: Delete a contact, 4: Exit"
            print 
            b = input(':')
            if b == 1:
                print "Please enter the full name: (Example: John Smith)"
                n = raw_input(':')
                print n
                print "Please enter the phone number: (Example: 970-432-5432)"
                p = raw_input(':')
                print p
                print '''Please enter the phone type: (0 = Unknown, 1 = Home, 2 = Work, 3 = Fax, 4 = Cell)'''
                t = raw_input(':')
                print t
                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)
                for entry in foo.lookup(n):
                    print "ADDED contact %s %s (%s)" % ( entry.name, entry.number, entry.showtype() )    
                else:
                    print phonemenu(b)
            if b == 2:
                print "Enter name to lookup in directory"
                l= raw_input(':')
                print l
                for entry in foo.lookup(l):
                    print '%s %s (%s)' % ( entry.name, entry.number, entry.showtype() )
                    print
                    print 'Returning to menu'
                    print
                    print phonemenu(b)
            if b==3:
                print 'Enter name to lookup and delete'
                ld= raw_input(':')
                for entry in foo.lookup(ld):
                    print 'Is this the entry you would like to delete?','%s %s (%s)' % ( entry.name, entry.number, entry.showtype() ) 
                    delete=raw_input(':')
                    #if delete=='y':
                        # How to delete the searched entry                   

                else: print phonemenu(b)
            # How to close database?
        else: exit

    b=1
    print phonemenu(b)

Recommended Answers

All 5 Replies

You have to set writeback=True for changes to be made to the db. Doug Hellmann has a nice write up Click Here

Thanks for the link, woooee. I am not having an issue with this code writing to the file. When I add a contact, it works with no problems. I am trying to figure out how to delete contacts in the directory after a search.

That is a little complicated because I get an error on the first test lookup at

e = self.shelve[key]

because the keys are not correct. If it works for you then there is possibly a difference because of the Python version being used. If it does not work for you, take another look at Doug Hellmann's page as to how he adds data to a shelve object. Deleting is just del self.shelve[key], but you also have to set writeback=True for the changes to take place in the db. Also, Python is already using "string" and "list" and so using them in your program "redefines" them so you can not convert something to a list for example because "list" no longer points to a function. Finally, "b" will never equal 4 because it is set to one on the previous line

    def phonemenu(b):
        b = 1
        if b != 4:
commented: Thank you. This homework is hard without a tutor! +0

Glad to know you figured it out. Good luck with programming in the future.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.