Skip to content

Commit db03057

Browse files
committed
Reorganize PGN docs
1 parent 08ef4e3 commit db03057

2 files changed

Lines changed: 58 additions & 33 deletions

File tree

chess/pgn.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,18 +751,29 @@ def read_game(handle, Visitor=GameModelCreator):
751751
"""
752752
Reads a game from a file opened in text mode.
753753
754+
>>> import chess.pgn
755+
>>>
754756
>>> pgn = open("data/pgn/kasparov-deep-blue-1997.pgn")
757+
>>>
755758
>>> first_game = chess.pgn.read_game(pgn)
756759
>>> second_game = chess.pgn.read_game(pgn)
757760
>>>
758761
>>> first_game.headers["Event"]
759762
'IBM Man-Machine, New York USA'
763+
>>>
764+
>>> board = first_game.board()
765+
>>> for move in first_game.main_line():
766+
... board.push(move)
767+
...
768+
>>> board
769+
Board('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1')
760770
761771
By using text mode the parser does not need to handle encodings. It is the
762772
callers responsibility to open the file with the correct encoding.
763773
PGN files are ASCII or UTF-8 most of the time. So the following should
764774
cover most relevant cases (ASCII, UTF-8, UTF-8 with BOM).
765775
776+
>>> # Python 3
766777
>>> pgn = open("data/pgn/kasparov-deep-blue-1997.pgn", encoding="utf-8-sig")
767778
768779
Use :class:`~io.StringIO` to parse games from a string.

docs/pgn.rst

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,44 @@
11
PGN parsing and writing
22
=======================
33

4+
Parsing
5+
-------
6+
7+
.. autofunction:: chess.pgn.read_game
8+
9+
Writing
10+
-------
11+
12+
If you want to export your game game with all headers, comments and variations
13+
you can use:
14+
15+
>>> import chess
16+
>>> import chess.pgn
17+
>>>
18+
>>> game = chess.pgn.Game()
19+
>>> game.headers["Event"] = "Example"
20+
>>> node = game.add_variation(chess.Move.from_uci("e2e4"))
21+
>>> node = node.add_variation(chess.Move.from_uci("e7e5"))
22+
>>> node.comment = "Comment"
23+
>>>
24+
>>> print(game)
25+
[Event "Example"]
26+
[Site "?"]
27+
[Date "????.??.??"]
28+
[Round "?"]
29+
[White "?"]
30+
[Black "?"]
31+
[Result "*"]
32+
<BLANKLINE>
33+
1. e4 e5 { Comment } *
34+
35+
Remember that games in files should be separated with extra blank lines.
36+
37+
>>> print(game, file=open("/dev/null", "w"), end="\n\n")
38+
39+
Use the :class:`~chess.pgn.StringExporter()` or
40+
:class:`~chess.pgn.FileExporter()` visitors if you need more control.
41+
442
Game model
543
----------
644

@@ -75,39 +113,6 @@ headers.
75113
76114
A list of child nodes.
77115

78-
Parsing
79-
-------
80-
81-
.. autofunction:: chess.pgn.read_game
82-
83-
.. autofunction:: chess.pgn.scan_headers
84-
85-
.. autofunction:: chess.pgn.scan_offsets
86-
87-
Writing
88-
-------
89-
90-
If you want to export your game game with all headers, comments and variations
91-
you can use:
92-
93-
>>> print(game)
94-
[Event "?"]
95-
[Site "?"]
96-
[Date "????.??.??"]
97-
[Round "?"]
98-
[White "?"]
99-
[Black "?"]
100-
[Result "*"]
101-
<BLANKLINE>
102-
1. e4 e5 { Comment } *
103-
104-
Remember that games in files should be separated with extra blank lines.
105-
106-
>>> print(game, file=handle, end="\n\n")
107-
108-
Use the :class:`~chess.pgn.StringExporter()` or
109-
:class:`~chess.pgn.FileExporter()` visitors if you need more control.
110-
111116
Visitors
112117
--------
113118

@@ -140,3 +145,12 @@ like ``!``, ``?``, ``!!``, etc. are also converted to NAGs.
140145
.. autodata:: chess.pgn.NAG_BLUNDER
141146
.. autodata:: chess.pgn.NAG_SPECULATIVE_MOVE
142147
.. autodata:: chess.pgn.NAG_DUBIOUS_MOVE
148+
149+
Skimming
150+
--------
151+
152+
These functions allow quickly skimming games without fully parsing them.
153+
154+
.. autofunction:: chess.pgn.scan_headers
155+
156+
.. autofunction:: chess.pgn.scan_offsets

0 commit comments

Comments
 (0)