Skip to content

Commit 3ad6e01

Browse files
Port codebase to Python3 (smogon#2)
* Make script compatible with Python 3 * Remove Stats/ dir * Remove superfluous encode stuff * Remove orjson * Catch cases where trainer names have Unicode * Remove Python 2 bug * Convert `json.loads(file.read())` to `json.load(file)` * Clarifying why we have extra encodes and decodes * Add correct file opening options * Revert file context code; will just be done later * Fix variable names * Update batchLogReader.py * Update environment.yml Co-authored-by: Christopher Monsanto <[email protected]>
1 parent c3ced17 commit 3ad6e01

20 files changed

+166
-164
lines changed

MegaCounter.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
from TA import megas
66
from common import keyify
77

8-
stats=json.load(open(sys.argv[1]))
8+
file = open(sys.argv[1])
9+
stats=json.load(file)
10+
file.close()
911

1012
megastats=[]
1113
total=0
@@ -33,5 +35,5 @@
3335

3436
megastats=sorted(megastats, key=lambda megastats:-megastats[1])
3537
for mega in megastats:
36-
print "%-18s%8.5f%%" % (mega[0],600.0*mega[1]/total)
38+
print("%-18s%8.5f%%" % (mega[0],600.0*mega[1]/total))
3739

PS-Extractor.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
used on Pokemon Showdown and pull out the data that's needed for the scripts."""
55

66
import json
7-
import cPickle as pickle
7+
import pickle
88
from onix import contexts
99

1010
ctx = contexts.get_standard_context(force_refresh=True)
@@ -30,6 +30,6 @@
3030
for k, v in ctx.natures.items():
3131
keyLookup[k] = v['name']
3232

33-
json.dump(baseStats, open('baseStats.json', 'w+'))
34-
json.dump(types, open('types.json', 'w+'))
35-
pickle.dump(keyLookup, open('keylookup.pickle', 'w+'))
33+
json.dump(baseStats, open('baseStats.json', 'w'))
34+
json.dump(types, open('types.json', 'w'))
35+
pickle.dump(keyLookup, open('keylookup.pickle', 'wb'))

StatCounter.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@
2323
import string
2424
import sys
2525
import math
26-
import cPickle as pickle
26+
import pickle
2727
import os
28-
#import ujson as json
2928
import json
3029
import gzip
3130

@@ -66,10 +65,6 @@
6665
metagamefile=open(filename,'w')
6766
else:
6867
metagamefile=False
69-
filename="Raw/moveset/"+tier+"/teammate"+specs+".pickle"
70-
teammatefile=open(filename,'w')
71-
filename="Raw/moveset/"+tier+"/encounterMatrix"+specs+".pickle"
72-
encounterfile=open(filename,'w')
7368

7469

7570
battleCount = 0
@@ -94,7 +89,7 @@
9489
t=t[:-11]
9590

9691
for line in file:
97-
#print line
92+
#print(line)
9893
battles = json.loads(line)
9994

10095
for battle in battles:
@@ -190,8 +185,8 @@
190185
if 'empty' in leads:
191186
if len(battle['matchups']) == 0: #1v1 (or similiar) battle forfeited before started
192187
continue
193-
print "Something went wrong."
194-
print battle
188+
print("Something went wrong.")
189+
print(battle)
195190

196191
for i in range(2):
197192
if ['p1','p2'][i] not in weight:
@@ -240,9 +235,13 @@
240235

241236

242237
#write teammates and encounter matrix to file
243-
pickle.dump(teammateMatrix,teammatefile)
238+
filename="Raw/moveset/"+tier+"/teammate"+specs+".pickle"
239+
teammatefile=open(filename,'wb')
240+
pickle.dump(teammateMatrix, teammatefile)
244241
teammatefile.close()
245-
pickle.dump(encounterMatrix,encounterfile)
242+
filename="Raw/moveset/"+tier+"/encounterMatrix"+specs+".pickle"
243+
encounterfile = open(filename, 'wb')
244+
pickle.dump(encounterMatrix, encounterfile)
246245
encounterfile.close()
247246

248247
#sort by weighted usage
@@ -316,8 +315,8 @@
316315

317316
if stallCounter:
318317
#figure out a good bin range by looking at .1% and 99.9% points
319-
low = stallCounter[len(stallCounter)/1000][0]
320-
high = stallCounter[len(stallCounter)-len(stallCounter)/1000-1][0]
318+
low = stallCounter[len(stallCounter)//1000][0]
319+
high = stallCounter[len(stallCounter)-len(stallCounter)//1000-1][0]
321320

322321

323322
nbins = 13 #this is actually only a rough idea--I think it might be the minimum?
@@ -347,7 +346,12 @@
347346
nbins = len(histogram)
348347

349348
for start in range(len(stallCounter)):
350-
if stallCounter[start] >= histogram[0][0]-binSize/2:
349+
# In Python 2, you could compare a list to a number, and the
350+
# list would always be greater than or equal to the number.
351+
# In other words, the commented statement (which would fail in
352+
# Python 3) would always return True.
353+
# if stallCounter[start] >= histogram[0][0]-binSize/2:
354+
if True:
351355
break
352356

353357
j=0
@@ -373,7 +377,7 @@
373377
x=x+score[0]*score[1]
374378
y=y+score[1]
375379

376-
#print histogram
380+
#print(histogram)
377381
metagamefile.write(' Stalliness (mean: %6.3f)\n'%(x/y))
378382
for i in range(len(histogram)):
379383
if histogram[i][0]%(2.0*binSize) < binSize/2:

TA.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import string
99
import sys
10-
#import ujson as json
1110
import json
1211
import math
1312
import copy

TierUpdate9Doubles.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import string
22
import sys
33
import json
4-
import cPickle as pickle
4+
import pickle
55
from common import keyify,readTable,getBattleFormatsData
66
reload(sys)
77
sys.setdefaultencoding('utf8')
@@ -16,18 +16,18 @@ def getUsage(filename,col,weight,usage):
1616

1717
def makeTable(table,name,keyLookup):
1818

19-
print "[HIDE="+name+"][CODE]"
20-
print "Combined usage for "+name
21-
print " + ---- + ------------------ + ------- + "
22-
print " | Rank | Pokemon | Percent | "
23-
print " + ---- + ------------------ + ------- + "
24-
print ' | %-4d | %-18s | %6.3f%% |' % (1,keyLookup[table[0][0]],table[0][1]*100)
19+
print("[HIDE="+name+"][CODE]")
20+
print("Combined usage for "+name)
21+
print(" + ---- + ------------------ + ------- + ")
22+
print(" | Rank | Pokemon | Percent | ")
23+
print(" + ---- + ------------------ + ------- + ")
24+
print(' | %-4d | %-18s | %6.3f%% |' % (1,keyLookup[table[0][0]],table[0][1]*100))
2525
for i in range(1,len(table)):
2626
if table[i][1] < 0.001:
2727
break
28-
print ' | %-4d | %-18s | %6.3f%% |' % (i+1,keyLookup[table[i][0]],100.0*table[i][1])
29-
print " + ---- + ------------------ + ------- + "
30-
print "[/CODE][/HIDE]"
28+
print(' | %-4d | %-18s | %6.3f%% |' % (i+1,keyLookup[table[i][0]],100.0*table[i][1]))
29+
print(" + ---- + ------------------ + ------- + ")
30+
print("[/CODE][/HIDE]")
3131

3232
tiers = ['DUber','DOU', 'DUU', 'DNU']
3333
usageTiers = ['doublesou', 'doublesuu']
@@ -76,8 +76,8 @@ def main(months):
7676

7777
usage = {} #track usage across all relevant tiers [OU,UU,RU,NU]
7878

79-
for i in xrange(len(months)):
80-
for j in xrange(len(usageTiers)):
79+
for i in range(len(months)):
80+
for j in range(len(usageTiers)):
8181
n = {}
8282
u = {}
8383

@@ -165,7 +165,7 @@ def main(months):
165165
if poke not in newTiers.keys():
166166
newTiers[poke] = tiers[-1]
167167

168-
print ""
168+
print("")
169169
for poke in curTiers:
170170
if newTiers[poke] == 'New' and poke in NFE:
171171
continue
@@ -176,7 +176,7 @@ def main(months):
176176
if tiers.index(newTiers[base]) < tiers.index(newTiers[poke]): #if the base is in a higher tier
177177
newTiers[poke] = newTiers[base]
178178
continue
179-
print species+" moved from "+curTiers[poke]+" to "+newTiers[poke]
179+
print(species+" moved from "+curTiers[poke]+" to "+newTiers[poke])
180180

181181
if __name__ == "__main__":
182182
main(sys.argv[1:])

TierUpdate9LC.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import string
22
import sys
33
import json
4-
import cPickle as pickle
4+
import pickle
55
from common import keyify,readTable,getBattleFormatsData
66
reload(sys)
77
sys.setdefaultencoding('utf8')
@@ -28,18 +28,18 @@ def usageToTiers(usage):
2828

2929
def makeTable(table,name,keyLookup):
3030

31-
print "[HIDE="+name+"][CODE]"
32-
print "Combined usage for "+name
33-
print " + ---- + ------------------ + ------- + "
34-
print " | Rank | Pokemon | Percent | "
35-
print " + ---- + ------------------ + ------- + "
36-
print ' | %-4d | %-18s | %6.3f%% |' % (1,keyLookup[table[0][0]],table[0][1]*100)
31+
print("[HIDE="+name+"][CODE]")
32+
print("Combined usage for "+name)
33+
print(" + ---- + ------------------ + ------- + ")
34+
print(" | Rank | Pokemon | Percent | ")
35+
print(" + ---- + ------------------ + ------- + ")
36+
print(' | %-4d | %-18s | %6.3f%% |' % (1,keyLookup[table[0][0]],table[0][1]*100))
3737
for i in range(1,len(table)):
3838
if table[i][1] < 0.001:
3939
break
40-
print ' | %-4d | %-18s | %6.3f%% |' % (i+1,keyLookup[table[i][0]],100.0*table[i][1])
41-
print " + ---- + ------------------ + ------- + "
42-
print "[/CODE][/HIDE]"
40+
print(' | %-4d | %-18s | %6.3f%% |' % (i+1,keyLookup[table[i][0]],100.0*table[i][1]))
41+
print(" + ---- + ------------------ + ------- + ")
42+
print("[/CODE][/HIDE]")
4343

4444
def main(months):
4545
file = open('keylookup.json', 'rb')
@@ -60,7 +60,7 @@ def main(months):
6060

6161

6262
remaining=24.0
63-
for i in xrange(len(months)):
63+
for i in range(len(months)):
6464
tiername = 'lc'
6565

6666
#bh
@@ -98,7 +98,7 @@ def main(months):
9898
for poke in dnuBanlist:
9999
printme += keyLookup[poke]+', '
100100
printme = printme[:-2]
101-
print printme
101+
print(printme)
102102

103103

104104

TierUpdate9NatDex.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import string
22
import sys
33
import json
4-
import cPickle as pickle
4+
import pickle
55
from common import keyify,readTable,getBattleFormatsData
66
reload(sys)
77
sys.setdefaultencoding('utf8')
@@ -16,18 +16,18 @@ def getUsage(filename,col,weight,usage):
1616

1717
def makeTable(table,name,keyLookup):
1818

19-
print "[HIDE="+name+"][CODE]"
20-
print "Combined usage for "+name
21-
print " + ---- + ------------------ + ------- + "
22-
print " | Rank | Pokemon | Percent | "
23-
print " + ---- + ------------------ + ------- + "
24-
print ' | %-4d | %-18s | %6.3f%% |' % (1,keyLookup[table[0][0]],table[0][1]*100)
19+
print("[HIDE="+name+"][CODE]")
20+
print("Combined usage for "+name)
21+
print(" + ---- + ------------------ + ------- + ")
22+
print(" | Rank | Pokemon | Percent | ")
23+
print(" + ---- + ------------------ + ------- + ")
24+
print(' | %-4d | %-18s | %6.3f%% |' % (1,keyLookup[table[0][0]],table[0][1]*100))
2525
for i in range(1,len(table)):
2626
if table[i][1] < 0.001:
2727
break
28-
print ' | %-4d | %-18s | %6.3f%% |' % (i+1,keyLookup[table[i][0]],100.0*table[i][1])
29-
print " + ---- + ------------------ + ------- + "
30-
print "[/CODE][/HIDE]"
28+
print(' | %-4d | %-18s | %6.3f%% |' % (i+1,keyLookup[table[i][0]],100.0*table[i][1]))
29+
print(" + ---- + ------------------ + ------- + ")
30+
print("[/CODE][/HIDE]")
3131

3232
tiers = ['Uber','OU','UUBL','UU','RUBL','RU','NUBL','NU','PUBL','PU','ZUBL','ZU', 'New']
3333
usageTiers = ['nationaldex', 'nationaldexuu']
@@ -79,8 +79,8 @@ def main(months):
7979

8080
usage = {} #track usage across all relevant tiers [OU,UU,RU,NU]
8181

82-
for i in xrange(len(months)):
83-
for j in xrange(len(usageTiers)):
82+
for i in range(len(months)):
83+
for j in range(len(usageTiers)):
8484
n = {}
8585
u = {}
8686

@@ -181,7 +181,7 @@ def main(months):
181181
if poke not in newTiers.keys():
182182
newTiers[poke] = 'RU'
183183

184-
print ""
184+
print("")
185185
for poke in curTiers:
186186
if newTiers[poke] == 'RU' and poke in NFE:
187187
continue
@@ -192,7 +192,7 @@ def main(months):
192192
if tiers.index(newTiers[base]) < tiers.index(newTiers[poke]): #if the base is in a higher tier
193193
newTiers[poke] = newTiers[base]
194194
continue
195-
print species+" moved from "+curTiers[poke]+" to "+newTiers[poke]
195+
print(species+" moved from "+curTiers[poke]+" to "+newTiers[poke])
196196

197197
if __name__ == "__main__":
198198
main(sys.argv[1:])

TierUpdate9Singles.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import string
22
import sys
33
import json
4-
import cPickle as pickle
4+
import pickle
55
from common import keyify,readTable,getBattleFormatsData
66
reload(sys)
77
sys.setdefaultencoding('utf8')
@@ -16,18 +16,18 @@ def getUsage(filename,col,weight,usage):
1616

1717
def makeTable(table,name,keyLookup):
1818

19-
print "[HIDE="+name+"][CODE]"
20-
print "Combined usage for "+name
21-
print " + ---- + ------------------ + ------- + "
22-
print " | Rank | Pokemon | Percent | "
23-
print " + ---- + ------------------ + ------- + "
24-
print ' | %-4d | %-18s | %6.3f%% |' % (1,keyLookup[table[0][0]],table[0][1]*100)
19+
print("[HIDE="+name+"][CODE]")
20+
print("Combined usage for "+name)
21+
print(" + ---- + ------------------ + ------- + ")
22+
print(" | Rank | Pokemon | Percent | ")
23+
print(" + ---- + ------------------ + ------- + ")
24+
print(' | %-4d | %-18s | %6.3f%% |' % (1,keyLookup[table[0][0]],table[0][1]*100))
2525
for i in range(1,len(table)):
2626
if table[i][1] < 0.001:
2727
break
28-
print ' | %-4d | %-18s | %6.3f%% |' % (i+1,keyLookup[table[i][0]],100.0*table[i][1])
29-
print " + ---- + ------------------ + ------- + "
30-
print "[/CODE][/HIDE]"
28+
print(' | %-4d | %-18s | %6.3f%% |' % (i+1,keyLookup[table[i][0]],100.0*table[i][1]))
29+
print(" + ---- + ------------------ + ------- + ")
30+
print("[/CODE][/HIDE]")
3131

3232
tiers = ['Uber','New','OU','UUBL','UU','RUBL','RU','NUBL','NU','PUBL','PU','ZUBL','ZU']
3333
usageTiers = ['ou', 'uu', 'ru', 'nu', 'pu']
@@ -75,8 +75,8 @@ def main(months):
7575

7676
usage = {} #track usage across all relevant tiers [OU,UU,RU,NU]
7777

78-
for i in xrange(len(months)):
79-
for j in xrange(len(usageTiers)):
78+
for i in range(len(months)):
79+
for j in range(len(usageTiers)):
8080
n = {}
8181
u = {}
8282

@@ -264,7 +264,7 @@ def main(months):
264264
if newTiers[poke] == 'PU' and poke in ['oricorio','oricoriopau','magneton','vivillon','sneaselhisui']:
265265
newTiers[poke] = 'PUBL'
266266

267-
print ""
267+
print("")
268268
for poke in curTiers:
269269
if newTiers[poke] == 'ZU' and poke in NFE:
270270
continue
@@ -275,7 +275,7 @@ def main(months):
275275
if tiers.index(newTiers[base]) < tiers.index(newTiers[poke]): #if the base is in a higher tier
276276
newTiers[poke] = newTiers[base]
277277
continue
278-
print species+" moved from "+curTiers[poke]+" to "+newTiers[poke]
278+
print(species+" moved from "+curTiers[poke]+" to "+newTiers[poke])
279279

280280
if __name__ == "__main__":
281281
main(sys.argv[1:])

0 commit comments

Comments
 (0)