<div class="gmail_quote">On Sat, Sep 10, 2011 at 1:08 PM, Richard D. Moores <span dir="ltr">&lt;<a href="mailto:rdmoores@gmail.com">rdmoores@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
So I&#39;ve done quite a bit more work. With phone_book.py the user can<br>
not only access phone numbers by the person&#39;s initials, but can add<br>
items to the data file. I&#39;ve also solved the problem of adding a<br>
person who&#39;s initials have already been used in a key.<br>
<br>
I&#39;ve pasted phone_book_for_pasting.py at &lt;<a href="http://pastebin.com/2wm4Vf1P" target="_blank">http://pastebin.com/2wm4Vf1P</a>&gt;.<br>
<br>
I&#39;d appreciate any comments, instructive criticism, etc.<br>
<br></blockquote></div><br>It looks pretty good overall, though I didn&#39;t examine it too closely.  IMHO there are some awkward bits which I think come from your representation of the data.<br><br>I would probably make the phonebook itself a list, with each entry being a dict.  Something like:<br>
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">book = [</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">{&#39;name&#39;:&#39;Mark Sanders&#39;, &#39;cell&#39;:&#39;422-318-2346&#39;, &#39;<a href="mailto:email%27%3A%27msanders@stanfordalumni.org">email&#39;:&#39;msanders@stanfordalumni.org</a>&#39;},</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">{&#39;name&#39;:&#39;AAA&#39;, &#39;phone&#39;:&#39;575-3992&#39;, &#39;phone2&#39;:&#39;1-800-472-4630&#39;, &#39;notes&#39;:&#39;Membership #422 260 0131863 00 8&#39;},</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#...</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">]</span><br><br><br>Then you can easily search your phone book by name, email, type of contact, relation, etc.  A search by name would look like this:<br>
<br><span style="font-family: courier new,monospace;">def find_by_name(name):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">  for entry in book:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    if entry[&#39;name&#39;] == name:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">      return entry</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">find_by_name(&#39;Mark Sanders&#39;)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#==&gt; {&#39;name&#39;:&#39;Mark Sanders&#39;, &#39;cell&#39;:&#39;422-318-2346&#39;, &#39;<a href="mailto:email%27%3A%27msanders@stanfordalumni.org">email&#39;:&#39;msanders@stanfordalumni.org</a>}</span><br>
<br>or a more general procedure for doing searches on your book could be:<br><br><span style="font-family: courier new,monospace;">def find(criteria, term):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">  for entry in book:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    if entry[criteria] == term:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">      return entry</span><br><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">find(&#39;name&#39;, &#39;Mark Sanders&#39;)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#==&gt; {&#39;name&#39;:&#39;Mark Sanders&#39;, &#39;cell&#39;:&#39;422-318-2346&#39;, &#39;<a href="mailto:email%27%3A%27msanders@stanfordalumni.org">email&#39;:&#39;msanders@stanfordalumni.org</a>}</span><br>
<br><br>Similarly you could search for initials by providing a to_initials procedure:<br><br><span style="font-family: courier new,monospace;">def to_initials(name):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">  return &#39;&#39;.join([i[0] for i in name.split(&#39; &#39;)])</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">def find_by_initials(initials):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">  for entry in book:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    if to_initials(entry[&#39;name&#39;]) == initials:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">      return entry</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">find_by_initials(&#39;MS&#39;)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#==&gt; {&#39;cell&#39;: &#39;422-318-2346&#39;, &#39;name&#39;: &#39;Mark Sanders&#39;, &#39;email&#39;: &#39;<a href="mailto:msanders@stanfordalumni.org">msanders@stanfordalumni.org</a>&#39;}</span><br>
<br><br>Adding a new entry would then be as simple as:<br><br><span style="font-family: courier new,monospace;">def add_new_entry(entry, book):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">  book.append(entry)</span><br>
<br><br>For storing data I would probably use Pickle, which would look something like this:<br><br><span style="font-family: courier new,monospace;">from cPickle import load, dump</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">f = open(&#39;<a href="http://book.pk">book.pk</a>&#39;, &#39;w&#39;)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">dump(book, f)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">f.close()</span><br><br>and loading your book is similar<br>
<br><span style="font-family: courier new,monospace;">f = open(&#39;<a href="http://book.pk">book.pk</a>&#39;, &#39;r&#39;)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">book = load(f)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">f.close()</span><br><br><br>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&#39;s not necessary to do that with all the tools that exist for that purpose.<br>
<br clear="all"><br>-- <br>Nick Zarczynski<br><a href="http://pointlessprogramming.wordpress.com" target="_blank">Pointless Programming Blog</a><br><br>