Bring back the assertions, cos they throw and that's ok

This commit is contained in:
Crista Lopes
2013-11-30 16:03:02 -08:00
parent cbfb524dd9
commit 844dd21de2

View File

@@ -1,8 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
import sys, re, operator, string import sys, re, operator, string
angry = "^*(&%#@^! (I don't want this)"
# #
# The PassiveAggressive class for this example # The PassiveAggressive class for this example
# #
@@ -16,8 +14,6 @@ class TFPassiveAggressive:
if self._e == None: if self._e == None:
try: try:
self._value = func(self._value) self._value = func(self._value)
if self._value == angry:
raise AssertionError(angry)
except Exception as e: except Exception as e:
self._e = e self._e = e
self._offending_func = func self._offending_func = func
@@ -33,14 +29,12 @@ class TFPassiveAggressive:
# The functions # The functions
# #
def get_input(arg): def get_input(arg):
if len(sys.argv) <= 1: assert(len(sys.argv) > 1), "You idiot! I need an input file! I quit!"
return angry
return sys.argv[1] return sys.argv[1]
def extract_words(path_to_file): def extract_words(path_to_file):
if type(path_to_file) != str or not path_to_file: assert(type(path_to_file) is str), "I need a string! I quit!"
return angry assert(path_to_file), "I need a non-empty string! I quit!"
with open(path_to_file) as f: with open(path_to_file) as f:
data = f.read() data = f.read()
@@ -49,8 +43,7 @@ def extract_words(path_to_file):
return word_list return word_list
def remove_stop_words(word_list): def remove_stop_words(word_list):
if type(word_list) != list or not word_list: assert(type(word_list) is list), "I need a list! I quit!"
return angry
with open('../stop_words.txt') as f: with open('../stop_words.txt') as f:
stop_words = f.read().split(',') stop_words = f.read().split(',')
@@ -59,8 +52,8 @@ def remove_stop_words(word_list):
return [w for w in word_list if not w in stop_words] return [w for w in word_list if not w in stop_words]
def frequencies(word_list): def frequencies(word_list):
if type(word_list) != list or not word_list: assert(type(word_list) is list), "I need a list! I quit!"
return angry assert(word_list <> []), "I need a non-empty list! I quit!"
word_freqs = {} word_freqs = {}
for w in word_list: for w in word_list:
@@ -71,14 +64,14 @@ def frequencies(word_list):
return word_freqs return word_freqs
def sort(word_freqs): def sort(word_freqs):
if type(word_freqs) != dict or not word_freqs: assert(type(word_freqs) is dict), "I need a dictionary! I quit!"
return angry assert(word_freqs <> {}), "I need a non-empty dictionary! I quit!"
return sorted(word_freqs.iteritems(), key=operator.itemgetter(1), reverse=True) return sorted(word_freqs.iteritems(), key=operator.itemgetter(1), reverse=True)
def top25_freqs(word_freqs): def top25_freqs(word_freqs):
if type(word_freqs) != dict or not word_freqs: assert(type(word_freqs) is list), "I need a list! I quit!"
return angry assert(word_freqs <> {}), "I need a non-empty dictionary! I quit!"
top25 = "" top25 = ""
for tf in word_freqs[0:25]: for tf in word_freqs[0:25]:
@@ -88,6 +81,5 @@ def top25_freqs(word_freqs):
# #
# The main function # The main function
# #
pa = TFPassiveAggressive(None) TFPassiveAggressive(None).bind(get_input).bind(extract_words).bind(remove_stop_words).bind(frequencies).bind(sort).bind(top25_freqs).printme()
pa.bind(get_input).bind(extract_words).bind(remove_stop_words).bind(frequencies).bind(sort).bind(top25_freqs).printme()