From 737aa9ef0d3f1e5e4e688fd010302e805a020e78 Mon Sep 17 00:00:00 2001 From: David Foster Date: Sun, 5 Jan 2014 20:54:14 -0800 Subject: [PATCH] things: Refactor. Specifically: * Remove dead assignments to _data and _stop_words. * Convert _word_freqs from class field to instance field to avoid side effects. * Remove no-op call ''.join(self._data) because self._data is guaranteed to be a string. --- 10-things/tf-10.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/10-things/tf-10.py b/10-things/tf-10.py index 0aa1f0c..531f217 100755 --- a/10-things/tf-10.py +++ b/10-things/tf-10.py @@ -13,7 +13,7 @@ class TFExercise(): class DataStorageManager(TFExercise): """ Models the contents of the file """ - _data = '' + def __init__(self, path_to_file): with open(path_to_file) as f: self._data = f.read() @@ -22,15 +22,14 @@ class DataStorageManager(TFExercise): def words(self): """ Returns the list words in storage """ - data_str = ''.join(self._data) - return data_str.split() + return self._data.split() def info(self): return super(DataStorageManager, self).info() + ": My major data structure is a " + self._data.__class__.__name__ class StopWordManager(TFExercise): """ Models the stop word filter """ - _stop_words = [] + def __init__(self): with open('../stop_words.txt') as f: self._stop_words = f.read().split(',') @@ -45,7 +44,9 @@ class StopWordManager(TFExercise): class WordFrequencyManager(TFExercise): """ Keeps the word frequency data """ - _word_freqs = {} + + def __init__(self): + self._word_freqs = {} def increment_count(self, word): if word in self._word_freqs: