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

View File

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

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)

View File

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