'''
Created on Jan 21, 2011

@author: ola
'''
_all_words = []
_word_lists = {}
_lastfile = ''

def load_words(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, _lastfile
    if _lastfile == filename:
        return
    _lastfile = filename
    words = open(filename).read().split()
    _all_words = words
    
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_words) == 0:
        load_words() 
        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()
    if wordlength in _word_lists:
        return _word_lists[wordlength]
    wlist = [w for w in _all_words if len(w) == wordlength]
    _word_lists[wordlength] = wlist
    return wlist

#load_words()
#print len(get_words(5))
#print len(get_words(5))
#print len(get_words(7))
    