-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepare_run.py
executable file
·55 lines (43 loc) · 1.74 KB
/
prepare_run.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
49
50
51
52
53
54
55
#!env python
import chess.pgn, urllib, boto, time, json
import sys
def write_batch(pgn_string, inputs_bucket, batch_name, batch_num):
games_key = '%s/%d.pgn' % (batch_name, batch_num)
results_key = '%s/%d.txt' % (batch_name, batch_num)
key = inputs_bucket.new_key(games_key)
key.set_contents_from_string(pgn_string)
key.close()
config = {'pgn_key': games_key,
'depth': 15,
'result_key': results_key}
config_key = '%s/%d.json' % (batch_name, batch_num)
key = config_bucket.new_key(config_key)
key.set_contents_from_string(json.dumps(config))
key.close()
print "Wrote batch #%d" % batch_num
conn = boto.connect_s3()
inputs_bucket = conn.get_bucket('bc-runinputs')
config_bucket = conn.get_bucket('bc-runconfigs')
batch_size = 60
game_num = 0
batch_name = time.strftime('%Y%m%d-%H%M%S')
print "Batch is named %s" % batch_name
urlfd = urllib.urlopen(sys.argv[1])
exporter = chess.pgn.StringExporter()
game = chess.pgn.read_game(urlfd)
while game is not None:
if 'FICSGamesDBGameNo' in game.headers:
game.headers['BCID'] = 'FICS.%s' % game.headers['FICSGamesDBGameNo']
else:
game.headers['BCID'] = 'Kaggle.%s' % game.headers['Event']
game.export(exporter, headers=True, variations=False, comments=False)
game_num = game_num + 1
if game_num % batch_size == 0:
batch_num = game_num / batch_size - 1
write_batch(str(exporter), inputs_bucket, batch_name, batch_num)
exporter = chess.pgn.StringExporter()
game = chess.pgn.read_game(urlfd)
# if we have some games unwritten in this batch, write them out
if game_num % batch_size != 0:
batch_num = game_num / batch_size
write_batch(str(exporter), inputs_bucket, batch_name, batch_num)