Skip to content

Commit

Permalink
Merge pull request #251 from Whist-Team/lint/docstring
Browse files Browse the repository at this point in the history
LINT: doc strings
  • Loading branch information
iTitus authored Oct 23, 2022
2 parents 8cc6cf0 + 4473948 commit 748e63e
Show file tree
Hide file tree
Showing 21 changed files with 186 additions and 83 deletions.
1 change: 1 addition & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ exclude =
build
dist
venv
ignore = D200, D204, D205, D400, D401
max-line-length = 100
max-complexity = 10
217 changes: 137 additions & 80 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ classifiers = [
python = "^3.9"
pydantic = "^1.10"
python-jose = { version = "^3.3", extras = ["cryptography"] }
flake8-docstrings = "^1.6.0"

[tool.poetry.dev-dependencies]
pytest = "^7.1"
pytest-cov = "^4.0"
pytest-asyncio = "^0.20"
flake8 = "^5.0"
flake8-docstrings = "^1.6.0"
pylint = "^2.15"

[build-system]
Expand Down
1 change: 1 addition & 0 deletions whist_core/cards/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Cards and their containers."""
5 changes: 5 additions & 0 deletions whist_core/cards/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,18 @@ def name(self) -> str:
return f'{self.rank} of {self.suit}'

def dict(self, *args, **kwargs):
"""
Returns the dictionary. See BaseModel for details.
"""
super_dict = super().dict(*args, **kwargs)
return enforce_str_on_dict(super_dict, ('suit', 'rank'))

def __lt__(self, other: Any) -> bool:
"""Checks if the other card is lower than this card."""
if self.__class__ is other.__class__:
return (self.suit, self.rank) < (other.suit, other.rank)
return NotImplemented

def __str__(self) -> str:
"""Returns string representation of this card."""
return self.name
10 changes: 10 additions & 0 deletions whist_core/cards/card_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,23 @@ def pop_random(self) -> Card:
return card

def __contains__(self, card: Card) -> bool:
"""Returns if a card is in container. True if yes else False."""
return card in self.cards

def __len__(self):
"""Returns the amount of cards in the container."""
return len(self.cards)

def __iter__(self) -> Iterator[Card]:
"""Iterates over all cards."""
return iter(self.cards)

def __str__(self) -> str:
"""Returns string representation of all cards."""
return str(self.cards)

def __repr__(self) -> str:
"""Returns string representation of all cards with class name."""
return f'{self.__class__.__name__}(cards={self.cards!r})'

def remove(self, card: Card) -> None:
Expand Down Expand Up @@ -139,11 +144,16 @@ class UnorderedCardContainer(CardContainer):
_cards_set: set[Card] = PrivateAttr()

def __init__(self, **data):
"""
Constructor.
:param data: set of cards
"""
super().__init__(**data)
self._cards_set = set(self.cards)
self.__resync()

def __contains__(self, card: Card) -> bool:
"""Returns if a card is in container. True if yes else False."""
return card in self._cards_set

def _remove_impl(self, card: Card) -> None:
Expand Down
1 change: 1 addition & 0 deletions whist_core/error/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Error collection"""
1 change: 1 addition & 0 deletions whist_core/game/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""All game logic."""
5 changes: 5 additions & 0 deletions whist_core/game/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class NotPlayersTurnError(Exception):
"""

def __init__(self, player: Player, turn_player: Player):
"""
Constructor.
:param player: Who tried to play a card.
:param turn_player: Which turn it actually is.
"""
super().__init__()
self.message = f'Is not {player} turn, but {turn_player}.'

Expand Down
1 change: 1 addition & 0 deletions whist_core/game/hand.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def next_trick(self, play_order: PlayOrder) -> Trick:
return next_trick

def dict(self, *args, **kwargs):
"""Returns as dictionary."""
super_dict = super().dict(*args, **kwargs)
return enforce_str_on_dict(super_dict, ['trump'])

Expand Down
1 change: 1 addition & 0 deletions whist_core/game/play_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class PlayOrder(BaseModel):
play_order: list[PlayerAtTable]

def __iter__(self):
"""Iteration over all players."""
return iter(self.play_order)

@staticmethod
Expand Down
2 changes: 2 additions & 0 deletions whist_core/game/player_at_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ class PlayerAtTable(BaseModel):
team: int

def __eq__(self, other):
"""Checks if the other is equal."""
if not isinstance(other, PlayerAtTable):
return False
return self.player == other.player

def __repr__(self):
"""String representation"""
return f'PlayerAtTable: {self.player}'
1 change: 1 addition & 0 deletions whist_core/game/trick.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@ def play_card(self, player: PlayerAtTable, card: Card) -> None:
player.hand.remove(card)

def dict(self, *args, **kwargs):
"""Returns dictionary."""
super_dict = super().dict(*args, **kwargs)
return enforce_str_on_dict(super_dict, {'trump'})
1 change: 1 addition & 0 deletions whist_core/scoring/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Everything wtr scoring plus teams."""
6 changes: 6 additions & 0 deletions whist_core/scoring/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@ class Score(BaseModel):
hand_score: dict = {}

def __init__(self, teams: list[Team], scores: list[int], **data: Any):
"""
Constructor.
:param teams: list of the teams
:param scores: list if the scores as int
"""
super().__init__(**data)
for team, score in zip(teams, scores):
self.hand_score.update({team: score})
team.games_played()

def __getitem__(self, item):
"""Returns a specific hand score."""
return self.hand_score[item]

@property
Expand Down
1 change: 1 addition & 0 deletions whist_core/scoring/score_card.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ScoreCard(BaseModel):
hands: list[Score] = []

def __len__(self):
"""Amount of hands played."""
return len(self.hands)

@property
Expand Down
1 change: 1 addition & 0 deletions whist_core/scoring/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Team(BaseModel):
players: list[Player]

def __hash__(self):
"""Hash value of the team object."""
return hash(tuple(self.players))

@property
Expand Down
1 change: 1 addition & 0 deletions whist_core/session/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""All logic related to game sessions."""
7 changes: 4 additions & 3 deletions whist_core/session/userlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class UserList(BaseModel):
users: Dict[str, UserListEntry] = {}

def __len__(self):
"""Amount of players"""
return len(self.users)

@property
Expand Down Expand Up @@ -57,10 +58,10 @@ def teams(self) -> list[Team]:
Returns the teams.
:return: list of teams
"""
players_by_team = list(sorted(self.users.values(), key=lambda x: x.status.team))
player_by_team: list[list[Player]] = [[entry.player for entry in list(grp)]
for k, grp in groupby(
list(sorted(self.users.values(), key=lambda x: x.status.team)),
lambda x: x.status.team)]
for k, grp in groupby(players_by_team,
lambda x: x.status.team)]
teams: list[Team] = [Team(players=players) for players in player_by_team]
return teams

Expand Down
1 change: 1 addition & 0 deletions whist_core/user/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Everything related to players."""
3 changes: 3 additions & 0 deletions whist_core/user/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ class Player(User):
rating: int

def __str__(self):
"""Returns string representation of the player."""
return self.username

def __hash__(self):
"""Returns the hash value of the player object."""
return hash(self.username)

def __eq__(self, other):
"""Checks if the other is the same player."""
if not isinstance(other, Player):
return False
return other.username == self.username
Expand Down

0 comments on commit 748e63e

Please sign in to comment.