-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpartition_games.py
executable file
·48 lines (40 loc) · 1.41 KB
/
partition_games.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python3
import argparse
import json
import math
import os
import random
import sys
from utility import load_games
def main(args=sys.argv[1:]):
parser = argparse.ArgumentParser("Split games into multiple sets.")
parser.add_argument("game_files", nargs="+",
help="Json files containing game data.")
parser.add_argument("-o", "--out-dir", required=True,
help="Directory name for output files.")
parser.add_argument("-n", "--num-parts", type=int, default=10,
help="Number of parts to split games into.")
config = parser.parse_args(args)
games = load_games(config.game_files)
random.shuffle(games)
num_parts = config.num_parts
parts = list()
end_game = 0
for i in range(num_parts):
start_game = end_game
end_game = start_game + (len(games) // num_parts)
if i < len(games) % num_parts:
end_game += 1
parts.append(games[start_game:end_game])
for p in parts:
p.sort(key=lambda x: x['gameID'])
pn_width = int(math.ceil(math.log10(num_parts)))
os.makedirs(config.out_dir)
for i, p in enumerate(parts):
pname = "part-%0*d.json" % (pn_width, i)
ppath = os.path.join(config.out_dir, pname)
with open(ppath, 'w') as pfile:
json.dump(p, pfile, indent=2)
print("Wrote %d games to %s" % (len(p), ppath))
if __name__ == "__main__":
main()