Shelve newbie??

R

Rob

I have been tasked, an assignment from school, to make a usable database
using this code...my question, Where is there some good doc on how to use
Shelve? The python.org doesn't say much of anything about it. It's driving
me nutty. I'm trying to figure out a way to delete enteries (with user
inputting the name). Here's the code so far...Obviously it's not ready for
turn in!

import shelve
import string
#import pickle

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)


# if not being loaded as a module, run a small test


b = 1
foo = phonedb()
while b != 3:
print b
print "Welcome to the Phone Database"

print "Please choose from the following"
print
print "If you would like to add an entry select 1"
print
print "If you would like to delete for an entry select 2"
print
print "To quit select 3 "
b = input(':')
if b == 1:

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)

print t
if b == 2:
print "Enter the name to delete"
d = raw_input(':')


#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() )
 
T

Terry Reedy

I have not used shelve myself, so cannot answer your question. But I
can suggest that you do not use the builting name 'type' for phone
number types and that you do use letters to store them and a dict to
display. So:
UNKNOWN = 0
HOME = 1
WORK = 2
FAX = 3
CELL = 4

becomes

ptypenames = {
'U': 'Unknown',
'H': 'Home',
'W': 'Work',
'F': 'Fax',
'C': 'Cell',
}
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')

return ptypenames[self.type]
print "Please enter the phone type: (0 = Unkown, 1 = Home, 2 = Work,
3 = Fax, 4 = Cell)"

print "Please enter first letter of the phone type:"
print ptypenames.values()
t = raw_input(':')

t = raw_input(':').upper()
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 hasattr(ptypenames, t): foo.add(n,p,t)
# else: <bad entry>

Besides shortening code, this localizes the type key/word info to one
master dict. Rest of code is synchronized to this and will
automatically continue to work if you change the master list, for
instance to add a new ptype.

Terry J. Reedy
 
R

Rob

Thanks Terry!

I knew my endless IF staments were not effecienct and was going to address
that later, once I got the Shelve thing figured out. I do appreciate your
help.

Rob

Terry Reedy said:
I have not used shelve myself, so cannot answer your question. But I
can suggest that you do not use the builting name 'type' for phone
number types and that you do use letters to store them and a dict to
display. So:
UNKNOWN = 0
HOME = 1
WORK = 2
FAX = 3
CELL = 4

becomes

ptypenames = {
'U': 'Unknown',
'H': 'Home',
'W': 'Work',
'F': 'Fax',
'C': 'Cell',
}
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')

return ptypenames[self.type]
print "Please enter the phone type: (0 = Unkown, 1 = Home, 2 = Work,
3 = Fax, 4 = Cell)"

print "Please enter first letter of the phone type:"
print ptypenames.values()
t = raw_input(':')

t = raw_input(':').upper()
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 hasattr(ptypenames, t): foo.add(n,p,t)
# else: <bad entry>

Besides shortening code, this localizes the type key/word info to one
master dict. Rest of code is synchronized to this and will
automatically continue to work if you change the master list, for
instance to add a new ptype.

Terry J. Reedy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top