from cppy.cp_util import * def extract_words(obj, path_to_file): obj['data'] = extract_file_words(path_to_file) def increment_count(obj, w): obj['freqs'][w] = 1 if w not in obj['freqs'] else obj['freqs'][w]+1 data_storage_obj = { 'data' : [], 'init' : lambda path_to_file : extract_words(data_storage_obj, path_to_file), 'words' : lambda : data_storage_obj['data'] } word_freqs_obj = { 'freqs' : {}, 'increment_count' : lambda w : increment_count(word_freqs_obj, w), 'sorted' : lambda : sort_dict(word_freqs_obj['freqs']) } if __name__ == '__main__': data_storage_obj['init']( testfilepath ) for word in data_storage_obj['words'](): word_freqs_obj['increment_count'](word) word_freqs = word_freqs_obj['sorted']() print_word_freqs(word_freqs)