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

View File

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