[Tutor] need advice about a dictionary ({})

Jack Trades jacktradespublic at gmail.com
Sat Sep 10 21:42:04 CEST 2011


On Sat, Sep 10, 2011 at 1:08 PM, Richard D. Moores <rdmoores at gmail.com>wrote:

> So I've done quite a bit more work. With phone_book.py the user can
> not only access phone numbers by the person's initials, but can add
> items to the data file. I've also solved the problem of adding a
> person who's initials have already been used in a key.
>
> I've pasted phone_book_for_pasting.py at <http://pastebin.com/2wm4Vf1P>.
>
> I'd appreciate any comments, instructive criticism, etc.
>
>
It looks pretty good overall, though I didn't examine it too closely.  IMHO
there are some awkward bits which I think come from your representation of
the data.

I would probably make the phonebook itself a list, with each entry being a
dict.  Something like:

book = [
{'name':'Mark Sanders', 'cell':'422-318-2346', '
email':'msanders at stanfordalumni.org'},
{'name':'AAA', 'phone':'575-3992', 'phone2':'1-800-472-4630',
'notes':'Membership #422 260 0131863 00 8'},
#...
]


Then you can easily search your phone book by name, email, type of contact,
relation, etc.  A search by name would look like this:

def find_by_name(name):
  for entry in book:
    if entry['name'] == name:
      return entry

find_by_name('Mark Sanders')
#==> {'name':'Mark Sanders', 'cell':'422-318-2346', '
email':'msanders at stanfordalumni.org}

or a more general procedure for doing searches on your book could be:

def find(criteria, term):
  for entry in book:
    if entry[criteria] == term:
      return entry

find('name', 'Mark Sanders')
#==> {'name':'Mark Sanders', 'cell':'422-318-2346', '
email':'msanders at stanfordalumni.org}


Similarly you could search for initials by providing a to_initials
procedure:

def to_initials(name):
  return ''.join([i[0] for i in name.split(' ')])

def find_by_initials(initials):
  for entry in book:
    if to_initials(entry['name']) == initials:
      return entry

find_by_initials('MS')
#==> {'cell': '422-318-2346', 'name': 'Mark Sanders', 'email': '
msanders at stanfordalumni.org'}


Adding a new entry would then be as simple as:

def add_new_entry(entry, book):
  book.append(entry)


For storing data I would probably use Pickle, which would look something
like this:

from cPickle import load, dump

f = open('book.pk', 'w')
dump(book, f)
f.close()

and loading your book is similar

f = open('book.pk', 'r')
book = load(f)
f.close()


If you want a human readable storage format I would look into json, but
pickle has served me well for most purposes.  Surely you can store your
phonebook as plain text and parse it the way you have, but it's not
necessary to do that with all the tools that exist for that purpose.


-- 
Nick Zarczynski
Pointless Programming Blog <http://pointlessprogramming.wordpress.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110910/c8fa67dd/attachment-0001.html>


More information about the Tutor mailing list