Sgf_game objects¶
SGF data is represented using Sgf_game objects. Each object represents the data for a single SGF file (corresponding to a GameTree in the SGF spec). This is typically used to represent a single game, possibly with variations (but it could be something else, such as a problem set).
Page contents
Creating Sgf_games¶
An Sgf_game can either be created from scratch or loaded from a string.
To create one from scratch, instantiate an Sgf_game object directly:
- class
sgfmill.sgf.Sgf_game(size[, encoding="UTF-8"])[source]¶ size is an integer from 1 to 26, indicating the board size.
The optional encoding parameter specifies the raw property encoding to use for the game.
When a game is created this way, the following root properties are initially set: FF[4], GM[1], SZ[size], and CA[encoding].
To create a game from existing SGF data, use the Sgf_game.from_bytes() or Sgf_game.from_string() classmethod:
- classmethod
Sgf_game.from_bytes(bb[, override_encoding=None])[source]¶ Return type: Sgf_gameCreates an
Sgf_gamefrom the SGF data in bb, which must be a bytes-like object.Raises
ValueErrorif it can’t parse the data, or if theSZorCAproperties are unacceptable. No error is reported for other malformed property values. See also Parser behaviour.Assumes the data is in the encoding described by the
CAproperty in the root node (defaulting to"ISO-8859-1"), and uses that as the raw property encoding.But if override_encoding is present, assumes the data is in that encoding (no matter what the
CAproperty says), and sets theCAproperty and raw property encoding to match.The board size is taken from the
SZpropery in the root node (defaulting to19). Board sizes greater than26are rejected.Example:
g = sgf.Sgf_game.from_bytes( b"(;FF[4]GM[1]SZ[9]CA[UTF-8];B[ee];W[ge])", override_encoding="iso8859-1")
- classmethod
Sgf_game.from_string(s)[source]¶ Return type: Sgf_gameCreates an
Sgf_gamefrom the SGF data in s, which must be a string.Raises
ValueErrorif it can’t parse the data, or if theSZorCAproperties are unacceptable. No error is reported for other malformed property values. See also Parser behaviour.The game’s raw property encoding and
CAproperty will be"UTF-8"(replacing anyCAproperty present in the string).The board size is taken from the
SZpropery in the root node (defaulting to19). Board sizes greater than26are rejected.Example:
g = sgf.Sgf_game.from_string( "(;FF[4]GM[1]SZ[9]CA[UTF-8];B[ee];W[ge])")
Serialising Sgf_games¶
To retrieve the SGF data as bytes, use the serialise() method:
-
Sgf_game.serialise([wrap])[source]¶ Return type: bytes Produces the SGF representation of the data in the
Sgf_game.Returns a bytes object, in the encoding specified by the
CAroot property (defaulting to"ISO-8859-1").See Changing the CA property for details of the behaviour if the
CAproperty is changed from its initial value.serialise()makes some effort to keep the output line length to no more than 79 bytes. PassNonein the wrap parameter to disable this behaviour, or pass an integer to specify a different limit.
Accessing the game’s nodes¶
The complete game tree is represented using Tree_node objects, which are used to access the SGF properties. An Sgf_game always has at least one node, the root node.
The complete game tree can be accessed through the root node, but the following convenience methods are also provided. They return the same Tree_node objects that would be reached via the root node.
Some of the convenience methods are for accessing the leftmost variation of the game tree. This is the variation which appears first in the SGF GameTree, often shown in graphical editors as the topmost horizontal line of nodes. In a game tree without variations, the leftmost variation is just the whole game.
-
Sgf_game.get_last_node()[source]¶ Return type: Tree_nodeReturns the last (leaf) node in the leftmost variation.
-
Sgf_game.get_main_sequence()[source]¶ Return type: list of Tree_nodeobjectsReturns the complete leftmost variation. The first element is the root node, and the last is a leaf.
-
Sgf_game.get_main_sequence_below(node)[source]¶ Return type: list of Tree_nodeobjectsReturns the leftmost variation beneath the
Tree_nodenode. The first element is the first child of node, and the last is a leaf.Note that this isn’t necessarily part of the leftmost variation of the game as a whole.
-
Sgf_game.get_main_sequence_above(node)¶ Return type: list of Tree_nodeobjectsReturns the partial variation leading to the
Tree_nodenode. The first element is the root node, and the last is the parent of node.
-
Sgf_game.extend_main_sequence()[source]¶ Return type: Tree_nodeCreates a new
Tree_node, adds it to the leftmost variation, and returns it.This is equivalent to
get_last_node().new_child()
Root node properties¶
The root node contains global properties for the game tree, and typically also contains game-info properties. It sometimes also contains setup properties (for example, if the game does not begin with an empty board).
Changing the FF and GM properties is permitted, but Sgfmill will carry on using the FF[4] and GM[1] (Go) rules.
Changing SZ is not permitted (but if the size is 19 you may remove the property).
Changing CA is permitted (this controls the encoding used by serialise()).
The following methods provide convenient access to some of the root node’s SGF properties. The main difference between using these methods and using get() on the root node is that these methods return the appropriate default value if the property is not present.
-
Sgf_game.get_size()[source]¶ Return type: integer Returns the board size (
19if theSZroot property isn’t present).
-
Sgf_game.get_charset()[source]¶ Return type: string Returns the effective value of the
CAroot property (ISO-8859-1if theCAroot property isn’t present).The returned value is a codec name in normalised form, which may not be identical to the string returned by
get_root().get("CA"). RaisesValueErrorif the property value doesn’t identify a Python codec.This gives the encoding that would be used by
serialise(). It is not necessarily the same as the raw property encoding (useget_encoding()on the root node to retrieve that).
-
Sgf_game.get_komi()[source]¶ Return type: float Returns the komi (
0.0if theKMroot property isn’t present).Raises
ValueErrorif theKMroot property is present but malformed.
-
Sgf_game.get_handicap()[source]¶ Return type: integer or NoneReturns the number of handicap stones.
Returns
Noneif theHAroot property isn’t present, or if it has value zero (which isn’t strictly permitted).Raises
ValueErrorif theHAproperty is otherwise malformed.
-
Sgf_game.get_player_name(colour)[source]¶ Return type: string or NoneReturns the name of the specified player, or
Noneif the requiredPBorPWroot property isn’t present.
-
Sgf_game.get_winner()[source]¶ Return type: colour Returns the colour of the winning player.
Returns
Noneif theREroot property isn’t present, or if neither player won.
-
Sgf_game.set_date([date])[source]¶ Sets the
DTroot property, to a single date.If date is specified, it should be a
datetime.date. Otherwise the current date is used.(SGF allows
DTto be rather more complicated than a single date, so there’s no corresponding get_date() method.)