Fix python 2-3 incompatibilities not got by 2to3.

Don't think I missed any here.
Did all flagged by python2 -3, and then did another pass.
This commit is contained in:
David Heyman 2016-08-13 22:25:34 -04:00
parent 2f95b79f10
commit e95b2bf71a
6 changed files with 49 additions and 47 deletions

View file

@ -173,8 +173,7 @@ class Datamine:
str(len(self.by_type)) + ' combinations') str(len(self.by_type)) + ' combinations')
print('Breakdown by type:') print('Breakdown by type:')
d = sorted(self.by_type_inclusive, d = sorted(self.by_type_inclusive,
lambda x, y: cmp(len(self.by_type_inclusive[x]), len( lambda x: len(self.by_type_inclusive[x]),
self.by_type_inclusive[y])),
reverse=True) reverse=True)
rows = [[k for k in d[:hsize]]] rows = [[k for k in d[:hsize]]]
rows += [[len(self.by_type_inclusive[k]) for k in rows[0]]] rows += [[len(self.by_type_inclusive[k]) for k in rows[0]]]
@ -184,15 +183,15 @@ class Datamine:
+ str(len(self.by_subtype)) + ' combinations')) + str(len(self.by_subtype)) + ' combinations'))
print('-- Popular subtypes: --') print('-- Popular subtypes: --')
d = sorted(self.by_subtype_inclusive, d = sorted(self.by_subtype_inclusive,
lambda x,y: cmp(len(self.by_subtype_inclusive[x]), len(self.by_subtype_inclusive[y])), lambda x: len(self.by_subtype_inclusive[x]),
reverse = True) reverse=True)
rows = [] rows = []
for k in d[0:vsize]: for k in d[0:vsize]:
rows += [[k, len(self.by_subtype_inclusive[k])]] rows += [[k, len(self.by_subtype_inclusive[k])]]
printrows(padrows(rows)) printrows(padrows(rows))
print('-- Top combinations: --') print('-- Top combinations: --')
d = sorted(self.by_subtype, d = sorted(self.by_subtype,
lambda x,y: cmp(len(self.by_subtype[x]), len(self.by_subtype[y])), lambda x: len(self.by_subtype[x]),
reverse = True) reverse = True)
rows = [] rows = []
for k in d[0:vsize]: for k in d[0:vsize]:
@ -203,8 +202,7 @@ class Datamine:
+ str(len(self.by_supertype)) + ' combinations')) + str(len(self.by_supertype)) + ' combinations'))
print('Breakdown by supertype:') print('Breakdown by supertype:')
d = sorted(self.by_supertype_inclusive, d = sorted(self.by_supertype_inclusive,
lambda x, y: cmp(len(self.by_supertype_inclusive[x]), len( lambda x: len(self.by_supertype_inclusive[x]),
self.by_supertype_inclusive[y])),
reverse=True) reverse=True)
rows = [[k for k in d[:hsize]]] rows = [[k for k in d[:hsize]]]
rows += [[len(self.by_supertype_inclusive[k]) for k in rows[0]]] rows += [[len(self.by_supertype_inclusive[k]) for k in rows[0]]]
@ -219,7 +217,7 @@ class Datamine:
printrows(padrows(rows)) printrows(padrows(rows))
print('-- Popular mana costs: --') print('-- Popular mana costs: --')
d = sorted(self.by_cost, d = sorted(self.by_cost,
lambda x,y: cmp(len(self.by_cost[x]), len(self.by_cost[y])), lambda x: len(self.by_cost[x]),
reverse = True) reverse = True)
rows = [] rows = []
for k in d[0:vsize]: for k in d[0:vsize]:
@ -232,7 +230,7 @@ class Datamine:
', largest toughness: ' + str(max(list(map(len, self.by_toughness))) - 1))) ', largest toughness: ' + str(max(list(map(len, self.by_toughness))) - 1)))
print('-- Popular p/t values: --') print('-- Popular p/t values: --')
d = sorted(self.by_pt, d = sorted(self.by_pt,
lambda x,y: cmp(len(self.by_pt[x]), len(self.by_pt[y])), lambda x: len(self.by_pt[x]),
reverse = True) reverse = True)
rows = [] rows = []
for k in d[0:vsize]: for k in d[0:vsize]:
@ -241,7 +239,7 @@ class Datamine:
print('--------------------') print('--------------------')
print('Loyalty values:') print('Loyalty values:')
d = sorted(self.by_loyalty, d = sorted(self.by_loyalty,
lambda x,y: cmp(len(self.by_loyalty[x]), len(self.by_loyalty[y])), lambda x: len(self.by_loyalty[x]),
reverse = True) reverse = True)
rows = [] rows = []
for k in d[0:vsize]: for k in d[0:vsize]:
@ -255,7 +253,7 @@ class Datamine:
+ str(max(self.by_textlines)) + ' lines')) + str(max(self.by_textlines)) + ' lines'))
print('-- Line counts by frequency: --') print('-- Line counts by frequency: --')
d = sorted(self.by_textlines, d = sorted(self.by_textlines,
lambda x,y: cmp(len(self.by_textlines[x]), len(self.by_textlines[y])), lambda x: len(self.by_textlines[x]),
reverse = True) reverse = True)
rows = [] rows = []
for k in d[0:vsize]: for k in d[0:vsize]:
@ -275,17 +273,17 @@ class Datamine:
print('********************') print('********************')
if len(self.by_name) > 0: if len(self.by_name) > 0:
scardname = sorted(self.by_name, scardname = sorted(self.by_name,
lambda x, y: cmp(len(x), len(y)), len,
reverse=False)[0] reverse=False)[0]
print('Shortest Cardname: (' + str(len(scardname)) + ')') print('Shortest Cardname: (' + str(len(scardname)) + ')')
print(' ' + scardname) print(' ' + scardname)
lcardname = sorted(self.by_name, lcardname = sorted(self.by_name,
lambda x, y: cmp(len(x), len(y)), len,
reverse=True)[0] reverse=True)[0]
print('Longest Cardname: (' + str(len(lcardname)) + ')') print('Longest Cardname: (' + str(len(lcardname)) + ')')
print(' ' + lcardname) print(' ' + lcardname)
d = sorted(self.by_name, d = sorted(self.by_name,
lambda x,y: cmp(len(self.by_name[x]), len(self.by_name[y])), lambda x: len(self.by_name[x]),
reverse = True) reverse = True)
rows = [] rows = []
for k in d[0:vsize]: for k in d[0:vsize]:
@ -301,7 +299,7 @@ class Datamine:
print('--------------------') print('--------------------')
if len(self.by_type) > 0: if len(self.by_type) > 0:
ltypes = sorted(self.by_type, ltypes = sorted(self.by_type,
lambda x, y: cmp(len(x), len(y)), len,
reverse=True)[0] reverse=True)[0]
print('Longest card type: (' + str(len(ltypes)) + ')') print('Longest card type: (' + str(len(ltypes)) + ')')
print(' ' + ltypes) print(' ' + ltypes)
@ -309,7 +307,7 @@ class Datamine:
print('No cards indexed by type?') print('No cards indexed by type?')
if len(self.by_subtype) > 0: if len(self.by_subtype) > 0:
lsubtypes = sorted(self.by_subtype, lsubtypes = sorted(self.by_subtype,
lambda x, y: cmp(len(x), len(y)), len,
reverse=True)[0] reverse=True)[0]
print('Longest subtype: (' + str(len(lsubtypes)) + ')') print('Longest subtype: (' + str(len(lsubtypes)) + ')')
print(' ' + lsubtypes) print(' ' + lsubtypes)
@ -317,7 +315,7 @@ class Datamine:
print('No cards indexed by subtype?') print('No cards indexed by subtype?')
if len(self.by_supertype) > 0: if len(self.by_supertype) > 0:
lsupertypes = sorted(self.by_supertype, lsupertypes = sorted(self.by_supertype,
lambda x, y: cmp(len(x), len(y)), len,
reverse=True)[0] reverse=True)[0]
print('Longest supertype: (' + str(len(lsupertypes)) + ')') print('Longest supertype: (' + str(len(lsupertypes)) + ')')
print(' ' + lsupertypes) print(' ' + lsupertypes)
@ -326,7 +324,7 @@ class Datamine:
print('--------------------') print('--------------------')
if len(self.by_cost) > 0: if len(self.by_cost) > 0:
lcost = sorted(self.by_cost, lcost = sorted(self.by_cost,
lambda x, y: cmp(len(x), len(y)), len,
reverse=True)[0] reverse=True)[0]
print('Longest mana cost: (' + str(len(lcost)) + ')') print('Longest mana cost: (' + str(len(lcost)) + ')')
print(' ' + utils.from_mana(lcost)) print(' ' + utils.from_mana(lcost))
@ -343,7 +341,7 @@ class Datamine:
print('--------------------') print('--------------------')
if len(self.by_power) > 0: if len(self.by_power) > 0:
lpower = sorted(self.by_power, lpower = sorted(self.by_power,
lambda x, y: cmp(len(x), len(y)), len,
reverse=True)[0] reverse=True)[0]
print('Largest creature power: ' + utils.from_unary(lpower)) print('Largest creature power: ' + utils.from_unary(lpower))
print('\n' + plimit(self.by_power[lpower][0].encode()) + '\n') print('\n' + plimit(self.by_power[lpower][0].encode()) + '\n')
@ -351,7 +349,7 @@ class Datamine:
print('No cards indexed by power?') print('No cards indexed by power?')
if len(self.by_toughness) > 0: if len(self.by_toughness) > 0:
ltoughness = sorted(self.by_toughness, ltoughness = sorted(self.by_toughness,
lambda x, y: cmp(len(x), len(y)), len,
reverse=True)[0] reverse=True)[0]
print('Largest creature toughness: ' + print('Largest creature toughness: ' +
utils.from_unary(ltoughness)) utils.from_unary(ltoughness))

View file

@ -601,7 +601,7 @@ def text_unpass_1_choice(s, delimit = False):
choice_regex = (re.escape(choice_open_delimiter) + re.escape(unary_marker) choice_regex = (re.escape(choice_open_delimiter) + re.escape(unary_marker)
+ r'.*' + re.escape(bullet_marker) + r'.*' + re.escape(choice_close_delimiter)) + r'.*' + re.escape(bullet_marker) + r'.*' + re.escape(choice_close_delimiter))
choices = re.findall(choice_regex, s) choices = re.findall(choice_regex, s)
for choice in sorted(choices, lambda x,y: cmp(len(x), len(y)), reverse = True): for choice in sorted(choices, len, reverse=True):
fragments = choice[1:-1].split(bullet_marker) fragments = choice[1:-1].split(bullet_marker)
countfrag = fragments[0] countfrag = fragments[0]
optfrags = fragments[1:] optfrags = fragments[1:]

View file

@ -114,7 +114,7 @@ unary_exceptions = config.unary_exceptions
def to_unary(s, warn = False): def to_unary(s, warn = False):
numbers = re.findall(r'[0123456789]+', s) numbers = re.findall(r'[0123456789]+', s)
# replace largest first to avoid accidentally replacing shared substrings # replace largest first to avoid accidentally replacing shared substrings
for n in sorted(numbers, cmp = lambda x,y: cmp(int(x), int(y)), reverse = True): for n in sorted(numbers, key=int, reverse=True):
i = int(n) i = int(n)
if i in unary_exceptions: if i in unary_exceptions:
s = s.replace(n, unary_exceptions[i]) s = s.replace(n, unary_exceptions[i])
@ -130,7 +130,7 @@ def to_unary(s, warn = False):
def from_unary(s): def from_unary(s):
numbers = re.findall(re.escape(unary_marker + unary_counter) + '*', s) numbers = re.findall(re.escape(unary_marker + unary_counter) + '*', s)
# again, largest first so we don't replace substrings and break everything # again, largest first so we don't replace substrings and break everything
for n in sorted(numbers, cmp = lambda x,y: cmp(len(x), len(y)), reverse = True): for n in sorted(numbers, len, reverse=True):
i = (len(n) - len(unary_marker)) / len(unary_counter) i = (len(n) - len(unary_marker)) / len(unary_counter)
s = s.replace(n, str(i)) s = s.replace(n, str(i))
return s return s
@ -381,16 +381,18 @@ mana_unary_regex = (re.escape(mana_json_open_delimiter) + number_unary_regex
def mana_translate(jmanastr): def mana_translate(jmanastr):
manastr = jmanastr manastr = jmanastr
for n in sorted(re.findall(mana_unary_regex, manastr), for n in sorted(re.findall(mana_unary_regex, manastr),
lambda x,y: cmp(len(x), len(y)), reverse = True): len, reverse=True):
ns = re.findall(number_unary_regex, n) ns = re.findall(number_unary_regex, n)
i = (len(ns[0]) - len(unary_marker)) / len(unary_counter) i = (len(ns[0]) - len(unary_marker)) // len(unary_counter)
manastr = manastr.replace(n, mana_unary_marker + mana_unary_counter * i) manastr = manastr.replace(
n, mana_unary_marker + mana_unary_counter * i)
for n in sorted(re.findall(mana_decimal_regex, manastr), for n in sorted(re.findall(mana_decimal_regex, manastr),
lambda x,y: cmp(len(x), len(y)), reverse = True): len, reverse=True):
ns = re.findall(number_decimal_regex, n) ns = re.findall(number_decimal_regex, n)
i = int(ns[0]) i = int(ns[0])
manastr = manastr.replace(n, mana_unary_marker + mana_unary_counter * i) manastr = manastr.replace(
for jsym in sorted(mana_symall_jdecode, lambda x,y: cmp(len(x), len(y)), reverse = True): n, mana_unary_marker + mana_unary_counter * i)
for jsym in sorted(mana_symall_jdecode, len, reverse=True):
if jsym in manastr: if jsym in manastr:
manastr = manastr.replace(jsym, mana_encode_direct(jsym)) manastr = manastr.replace(jsym, mana_encode_direct(jsym))
return mana_open_delimiter + manastr + mana_close_delimiter return mana_open_delimiter + manastr + mana_close_delimiter
@ -458,13 +460,14 @@ def mana_untranslate(manastr, for_forum = False, for_html = False):
# notice the calls to .upper(), this way we recognize lowercase symbols as well just in case # notice the calls to .upper(), this way we recognize lowercase symbols as well just in case
def to_mana(s): def to_mana(s):
jmanastrs = re.findall(mana_json_regex, s) jmanastrs = re.findall(mana_json_regex, s)
for jmanastr in sorted(jmanastrs, lambda x,y: cmp(len(x), len(y)), reverse = True): for jmanastr in sorted(jmanastrs, len, reverse=True):
s = s.replace(jmanastr, mana_translate(jmanastr.upper())) s = s.replace(jmanastr, mana_translate(jmanastr.upper()))
return s return s
def from_mana(s, for_forum = False):
def from_mana(s, for_forum=False):
manastrs = re.findall(mana_regex, s) manastrs = re.findall(mana_regex, s)
for manastr in sorted(manastrs, lambda x,y: cmp(len(x), len(y)), reverse = True): for manastr in sorted(manastrs, len, reverse=True):
s = s.replace(manastr, mana_untranslate(manastr.upper(), for_forum = for_forum)) s = s.replace(manastr, mana_untranslate(manastr.upper(), for_forum = for_forum))
return s return s
@ -504,13 +507,14 @@ symbol_regex = '[' + tap_marker + untap_marker + ']'
def to_symbols(s): def to_symbols(s):
jsymstrs = re.findall(json_symbol_regex, s) jsymstrs = re.findall(json_symbol_regex, s)
for jsymstr in sorted(jsymstrs, lambda x,y: cmp(len(x), len(y)), reverse = True): for jsymstr in sorted(jsymstrs, len, reverse=True):
s = s.replace(jsymstr, json_symbol_trans[jsymstr]) s = s.replace(jsymstr, json_symbol_trans[jsymstr])
return s return s
def from_symbols(s, for_forum = False, for_html = False):
def from_symbols(s, for_forum=False, for_html=False):
symstrs = re.findall(symbol_regex, s) symstrs = re.findall(symbol_regex, s)
#for symstr in sorted(symstrs, lambda x,y: cmp(len(x), len(y)), reverse = True): #for symstr in sorted(symstrs, len, reverse = True):
# We have to do the right thing here, because the thing we replace exists in the thing # We have to do the right thing here, because the thing we replace exists in the thing
# we replace it with... # we replace it with...
for symstr in set(symstrs): for symstr in set(symstrs):

View file

@ -50,18 +50,18 @@ def main(fname1, fname2, verbose = True):
ratios[k] = float(v2 * tot1) / float(v1 * tot2) ratios[k] = float(v2 * tot1) / float(v1 * tot2)
print('shared: ' + str(len(ratios))) print('shared: ' + str(len(ratios)))
for k in sorted(ratios, lambda x, y: cmp(d2[x], d2[y]), reverse=True): for k in sorted(ratios, lambda x: d2[x], reverse=True):
print(' ' + k + ': ' + str(d2[k]) + '/' + print(' ' + k + ': ' + str(d2[k]) + '/' +
str(d1[k]) + ' (' + str(ratios[k]) + ')') str(d1[k]) + ' (' + str(ratios[k]) + ')')
print('') print('')
print('1 only: ' + str(len(only_1))) print('1 only: ' + str(len(only_1)))
for k in sorted(only_1, lambda x, y: cmp(d1[x], d1[y]), reverse=True): for k in sorted(only_1, lambda x: d1[x], reverse=True):
print(' ' + k + ': ' + str(d1[k])) print(' ' + k + ': ' + str(d1[k]))
print('') print('')
print('2 only: ' + str(len(only_2))) print('2 only: ' + str(len(only_2)))
for k in sorted(only_2, lambda x, y: cmp(d2[x], d2[y]), reverse=True): for k in sorted(only_2, lambda x: d2[x], reverse=True):
print(' ' + k + ': ' + str(d2[k])) print(' ' + k + ': ' + str(d2[k]))
print('') print('')

View file

@ -95,7 +95,7 @@ def main(fname, oname, gmin = 2, gmax = 8, nltk = False, sep = False, verbose =
with open(oname_full, 'wt') as f: with open(oname_full, 'wt') as f:
for ngram in sorted(gramdict, for ngram in sorted(gramdict,
lambda x,y: cmp(gramdict[x], gramdict[y]), lambda x: gramdict[x],
reverse = True): reverse = True):
f.write((ngram + ': ' + str(gramdict[ngram]) + '\n').encode('utf-8')) f.write((ngram + ': ' + str(gramdict[ngram]) + '\n').encode('utf-8'))

View file

@ -108,7 +108,7 @@ def check_vocab(fname):
else: else:
vocab[word] += 1 vocab[word] += 1
for word in sorted(vocab, lambda x,y: cmp(vocab[x], vocab[y]), reverse = True): for word in sorted(vocab, lambda x: vocab[x], reverse=True):
print(('{:8d} : {:s}'.format(vocab[word], word))) print(('{:8d} : {:s}'.format(vocab[word], word)))
n = 3 n = 3