################ 待整理 ''' 应用场景针对各个组件的 notify 方法发指令来驱动所有工作 这是一个示例性质的原型,具体分布式环境下需要调整 notify 用了四种写法,是和本主题无关的测试 ''' from cppy.cp_util import * from collections import defaultdict badmsg = lambda : exec (''' raise Exception("Message not understood " , action ) ''') class fff: def __init__(self, d): self._data = defaultdict( badmsg ) self._data.update(d) def __getitem__(self, key): return self._data[key] class DataStorageMod(): def __init__(self): self._data = [] def notify(self, action, *args): return { 'init': lambda : self._init, 'words': lambda : self._words }.get( action , badmsg )()(*args) def _init(self, path_to_file): self._data = re_split( read_file(path_to_file) ) def _words(self): return self._data class StopWordMod(): _stop_words = [] def notify(self, action, *args): return { 'init': self._init, 'is_stop_word': self._is_stop_word }[ action ](*args) def _init(self): self._stop_words = get_stopwords() def _is_stop_word(self, wordx): return wordx in self._stop_words class WordFrequencyMod(): _word_freqs = {} def notify(self, action, *args): return fff( { 'increment_count': lambda : self._increment_count, 'sorted': lambda : self._sorted })[ action ]()(*args) def _increment_count(self, word): self._word_freqs[word] = self._word_freqs.get(word,0) + 1 def _sorted(self): return sort_dict(self._word_freqs) class ScenarioManager(): def notify(self, action, *args): if action == 'init': return self._init( *args) elif action == 'run': return self._run() else: raise Exception("Message not understood " + action ) def _init(self, path_to_file): self._storage_manager = DataStorageMod() self._stop_word_manager = StopWordMod() self._word_freq_manager = WordFrequencyMod() self._storage_manager.notify('init', path_to_file) self._stop_word_manager.notify('init') def _run(self): for word in self._storage_manager.notify('words'): if not self._stop_word_manager.notify('is_stop_word', word): self._word_freq_manager.notify('increment_count', word ) word_freqs = self._word_freq_manager.notify('sorted') print_word_freqs(word_freqs) if __name__ == '__main__': sm = ScenarioManager() sm.notify('init', testfilepath) sm.notify('run')