'''
Created on Jan 21, 2011

Modified on Feb 22, 2012, remove use
of dictionaries and read file by lines
rather than by words --- this will
make it easier to use non-words, i.e.,
lines like "Animal Farm" or "Jackson Pollock" 
if user wants to guess books or artists or ... 

@author: ola
'''
_all_lines = []        # all the lines read from file
_filename_used = ''         # file name last read

def load_lines(filename = "lowerwords.txt"):
    """
    read words from specified file name for future use
    in this module. Default filename is local, otherwise
    specify full path to file
    """
    global _all_words, _filename_used
    if _filename_used == filename:
        return
    _filename_used = filename
    f = open(filename)
    for line in f.readlines():
        line = line.strip()
        _all_lines.append(line)
    
    
def check_loaded():
    """
    will read local file if no words have been read, returns
    True if words are newly loaded, returns False if words already loaded
    """
    if len(_all_lines) == 0:
        load_lines() 
        return True
    return False
    
def get_words(wordlength = 5):
    """
    returns a list of words having a specified length,
    default length is 5 (if parameter not specified)
    will use words already read or will cause
    default file to be read if no file has been loaded before call
    """
    check_loaded()
    wlist = [w for w in _all_lines if len(w) == wordlength]
    return wlist

if __name__ == "__main__":
    load_lines("lowerwords.txt")
    l5 = get_words(5)
    print "# 5 letter words = ",len(l5)
    l7 = get_words(7)
    print "# 7 letter words = ",len(l7)
    
