<br>Thanks Andrei for your input.<br><br>I've already implemented a couple of your suggestions and will certainly<br>give the others a go. <br><br><br><div><span class="gmail_quote">On 2/18/06, <b class="gmail_sendername">
Andrei</b> <<a href="mailto:project5@redrival.net">project5@redrival.net</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<a href="mailto:benmarkwell@gmail.com">benmarkwell@gmail.com</a> wrote:<br>> Here is my first stab at putting together a working program. It is a<br>> glossary that you can add words and definitions to, or look up words and
<br>> their definitions. There are a couple of problems here and there, but<br>> it basically does what I set out for it to do. All comments and<br>> criticisms are welcome. I attached the data file that goes with the
<br>> script in case anybody wanted to actually run it, so they wouldn't have<br>> to make their own dictionary file to access.<br><br>I haven't executed it, but it seems quite good, certainly for a first<br>working program. For small programs there's no point in overengineering,
<br>but here are some simple improvements you could look into:<br><br>- place a "if __name__=="__main__":" condition at the bottom and put<br>your program code in there (or better yet, extract it in a function and
<br>call that function after the if __name__ stuff). This way the module can<br>also be imported.<br><br>- make the location of the dictionary a 'constant' (which in Python<br>means a variable with all-uppercase name :)). Having magic strings in
<br>code is bad practice.<br><br>- the menu system could be redesigned to make it more<br>maintenance-friendly. At this point if you want to add/modify a choice,<br>you'd have to modify a procedure and the main code. You could instead
<br>make a dictionary mapping each possible choice (keys) to tuples<br>containing a description and a function name. Loop over the keys and<br>print them with the description. Once the user makes a choice, you could<br>look up the function in the dictionary and execute it. This way adding
<br>new functionality items only means you need to add an entry to that one<br>dictionary.<br><br>- you could replace the "while num != '3':" condition with a "while<br>True:" - the loop is interrupted anyway if '3' is selected, because of
<br>the 'break'. This way you can also remove the initialization of num.<br><br>- you could extract the actual logic (the glossary) to a class with<br>methods like "load", "save", "lookup" and separate it completely from
<br>all the user interaction. The glossary class shouldn't print/ask for<br>user input. It's overkill for such a small program, but you could then<br>theoretically reuse the glossary in a different application (e.g. put a
<br>webbased interface onto it, or subclass it and make a glossary which can<br>also do multiple language translations, or use a different storage<br>backend, etc.).<br><br><snip original code><br><br>--<br>Yours,<br>
<br>Andrei<br><br>=====<br>Mail address in header catches spam. Real contact info:<br>''.join([''.join(s) for s in zip(<br>"<a href="mailto:poet@aao.l">poet@aao.l</a> pmfe!Pes ontuei ulcpss edtels,s hr' one oC.",
<br>"rjc5wndon.Sa-re laed o s npbi ot.Ira h it oteesn edt C")])<br><br>_______________________________________________<br>Tutor maillist - <a href="mailto:Tutor@python.org">Tutor@python.org</a><br><a href="http://mail.python.org/mailman/listinfo/tutor">
http://mail.python.org/mailman/listinfo/tutor</a><br></blockquote></div><br>