From e95b2bf71a0060f71ff6d72271f36297c4a02fa6 Mon Sep 17 00:00:00 2001 From: David Heyman Date: Sat, 13 Aug 2016 22:25:34 -0400 Subject: [PATCH] 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. --- lib/datalib.py | 52 ++++++++++++++++++++++------------------------ lib/transforms.py | 2 +- lib/utils.py | 32 +++++++++++++++------------- scripts/keydiff.py | 6 +++--- scripts/ngrams.py | 2 +- scripts/sanity.py | 2 +- 6 files changed, 49 insertions(+), 47 deletions(-) diff --git a/lib/datalib.py b/lib/datalib.py index 8613ad9..71ae304 100644 --- a/lib/datalib.py +++ b/lib/datalib.py @@ -173,8 +173,7 @@ class Datamine: str(len(self.by_type)) + ' combinations') print('Breakdown by type:') d = sorted(self.by_type_inclusive, - lambda x, y: cmp(len(self.by_type_inclusive[x]), len( - self.by_type_inclusive[y])), + lambda x: len(self.by_type_inclusive[x]), reverse=True) rows = [[k for k in d[:hsize]]] rows += [[len(self.by_type_inclusive[k]) for k in rows[0]]] @@ -183,16 +182,16 @@ class Datamine: print((str(len(self.by_subtype_inclusive)) + ' unique subtypes, ' + str(len(self.by_subtype)) + ' combinations')) print('-- Popular subtypes: --') - d = sorted(self.by_subtype_inclusive, - lambda x,y: cmp(len(self.by_subtype_inclusive[x]), len(self.by_subtype_inclusive[y])), - reverse = True) + d = sorted(self.by_subtype_inclusive, + lambda x: len(self.by_subtype_inclusive[x]), + reverse=True) rows = [] for k in d[0:vsize]: rows += [[k, len(self.by_subtype_inclusive[k])]] printrows(padrows(rows)) print('-- Top combinations: --') - d = sorted(self.by_subtype, - lambda x,y: cmp(len(self.by_subtype[x]), len(self.by_subtype[y])), + d = sorted(self.by_subtype, + lambda x: len(self.by_subtype[x]), reverse = True) rows = [] for k in d[0:vsize]: @@ -203,8 +202,7 @@ class Datamine: + str(len(self.by_supertype)) + ' combinations')) print('Breakdown by supertype:') d = sorted(self.by_supertype_inclusive, - lambda x, y: cmp(len(self.by_supertype_inclusive[x]), len( - self.by_supertype_inclusive[y])), + lambda x: len(self.by_supertype_inclusive[x]), reverse=True) rows = [[k for k in d[:hsize]]] rows += [[len(self.by_supertype_inclusive[k]) for k in rows[0]]] @@ -218,8 +216,8 @@ class Datamine: rows += [[len(self.by_cmc[k]) for k in rows[0]]] printrows(padrows(rows)) print('-- Popular mana costs: --') - d = sorted(self.by_cost, - lambda x,y: cmp(len(self.by_cost[x]), len(self.by_cost[y])), + d = sorted(self.by_cost, + lambda x: len(self.by_cost[x]), reverse = True) rows = [] for k in d[0:vsize]: @@ -231,8 +229,8 @@ class Datamine: print(('Largest power: ' + str(max(list(map(len, self.by_power))) - 1) + ', largest toughness: ' + str(max(list(map(len, self.by_toughness))) - 1))) print('-- Popular p/t values: --') - d = sorted(self.by_pt, - lambda x,y: cmp(len(self.by_pt[x]), len(self.by_pt[y])), + d = sorted(self.by_pt, + lambda x: len(self.by_pt[x]), reverse = True) rows = [] for k in d[0:vsize]: @@ -240,8 +238,8 @@ class Datamine: printrows(padrows(rows)) print('--------------------') print('Loyalty values:') - d = sorted(self.by_loyalty, - lambda x,y: cmp(len(self.by_loyalty[x]), len(self.by_loyalty[y])), + d = sorted(self.by_loyalty, + lambda x: len(self.by_loyalty[x]), reverse = True) rows = [] for k in d[0:vsize]: @@ -254,8 +252,8 @@ class Datamine: print(('Card text ranges from ' + str(min(self.by_textlines)) + ' to ' + str(max(self.by_textlines)) + ' lines')) print('-- Line counts by frequency: --') - d = sorted(self.by_textlines, - lambda x,y: cmp(len(self.by_textlines[x]), len(self.by_textlines[y])), + d = sorted(self.by_textlines, + lambda x: len(self.by_textlines[x]), reverse = True) rows = [] for k in d[0:vsize]: @@ -275,17 +273,17 @@ class Datamine: print('********************') if len(self.by_name) > 0: scardname = sorted(self.by_name, - lambda x, y: cmp(len(x), len(y)), + len, reverse=False)[0] print('Shortest Cardname: (' + str(len(scardname)) + ')') print(' ' + scardname) lcardname = sorted(self.by_name, - lambda x, y: cmp(len(x), len(y)), + len, reverse=True)[0] print('Longest Cardname: (' + str(len(lcardname)) + ')') print(' ' + lcardname) - d = sorted(self.by_name, - lambda x,y: cmp(len(self.by_name[x]), len(self.by_name[y])), + d = sorted(self.by_name, + lambda x: len(self.by_name[x]), reverse = True) rows = [] for k in d[0:vsize]: @@ -301,7 +299,7 @@ class Datamine: print('--------------------') if len(self.by_type) > 0: ltypes = sorted(self.by_type, - lambda x, y: cmp(len(x), len(y)), + len, reverse=True)[0] print('Longest card type: (' + str(len(ltypes)) + ')') print(' ' + ltypes) @@ -309,7 +307,7 @@ class Datamine: print('No cards indexed by type?') if len(self.by_subtype) > 0: lsubtypes = sorted(self.by_subtype, - lambda x, y: cmp(len(x), len(y)), + len, reverse=True)[0] print('Longest subtype: (' + str(len(lsubtypes)) + ')') print(' ' + lsubtypes) @@ -317,7 +315,7 @@ class Datamine: print('No cards indexed by subtype?') if len(self.by_supertype) > 0: lsupertypes = sorted(self.by_supertype, - lambda x, y: cmp(len(x), len(y)), + len, reverse=True)[0] print('Longest supertype: (' + str(len(lsupertypes)) + ')') print(' ' + lsupertypes) @@ -326,7 +324,7 @@ class Datamine: print('--------------------') if len(self.by_cost) > 0: lcost = sorted(self.by_cost, - lambda x, y: cmp(len(x), len(y)), + len, reverse=True)[0] print('Longest mana cost: (' + str(len(lcost)) + ')') print(' ' + utils.from_mana(lcost)) @@ -343,7 +341,7 @@ class Datamine: print('--------------------') if len(self.by_power) > 0: lpower = sorted(self.by_power, - lambda x, y: cmp(len(x), len(y)), + len, reverse=True)[0] print('Largest creature power: ' + utils.from_unary(lpower)) print('\n' + plimit(self.by_power[lpower][0].encode()) + '\n') @@ -351,7 +349,7 @@ class Datamine: print('No cards indexed by power?') if len(self.by_toughness) > 0: ltoughness = sorted(self.by_toughness, - lambda x, y: cmp(len(x), len(y)), + len, reverse=True)[0] print('Largest creature toughness: ' + utils.from_unary(ltoughness)) diff --git a/lib/transforms.py b/lib/transforms.py index cac46c4..b93b82c 100644 --- a/lib/transforms.py +++ b/lib/transforms.py @@ -601,7 +601,7 @@ def text_unpass_1_choice(s, delimit = False): choice_regex = (re.escape(choice_open_delimiter) + re.escape(unary_marker) + r'.*' + re.escape(bullet_marker) + r'.*' + re.escape(choice_close_delimiter)) 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) countfrag = fragments[0] optfrags = fragments[1:] diff --git a/lib/utils.py b/lib/utils.py index a209a69..611577b 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -114,7 +114,7 @@ unary_exceptions = config.unary_exceptions def to_unary(s, warn = False): numbers = re.findall(r'[0123456789]+', s) # 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) if i in unary_exceptions: s = s.replace(n, unary_exceptions[i]) @@ -130,7 +130,7 @@ def to_unary(s, warn = False): def from_unary(s): numbers = re.findall(re.escape(unary_marker + unary_counter) + '*', s) # 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) s = s.replace(n, str(i)) return s @@ -381,16 +381,18 @@ mana_unary_regex = (re.escape(mana_json_open_delimiter) + number_unary_regex def mana_translate(jmanastr): manastr = jmanastr 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) - i = (len(ns[0]) - len(unary_marker)) / len(unary_counter) - manastr = manastr.replace(n, mana_unary_marker + mana_unary_counter * i) + i = (len(ns[0]) - len(unary_marker)) // len(unary_counter) + manastr = manastr.replace( + n, mana_unary_marker + mana_unary_counter * i) 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) i = int(ns[0]) - manastr = manastr.replace(n, mana_unary_marker + mana_unary_counter * i) - for jsym in sorted(mana_symall_jdecode, lambda x,y: cmp(len(x), len(y)), reverse = True): + manastr = manastr.replace( + n, mana_unary_marker + mana_unary_counter * i) + for jsym in sorted(mana_symall_jdecode, len, reverse=True): if jsym in manastr: manastr = manastr.replace(jsym, mana_encode_direct(jsym)) 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 def to_mana(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())) return s -def from_mana(s, for_forum = False): + +def from_mana(s, for_forum=False): 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)) return s @@ -504,13 +507,14 @@ symbol_regex = '[' + tap_marker + untap_marker + ']' def to_symbols(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]) 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) - #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 replace it with... for symstr in set(symstrs): diff --git a/scripts/keydiff.py b/scripts/keydiff.py index 28b7c0f..ccec1a5 100755 --- a/scripts/keydiff.py +++ b/scripts/keydiff.py @@ -50,18 +50,18 @@ def main(fname1, fname2, verbose = True): ratios[k] = float(v2 * tot1) / float(v1 * tot2) 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]) + '/' + str(d1[k]) + ' (' + str(ratios[k]) + ')') print('') 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('') 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('') diff --git a/scripts/ngrams.py b/scripts/ngrams.py index c8cd466..e3e317f 100755 --- a/scripts/ngrams.py +++ b/scripts/ngrams.py @@ -95,7 +95,7 @@ def main(fname, oname, gmin = 2, gmax = 8, nltk = False, sep = False, verbose = with open(oname_full, 'wt') as f: for ngram in sorted(gramdict, - lambda x,y: cmp(gramdict[x], gramdict[y]), + lambda x: gramdict[x], reverse = True): f.write((ngram + ': ' + str(gramdict[ngram]) + '\n').encode('utf-8')) diff --git a/scripts/sanity.py b/scripts/sanity.py index 56e0bf0..a81b6f8 100755 --- a/scripts/sanity.py +++ b/scripts/sanity.py @@ -108,7 +108,7 @@ def check_vocab(fname): else: 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))) n = 3