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:
@@ -1,13 +1,15 @@
|
||||
Style #21
|
||||
Style #22
|
||||
==============================
|
||||
|
||||
Constraints:
|
||||
|
||||
- Every single procedure and function checks the sanity of its
|
||||
arguments and refuses to work when the arguments are unreasonable
|
||||
arguments and refuses to continue when the arguments are
|
||||
unreasonable, throwing an exception
|
||||
|
||||
- All code blocks check for all possible errors and refuse to continue
|
||||
when things go wrong
|
||||
- All code blocks check for all possible errors, print out
|
||||
context-specific messages when errors occur, and pass the exceptions
|
||||
up the function call chain
|
||||
|
||||
Possible names:
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user