I have a simple program started and need help enhancing it and making it a more realistic database tool giving easy entry and retrieval.

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 = '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)

# if not being loaded as a module, run a small test
if __name__ == '__main__':
foo = phonedb()
foo.add('Sean Reifschneider', '970-555-1111', HOME)
foo.add('Sean Reifschneider', '970-555-2222', CELL)
foo.add('Evelyn Mitchell', '970-555-1111', HOME)

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

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

Looks like first thing you need to add is menu loop to add, edit, search data, also exit program should be in loop.

Took the liberty to put your code into code tags, added some remarks:

import shelve
import string

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

class Phoneentry:  # by convention class names start with capital letter
    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 of 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.slv'):  # added ext .slv
        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)
        
#  if not being loaded as a module, run a small test
if __name__ == '__main__':
    foo = Phonedb()
    foo.add('Sean Reifschneider', '970-555-1111', HOME)
    foo.add('Sean Reifschneider', '970-555-2222', CELL)
    foo.add('Evelyn Mitchell', '970-555-1111', HOME)

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

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

Note: Shows needed tags in reverse so they don't activate ...
at the end of your code add this tag
[/code]
at the start of your code add tag

[code]

You made this seem so simple how many years experience do you have with Python and programming?

So with your notes at the beginning and end I need to add those two codes?

Yes, wrap your Python code into these code tags to have it show up correctly indented on DaniWeb. I started Python about one year ago, but have used C/C++ much longer.

Please bring up any of your code question here on the forum, so others can help and learn too!

This question has already been answered. Start a new discussion instead.