OK, so explaining the passive aggressive style with monadic exceptions in a language that already has exceptions does not work. Back to regular exceptions in style 23. I left the monadic version as an academically interesting variation. In the process of returning to basic exceptions, I needed to clarify the tantrum style a little better too.

This commit is contained in:
Crista Lopes
2013-11-30 19:05:07 -08:00
parent 76f7ccb1d3
commit 9a9c525326
5 changed files with 122 additions and 41 deletions

View File

@@ -1,30 +1,6 @@
#!/usr/bin/env python
import sys, re, operator, string
#
# The PassiveAggressive class for this example
#
class TFPassiveAggressive:
def __init__(self, v):
self._e = None
self._offending_func = None
self._value = v
def bind(self, func):
if self._e == None:
try:
self._value = func(self._value)
except Exception as e:
self._e = e
self._offending_func = func
return self
def printme(self):
if self._e == None:
print self._value
else:
print self._e, " in ", self._offending_func.__name__
#
# The functions
#
@@ -81,5 +57,14 @@ def top25_freqs(word_freqs):
#
# The main function
#
TFPassiveAggressive(None).bind(get_input).bind(extract_words).bind(remove_stop_words).bind(frequencies).bind(sort).bind(top25_freqs).printme()
try:
assert(len(sys.argv) > 1), "You idiot! I need an input file! I quit!"
word_freqs = sort(frequencies(extract_words(sys.argv[1])))
assert(len(word_freqs) > 25), "OMG! Less than 25 words! I QUIT!"
for tf in word_freqs[0:25]:
print tf[0], ' - ', tf[1]
except Exception as e:
print "Something wrong: {0}".format(e)