Skip to content

Commit

Permalink
Use a context manager to close the open file
Browse files Browse the repository at this point in the history
Fixes #24
  • Loading branch information
rpdelaney committed Aug 25, 2019
1 parent 23c36e7 commit 00961cf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 47 deletions.
75 changes: 38 additions & 37 deletions annotator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,51 +448,52 @@ def classify_opening(game):
Returns the classified game and root_node, which is the node where the
classification was made
"""
ecofile = os.path.join(os.path.dirname(__file__), 'eco/eco.json')
ecodata = json.load(open(ecofile, 'r'))
ecopath = os.path.join(os.path.dirname(__file__), 'eco/eco.json')
with open(ecopath, 'r') as ecofile:
ecodata = json.load(ecofile)

ply_count = 0
ply_count = 0

root_node = game.root()
node = game.end()
root_node = game.root()
node = game.end()

# Opening classification for variant games is not implemented (yet?)
is_960 = root_node.board().chess960
if is_960:
variant = "chess960"
else:
variant = type(node.board()).uci_variant
# Opening classification for variant games is not implemented (yet?)
is_960 = root_node.board().chess960
if is_960:
variant = "chess960"
else:
variant = type(node.board()).uci_variant

if variant != "chess":
logger.info("Skipping opening classification in variant "
"game: {}".format(variant))
return node.root(), root_node, game_length(game)
if variant != "chess":
logger.info("Skipping opening classification in variant "
"game: {}".format(variant))
return node.root(), root_node, game_length(game)

logger.info("Classifying the opening for non-variant {} "
"game...".format(variant))
logger.info("Classifying the opening for non-variant {} "
"game...".format(variant))

while not node == game.root():
prev_node = node.parent

fen = eco_fen(node.board())
classification = classify_fen(fen, ecodata)

if classification["code"] != "":
# Add some comments classifying the opening
node.root().headers["ECO"] = classification["code"]
node.root().headers["Opening"] = classification["desc"]
node.comment = "{} {}".format(classification["code"],
classification["desc"])
# Remember this position so we don't analyze the moves preceding it
# later
root_node = node
# Break (don't classify previous positions)
break
while not node == game.root():
prev_node = node.parent

ply_count += 1
node = prev_node
fen = eco_fen(node.board())
classification = classify_fen(fen, ecodata)

if classification["code"] != "":
# Add some comments classifying the opening
node.root().headers["ECO"] = classification["code"]
node.root().headers["Opening"] = classification["desc"]
node.comment = "{} {}".format(classification["code"],
classification["desc"])
# Remember this position so we don't analyze the moves
# preceding it later
root_node = node
# Break (don't classify previous positions)
break

ply_count += 1
node = prev_node

return node.root(), root_node, ply_count
return node.root(), root_node, ply_count


def add_acpl(game, root_node):
Expand Down
22 changes: 12 additions & 10 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,19 +401,21 @@ class test_add_annotation(unittest.TestCase):
class test_classify_fen(unittest.TestCase):

def test_eco_json(self):
ecofile = os.path.join(os.path.dirname(__file__),
'../annotator/eco/eco.json')
ecodata = json.load(open(ecofile, 'r'))
ecopath = os.path.join(
os.path.dirname(__file__), '../annotator/eco/eco.json'
)
with open(ecopath, 'r') as ecofile:
ecodata = json.load(ecofile)

for row in ecodata:
fen = "{} {}".format(row["f"], '- 0 1')
chess.Board(fen=fen)
for row in ecodata:
fen = "{} {}".format(row["f"], '- 0 1')
chess.Board(fen=fen)

classification = annotator.classify_fen(row["f"], ecodata)
classification = annotator.classify_fen(row["f"], ecodata)

assert classification["code"] == row["c"]
assert classification["desc"] == row["n"]
assert classification["path"] == row["m"]
assert classification["code"] == row["c"]
assert classification["desc"] == row["n"]
assert classification["path"] == row["m"]


class test_clean_game(unittest.TestCase):
Expand Down

0 comments on commit 00961cf

Please sign in to comment.