Tantrum is not tied to exceptions. Tantrum is the style of C programs. Remove the word exceptions out of the constraints. Make the example program exactly the same as the next.

This commit is contained in:
Crista Lopes
2013-12-01 08:00:47 -08:00
parent 30eb880893
commit 05c5aa9bc1
2 changed files with 21 additions and 30 deletions

View File

@@ -5,10 +5,10 @@ Constraints:
- Every single procedure and function checks the sanity of its - Every single procedure and function checks the sanity of its
arguments and refuses to continue when the arguments are arguments and refuses to continue when the arguments are
unreasonable, throwing an exception unreasonable
- All code blocks check for all possible errors, print out - All code blocks check for all possible errors, possibly print out
context-specific messages when errors occur, and pass the exceptions context-specific messages when errors occur, and pass the errors
up the function call chain up the function call chain
Possible names: Possible names:

View File

@@ -1,18 +1,13 @@
#!/usr/bin/env python #!/usr/bin/env python
import sys, re, operator, string, inspect import sys, re, operator, string, traceback
# #
# The functions # The functions
# #
def extract_words(path_to_file): def extract_words(path_to_file):
""" assert(type(path_to_file) is str), "I need a string!"
Takes a path to a file and returns the non-stop assert(path_to_file), "I need a non-empty string!"
words, after properly removing nonalphanumeric chars
and normalizing for lower case
"""
assert(type(path_to_file) is str), "I need a string! I quit!"
assert(path_to_file), "I need a non-empty string! I quit!"
try: try:
with open(path_to_file) as f: with open(path_to_file) as f:
@@ -23,6 +18,10 @@ def extract_words(path_to_file):
pattern = re.compile('[\W_]+') pattern = re.compile('[\W_]+')
word_list = pattern.sub(' ', str_data).lower().split() word_list = pattern.sub(' ', str_data).lower().split()
return word_list
def remove_stop_words(word_list):
assert(type(word_list) is list), "I need a list!"
try: try:
with open('../stop_words.txt') as f: with open('../stop_words.txt') as f:
@@ -35,12 +34,8 @@ def extract_words(path_to_file):
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):
""" assert(type(word_list) is list), "I need a list!"
Takes a list of words and returns a dictionary associating assert(word_list <> []), "I need a non-empty list!"
words with frequencies of occurrence
"""
assert(type(word_list) is list), "I need a list! I quit!"
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:
@@ -51,13 +46,8 @@ def frequencies(word_list):
return word_freqs return word_freqs
def sort(word_freq): def sort(word_freq):
""" assert(type(word_freq) is dict), "I need a dictionary!"
Takes a dictionary of words and their frequencies assert(word_freq <> {}), "I need a non-empty dictionary!"
and returns a list of pairs where the entries are
sorted by frequency
"""
assert(type(word_freq) is dict), "I need a dictionary! I quit!"
assert(word_freq <> {}), "I need a non-empty dictionary! I quit!"
try: try:
return sorted(word_freq.iteritems(), key=operator.itemgetter(1), reverse=True) return sorted(word_freq.iteritems(), key=operator.itemgetter(1), reverse=True)
@@ -69,13 +59,14 @@ def sort(word_freq):
# The main function # The main function
# #
try: try:
assert(len(sys.argv) > 1), "You idiot! I need an input file! I quit!" assert(len(sys.argv) > 1), "You idiot! I need an input file!"
word_freqs = sort(frequencies(extract_words(sys.argv[1]))) word_freqs = sort(frequencies(remove_stop_words(extract_words(sys.argv[1]))))
assert(type(word_freqs) is list), "OMG! This is not a list! I quit!" assert(type(word_freqs) is list), "OMG! This is not a list!"
assert(len(word_freqs) > 25), "SRSLY? Less than 25 words! I QUIT!" assert(len(word_freqs) > 25), "SRSLY? Less than 25 words!"
for tf in word_freqs[0:25]: for (w, c) in word_freqs[0:25]:
print tf[0], ' - ', tf[1] print w, ' - ', c
except Exception as e: except Exception as e:
print "Something wrong: {0}".format(e) print "Something wrong: {0}".format(e)
traceback.print_exc()