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

@@ -59,15 +59,23 @@ def sort(word_freq):
assert(type(word_freq) is dict), "I need a dictionary! I quit!"
assert(word_freq <> {}), "I need a non-empty dictionary! I quit!"
return sorted(word_freq.iteritems(), key=operator.itemgetter(1), reverse=True)
try:
return sorted(word_freq.iteritems(), key=operator.itemgetter(1), reverse=True)
except Exception as e:
print "Sorted threw {0}: {1}".format(e)
raise e
#
# The main function
#
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]
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(type(word_freqs) is list), "OMG! This is not a list! I quit!"
assert(len(word_freqs) > 25), "SRSLY? 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)