|
1 | 1 | PGN parsing and writing |
2 | 2 | ======================= |
3 | 3 |
|
| 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 | + |
4 | 42 | Game model |
5 | 43 | ---------- |
6 | 44 |
|
@@ -75,39 +113,6 @@ headers. |
75 | 113 |
|
76 | 114 | A list of child nodes. |
77 | 115 |
|
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 | | - |
111 | 116 | Visitors |
112 | 117 | -------- |
113 | 118 |
|
@@ -140,3 +145,12 @@ like ``!``, ``?``, ``!!``, etc. are also converted to NAGs. |
140 | 145 | .. autodata:: chess.pgn.NAG_BLUNDER |
141 | 146 | .. autodata:: chess.pgn.NAG_SPECULATIVE_MOVE |
142 | 147 | .. 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