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,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)