Skip to content

Commit bd8074d

Browse files
Add parse_file/parse_rank, file_name/rank_name`
1 parent 71f5a21 commit bd8074d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

chess/__init__.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,32 @@ def square(file_index: File, rank_index: Rank) -> Square:
275275
"""Gets a square number by file and rank index."""
276276
return rank_index * 8 + file_index
277277

278+
def parse_file(name: str) -> File:
279+
"""
280+
Gets the file index for the given file *name*
281+
(e.g., ``a`` returns ``0``).
282+
283+
:raises: :exc:`ValueError` if the file name is invalid.
284+
"""
285+
return FILE_NAMES.index(name)
286+
287+
def file_name(file: File) -> str:
288+
"""Gets the name of the file, like ``a``."""
289+
return FILE_NAMES[file]
290+
291+
def parse_rank(name: str) -> File:
292+
"""
293+
Gets the rank index for the given rank *name*
294+
(e.g., ``1`` returns ``0``).
295+
296+
:raises: :exc:`ValueError` if the rank name is invalid.
297+
"""
298+
return FILE_NAMES.index(name)
299+
300+
def rank_name(rank: Rank) -> str:
301+
"""Gets the name of the rank, like ``1``."""
302+
return FILE_NAMES[rank]
303+
278304
def square_file(square: Square) -> File:
279305
"""Gets the file index of the square where ``0`` is the a-file."""
280306
return square & 7

0 commit comments

Comments
 (0)