Skip to content

Commit 00961cf

Browse files
committed
Use a context manager to close the open file
Fixes #24
1 parent 23c36e7 commit 00961cf

2 files changed

Lines changed: 50 additions & 47 deletions

File tree

annotator/__main__.py

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -448,51 +448,52 @@ def classify_opening(game):
448448
Returns the classified game and root_node, which is the node where the
449449
classification was made
450450
"""
451-
ecofile = os.path.join(os.path.dirname(__file__), 'eco/eco.json')
452-
ecodata = json.load(open(ecofile, 'r'))
451+
ecopath = os.path.join(os.path.dirname(__file__), 'eco/eco.json')
452+
with open(ecopath, 'r') as ecofile:
453+
ecodata = json.load(ecofile)
453454

454-
ply_count = 0
455+
ply_count = 0
455456

456-
root_node = game.root()
457-
node = game.end()
457+
root_node = game.root()
458+
node = game.end()
458459

459-
# Opening classification for variant games is not implemented (yet?)
460-
is_960 = root_node.board().chess960
461-
if is_960:
462-
variant = "chess960"
463-
else:
464-
variant = type(node.board()).uci_variant
460+
# Opening classification for variant games is not implemented (yet?)
461+
is_960 = root_node.board().chess960
462+
if is_960:
463+
variant = "chess960"
464+
else:
465+
variant = type(node.board()).uci_variant
465466

466-
if variant != "chess":
467-
logger.info("Skipping opening classification in variant "
468-
"game: {}".format(variant))
469-
return node.root(), root_node, game_length(game)
467+
if variant != "chess":
468+
logger.info("Skipping opening classification in variant "
469+
"game: {}".format(variant))
470+
return node.root(), root_node, game_length(game)
470471

471-
logger.info("Classifying the opening for non-variant {} "
472-
"game...".format(variant))
472+
logger.info("Classifying the opening for non-variant {} "
473+
"game...".format(variant))
473474

474-
while not node == game.root():
475-
prev_node = node.parent
476-
477-
fen = eco_fen(node.board())
478-
classification = classify_fen(fen, ecodata)
479-
480-
if classification["code"] != "":
481-
# Add some comments classifying the opening
482-
node.root().headers["ECO"] = classification["code"]
483-
node.root().headers["Opening"] = classification["desc"]
484-
node.comment = "{} {}".format(classification["code"],
485-
classification["desc"])
486-
# Remember this position so we don't analyze the moves preceding it
487-
# later
488-
root_node = node
489-
# Break (don't classify previous positions)
490-
break
475+
while not node == game.root():
476+
prev_node = node.parent
491477

492-
ply_count += 1
493-
node = prev_node
478+
fen = eco_fen(node.board())
479+
classification = classify_fen(fen, ecodata)
480+
481+
if classification["code"] != "":
482+
# Add some comments classifying the opening
483+
node.root().headers["ECO"] = classification["code"]
484+
node.root().headers["Opening"] = classification["desc"]
485+
node.comment = "{} {}".format(classification["code"],
486+
classification["desc"])
487+
# Remember this position so we don't analyze the moves
488+
# preceding it later
489+
root_node = node
490+
# Break (don't classify previous positions)
491+
break
492+
493+
ply_count += 1
494+
node = prev_node
494495

495-
return node.root(), root_node, ply_count
496+
return node.root(), root_node, ply_count
496497

497498

498499
def add_acpl(game, root_node):

tests/test_functions.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -401,19 +401,21 @@ class test_add_annotation(unittest.TestCase):
401401
class test_classify_fen(unittest.TestCase):
402402

403403
def test_eco_json(self):
404-
ecofile = os.path.join(os.path.dirname(__file__),
405-
'../annotator/eco/eco.json')
406-
ecodata = json.load(open(ecofile, 'r'))
404+
ecopath = os.path.join(
405+
os.path.dirname(__file__), '../annotator/eco/eco.json'
406+
)
407+
with open(ecopath, 'r') as ecofile:
408+
ecodata = json.load(ecofile)
407409

408-
for row in ecodata:
409-
fen = "{} {}".format(row["f"], '- 0 1')
410-
chess.Board(fen=fen)
410+
for row in ecodata:
411+
fen = "{} {}".format(row["f"], '- 0 1')
412+
chess.Board(fen=fen)
411413

412-
classification = annotator.classify_fen(row["f"], ecodata)
414+
classification = annotator.classify_fen(row["f"], ecodata)
413415

414-
assert classification["code"] == row["c"]
415-
assert classification["desc"] == row["n"]
416-
assert classification["path"] == row["m"]
416+
assert classification["code"] == row["c"]
417+
assert classification["desc"] == row["n"]
418+
assert classification["path"] == row["m"]
417419

418420

419421
class test_clean_game(unittest.TestCase):

0 commit comments

Comments
 (0)