Assignment 3 Part 4 (submitted by Hunter on Feb 9, 20:49)

Beautiful Code
Ka-Ping Yee

AUTHORS:   Adam   Calvin   Chris   David   Derek   Hunter   Jacob   Jason   Jun   Karl   Kevin   Michael   Morgan   Nadia   Nerissa   Omair   Peter   Peterson   Ping   Richard   Scott   Thanh   Varun

Download this file.

COMMENTS

Feb 13, 15:46 - Calvin: What is here is clear, and the docstrings are helpful and explicit, but it seems incomplete (e.g., it seems that you don't handle punctuation or capital letters in the input text) and the check_word docstring doesn't agree with what the function actually does.

Feb 16, 17:35 - Hunter: Right. Punctuation was left to the implementor of the module to deal with. As to check_word, I think "any possible corrections" makes it clear that the function could return a list containing any number (including 0) corrections. This means that the module could eventually supply words as suggestions, and people could program their code so that it would be prepared for that, but it doesn't have to right now.

Feb 23, 16:14 - Ping (lines 5, 6): The global declaration doesn't have any effect here (when you use a variable outside of any functions, it is already global). You only need to add a global declaration inside a function when assigning to a global variable.

Feb 23, 16:15 - Ping (line 31): We'll talk more about exceptions later, but for now: there's no need to catch this error explicitly (unless you intend to provide additional information with the error). If an error occurs, the caller will have to handle it anyway.

Feb 23, 16:16 - Ping (line 39): This should be an exception, not a return value. But this hasn't been taught yet; we'll discuss it in class.

Feb 23, 16:16 - Ping (lines 8, 19, 21, 23): Avoid inserting spaces immediately inside braces or brackets.

Please log in if you would like to add comments.

   

  1 # speling.py
  2 # a happy little python module that provides spellchecking
  3 
  4 import xreadlines,re
  5 global mydicta
  6 global wordpattern
  7 
  8 mydicta = { }
  9 wordpattern = re.compile('^[\\w\-\'\"]*$')
 10 
 11 def check_word(word):
 12     """Takes a word (string) and looks it up in the dictionary
 13     object.  Returns a list, containing the same word if the word is
 14     spelled correctly, or else a list of any possible corrections out
 15     of the dictionary object.  This lookup is case-sensitive."""
 16 
 17     try:
 18         if mydicta[word] == 1:
 19             return [ word ]
 20         else:
 21             return [ ]
 22     except StandardError:
 23         return [ ]
 24 
 25 def import_entries(filename):
 26     """Takes a file name and imports words separated by \\n from a text
 27     file. Returns 0 if successful.  Returns error if failure."""
 28 
 29     try:
 30         dictafile = open(filename)
 31     except StandardError:
 32         return StandardError
 33     
 34     for line in xreadlines.xreadlines(dictafile):
 35         word = line.replace('\n','')
 36         if wordpattern.search(word):
 37             mydicta[word] = 1
 38         else:
 39             word = "Import file is not of appropriate word\nword\n format, " +\
 40                 "but instead: " + word
 41             return word
 42     dictafile.close()
 43     return 0
 44 
 45 # the rest of these funtions are not implemented currently.        
 46 def export_entries():
 47     """Takes a file name and exports dictionary entries into a text file. 
 48        Returns 0 if successful."""
 49     pass
 50 
 51 def load_dictionary():
 52     """Takes a file path and loads a dictionary file into memory,
 53        returning the dictionary object. This function is called whenever a 
 54        dictionary object is created."""
 55     pass
 56 
 57 def save_dictionary():
 58     """Saves a dictionary object to disk, takes an optional file path to save    
 59        it in a place different than its original location."""
 60     pass
 61 
 62 def add_entry():
 63     """Takes a word (string) and adds it to the dictionary object."""
 64     pass
 65     
 66 def remove_entry():
 67     """Takes a word (string) and removes it from the dictionary object.  
 68        Returns true if it was successful."""
 69     pass