Skip to content

Commit

Permalink
add pre-commit config
Browse files Browse the repository at this point in the history
  • Loading branch information
boldandbrad committed Feb 19, 2024
1 parent c0c7efb commit 56690b6
Show file tree
Hide file tree
Showing 19 changed files with 158 additions and 64 deletions.
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
extend-ignore = D1, D2, E1, E2, E3, E501, W1, W2, W3, W5
51 changes: 51 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-toml
- id: check-added-large-files
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.1.6
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- repo: https://github.com/psf/black
rev: 23.11.0
hooks:
- id: black
- repo: https://github.com/PyCQA/flake8
rev: 6.1.0
hooks:
- id: flake8
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.27.1
hooks:
- id: check-github-workflows
- repo: https://github.com/gitleaks/gitleaks
rev: v8.18.1
hooks:
- id: gitleaks
# - repo: https://github.com/pre-commit/mirrors-prettier
# rev: v3.1.0
# hooks:
# - id: prettier
# - repo: https://github.com/returntocorp/semgrep
# rev: v1.50.0
# hooks:
# - id: semgrep
# args: ["--config", "auto", "--error", "--skip-unknown-extensions"]
- repo: https://github.com/dosisod/refurb
rev: v1.24.0
hooks:
- id: refurb
# additional_dependencies:
# - mypy<1.7 # SEE: https://github.com/dosisod/refurb/issues/305
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@
<script src="//unpkg.com/docsify-footer-enh/dist/docsify-footer-enh.min.js"></script>
</body>

</html>
</html>
49 changes: 49 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# activate venv
venv:
. .venv/bin/activate

# install euchre-cli from local
install:
pip install -q ."[test]"

# install euchre-cli via flit
flit-install:
pip install flit
flit install

# install editable
dev-install:
pip install -q -e .

# lint and format
lint:
pre-commit run --show-diff-on-failure --all-files

# run all tests
test: install
pytest

# run all tests with coverage
test-cov: install
pytest -v --cov-report xml --cov euchre

# build dist
build:
flit build

# generate homebrew formula
brew:
pip install -q ."[dev]"
poet -f euchre-cli >> formula.rb

# remove artifacts
# TODO: remove __pycache__ dirs from src/ and tests/
cleanup:
rm -f .coverage
rm -f coverage.xml
rm -f formula.rb
rm -rf .pytest_cache
rm -rf build
rm -rf dist
rm -rf *.egg-info
rm -rf .ruff_cache
9 changes: 4 additions & 5 deletions src/euchre/abstract/card.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from . import Suit, Face
from . import Face, Suit


class Card:
Expand Down Expand Up @@ -35,8 +35,7 @@ def adjusted_suit(self, trump_suit: Suit) -> Suit:
"""
if self.is_left_bower(trump_suit):
return trump_suit
else:
return self.suit
return self.suit

def weighted_value(self, trump_suit: Suit, lead_suit: Suit = None) -> int:
"""Calculate card weighted value relative to trump and/or lead suits.
Expand All @@ -62,9 +61,9 @@ def weighted_value(self, trump_suit: Suit, lead_suit: Suit = None) -> int:
if self.is_left_bower(trump_suit):
weighted_val += 15 # 26
elif self.suit.name == trump_suit.name:
if self.face.name in ["Nine", "Ten"]:
if self.face.name in ("Nine", "Ten"):
weighted_val += 12 # 21, 22
elif self.face.name in ["Queen", "King", "Ace"]:
elif self.face.name in ("Queen", "King", "Ace"):
weighted_val += 11 # 23, 24, 25
elif self.face.name == "Jack":
weighted_val += 16 # 27 (right_bower)
Expand Down
13 changes: 5 additions & 8 deletions src/euchre/abstract/computer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from random import choices
from typing import List, Tuple

from random import choice, choices

from . import Team, Player, Card, Suit
from . import Card, Player, Suit, Team


class Computer(Player):
Expand All @@ -29,9 +28,8 @@ def call_pick_up(self, face_up_card: Card, partner_is_dealer: bool) -> bool:
return True
elif len(cards_of_suit) >= 3 and face_up_card.face.name != "Jack":
return True
else:
# only occasionally call on 'accident'
return choices([True, False], weights=[1, 10])[0]
# only occasionally call on 'accident'
return choices([True, False], weights=[1, 10])[0]

def pick_up_card(self, pick_up: Card) -> Card:
"""Choose whether or not to replace card in hand with picked up one.
Expand Down Expand Up @@ -92,8 +90,7 @@ def call_trump_suit(self, unsuitable: Suit) -> Suit:
# TODO: consider if player is 2, 3, or 4 suited
if suit_counts[0]["count"] >= 3:
return suit_counts[0]["suit"]
else:
return unsuitable
return unsuitable

def play_card(self, played_cards: List[Card], trump_suit: Suit) -> Card:
"""Choose which card to play from hand.
Expand Down
25 changes: 12 additions & 13 deletions src/euchre/abstract/human.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

from click import style

from . import Team, Player, Card, Suit
from euchre.util import output
from euchre.util.input_util import bool_input, int_input, str_input

from . import Card, Player, Suit, Team


class Human(Player):
"""Representation of a human player. Extends Player class."""
Expand All @@ -24,12 +25,10 @@ def call_pick_up(self, face_up_card: Card, partner_is_dealer: bool) -> bool:
self.print_hand()
choice = False
if self.is_dealer:
choice = bool_input(
f"Would you like to pick up the " + f"{str(face_up_card)}?"
)
choice = bool_input("Would you like to pick up the " + f"{face_up_card}?")
else:
choice = bool_input(
f"Would you like the dealer to pick up the " + f"{str(face_up_card)}?"
"Would you like the dealer to pick up the " + f"{face_up_card}?"
)
return choice

Expand All @@ -47,9 +46,9 @@ def pick_up_card(self, pick_up: Card) -> Card:

output(f"Trump suit will be {pick_up.suit}s.")
self.print_hand(indicies=True)
choice = int_input(f"Which card would you like to discard?", len(self.hand))
choice = int_input("Which card would you like to discard?", len(self.hand))
discard = self.hand.pop(choice)
output(f"You discarded the {str(discard)} to the deck.", 0.75)
output(f"You discarded the {discard} to the deck.", 0.75)
return discard

def call_trump_suit(self, unsuitable: Suit) -> Suit:
Expand All @@ -69,14 +68,14 @@ def call_trump_suit(self, unsuitable: Suit) -> Suit:
suits_in_hand = list(suits_in_hand)
suits_in_hand.append(unsuitable)

output(f"Possible options:")
output("Possible options:")
for idx, suit in enumerate(suits_in_hand):
if suit != unsuitable:
output(f"\t{idx} - {str(suit)}", 0.75)
output(f"\t{idx} - {suit}", 0.75)
else:
output(f"\t{idx} - Pass", 0.75)

choice = int_input(f"Would you like to call a trump suit?", len(suits_in_hand))
choice = int_input("Would you like to call a trump suit?", len(suits_in_hand))

return suits_in_hand[choice]

Expand All @@ -91,7 +90,7 @@ def play_card(self, played_cards: List[Card], trump_suit: Suit) -> Card:
Card: Card to play.
"""
self.print_hand(indicies=True)
choice = int_input(f"Which card would you like to play?", len(self.hand))
choice = int_input("Which card would you like to play?", len(self.hand))
return self.hand[choice]

def print_hand(self, indicies: bool = False):
Expand All @@ -104,6 +103,6 @@ def print_hand(self, indicies: bool = False):
output(f"{self.name}, this is your hand:")
for idx, card in enumerate(self.hand):
if indicies:
output(f"\t{idx} - {str(card)}", 0.75)
output(f"\t{idx} - {card}", 0.75)
else:
output(f"\t{str(card)}", 0.75)
output(f"\t{card}", 0.75)
5 changes: 2 additions & 3 deletions src/euchre/abstract/player.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from . import Team, Card, Suit
from . import Team


class Player:
Expand Down Expand Up @@ -26,5 +26,4 @@ def __repr__(self) -> str:
def __str__(self) -> str:
if self.is_dealer:
return f"{self.name} (Dealer)"
else:
return f"{self.name}"
return f"{self.name}"
32 changes: 18 additions & 14 deletions src/euchre/command/play.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
import click
from loguru import logger

from euchre.abstract import Suit, Card, Team, Player, Computer, Human
from euchre.abstract import Card, Computer, Human, Player, Suit, Team
from euchre.util import (
create_deck,
deal_hand,
hand_winner,
output,
unique_cpu_name,
rotate_dealer,
rotate_trick_order,
valid_play,
trick_winner,
hand_winner,
unique_cpu_name,
valid_play,
)


Expand All @@ -32,7 +32,7 @@ def play(watch: bool):

def setup(watch_mode: bool):
"""Setup players and teams."""
output(click.style(f"Welcome to euchre-cli!", fg="green"), 0)
output(click.style("Welcome to euchre-cli!", fg="green"), 0)

teams = [Team("Team 1"), Team("Team 2")]

Expand All @@ -43,13 +43,17 @@ def setup(watch_mode: bool):
else:
player = Human(teams[0])

players.append(player)
players.append(Computer(unique_cpu_name(players), teams[1]))
players.append(Computer(unique_cpu_name(players), teams[0]))
players.append(Computer(unique_cpu_name(players), teams[1]))
players.extend(
[
player,
Computer(unique_cpu_name(players), teams[1]),
Computer(unique_cpu_name(players), teams[0]),
Computer(unique_cpu_name(players), teams[1]),
]
)

output()
output(f"Players:", 0.75)
output("Players:", 0.75)
for player in players:
output(f"\t{player.name}, {player.team.name}", 0.75)

Expand Down Expand Up @@ -184,7 +188,7 @@ def set_trump_suit(players: List[Player], deck: List[Card]) -> Suit:
# print play order for hand
output("Play Order:")
for player in players:
output(f"\t{str(player)}, {player.team.name}", 0.75)
output(f"\t{player}, {player.team.name}", 0.75)
output()

# pickup round
Expand Down Expand Up @@ -244,13 +248,13 @@ def trick(
break
if isinstance(player, Human):
output(
f"\tInvalid play ({str(card_to_play)}). You must "
+ f"follow the lead suit "
f"\tInvalid play ({card_to_play}). You must "
+ "follow the lead suit "
+ f"({played_cards[0].adjusted_suit(trump_suit)}s).",
0.5,
)
else:
logger.debug(f"\tInvalid play ({str(card_to_play)}).")
logger.debug(f"\tInvalid play ({card_to_play}).")

# play proposed card from player hand
output(f"{player.name} plays the")
Expand Down
1 change: 0 additions & 1 deletion src/euchre/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from euchre.command.rules import rules
from euchre.util.log_util import logger_init


logger_init()


Expand Down
2 changes: 1 addition & 1 deletion src/euchre/util/card_util.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from random import shuffle
from typing import List

from euchre.abstract import Player, Suit, Face, Card
from euchre.abstract import Card, Face, Player, Suit


def create_deck() -> List[Card]:
Expand Down
4 changes: 2 additions & 2 deletions src/euchre/util/input_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def bool_input(prompt: str) -> bool:
bool: User boolean response.
"""
response = input(f"{prompt} (y/N) ")
return response.lower() in ["y", "yes"]
return response.lower() in ("y", "yes")


def int_input(prompt: str, input_range: int) -> int:
Expand All @@ -32,7 +32,7 @@ def int_input(prompt: str, input_range: int) -> int:
response = -1
valid = False
while not valid:
range_str = f"(0-{input_range - 1})" if input_range > 1 else f"(0)"
range_str = f"(0-{input_range - 1})" if input_range > 1 else "(0)"
try:
response = int(input(f"{prompt} {range_str}: "))
if response in range(input_range):
Expand Down
Loading

0 comments on commit 56690b6

Please sign in to comment.