Python implementation of Smart Game Format
After 14 years, I've extracted my old SGF code from PyGo and am in the process of cleaning it up and making it available under an MIT license.
pip install sgf==0.5
import sgf
with open("examples/ff4_ex.sgf") as f:
collection = sgf.parse(f.read())
collection
now represents the SGF collection.
with open("output.sgf", "w") as f:
collection.output(f)
-
Collection
has-
children[]
each of which is aGameTree
-
-
GameTree
has-
nodes[]
each of which is aNode
(nodes up to first variation) -
children[]
each of which is aGameTree
(game tree for each variation) -
game.root
the first node of the game tree (technically this is only
called the "root" for game trees immediately under a collection) -
game.rest
an iterable over the rest of the nodes in the mainline
-
-
Node
has-
properties[]
dictionary with string keys and values -
previous
- previous node in SGF -
next
- next node in SGF -
previous_variation
- previous variation (if first node in a variation) -
next_variation
- next variation (if first node in a variation) -
first
- boolean indicating when first node in a variation -
variations[]
- list of variations immediately from this node
-
Collection
is indexable and iterable. collection[0]
will return the first
game in a collection and for game in collection
will iterate over the games.
len(collection)
will return the number of games it contains.
GameTree
is iterable over the mainline nodes (i.e. following the first of
any variations). e.g. for node in game
.