Re: [Tutor] First program -- would like comments and criticisms
[EMAIL PROTECTED] wrote: Here is my first stab at putting together a working program. It is a glossary that you can add words and definitions to, or look up words and their definitions. There are a couple of problems here and there, but it basically does what I set out for it to do. All comments and criticisms are welcome. I attached the data file that goes with the script in case anybody wanted to actually run it, so they wouldn't have to make their own dictionary file to access. See my suggestions below. Kent Thanks in advance. Ben # File gloss.py # AuthorBen Markwell # Date Feb 2006 ''' This script is a glossary in which you can input definitions and look up words and their definitions. ''' import cPickle as p import textwrap as t f = file(r'g:\dev\python\data\gloss.data', 'r') gloss = p.load(f) f.close() should be here, you're done with reading this file now. #---Def Functions---#00#FF-- def findWord(): words = gloss.keys() letter = raw_input('That word is not in the glossary. \nWhat is the first letter of the word? ') for x in range(len(words)): if words[x].startswith(letter): print words[x] To iterate over the elements of a sequence you don't need to use an index, you can iterate the sequence directly: for word in words: if word.startswith(letter): print word Since words isn't used anywhere else I would write this as for word in gloss.keys(): Finally, since you just asked for the word (if I understand correctly the intent), it would be friendlier to figure out the first letter yourself and pass it as a parameter to findWord(), rather than having the user enter it again. # Get word and check if it is in the glossary then print the def. def getWordDef(): word = raw_input('enter a word: ') if gloss.has_key(word): line = gloss.get(word) lineWrap(word) else: findWord() word = raw_input('Enter the word: ') line = gloss.get(word) lineWrap(word) # Print a menu of choices to the screen def printMenu(): print '1 - Enter a word and definition.' print '2 - Look up a word.' print '3 - Quit' print # Wrap the output so it fits the screen nicely def lineWrap(word): line = gloss.get(word) if len(line) 81: print line else: wrapLines = t.wrap(line, 80) for line in range(len(wrapLines)): print wrapLines[line] for wrapped in t.wrap(line, 80): # End of Def Functions xxx num = '0' printMenu() while num != '3': num = raw_input('What do you want to do? ') if num == '1':# if 1 in choice enter a word to look up word = raw_input('Enter word: ') _def = raw_input('Enter definition: ') gloss[word] = _def elif num == '2': # if 2 is the choice enter word and def getWordDef() elif num == '3': # if 3 is the choice, save changes and quit newGloss = file(r'g:\dev\python\data\gloss.data', 'w') p.dump(gloss, newGloss) newGloss.close() f.close() print print 'Goodbye' break else: printMenu() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] First program -- would like comments and criticisms
Thanks Andrei for your input.I've already implemented a couple of your suggestions and will certainlygive the others a go. On 2/18/06, Andrei [EMAIL PROTECTED] wrote: [EMAIL PROTECTED] wrote: Here is my first stab at putting together a working program. It is a glossary that you can add words and definitions to, or look up words and their definitions.There are a couple of problems here and there, but it basically does what I set out for it to do. All comments and criticisms are welcome. I attached the data file that goes with the script in case anybody wanted to actually run it, so they wouldn't have to make their own dictionary file to access.I haven't executed it, but it seems quite good, certainly for a firstworking program. For small programs there's no point in overengineering, but here are some simple improvements you could look into:- place a if __name__==__main__: condition at the bottom and putyour program code in there (or better yet, extract it in a function and call that function after the if __name__ stuff). This way the module canalso be imported.- make the location of the dictionary a 'constant' (which in Pythonmeans a variable with all-uppercase name :)). Having magic strings in code is bad practice.- the menu system could be redesigned to make it moremaintenance-friendly. At this point if you want to add/modify a choice,you'd have to modify a procedure and the main code. You could instead make a dictionary mapping each possible choice (keys) to tuplescontaining a description and a function name. Loop over the keys andprint them with the description. Once the user makes a choice, you couldlook up the function in the dictionary and execute it. This way adding new functionality items only means you need to add an entry to that onedictionary.- you could replace the while num != '3': condition with a whileTrue: - the loop is interrupted anyway if '3' is selected, because of the 'break'. This way you can also remove the initialization of num.- you could extract the actual logic (the glossary) to a class withmethods like load, save, lookup and separate it completely from all the user interaction. The glossary class shouldn't print/ask foruser input. It's overkill for such a small program, but you could thentheoretically reuse the glossary in a different application (e.g. put a webbased interface onto it, or subclass it and make a glossary which canalso do multiple language translations, or use a different storagebackend, etc.).snip original code--Yours, Andrei=Mail address in header catches spam. Real contact info:''.join([''.join(s) for s in zip([EMAIL PROTECTED] pmfe!Pes ontuei ulcpssedtels,s hr' one oC., rjc5wndon.Sa-re laed o s npbi ot.Ira h it oteesn edt C)])___Tutor maillist-Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] First program -- would like comments and criticisms
Here is my first stab at putting together a working program. It is a glossary that you can add words and definitions to, or look up words and their definitions. There are a couple of problems here and there, but it basically does what I set out for it to do. All comments and criticisms are welcome. I attached the data file that goes with the script in case anybody wanted to actually run it, so they wouldn't have to make their own dictionary file to access. Thanks in advance. Ben # File gloss.py# Author Ben Markwell# Date Feb 2006'''This script is a glossary in which you can input definitionsand look up words and their definitions.'''import cPickle as p import textwrap as tf = file(r'g:\dev\python\data\gloss.data', 'r')gloss = p.load(f)#---Def Functions---#00#FF--def findWord(): words = gloss.keys() letter = raw_input('That word is not in the glossary. \nWhat is the first letter of the word? ') for x in range(len(words)): if words[x].startswith(letter): print words[x] # Get word and check if it is in the glossary then print the def.def getWordDef(): word = raw_input('enter a word: ') if gloss.has_key(word): line = gloss.get(word) lineWrap(word) else: findWord() word = raw_input('Enter the word: ') line = gloss.get(word) lineWrap(word) # Print a menu of choices to the screendef printMenu(): print '1 - Enter a word and definition.' print '2 - Look up a word.' print '3 - Quit' print# Wrap the output so it fits the screen nicelydef lineWrap(word): line = gloss.get(word) if len(line) 81: print line else: wrapLines = t.wrap(line, 80) for line in range(len(wrapLines)): print wrapLines[line] # End of Def Functions xxx num = '0'printMenu()while num != '3': num = raw_input('What do you want to do? ') if num == '1': # if 1 in choice enter a word to look up word = raw_input('Enter word: ') _def = raw_input('Enter definition: ') gloss[word] = _def elif num == '2': # if 2 is the choice enter word and def getWordDef() elif num == '3': # if 3 is the choice, save changes and quit newGloss = file(r'g:\dev\python\data\gloss.data', 'w') p.dump(gloss, newGloss) newGloss.close() f.close() print print 'Goodbye' break else: printMenu() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] First program -- would like comments and criticisms
[EMAIL PROTECTED] wrote: Here is my first stab at putting together a working program. It is a glossary that you can add words and definitions to, or look up words and their definitions. There are a couple of problems here and there, but it basically does what I set out for it to do. All comments and criticisms are welcome. I attached the data file that goes with the script in case anybody wanted to actually run it, so they wouldn't have to make their own dictionary file to access. I haven't executed it, but it seems quite good, certainly for a first working program. For small programs there's no point in overengineering, but here are some simple improvements you could look into: - place a if __name__==__main__: condition at the bottom and put your program code in there (or better yet, extract it in a function and call that function after the if __name__ stuff). This way the module can also be imported. - make the location of the dictionary a 'constant' (which in Python means a variable with all-uppercase name :)). Having magic strings in code is bad practice. - the menu system could be redesigned to make it more maintenance-friendly. At this point if you want to add/modify a choice, you'd have to modify a procedure and the main code. You could instead make a dictionary mapping each possible choice (keys) to tuples containing a description and a function name. Loop over the keys and print them with the description. Once the user makes a choice, you could look up the function in the dictionary and execute it. This way adding new functionality items only means you need to add an entry to that one dictionary. - you could replace the while num != '3': condition with a while True: - the loop is interrupted anyway if '3' is selected, because of the 'break'. This way you can also remove the initialization of num. - you could extract the actual logic (the glossary) to a class with methods like load, save, lookup and separate it completely from all the user interaction. The glossary class shouldn't print/ask for user input. It's overkill for such a small program, but you could then theoretically reuse the glossary in a different application (e.g. put a webbased interface onto it, or subclass it and make a glossary which can also do multiple language translations, or use a different storage backend, etc.). snip original code -- Yours, Andrei = Mail address in header catches spam. Real contact info: ''.join([''.join(s) for s in zip( [EMAIL PROTECTED] pmfe!Pes ontuei ulcpss edtels,s hr' one oC., rjc5wndon.Sa-re laed o s npbi ot.Ira h it oteesn edt C)]) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor