diff --git a/01-good-old-times/tf-01.py b/01-good-old-times/tf-01.py index d33cb4f..bb2ba0c 100755 --- a/01-good-old-times/tf-01.py +++ b/01-good-old-times/tf-01.py @@ -1,5 +1,6 @@ #!/usr/bin/env python +from __future__ import print_function import sys, os, string # Utility for handling the intermediate 'secondary memory' @@ -121,6 +122,6 @@ while True: for tf in data[0:25]: # elimination of symbol tf is exercise if len(tf) == 2: - print tf[0], ' - ', tf[1] + print(tf[0], ' - ', tf[1]) # We're done word_freqs.close() diff --git a/02-go-forth/forth.py b/02-go-forth/forth.py index 710b754..1ab978c 100755 --- a/02-go-forth/forth.py +++ b/02-go-forth/forth.py @@ -1,10 +1,16 @@ #!/usr/local/bin/python # # f o r t h . p y -# Author: Chris Meyers @ +# Author: Chris Meyers @ # http://openbookproject.net/py4fun/forth/forth.html # -import sys, re +from __future__ import print_function +import re + +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 ds = [] # The data stack cStack = [] # The control struct stack @@ -15,14 +21,14 @@ words = [] # The input stream of tokens def main() : while 1 : pcode = compile() # compile/run from user - if pcode == None : print; return + if pcode == None : print(); return execute(pcode) #============================== Lexical Parsing - + def getWord (prompt="... ") : global words - while not words : + while not words : try : lin = raw_input(prompt)+"\n" except : return None if lin[0:1] == "@" : lin = open(lin[1:-1]).read() @@ -56,8 +62,8 @@ def rSwap(cod,p) : a=ds.pop(); b=ds.pop(); ds.append(a); ds.append(b) def rDup (cod,p) : ds.append(ds[-1]) def rDrop(cod,p) : ds.pop() def rOver(cod,p) : ds.append(ds[-2]) -def rDump(cod,p) : print "ds = ", ds -def rDot (cod,p) : print ds.pop() +def rDump(cod,p) : print("ds = ", ds) +def rDot (cod,p) : print(ds.pop()) def rJmp (cod,p) : return cod[p] def rJnz (cod,p) : return (cod[p],p+1)[ds.pop()] def rJz (cod,p) : return (p+1,cod[p])[ds.pop()==0] @@ -92,7 +98,7 @@ rDict = { 'create': rCreate, 'does>': rDoes, } -#================================= Compile time +#================================= Compile time def compile() : pcode = []; prompt = "Forth> " @@ -114,12 +120,12 @@ def compile() : try : pcode.append(int(word)) except : try: pcode.append(float(word)) - except : + except : pcode[-1] = rRun # Change rPush to rRun pcode.append(word) # Assume word will be defined if not cStack : return pcode prompt = "... " - + def fatal (mesg) : raise mesg def cColon (pcode) : @@ -168,5 +174,5 @@ cDict = { ':' : cColon, ';' : cSemi, 'if': cIf, 'else': cElse, 'then': cThen, 'begin': cBegin, 'until': cUntil, } - + if __name__ == "__main__" : main() diff --git a/02-go-forth/tf-02.py b/02-go-forth/tf-02.py index 0a1f605..8b9682f 100755 --- a/02-go-forth/tf-02.py +++ b/02-go-forth/tf-02.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import sys, re, operator, string # @@ -104,7 +105,7 @@ stack.append(0) # the last word there will be one item left while stack[-1] < 25 and len(stack) > 1: heap['i'] = stack.pop() - (w, f) = stack.pop(); print w, ' - ', f + (w, f) = stack.pop(); print(w, ' - ', f) stack.append(heap['i']); stack.append(1) stack.append(stack.pop() + stack.pop()) diff --git a/03-monolith/tf-03.py b/03-monolith/tf-03.py index 3c74362..08cfb55 100755 --- a/03-monolith/tf-03.py +++ b/03-monolith/tf-03.py @@ -1,5 +1,6 @@ #!/usr/bin/env python +from __future__ import print_function import sys, string # the global list of [word, frequency] pairs word_freqs = [] @@ -47,5 +48,5 @@ for line in open(sys.argv[1]): i += 1 for tf in word_freqs[0:25]: - print tf[0], ' - ', tf[1] + print(tf[0], ' - ', tf[1]) diff --git a/04-cookbook/tf-04.py b/04-cookbook/tf-04.py index 423f403..4c9a193 100755 --- a/04-cookbook/tf-04.py +++ b/04-cookbook/tf-04.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import sys, string # The shared mutable data @@ -54,7 +55,7 @@ def remove_stop_words(): def frequencies(): """ Creates a list of pairs associating - words with frequencies + words with frequencies """ global words global word_freqs @@ -70,7 +71,7 @@ def sort(): Sorts word_freqs by frequency """ global word_freqs - word_freqs.sort(lambda x, y: cmp(y[1], x[1])) + word_freqs.sort(key=lambda x: x[1], reverse=True) # @@ -84,5 +85,4 @@ frequencies() sort() for tf in word_freqs[0:25]: - print tf[0], ' - ', tf[1] - + print(tf[0], ' - ', tf[1]) diff --git a/05-pipeline/tf-05.py b/05-pipeline/tf-05.py index 79c916b..f95e102 100755 --- a/05-pipeline/tf-05.py +++ b/05-pipeline/tf-05.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import sys, re, operator, string # @@ -65,7 +66,7 @@ def print_all(word_freqs): Takes a list of pairs where the entries are sorted by frequency and print them recursively. """ if(len(word_freqs) > 0): - print word_freqs[0][0], ' - ', word_freqs[0][1] + print(word_freqs[0][0], ' - ', word_freqs[0][1]) print_all(word_freqs[1:]); # diff --git a/06-code-golf/tf-06-1.py b/06-code-golf/tf-06-1.py index 668fedd..711c89d 100644 --- a/06-code-golf/tf-06-1.py +++ b/06-code-golf/tf-06-1.py @@ -1,8 +1,9 @@ #!/usr/bin/env python +from __future__ import print_function import re, string, sys stops = set(open("../stop_words.txt").read().split(",") + list(string.ascii_lowercase)) words = [x.lower() for x in re.split("[^a-zA-Z]+", open(sys.argv[1]).read()) if len(x) > 0 and x.lower() not in stops] unique_words = list(set(words)) -unique_words.sort(lambda x, y: cmp(words.count(y), words.count(x))) -print "\n".join(["%s - %s" % (x, words.count(x)) for x in unique_words[:25]]) +unique_words.sort(key=lambda x: words.count(x), reverse=True) +print("\n".join(["%s - %s" % (x, words.count(x)) for x in unique_words[:25]])) diff --git a/06-code-golf/tf-06-bm.py b/06-code-golf/tf-06-bm.py index 2a1fb63..34e8d50 100644 --- a/06-code-golf/tf-06-bm.py +++ b/06-code-golf/tf-06-bm.py @@ -1 +1,3 @@ +from __future__ import print_function +from functools import reduce print (reduce(lambda string, tup: string + tup[0] + ' - ' + str(tup[1]) + '\n', sorted( filter(lambda tup: tup[0] not in open(__import__('os').path.join(__import__('os').path.dirname(__file__), '..', 'stop_words.txt')).read().lower().split(','), reduce(lambda word_dict, word: word_dict if (word_dict.__setitem__(word, word_dict.get(word, 0) + 1) if True else None) else word_dict, filter(lambda word: len(word) > 1, (''.join(map(lambda letter: ' ' if ord(letter) not in set(range(ord('a'), ord('z') + 1)) else letter, open(__import__('sys').argv[1]).read().lower()))).split()), {}).iteritems()), key=lambda tup: tup[1], reverse=True)[0:25], '')) # hole in one? diff --git a/06-code-golf/tf-06-pn.py b/06-code-golf/tf-06-pn.py index 3071f64..1df5e05 100755 --- a/06-code-golf/tf-06-pn.py +++ b/06-code-golf/tf-06-pn.py @@ -2,10 +2,11 @@ # My golf score is slightly lower! # Best wishes, Peter Norvig +from __future__ import print_function import re, sys, collections stopwords = set(open('../stop_words.txt').read().split(',')) words = re.findall('[a-z]{2,}', open(sys.argv[1]).read().lower()) counts = collections.Counter(w for w in words if w not in stopwords) for (w, c) in counts.most_common(25): - print w, '-', c + print(w, '-', c) diff --git a/06-code-golf/tf-06.py b/06-code-golf/tf-06.py index 4dcce5e..ad02820 100755 --- a/06-code-golf/tf-06.py +++ b/06-code-golf/tf-06.py @@ -1,6 +1,7 @@ #!/usr/bin/env python +from __future__ import print_function import heapq, re, sys words = re.findall("[a-z]{2,}", open(sys.argv[1]).read().lower()) for w in heapq.nlargest(25, set(words) - set(open("../stop_words.txt").read().split(",")), words.count): - print w, "-", words.count(w) + print(w, "-", words.count(w)) diff --git a/07-infinite-mirror/tf-07.py b/07-infinite-mirror/tf-07.py index e622226..98a7798 100755 --- a/07-infinite-mirror/tf-07.py +++ b/07-infinite-mirror/tf-07.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import re, sys, operator # Mileage may vary. If this crashes, make it lower @@ -29,7 +30,7 @@ def wf_print(wordfreq): return else: (w, c) = wordfreq[0] - print w, '-', c + print(w, '-', c) wf_print(wordfreq[1:]) stop_words = set(open('../stop_words.txt').read().split(',')) diff --git a/08-kick-forward/tf-08.py b/08-kick-forward/tf-08.py index 637e262..8044f58 100755 --- a/08-kick-forward/tf-08.py +++ b/08-kick-forward/tf-08.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import sys, re, operator, string # @@ -40,7 +41,7 @@ def sort(wf, func): def print_text(word_freqs, func): for (w, c) in word_freqs[0:25]: - print w, "-", c + print(w, "-", c) func(None) def no_op(func): diff --git a/09-the-one/tf-09.py b/09-the-one/tf-09.py index 5b8cf7c..eaf6deb 100755 --- a/09-the-one/tf-09.py +++ b/09-the-one/tf-09.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import sys, re, operator, string # @@ -13,7 +14,7 @@ class TFTheOne: return self def printme(self): - print self._value + print(self._value) # # The functions diff --git a/10-things/tf-10.py b/10-things/tf-10.py index 531f217..2bb3544 100755 --- a/10-things/tf-10.py +++ b/10-things/tf-10.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import sys, re, operator, string from abc import ABCMeta @@ -73,7 +74,7 @@ class WordFrequencyController(TFExercise): word_freqs = self._word_freq_manager.sorted() for (w, c) in word_freqs[0:25]: - print w, ' - ', c + print(w, ' - ', c) # # The main function diff --git a/11-letterbox/tf-11.py b/11-letterbox/tf-11.py index bdd7696..e9f041c 100755 --- a/11-letterbox/tf-11.py +++ b/11-letterbox/tf-11.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import sys, re, operator, string class DataStorageManager(): @@ -89,7 +90,7 @@ class WordFrequencyController(): word_freqs = self._word_freq_manager.dispatch(['sorted']) for (w, c) in word_freqs[0:25]: - print w, ' - ', c + print(w, ' - ', c) # # The main function diff --git a/12-closed-maps/tf-12.py b/12-closed-maps/tf-12.py index 79b8ba1..7e53560 100644 --- a/12-closed-maps/tf-12.py +++ b/12-closed-maps/tf-12.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import sys, re, operator, string # Auxiliary functions that can't be lambdas @@ -46,4 +47,4 @@ for w in data_storage_obj['words'](): word_freqs = word_freqs_obj['sorted']() for (w, c) in word_freqs[0:25]: - print w, ' - ', c + print(w, ' - ', c) diff --git a/13-abstract-things/tf-13.py b/13-abstract-things/tf-13.py index 477216c..41af3a7 100755 --- a/13-abstract-things/tf-13.py +++ b/13-abstract-things/tf-13.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import abc, sys, re, operator, string # @@ -97,7 +98,7 @@ class WordFrequencyController: word_freqs = self._word_freq_counter.sorted() for (w, c) in word_freqs[0:25]: - print w, ' - ', c + print(w, ' - ', c) # # The main function diff --git a/14-hollywood/tf-14.py b/14-hollywood/tf-14.py index b05d661..551a937 100755 --- a/14-hollywood/tf-14.py +++ b/14-hollywood/tf-14.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import sys, re, operator, string # @@ -89,7 +90,7 @@ class WordFrequencyCounter: def __print_freqs(self): word_freqs = sorted(self._word_freqs.iteritems(), key=operator.itemgetter(1), reverse=True) for (w, c) in word_freqs[0:25]: - print w, ' - ', c + print(w, ' - ', c) # # The main function diff --git a/15-bulletin-board/tf-15.py b/15-bulletin-board/tf-15.py index d08a14f..4327f73 100755 --- a/15-bulletin-board/tf-15.py +++ b/15-bulletin-board/tf-15.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import sys, re, operator, string # @@ -79,7 +80,7 @@ class WordFrequencyCounter: def print_freqs(self, event): word_freqs = sorted(self._word_freqs.iteritems(), key=operator.itemgetter(1), reverse=True) for (w, c) in word_freqs[0:25]: - print w, ' - ', c + print(w, ' - ', c) class WordFrequencyApplication: def __init__(self, event_manager): diff --git a/16-introspective/tf-16.py b/16-introspective/tf-16.py index ab4af9d..5c9659a 100755 --- a/16-introspective/tf-16.py +++ b/16-introspective/tf-16.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import sys, re, operator, string, inspect def read_stop_words(): @@ -39,7 +40,7 @@ def sort(word_freq): def main(): word_freqs = sort(frequencies(extract_words(sys.argv[1]))) for (w, c) in word_freqs[0:25]: - print w, ' - ', c + print(w, ' - ', c) if __name__ == "__main__": main() diff --git a/17-reflective/tf-17.py b/17-reflective/tf-17.py index 11ac5cf..8495ed6 100755 --- a/17-reflective/tf-17.py +++ b/17-reflective/tf-17.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import sys, re, operator, string, os # @@ -44,5 +45,5 @@ exec('sort = ' + sort_func) word_freqs = locals()['sort'](locals()['frequencies'](locals()['extract_words'](filename))) for (w, c) in word_freqs[0:25]: - print w, ' - ', c + print(w, ' - ', c) diff --git a/18-aspects/tf-18.py b/18-aspects/tf-18.py index ad0a993..b4f0006 100755 --- a/18-aspects/tf-18.py +++ b/18-aspects/tf-18.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import sys, re, operator, string, time # @@ -32,7 +33,7 @@ def profile(f): start_time = time.time() ret_value = f(*arg, **kw) elapsed = time.time() - start_time - print "%s(...) took %s secs" % (f.__name__, elapsed) + print("%s(...) took %s secs" % (f.__name__, elapsed)) return ret_value return profilewrapper @@ -40,10 +41,10 @@ def profile(f): tracked_functions = [extract_words, frequencies, sort] # weaver for func in tracked_functions: - globals()[func.func_name]=profile(func) + globals()[func.__name__]=profile(func) word_freqs = sort(frequencies(extract_words(sys.argv[1]))) for (w, c) in word_freqs[0:25]: - print w, ' - ', c + print(w, ' - ', c) diff --git a/19-plugins/tf-19.py b/19-plugins/tf-19.py index aaf752c..e94096d 100755 --- a/19-plugins/tf-19.py +++ b/19-plugins/tf-19.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import sys, ConfigParser, imp def load_plugins(): @@ -14,5 +15,5 @@ load_plugins() word_freqs = tffreqs.top25(tfwords.extract_words(sys.argv[1])) for (w, c) in word_freqs: - print w, ' - ', c + print(w, ' - ', c) diff --git a/20-constructivist/tf-20.py b/20-constructivist/tf-20.py index 5e4c859..b3c727a 100755 --- a/20-constructivist/tf-20.py +++ b/20-constructivist/tf-20.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import sys, re, operator, string, inspect # @@ -12,7 +13,7 @@ def extract_words(path_to_file): with open(path_to_file) as f: str_data = f.read() except IOError as e: - print "I/O error({0}) when opening {1}: {2}".format(e.errno, path_to_file, e.strerror) + print("I/O error({0}) when opening {1}: {2}".format(e.errno, path_to_file, e.strerror)) return [] pattern = re.compile('[\W_]+') @@ -27,7 +28,7 @@ def remove_stop_words(word_list): with open('../stop_words.txt') as f: stop_words = f.read().split(',') except IOError as e: - print "I/O error({0}) when opening ../stops_words.txt: {1}".format(e.errno, e.strerror) + print("I/O error({0}) when opening ../stops_words.txt: {1}".format(e.errno, e.strerror)) return word_list stop_words.extend(list(string.ascii_lowercase)) @@ -58,5 +59,5 @@ filename = sys.argv[1] if len(sys.argv) > 1 else "../input.txt" word_freqs = sort(frequencies(remove_stop_words(extract_words(filename)))) for tf in word_freqs[0:25]: - print tf[0], ' - ', tf[1] + print(tf[0], ' - ', tf[1]) diff --git a/21-tantrum/tf-21.py b/21-tantrum/tf-21.py index 1f1d35b..b649656 100755 --- a/21-tantrum/tf-21.py +++ b/21-tantrum/tf-21.py @@ -1,5 +1,6 @@ #!/usr/bin/env python +from __future__ import print_function import sys, re, operator, string, traceback # @@ -13,7 +14,7 @@ def extract_words(path_to_file): with open(path_to_file) as f: str_data = f.read() except IOError as e: - print "I/O error({0}) when opening {1}: {2}! I quit!".format(e.errno, path_to_file, e.strerror) + print("I/O error({0}) when opening {1}: {2}! I quit!".format(e.errno, path_to_file, e.strerror)) raise e pattern = re.compile('[\W_]+') @@ -27,7 +28,7 @@ def remove_stop_words(word_list): with open('../stop_words.txt') as f: stop_words = f.read().split(',') except IOError as e: - print "I/O error({0}) when opening ../stops_words.txt: {1}! I quit!".format(e.errno, e.strerror) + print("I/O error({0}) when opening ../stops_words.txt: {1}! I quit!".format(e.errno, e.strerror)) raise e stop_words.extend(list(string.ascii_lowercase)) @@ -35,7 +36,7 @@ def remove_stop_words(word_list): def frequencies(word_list): assert(type(word_list) is list), "I need a list!" - assert(word_list <> []), "I need a non-empty list!" + assert(word_list != []), "I need a non-empty list!" word_freqs = {} for w in word_list: @@ -47,12 +48,12 @@ def frequencies(word_list): def sort(word_freq): assert(type(word_freq) is dict), "I need a dictionary!" - assert(word_freq <> {}), "I need a non-empty dictionary!" + assert(word_freq != {}), "I need a non-empty dictionary!" try: return sorted(word_freq.iteritems(), key=operator.itemgetter(1), reverse=True) except Exception as e: - print "Sorted threw {0}: {1}".format(e) + print("Sorted threw {0}: {1}".format(e)) raise e # @@ -65,8 +66,8 @@ try: assert(type(word_freqs) is list), "OMG! This is not a list!" assert(len(word_freqs) > 25), "SRSLY? Less than 25 words!" for (w, c) in word_freqs[0:25]: - print w, ' - ', c + print(w, ' - ', c) except Exception as e: - print "Something wrong: {0}".format(e) + print("Something wrong: {0}".format(e)) traceback.print_exc() diff --git a/22-passive-aggressive/tf-22.py b/22-passive-aggressive/tf-22.py index 573df16..ac2bd04 100644 --- a/22-passive-aggressive/tf-22.py +++ b/22-passive-aggressive/tf-22.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import sys, re, operator, string # @@ -25,7 +26,7 @@ def remove_stop_words(word_list): def frequencies(word_list): assert(type(word_list) is list), "I need a list! I quit!" - assert(word_list <> []), "I need a non-empty list! I quit!" + assert(word_list != []), "I need a non-empty list! I quit!" word_freqs = {} for w in word_list: @@ -37,7 +38,7 @@ def frequencies(word_list): def sort(word_freqs): assert(type(word_freqs) is dict), "I need a dictionary! I quit!" - assert(word_freqs <> {}), "I need a non-empty dictionary! I quit!" + assert(word_freqs != {}), "I need a non-empty dictionary! I quit!" return sorted(word_freqs.iteritems(), key=operator.itemgetter(1), reverse=True) @@ -50,8 +51,8 @@ try: assert(len(word_freqs) > 25), "OMG! Less than 25 words! I QUIT!" for tf in word_freqs[0:25]: - print tf[0], ' - ', tf[1] + print(tf[0], ' - ', tf[1]) except Exception as e: - print "Something wrong: {0}".format(e) + print("Something wrong: {0}".format(e)) diff --git a/22-passive-aggressive/tf-23-monadic.py b/22-passive-aggressive/tf-23-monadic.py index 77f821d..8fd1bc4 100644 --- a/22-passive-aggressive/tf-23-monadic.py +++ b/22-passive-aggressive/tf-23-monadic.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import sys, re, operator, string # @@ -21,9 +22,9 @@ class TFPassiveAggressive: def printme(self): if self._e == None: - print self._value + print(self._value) else: - print self._e, " in ", self._offending_func.__name__ + print(self._e, " in ", self._offending_func.__name__) # # The functions @@ -53,7 +54,7 @@ def remove_stop_words(word_list): def frequencies(word_list): assert(type(word_list) is list), "I need a list! I quit!" - assert(word_list <> []), "I need a non-empty list! I quit!" + assert(word_list != []), "I need a non-empty list! I quit!" word_freqs = {} for w in word_list: @@ -65,13 +66,13 @@ def frequencies(word_list): def sort(word_freqs): assert(type(word_freqs) is dict), "I need a dictionary! I quit!" - assert(word_freqs <> {}), "I need a non-empty dictionary! I quit!" + assert(word_freqs != {}), "I need a non-empty dictionary! I quit!" return sorted(word_freqs.iteritems(), key=operator.itemgetter(1), reverse=True) def top25_freqs(word_freqs): assert(type(word_freqs) is list), "I need a list! I quit!" - assert(word_freqs <> {}), "I need a non-empty dictionary! I quit!" + assert(word_freqs != {}), "I need a non-empty dictionary! I quit!" top25 = "" for tf in word_freqs[0:25]: diff --git a/23-declared-intentions/tf-23.py b/23-declared-intentions/tf-23.py index 9eac854..1b703d1 100644 --- a/23-declared-intentions/tf-23.py +++ b/23-declared-intentions/tf-23.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import sys, re, operator, string, inspect # @@ -11,7 +12,7 @@ class AcceptTypes(): def __call__(self, f): def wrapped_f(*args): for i in range(len(self._args)): - if type(args[i]) <> self._args[i]: + if type(args[i]) != self._args[i]: raise TypeError("Expecting %s got %s" % (str(self._args[i]), str(type(args[i])))) return f(*args) return wrapped_f @@ -45,5 +46,5 @@ def sort(word_freq): word_freqs = sort(frequencies(extract_words(sys.argv[1]))) for (w, c) in word_freqs[0:25]: - print w, ' - ', c + print(w, ' - ', c) diff --git a/24-quarantine/tf-24.py b/24-quarantine/tf-24.py index 6501599..3a69824 100644 --- a/24-quarantine/tf-24.py +++ b/24-quarantine/tf-24.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import sys, re, operator, string # @@ -19,7 +20,7 @@ class TFQuarantine: value = lambda : None for func in self._funcs: value = func(guard_callable(value)) - print guard_callable(value) + print(guard_callable(value)) # # The functions diff --git a/25-persistent-tables/tf-25.py b/25-persistent-tables/tf-25.py index 4989688..67dd107 100755 --- a/25-persistent-tables/tf-25.py +++ b/25-persistent-tables/tf-25.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import sys, re, string, sqlite3, os.path # @@ -66,4 +67,4 @@ with sqlite3.connect('tf.db') as connection: for i in range(25): row = c.fetchone() if row != None: - print row[0] + ' - ' + str(row[1]) + print(row[0] + ' - ' + str(row[1])) diff --git a/26-spreadsheet/tf-26.py b/26-spreadsheet/tf-26.py index c1816d7..b07656c 100755 --- a/26-spreadsheet/tf-26.py +++ b/26-spreadsheet/tf-26.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import sys, re, itertools, operator # @@ -46,4 +47,4 @@ stop_words[0] = set(open('../stop_words.txt').read().split(',')) update() for (w, c) in sorted_data[0][:25]: - print w, '-', c + print(w, '-', c) diff --git a/27-lazy-rivers/tf-27.py b/27-lazy-rivers/tf-27.py index 66f42b6..b1cdbb7 100755 --- a/27-lazy-rivers/tf-27.py +++ b/27-lazy-rivers/tf-27.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import sys, operator, string def characters(filename): @@ -42,7 +43,7 @@ def count_and_sort(filename): # The main function # for word_freqs in count_and_sort(sys.argv[1]): - print "-----------------------------" + print("-----------------------------") for (w, c) in word_freqs[0:25]: - print w, ' - ', c + print(w, ' - ', c) diff --git a/28-actors/tf-28.py b/28-actors/tf-28.py index 69f76c2..3ee5740 100755 --- a/28-actors/tf-28.py +++ b/28-actors/tf-28.py @@ -1,5 +1,6 @@ #!/usr/bin/env python +from __future__ import print_function import sys, re, operator, string from threading import Thread from Queue import Queue @@ -114,7 +115,7 @@ class WordFrequencyController(ActiveWFObject): def _display(self, message): word_freqs = message[0] for (w, f) in word_freqs[0:25]: - print w, ' - ', f + print(w, ' - ', f) send(self._storage_manager, ['die']) self._stop = True diff --git a/29-dataspaces/tf-29.py b/29-dataspaces/tf-29.py index a22b385..76afb9e 100755 --- a/29-dataspaces/tf-29.py +++ b/29-dataspaces/tf-29.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import re, sys, operator, Queue, threading # Two data spaces @@ -49,4 +50,4 @@ while not freq_space.empty(): word_freqs[k] = count for (w, c) in sorted(word_freqs.iteritems(), key=operator.itemgetter(1), reverse=True)[:25]: - print w, '-', c + print(w, '-', c) diff --git a/30-map-reduce/tf-30.py b/30-map-reduce/tf-30.py index c94c875..ce2db3b 100755 --- a/30-map-reduce/tf-30.py +++ b/30-map-reduce/tf-30.py @@ -1,11 +1,19 @@ #!/usr/bin/env python +from __future__ import print_function import sys, re, operator, string +from functools import reduce + +try: + xrange # Python 2 +except NameError: + xrange = range # Python 3 + # # Functions for map reduce # def partition(data_str, nlines): - """ + """ Partitions the input data_str (a big string) into chunks of nlines. """ @@ -14,8 +22,8 @@ def partition(data_str, nlines): yield '\n'.join(lines[i:i+nlines]) def split_words(data_str): - """ - Takes a string, returns a list of pairs (word, 1), + """ + Takes a string, returns a list of pairs (word, 1), one for each word in the input, so [(w1, 1), (w2, 1), ..., (wn, 1)] """ @@ -37,10 +45,10 @@ def split_words(data_str): return result def count_words(pairs_list_1, pairs_list_2): - """ + """ Takes two lists of pairs of the form [(w1, 1), ...] - and returns a list of pairs [(w1, frequency), ...], + and returns a list of pairs [(w1, frequency), ...], where frequency is the sum of all the reported occurrences """ mapping = dict((k, v) for k, v in pairs_list_1) @@ -70,5 +78,4 @@ splits.insert(0, []) # Normalize input to reduce word_freqs = sort(reduce(count_words, splits)) for (w, c) in word_freqs[0:25]: - print w, ' - ', c - + print(w, ' - ', c) diff --git a/31-double-map-reduce/tf-31.py b/31-double-map-reduce/tf-31.py index b6d112a..997c05f 100755 --- a/31-double-map-reduce/tf-31.py +++ b/31-double-map-reduce/tf-31.py @@ -1,11 +1,19 @@ #!/usr/bin/env python +from __future__ import print_function import sys, re, operator, string +from functools import reduce + +try: + xrange # Python 2 +except NameError: + xrange = range # Python 3 + # # Functions for map reduce # def partition(data_str, nlines): - """ + """ Partitions the input data_str (a big string) into chunks of nlines. """ @@ -14,8 +22,8 @@ def partition(data_str, nlines): yield '\n'.join(lines[i:i+nlines]) def split_words(data_str): - """ - Takes a string, returns a list of pairs (word, 1), + """ + Takes a string, returns a list of pairs (word, 1), one for each word in the input, so [(w1, 1), (w2, 1), ..., (wn, 1)] """ @@ -38,14 +46,14 @@ def split_words(data_str): def regroup(pairs_list): """ - Takes a list of lists of pairs of the form + Takes a list of lists of pairs of the form [[(w1, 1), (w2, 1), ..., (wn, 1)], [(w1, 1), (w2, 1), ..., (wn, 1)], ...] - and returns a dictionary mapping each unique word to the + and returns a dictionary mapping each unique word to the corresponding list of pairs, so - { w1 : [(w1, 1), (w1, 1)...], - w2 : [(w2, 1), (w2, 1)...], + { w1 : [(w1, 1), (w1, 1)...], + w2 : [(w2, 1), (w2, 1)...], ...} """ mapping = {} @@ -56,11 +64,11 @@ def regroup(pairs_list): else: mapping[p[0]] = [p] return mapping - + def count_words(mapping): - """ + """ Takes a mapping of the form (word, [(word, 1), (word, 1)...)]) - and returns a pair (word, frequency), where frequency is the + and returns a pair (word, frequency), where frequency is the sum of all the reported occurrences """ def add(x, y): @@ -87,5 +95,4 @@ splits_per_word = regroup(splits) word_freqs = sort(map(count_words, splits_per_word.items())) for (w, c) in word_freqs[0:25]: - print w, ' - ', c - + print(w, ' - ', c) diff --git a/32-trinity/tf-32-active.py b/32-trinity/tf-32-active.py index 47e3fd3..4b14575 100755 --- a/32-trinity/tf-32-active.py +++ b/32-trinity/tf-32-active.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import sys, operator, string, os, threading, re from util import getch, cls, get_input from time import sleep @@ -38,7 +39,7 @@ class FreqObserver(threading.Thread): def refresh_screen(data): # clear screen cls() - print data + print(data) sys.stdout.flush() data_str = "" @@ -57,7 +58,7 @@ class WordsCounter: for line in f: yield [w for w in re.findall('[a-z]{2,}', line.lower()) if w not in stopwords] - words = non_stop_words().next() + words = next(non_stop_words()) lock.acquire() for w in words: self.freqs[w] = 1 if w not in self.freqs else self.freqs[w]+1 @@ -66,8 +67,8 @@ class WordsCounter: # # The controller # -print "Press space bar to fetch words from the file one by one" -print "Press ESC to switch to automatic mode" +print("Press space bar to fetch words from the file one by one") +print("Press ESC to switch to automatic mode") model = WordsCounter() view = FreqObserver(model.freqs) with open(sys.argv[1]) as f: diff --git a/32-trinity/tf-32-reactive.py b/32-trinity/tf-32-reactive.py index 89e06fb..65c9399 100644 --- a/32-trinity/tf-32-reactive.py +++ b/32-trinity/tf-32-reactive.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import sys, re, operator, collections class WordFrequenciesModel: @@ -19,7 +20,7 @@ class WordFrequenciesModel: for obs in self._observers: obs.render() except IOError: - print "File not found" + print("File not found") self.freqs = {} class WordFrequenciesView: @@ -30,7 +31,7 @@ class WordFrequenciesView: def render(self): sorted_freqs = sorted(self._model.freqs.iteritems(), key=operator.itemgetter(1), reverse=True) for (w, c) in sorted_freqs[:25]: - print w, '-', c + print(w, '-', c) class WordFrequencyController: def __init__(self, model, view): @@ -39,7 +40,7 @@ class WordFrequencyController: def run(self): self._model.update(sys.argv[1]) while True: - print "Next file: " + print("Next file: ") sys.stdout.flush() filename = sys.stdin.readline().strip() self._model.update(filename) diff --git a/32-trinity/tf-32.py b/32-trinity/tf-32.py index 383ca56..fee95d6 100755 --- a/32-trinity/tf-32.py +++ b/32-trinity/tf-32.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import sys, re, operator, collections class WordFrequenciesModel: @@ -14,7 +15,7 @@ class WordFrequenciesModel: words = re.findall('[a-z]{2,}', open(path_to_file).read().lower()) self.freqs = collections.Counter(w for w in words if w not in self.stopwords) except IOError: - print "File not found" + print("File not found") self.freqs = {} class WordFrequenciesView: @@ -24,7 +25,7 @@ class WordFrequenciesView: def render(self): sorted_freqs = sorted(self._model.freqs.iteritems(), key=operator.itemgetter(1), reverse=True) for (w, c) in sorted_freqs[0:25]: - print w, '-', c + print(w, '-', c) class WordFrequencyController: def __init__(self, model, view): @@ -33,7 +34,7 @@ class WordFrequencyController: def run(self): while True: - print "Next file: " + print("Next file: ") sys.stdout.flush() filename = sys.stdin.readline().strip() self._model.update(filename) diff --git a/32-trinity/util.py b/32-trinity/util.py index e7c2526..92c8d85 100755 --- a/32-trinity/util.py +++ b/32-trinity/util.py @@ -1,4 +1,4 @@ -import sys, os +import os # # getch in a platform-independent way @@ -65,11 +65,10 @@ def get_input(): if not interactive: return True - while True: + while True: key = ord(getch()) if key == 32: # space bar return True elif key == 27: # ESC interactive = False return True - diff --git a/33-restful/tf-33.py b/33-restful/tf-33.py index 7b5960a..483e9a8 100755 --- a/33-restful/tf-33.py +++ b/33-restful/tf-33.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import re, string, sys with open("../stop_words.txt") as f: @@ -22,7 +23,7 @@ def quit_handler(args): def upload_get_handler(args): return "Name of file to upload?", ["post", "file"] - + def upload_post_handler(args): def create_data(filename): if filename in data: @@ -32,7 +33,7 @@ def upload_post_handler(args): for w in [x.lower() for x in re.split("[^a-zA-Z]+", f.read()) if len(x) > 0 and x.lower() not in stops]: word_freqs[w] = word_freqs.get(w, 0) + 1 word_freqsl = word_freqs.items() - word_freqsl.sort(lambda x, y: cmp(y[1], x[1])) + word_freqsl.sort(key=lambda x: x[1], reverse=True) data[filename] = word_freqsl if args == None: @@ -49,7 +50,7 @@ def word_get_handler(args): if word_index < len(data[filename]): return data[filename][word_index] else: - return ("no more words", 0) + return ("no more words", 0) filename = args[0]; word_index = args[1] word_info = get_word(filename, word_index) @@ -57,16 +58,16 @@ def word_get_handler(args): rep += "\n\nWhat would you like to do next?" rep += "\n1 - Quit" + "\n2 - Upload file" rep += "\n3 - See next most-frequently occurring word" - links = {"1" : ["post", "execution", None], - "2" : ["get", "file_form", None], + links = {"1" : ["post", "execution", None], + "2" : ["get", "file_form", None], "3" : ["get", "word", [filename, word_index+1]]} return rep, links # Handler registration handlers = {"post_execution" : quit_handler, - "get_default" : default_get_handler, - "get_file_form" : upload_get_handler, - "post_file" : upload_post_handler, + "get_default" : default_get_handler, + "get_file_form" : upload_get_handler, + "post_file" : upload_post_handler, "get_word" : word_get_handler } # The "server" core @@ -81,7 +82,7 @@ def handle_request(verb, uri, args): # A very simple client "browser" def render_and_get_input(state_representation, links): - print state_representation + print(state_representation) sys.stdout.flush() if type(links) is dict: # many possible next states input = sys.stdin.readline().strip() diff --git a/34-the-c-flow/tf-34.py b/34-the-c-flow/tf-34.py index 67f5220..2f76dd2 100755 --- a/34-the-c-flow/tf-34.py +++ b/34-the-c-flow/tf-34.py @@ -1,5 +1,6 @@ #!/usr/bin/env python +from __future__ import print_function import sys, re, operator, string, inspect # Reusing the defensive style program to illustrate this @@ -20,7 +21,7 @@ def extract_words(path_to_file): with open(path_to_file) as f: str_data = f.read() except IOError as e: - print "I/O error({0}) when opening {1}: {2}".format(e.errno, path_to_file, e.strerror) + print("I/O error({0}) when opening {1}: {2}".format(e.errno, path_to_file, e.strerror)) fail = True if not fail: @@ -31,7 +32,7 @@ def extract_words(path_to_file): with open('../stop_words.txt') as f: stop_words = f.read().split(',') except IOError as e: - print "I/O error({0}) when opening ../stops_words.txt: {1}".format(e.errno, e.strerror) + print("I/O error({0}) when opening ../stops_words.txt: {1}".format(e.errno, e.strerror)) fail = True if not fail: @@ -44,7 +45,7 @@ def frequencies(word_list): Takes a list of words and returns a dictionary associating words with frequencies of occurrence """ - if type(word_list) is list and word_list <> []: + if type(word_list) is list and word_list != []: word_freqs = {} for w in word_list: if w in word_freqs: @@ -61,7 +62,7 @@ def sort(word_freq): and returns a list of pairs where the entries are sorted by frequency """ - if type(word_freq) is dict and word_freq <> {}: + if type(word_freq) is dict and word_freq != {}: return sorted(word_freq.iteritems(), key=operator.itemgetter(1), reverse=True) else: return [] @@ -73,5 +74,5 @@ filename = sys.argv[1] if len(sys.argv) > 1 else "../input.txt" word_freqs = sort(frequencies(extract_words(filename))) for tf in word_freqs[0:25]: - print tf[0], ' - ', tf[1] + print(tf[0], ' - ', tf[1])