Shelve newbie??

Terry Reedy tjreedy at udel.edu
Sun Nov 2 22:54:53 EST 2003


"Rob" <talon2lm at yahoo.com> wrote in message
news:wsqdnSfKPajoJziiRVn-gg at comcast.com...

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






More information about the Python-list mailing list