Modernize Python 2 code to get ready for Python 3

This commit is contained in:
cclauss
2018-03-21 21:41:35 +01:00
parent d521abd5d7
commit 1fa9eb3c2b
42 changed files with 169 additions and 112 deletions

View File

@@ -1,5 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, os, string import sys, os, string
# Utility for handling the intermediate 'secondary memory' # 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 for tf in data[0:25]: # elimination of symbol tf is exercise
if len(tf) == 2: if len(tf) == 2:
print tf[0], ' - ', tf[1] print(tf[0], ' - ', tf[1])
# We're done # We're done
word_freqs.close() word_freqs.close()

View File

@@ -1,10 +1,16 @@
#!/usr/local/bin/python #!/usr/local/bin/python
# #
# f o r t h . p y # f o r t h . p y
# Author: Chris Meyers @ # Author: Chris Meyers @
# http://openbookproject.net/py4fun/forth/forth.html # 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 ds = [] # The data stack
cStack = [] # The control struct stack cStack = [] # The control struct stack
@@ -15,14 +21,14 @@ words = [] # The input stream of tokens
def main() : def main() :
while 1 : while 1 :
pcode = compile() # compile/run from user pcode = compile() # compile/run from user
if pcode == None : print; return if pcode == None : print(); return
execute(pcode) execute(pcode)
#============================== Lexical Parsing #============================== Lexical Parsing
def getWord (prompt="... ") : def getWord (prompt="... ") :
global words global words
while not words : while not words :
try : lin = raw_input(prompt)+"\n" try : lin = raw_input(prompt)+"\n"
except : return None except : return None
if lin[0:1] == "@" : lin = open(lin[1:-1]).read() 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 rDup (cod,p) : ds.append(ds[-1])
def rDrop(cod,p) : ds.pop() def rDrop(cod,p) : ds.pop()
def rOver(cod,p) : ds.append(ds[-2]) def rOver(cod,p) : ds.append(ds[-2])
def rDump(cod,p) : print "ds = ", ds def rDump(cod,p) : print("ds = ", ds)
def rDot (cod,p) : print ds.pop() def rDot (cod,p) : print(ds.pop())
def rJmp (cod,p) : return cod[p] def rJmp (cod,p) : return cod[p]
def rJnz (cod,p) : return (cod[p],p+1)[ds.pop()] def rJnz (cod,p) : return (cod[p],p+1)[ds.pop()]
def rJz (cod,p) : return (p+1,cod[p])[ds.pop()==0] def rJz (cod,p) : return (p+1,cod[p])[ds.pop()==0]
@@ -92,7 +98,7 @@ rDict = {
'create': rCreate, 'does>': rDoes, 'create': rCreate, 'does>': rDoes,
} }
#================================= Compile time #================================= Compile time
def compile() : def compile() :
pcode = []; prompt = "Forth> " pcode = []; prompt = "Forth> "
@@ -114,12 +120,12 @@ def compile() :
try : pcode.append(int(word)) try : pcode.append(int(word))
except : except :
try: pcode.append(float(word)) try: pcode.append(float(word))
except : except :
pcode[-1] = rRun # Change rPush to rRun pcode[-1] = rRun # Change rPush to rRun
pcode.append(word) # Assume word will be defined pcode.append(word) # Assume word will be defined
if not cStack : return pcode if not cStack : return pcode
prompt = "... " prompt = "... "
def fatal (mesg) : raise mesg def fatal (mesg) : raise mesg
def cColon (pcode) : def cColon (pcode) :
@@ -168,5 +174,5 @@ cDict = {
':' : cColon, ';' : cSemi, 'if': cIf, 'else': cElse, 'then': cThen, ':' : cColon, ';' : cSemi, 'if': cIf, 'else': cElse, 'then': cThen,
'begin': cBegin, 'until': cUntil, 'begin': cBegin, 'until': cUntil,
} }
if __name__ == "__main__" : main() if __name__ == "__main__" : main()

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, re, operator, string import sys, re, operator, string
# #
@@ -104,7 +105,7 @@ stack.append(0)
# the last word there will be one item left # the last word there will be one item left
while stack[-1] < 25 and len(stack) > 1: while stack[-1] < 25 and len(stack) > 1:
heap['i'] = stack.pop() 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(heap['i']); stack.append(1)
stack.append(stack.pop() + stack.pop()) stack.append(stack.pop() + stack.pop())

View File

@@ -1,5 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, string import sys, string
# the global list of [word, frequency] pairs # the global list of [word, frequency] pairs
word_freqs = [] word_freqs = []
@@ -47,5 +48,5 @@ for line in open(sys.argv[1]):
i += 1 i += 1
for tf in word_freqs[0:25]: for tf in word_freqs[0:25]:
print tf[0], ' - ', tf[1] print(tf[0], ' - ', tf[1])

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, string import sys, string
# The shared mutable data # The shared mutable data
@@ -54,7 +55,7 @@ def remove_stop_words():
def frequencies(): def frequencies():
""" """
Creates a list of pairs associating Creates a list of pairs associating
words with frequencies words with frequencies
""" """
global words global words
global word_freqs global word_freqs
@@ -70,7 +71,7 @@ def sort():
Sorts word_freqs by frequency Sorts word_freqs by frequency
""" """
global word_freqs 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() sort()
for tf in word_freqs[0:25]: for tf in word_freqs[0:25]:
print tf[0], ' - ', tf[1] print(tf[0], ' - ', tf[1])

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, re, operator, string 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. Takes a list of pairs where the entries are sorted by frequency and print them recursively.
""" """
if(len(word_freqs) > 0): 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:]); print_all(word_freqs[1:]);
# #

View File

@@ -1,8 +1,9 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import re, string, sys import re, string, sys
stops = set(open("../stop_words.txt").read().split(",") + list(string.ascii_lowercase)) 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] 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 = list(set(words))
unique_words.sort(lambda x, y: cmp(words.count(y), words.count(x))) 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]]) print("\n".join(["%s - %s" % (x, words.count(x)) for x in unique_words[:25]]))

View File

@@ -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? 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?

View File

@@ -2,10 +2,11 @@
# My golf score is slightly lower! # My golf score is slightly lower!
# Best wishes, Peter Norvig # Best wishes, Peter Norvig
from __future__ import print_function
import re, sys, collections import re, sys, collections
stopwords = set(open('../stop_words.txt').read().split(',')) stopwords = set(open('../stop_words.txt').read().split(','))
words = re.findall('[a-z]{2,}', open(sys.argv[1]).read().lower()) 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) counts = collections.Counter(w for w in words if w not in stopwords)
for (w, c) in counts.most_common(25): for (w, c) in counts.most_common(25):
print w, '-', c print(w, '-', c)

View File

@@ -1,6 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import heapq, re, sys import heapq, re, sys
words = re.findall("[a-z]{2,}", open(sys.argv[1]).read().lower()) 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): 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))

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import re, sys, operator import re, sys, operator
# Mileage may vary. If this crashes, make it lower # Mileage may vary. If this crashes, make it lower
@@ -29,7 +30,7 @@ def wf_print(wordfreq):
return return
else: else:
(w, c) = wordfreq[0] (w, c) = wordfreq[0]
print w, '-', c print(w, '-', c)
wf_print(wordfreq[1:]) wf_print(wordfreq[1:])
stop_words = set(open('../stop_words.txt').read().split(',')) stop_words = set(open('../stop_words.txt').read().split(','))

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, re, operator, string import sys, re, operator, string
# #
@@ -40,7 +41,7 @@ def sort(wf, func):
def print_text(word_freqs, func): def print_text(word_freqs, func):
for (w, c) in word_freqs[0:25]: for (w, c) in word_freqs[0:25]:
print w, "-", c print(w, "-", c)
func(None) func(None)
def no_op(func): def no_op(func):

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, re, operator, string import sys, re, operator, string
# #
@@ -13,7 +14,7 @@ class TFTheOne:
return self return self
def printme(self): def printme(self):
print self._value print(self._value)
# #
# The functions # The functions

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, re, operator, string import sys, re, operator, string
from abc import ABCMeta from abc import ABCMeta
@@ -73,7 +74,7 @@ class WordFrequencyController(TFExercise):
word_freqs = self._word_freq_manager.sorted() word_freqs = self._word_freq_manager.sorted()
for (w, c) in word_freqs[0:25]: for (w, c) in word_freqs[0:25]:
print w, ' - ', c print(w, ' - ', c)
# #
# The main function # The main function

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, re, operator, string import sys, re, operator, string
class DataStorageManager(): class DataStorageManager():
@@ -89,7 +90,7 @@ class WordFrequencyController():
word_freqs = self._word_freq_manager.dispatch(['sorted']) word_freqs = self._word_freq_manager.dispatch(['sorted'])
for (w, c) in word_freqs[0:25]: for (w, c) in word_freqs[0:25]:
print w, ' - ', c print(w, ' - ', c)
# #
# The main function # The main function

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, re, operator, string import sys, re, operator, string
# Auxiliary functions that can't be lambdas # Auxiliary functions that can't be lambdas
@@ -46,4 +47,4 @@ for w in data_storage_obj['words']():
word_freqs = word_freqs_obj['sorted']() word_freqs = word_freqs_obj['sorted']()
for (w, c) in word_freqs[0:25]: for (w, c) in word_freqs[0:25]:
print w, ' - ', c print(w, ' - ', c)

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import abc, sys, re, operator, string import abc, sys, re, operator, string
# #
@@ -97,7 +98,7 @@ class WordFrequencyController:
word_freqs = self._word_freq_counter.sorted() word_freqs = self._word_freq_counter.sorted()
for (w, c) in word_freqs[0:25]: for (w, c) in word_freqs[0:25]:
print w, ' - ', c print(w, ' - ', c)
# #
# The main function # The main function

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, re, operator, string import sys, re, operator, string
# #
@@ -89,7 +90,7 @@ class WordFrequencyCounter:
def __print_freqs(self): def __print_freqs(self):
word_freqs = sorted(self._word_freqs.iteritems(), key=operator.itemgetter(1), reverse=True) word_freqs = sorted(self._word_freqs.iteritems(), key=operator.itemgetter(1), reverse=True)
for (w, c) in word_freqs[0:25]: for (w, c) in word_freqs[0:25]:
print w, ' - ', c print(w, ' - ', c)
# #
# The main function # The main function

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, re, operator, string import sys, re, operator, string
# #
@@ -79,7 +80,7 @@ class WordFrequencyCounter:
def print_freqs(self, event): def print_freqs(self, event):
word_freqs = sorted(self._word_freqs.iteritems(), key=operator.itemgetter(1), reverse=True) word_freqs = sorted(self._word_freqs.iteritems(), key=operator.itemgetter(1), reverse=True)
for (w, c) in word_freqs[0:25]: for (w, c) in word_freqs[0:25]:
print w, ' - ', c print(w, ' - ', c)
class WordFrequencyApplication: class WordFrequencyApplication:
def __init__(self, event_manager): def __init__(self, event_manager):

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, re, operator, string, inspect import sys, re, operator, string, inspect
def read_stop_words(): def read_stop_words():
@@ -39,7 +40,7 @@ def sort(word_freq):
def main(): def main():
word_freqs = sort(frequencies(extract_words(sys.argv[1]))) word_freqs = sort(frequencies(extract_words(sys.argv[1])))
for (w, c) in word_freqs[0:25]: for (w, c) in word_freqs[0:25]:
print w, ' - ', c print(w, ' - ', c)
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, re, operator, string, os import sys, re, operator, string, os
# #
@@ -44,5 +45,5 @@ exec('sort = ' + sort_func)
word_freqs = locals()['sort'](locals()['frequencies'](locals()['extract_words'](filename))) word_freqs = locals()['sort'](locals()['frequencies'](locals()['extract_words'](filename)))
for (w, c) in word_freqs[0:25]: for (w, c) in word_freqs[0:25]:
print w, ' - ', c print(w, ' - ', c)

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, re, operator, string, time import sys, re, operator, string, time
# #
@@ -32,7 +33,7 @@ def profile(f):
start_time = time.time() start_time = time.time()
ret_value = f(*arg, **kw) ret_value = f(*arg, **kw)
elapsed = time.time() - start_time 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 ret_value
return profilewrapper return profilewrapper
@@ -40,10 +41,10 @@ def profile(f):
tracked_functions = [extract_words, frequencies, sort] tracked_functions = [extract_words, frequencies, sort]
# weaver # weaver
for func in tracked_functions: 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]))) word_freqs = sort(frequencies(extract_words(sys.argv[1])))
for (w, c) in word_freqs[0:25]: for (w, c) in word_freqs[0:25]:
print w, ' - ', c print(w, ' - ', c)

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, ConfigParser, imp import sys, ConfigParser, imp
def load_plugins(): def load_plugins():
@@ -14,5 +15,5 @@ load_plugins()
word_freqs = tffreqs.top25(tfwords.extract_words(sys.argv[1])) word_freqs = tffreqs.top25(tfwords.extract_words(sys.argv[1]))
for (w, c) in word_freqs: for (w, c) in word_freqs:
print w, ' - ', c print(w, ' - ', c)

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, re, operator, string, inspect import sys, re, operator, string, inspect
# #
@@ -12,7 +13,7 @@ def extract_words(path_to_file):
with open(path_to_file) as f: with open(path_to_file) as f:
str_data = f.read() str_data = f.read()
except IOError as e: 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 [] return []
pattern = re.compile('[\W_]+') pattern = re.compile('[\W_]+')
@@ -27,7 +28,7 @@ def remove_stop_words(word_list):
with open('../stop_words.txt') as f: with open('../stop_words.txt') as f:
stop_words = f.read().split(',') stop_words = f.read().split(',')
except IOError as e: 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 return word_list
stop_words.extend(list(string.ascii_lowercase)) 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)))) word_freqs = sort(frequencies(remove_stop_words(extract_words(filename))))
for tf in word_freqs[0:25]: for tf in word_freqs[0:25]:
print tf[0], ' - ', tf[1] print(tf[0], ' - ', tf[1])

View File

@@ -1,5 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, re, operator, string, traceback import sys, re, operator, string, traceback
# #
@@ -13,7 +14,7 @@ def extract_words(path_to_file):
with open(path_to_file) as f: with open(path_to_file) as f:
str_data = f.read() str_data = f.read()
except IOError as e: 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 raise e
pattern = re.compile('[\W_]+') pattern = re.compile('[\W_]+')
@@ -27,7 +28,7 @@ def remove_stop_words(word_list):
with open('../stop_words.txt') as f: with open('../stop_words.txt') as f:
stop_words = f.read().split(',') stop_words = f.read().split(',')
except IOError as e: 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 raise e
stop_words.extend(list(string.ascii_lowercase)) stop_words.extend(list(string.ascii_lowercase))
@@ -35,7 +36,7 @@ def remove_stop_words(word_list):
def frequencies(word_list): def frequencies(word_list):
assert(type(word_list) is list), "I need a 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 = {} word_freqs = {}
for w in word_list: for w in word_list:
@@ -47,12 +48,12 @@ def frequencies(word_list):
def sort(word_freq): def sort(word_freq):
assert(type(word_freq) is dict), "I need a dictionary!" 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: try:
return sorted(word_freq.iteritems(), key=operator.itemgetter(1), reverse=True) return sorted(word_freq.iteritems(), key=operator.itemgetter(1), reverse=True)
except Exception as e: except Exception as e:
print "Sorted threw {0}: {1}".format(e) print("Sorted threw {0}: {1}".format(e))
raise e raise e
# #
@@ -65,8 +66,8 @@ try:
assert(type(word_freqs) is list), "OMG! This is not a list!" assert(type(word_freqs) is list), "OMG! This is not a list!"
assert(len(word_freqs) > 25), "SRSLY? Less than 25 words!" assert(len(word_freqs) > 25), "SRSLY? Less than 25 words!"
for (w, c) in word_freqs[0:25]: for (w, c) in word_freqs[0:25]:
print w, ' - ', c 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() traceback.print_exc()

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, re, operator, string import sys, re, operator, string
# #
@@ -25,7 +26,7 @@ def remove_stop_words(word_list):
def frequencies(word_list): def frequencies(word_list):
assert(type(word_list) is list), "I need a list! I quit!" 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 = {} word_freqs = {}
for w in word_list: for w in word_list:
@@ -37,7 +38,7 @@ def frequencies(word_list):
def sort(word_freqs): def sort(word_freqs):
assert(type(word_freqs) is dict), "I need a dictionary! I quit!" 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) 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!" assert(len(word_freqs) > 25), "OMG! Less than 25 words! I QUIT!"
for tf in word_freqs[0:25]: for tf in word_freqs[0:25]:
print tf[0], ' - ', tf[1] print(tf[0], ' - ', tf[1])
except Exception as e: except Exception as e:
print "Something wrong: {0}".format(e) print("Something wrong: {0}".format(e))

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, re, operator, string import sys, re, operator, string
# #
@@ -21,9 +22,9 @@ class TFPassiveAggressive:
def printme(self): def printme(self):
if self._e == None: if self._e == None:
print self._value print(self._value)
else: else:
print self._e, " in ", self._offending_func.__name__ print(self._e, " in ", self._offending_func.__name__)
# #
# The functions # The functions
@@ -53,7 +54,7 @@ def remove_stop_words(word_list):
def frequencies(word_list): def frequencies(word_list):
assert(type(word_list) is list), "I need a list! I quit!" 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 = {} word_freqs = {}
for w in word_list: for w in word_list:
@@ -65,13 +66,13 @@ def frequencies(word_list):
def sort(word_freqs): def sort(word_freqs):
assert(type(word_freqs) is dict), "I need a dictionary! I quit!" 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) return sorted(word_freqs.iteritems(), key=operator.itemgetter(1), reverse=True)
def top25_freqs(word_freqs): def top25_freqs(word_freqs):
assert(type(word_freqs) is list), "I need a list! I quit!" 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 = "" top25 = ""
for tf in word_freqs[0:25]: for tf in word_freqs[0:25]:

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, re, operator, string, inspect import sys, re, operator, string, inspect
# #
@@ -11,7 +12,7 @@ class AcceptTypes():
def __call__(self, f): def __call__(self, f):
def wrapped_f(*args): def wrapped_f(*args):
for i in range(len(self._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])))) raise TypeError("Expecting %s got %s" % (str(self._args[i]), str(type(args[i]))))
return f(*args) return f(*args)
return wrapped_f return wrapped_f
@@ -45,5 +46,5 @@ def sort(word_freq):
word_freqs = sort(frequencies(extract_words(sys.argv[1]))) word_freqs = sort(frequencies(extract_words(sys.argv[1])))
for (w, c) in word_freqs[0:25]: for (w, c) in word_freqs[0:25]:
print w, ' - ', c print(w, ' - ', c)

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, re, operator, string import sys, re, operator, string
# #
@@ -19,7 +20,7 @@ class TFQuarantine:
value = lambda : None value = lambda : None
for func in self._funcs: for func in self._funcs:
value = func(guard_callable(value)) value = func(guard_callable(value))
print guard_callable(value) print(guard_callable(value))
# #
# The functions # The functions

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, re, string, sqlite3, os.path import sys, re, string, sqlite3, os.path
# #
@@ -66,4 +67,4 @@ with sqlite3.connect('tf.db') as connection:
for i in range(25): for i in range(25):
row = c.fetchone() row = c.fetchone()
if row != None: if row != None:
print row[0] + ' - ' + str(row[1]) print(row[0] + ' - ' + str(row[1]))

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, re, itertools, operator import sys, re, itertools, operator
# #
@@ -46,4 +47,4 @@ stop_words[0] = set(open('../stop_words.txt').read().split(','))
update() update()
for (w, c) in sorted_data[0][:25]: for (w, c) in sorted_data[0][:25]:
print w, '-', c print(w, '-', c)

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, operator, string import sys, operator, string
def characters(filename): def characters(filename):
@@ -42,7 +43,7 @@ def count_and_sort(filename):
# The main function # The main function
# #
for word_freqs in count_and_sort(sys.argv[1]): for word_freqs in count_and_sort(sys.argv[1]):
print "-----------------------------" print("-----------------------------")
for (w, c) in word_freqs[0:25]: for (w, c) in word_freqs[0:25]:
print w, ' - ', c print(w, ' - ', c)

View File

@@ -1,5 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, re, operator, string import sys, re, operator, string
from threading import Thread from threading import Thread
from Queue import Queue from Queue import Queue
@@ -114,7 +115,7 @@ class WordFrequencyController(ActiveWFObject):
def _display(self, message): def _display(self, message):
word_freqs = message[0] word_freqs = message[0]
for (w, f) in word_freqs[0:25]: for (w, f) in word_freqs[0:25]:
print w, ' - ', f print(w, ' - ', f)
send(self._storage_manager, ['die']) send(self._storage_manager, ['die'])
self._stop = True self._stop = True

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import re, sys, operator, Queue, threading import re, sys, operator, Queue, threading
# Two data spaces # Two data spaces
@@ -49,4 +50,4 @@ while not freq_space.empty():
word_freqs[k] = count word_freqs[k] = count
for (w, c) in sorted(word_freqs.iteritems(), key=operator.itemgetter(1), reverse=True)[:25]: for (w, c) in sorted(word_freqs.iteritems(), key=operator.itemgetter(1), reverse=True)[:25]:
print w, '-', c print(w, '-', c)

View File

@@ -1,11 +1,19 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, re, operator, string import sys, re, operator, string
from functools import reduce
try:
xrange # Python 2
except NameError:
xrange = range # Python 3
# #
# Functions for map reduce # Functions for map reduce
# #
def partition(data_str, nlines): def partition(data_str, nlines):
""" """
Partitions the input data_str (a big string) Partitions the input data_str (a big string)
into chunks of nlines. into chunks of nlines.
""" """
@@ -14,8 +22,8 @@ def partition(data_str, nlines):
yield '\n'.join(lines[i:i+nlines]) yield '\n'.join(lines[i:i+nlines])
def split_words(data_str): 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 one for each word in the input, so
[(w1, 1), (w2, 1), ..., (wn, 1)] [(w1, 1), (w2, 1), ..., (wn, 1)]
""" """
@@ -37,10 +45,10 @@ def split_words(data_str):
return result return result
def count_words(pairs_list_1, pairs_list_2): def count_words(pairs_list_1, pairs_list_2):
""" """
Takes two lists of pairs of the form Takes two lists of pairs of the form
[(w1, 1), ...] [(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 where frequency is the sum of all the reported occurrences
""" """
mapping = dict((k, v) for k, v in pairs_list_1) 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)) word_freqs = sort(reduce(count_words, splits))
for (w, c) in word_freqs[0:25]: for (w, c) in word_freqs[0:25]:
print w, ' - ', c print(w, ' - ', c)

View File

@@ -1,11 +1,19 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, re, operator, string import sys, re, operator, string
from functools import reduce
try:
xrange # Python 2
except NameError:
xrange = range # Python 3
# #
# Functions for map reduce # Functions for map reduce
# #
def partition(data_str, nlines): def partition(data_str, nlines):
""" """
Partitions the input data_str (a big string) Partitions the input data_str (a big string)
into chunks of nlines. into chunks of nlines.
""" """
@@ -14,8 +22,8 @@ def partition(data_str, nlines):
yield '\n'.join(lines[i:i+nlines]) yield '\n'.join(lines[i:i+nlines])
def split_words(data_str): 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 one for each word in the input, so
[(w1, 1), (w2, 1), ..., (wn, 1)] [(w1, 1), (w2, 1), ..., (wn, 1)]
""" """
@@ -38,14 +46,14 @@ def split_words(data_str):
def regroup(pairs_list): 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)],
[(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 corresponding list of pairs, so
{ w1 : [(w1, 1), (w1, 1)...], { w1 : [(w1, 1), (w1, 1)...],
w2 : [(w2, 1), (w2, 1)...], w2 : [(w2, 1), (w2, 1)...],
...} ...}
""" """
mapping = {} mapping = {}
@@ -56,11 +64,11 @@ def regroup(pairs_list):
else: else:
mapping[p[0]] = [p] mapping[p[0]] = [p]
return mapping return mapping
def count_words(mapping): def count_words(mapping):
""" """
Takes a mapping of the form (word, [(word, 1), (word, 1)...)]) 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 sum of all the reported occurrences
""" """
def add(x, y): def add(x, y):
@@ -87,5 +95,4 @@ splits_per_word = regroup(splits)
word_freqs = sort(map(count_words, splits_per_word.items())) word_freqs = sort(map(count_words, splits_per_word.items()))
for (w, c) in word_freqs[0:25]: for (w, c) in word_freqs[0:25]:
print w, ' - ', c print(w, ' - ', c)

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, operator, string, os, threading, re import sys, operator, string, os, threading, re
from util import getch, cls, get_input from util import getch, cls, get_input
from time import sleep from time import sleep
@@ -38,7 +39,7 @@ class FreqObserver(threading.Thread):
def refresh_screen(data): def refresh_screen(data):
# clear screen # clear screen
cls() cls()
print data print(data)
sys.stdout.flush() sys.stdout.flush()
data_str = "" data_str = ""
@@ -57,7 +58,7 @@ class WordsCounter:
for line in f: for line in f:
yield [w for w in re.findall('[a-z]{2,}', line.lower()) if w not in stopwords] 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() lock.acquire()
for w in words: for w in words:
self.freqs[w] = 1 if w not in self.freqs else self.freqs[w]+1 self.freqs[w] = 1 if w not in self.freqs else self.freqs[w]+1
@@ -66,8 +67,8 @@ class WordsCounter:
# #
# The controller # The controller
# #
print "Press space bar to fetch words from the file one by one" print("Press space bar to fetch words from the file one by one")
print "Press ESC to switch to automatic mode" print("Press ESC to switch to automatic mode")
model = WordsCounter() model = WordsCounter()
view = FreqObserver(model.freqs) view = FreqObserver(model.freqs)
with open(sys.argv[1]) as f: with open(sys.argv[1]) as f:

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, re, operator, collections import sys, re, operator, collections
class WordFrequenciesModel: class WordFrequenciesModel:
@@ -19,7 +20,7 @@ class WordFrequenciesModel:
for obs in self._observers: for obs in self._observers:
obs.render() obs.render()
except IOError: except IOError:
print "File not found" print("File not found")
self.freqs = {} self.freqs = {}
class WordFrequenciesView: class WordFrequenciesView:
@@ -30,7 +31,7 @@ class WordFrequenciesView:
def render(self): def render(self):
sorted_freqs = sorted(self._model.freqs.iteritems(), key=operator.itemgetter(1), reverse=True) sorted_freqs = sorted(self._model.freqs.iteritems(), key=operator.itemgetter(1), reverse=True)
for (w, c) in sorted_freqs[:25]: for (w, c) in sorted_freqs[:25]:
print w, '-', c print(w, '-', c)
class WordFrequencyController: class WordFrequencyController:
def __init__(self, model, view): def __init__(self, model, view):
@@ -39,7 +40,7 @@ class WordFrequencyController:
def run(self): def run(self):
self._model.update(sys.argv[1]) self._model.update(sys.argv[1])
while True: while True:
print "Next file: " print("Next file: ")
sys.stdout.flush() sys.stdout.flush()
filename = sys.stdin.readline().strip() filename = sys.stdin.readline().strip()
self._model.update(filename) self._model.update(filename)

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, re, operator, collections import sys, re, operator, collections
class WordFrequenciesModel: class WordFrequenciesModel:
@@ -14,7 +15,7 @@ class WordFrequenciesModel:
words = re.findall('[a-z]{2,}', open(path_to_file).read().lower()) 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) self.freqs = collections.Counter(w for w in words if w not in self.stopwords)
except IOError: except IOError:
print "File not found" print("File not found")
self.freqs = {} self.freqs = {}
class WordFrequenciesView: class WordFrequenciesView:
@@ -24,7 +25,7 @@ class WordFrequenciesView:
def render(self): def render(self):
sorted_freqs = sorted(self._model.freqs.iteritems(), key=operator.itemgetter(1), reverse=True) sorted_freqs = sorted(self._model.freqs.iteritems(), key=operator.itemgetter(1), reverse=True)
for (w, c) in sorted_freqs[0:25]: for (w, c) in sorted_freqs[0:25]:
print w, '-', c print(w, '-', c)
class WordFrequencyController: class WordFrequencyController:
def __init__(self, model, view): def __init__(self, model, view):
@@ -33,7 +34,7 @@ class WordFrequencyController:
def run(self): def run(self):
while True: while True:
print "Next file: " print("Next file: ")
sys.stdout.flush() sys.stdout.flush()
filename = sys.stdin.readline().strip() filename = sys.stdin.readline().strip()
self._model.update(filename) self._model.update(filename)

View File

@@ -1,4 +1,4 @@
import sys, os import os
# #
# getch in a platform-independent way # getch in a platform-independent way
@@ -65,11 +65,10 @@ def get_input():
if not interactive: if not interactive:
return True return True
while True: while True:
key = ord(getch()) key = ord(getch())
if key == 32: # space bar if key == 32: # space bar
return True return True
elif key == 27: # ESC elif key == 27: # ESC
interactive = False interactive = False
return True return True

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import re, string, sys import re, string, sys
with open("../stop_words.txt") as f: with open("../stop_words.txt") as f:
@@ -22,7 +23,7 @@ def quit_handler(args):
def upload_get_handler(args): def upload_get_handler(args):
return "Name of file to upload?", ["post", "file"] return "Name of file to upload?", ["post", "file"]
def upload_post_handler(args): def upload_post_handler(args):
def create_data(filename): def create_data(filename):
if filename in data: 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]: 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_freqs[w] = word_freqs.get(w, 0) + 1
word_freqsl = word_freqs.items() 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 data[filename] = word_freqsl
if args == None: if args == None:
@@ -49,7 +50,7 @@ def word_get_handler(args):
if word_index < len(data[filename]): if word_index < len(data[filename]):
return data[filename][word_index] return data[filename][word_index]
else: else:
return ("no more words", 0) return ("no more words", 0)
filename = args[0]; word_index = args[1] filename = args[0]; word_index = args[1]
word_info = get_word(filename, word_index) 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 += "\n\nWhat would you like to do next?"
rep += "\n1 - Quit" + "\n2 - Upload file" rep += "\n1 - Quit" + "\n2 - Upload file"
rep += "\n3 - See next most-frequently occurring word" rep += "\n3 - See next most-frequently occurring word"
links = {"1" : ["post", "execution", None], links = {"1" : ["post", "execution", None],
"2" : ["get", "file_form", None], "2" : ["get", "file_form", None],
"3" : ["get", "word", [filename, word_index+1]]} "3" : ["get", "word", [filename, word_index+1]]}
return rep, links return rep, links
# Handler registration # Handler registration
handlers = {"post_execution" : quit_handler, handlers = {"post_execution" : quit_handler,
"get_default" : default_get_handler, "get_default" : default_get_handler,
"get_file_form" : upload_get_handler, "get_file_form" : upload_get_handler,
"post_file" : upload_post_handler, "post_file" : upload_post_handler,
"get_word" : word_get_handler } "get_word" : word_get_handler }
# The "server" core # The "server" core
@@ -81,7 +82,7 @@ def handle_request(verb, uri, args):
# A very simple client "browser" # A very simple client "browser"
def render_and_get_input(state_representation, links): def render_and_get_input(state_representation, links):
print state_representation print(state_representation)
sys.stdout.flush() sys.stdout.flush()
if type(links) is dict: # many possible next states if type(links) is dict: # many possible next states
input = sys.stdin.readline().strip() input = sys.stdin.readline().strip()

View File

@@ -1,5 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys, re, operator, string, inspect import sys, re, operator, string, inspect
# Reusing the defensive style program to illustrate this # 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: with open(path_to_file) as f:
str_data = f.read() str_data = f.read()
except IOError as e: 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 fail = True
if not fail: if not fail:
@@ -31,7 +32,7 @@ def extract_words(path_to_file):
with open('../stop_words.txt') as f: with open('../stop_words.txt') as f:
stop_words = f.read().split(',') stop_words = f.read().split(',')
except IOError as e: 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 fail = True
if not fail: if not fail:
@@ -44,7 +45,7 @@ def frequencies(word_list):
Takes a list of words and returns a dictionary associating Takes a list of words and returns a dictionary associating
words with frequencies of occurrence 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 = {} word_freqs = {}
for w in word_list: for w in word_list:
if w in word_freqs: if w in word_freqs:
@@ -61,7 +62,7 @@ def sort(word_freq):
and returns a list of pairs where the entries are and returns a list of pairs where the entries are
sorted by frequency 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) return sorted(word_freq.iteritems(), key=operator.itemgetter(1), reverse=True)
else: else:
return [] return []
@@ -73,5 +74,5 @@ filename = sys.argv[1] if len(sys.argv) > 1 else "../input.txt"
word_freqs = sort(frequencies(extract_words(filename))) word_freqs = sort(frequencies(extract_words(filename)))
for tf in word_freqs[0:25]: for tf in word_freqs[0:25]:
print tf[0], ' - ', tf[1] print(tf[0], ' - ', tf[1])