Skip to content

Commit

Permalink
Add docstrings for methods
Browse files Browse the repository at this point in the history
Also, fix spacing.
  • Loading branch information
MarkZH committed Feb 28, 2024
1 parent cce50dc commit 4e695e2
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion chess/pgn.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ class GameNodeComment:
A class that can hold one or more comments for a GameNode.
"""

def __init__(self, comment: Union[str, list[str]] = ""):
"""Create a new comment."""
self.set(comment)

def set(self, new_comment: Union[str, list[str]]) -> None:
Expand Down Expand Up @@ -224,18 +226,23 @@ def pop(self, index: int = -1) -> None:
self._comments.pop(index)

def __len__(self) -> int:
"""Get the number of comments in this node."""
return len(self._comments)

def __getitem__(self, index: int) -> str:
"""Get the comment at the given index."""
return self._comments[index]

def __setitem__(self, index: int, new_comment: str) -> None:
"""Change the comment at the given index."""
self._comments[index] = new_comment

def __iter__(self) -> Iterator[str]:
"""Return an iterator over all comments."""
return iter(self._comments)

def __add__(self, other: Union[str, list[str], GameNodeComment]) -> GameNodeComment:
"""Create a new comment set by adding two comment sets with +."""
if isinstance(other, GameNodeComment):
return GameNodeComment(self._comments + other._comments)
else:
Expand All @@ -244,6 +251,7 @@ def __add__(self, other: Union[str, list[str], GameNodeComment]) -> GameNodeComm
return new_node_comment

def __eq__(self, other: object) -> bool:
"""Check for equality between two comment sets."""
if isinstance(other, str):
return (len(self) == 1 and self[0] == other) or (not self and not other)
elif isinstance(other, list):
Expand All @@ -254,9 +262,11 @@ def __eq__(self, other: object) -> bool:
return False

def __repr__(self) -> str:
"""Return a code-like representation of the class."""
return f"{self.__class__.__name__}({self._comments})"

def __str__(self) -> str:
"""Return a string representation of the comments in PGN format."""
return self.pgn_format()


Expand Down Expand Up @@ -290,7 +300,6 @@ def comment(self, new_comment: Union[str, list[str], GameNodeComment]) -> None:
else:
self._comment = GameNodeComment(new_comment)


_starting_comment: GameNodeComment

@property
Expand Down

0 comments on commit 4e695e2

Please sign in to comment.