-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
73 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# How to run tests? | ||
python -m pytest |
Empty file.
Empty file.
62 changes: 0 additions & 62 deletions
62
python_sandbox/python_sandbox/fluent_python/chapter1/cards.py
This file was deleted.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
python_sandbox/python_sandbox/fluent_python/python_data_model/cards.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import collections | ||
|
||
Card = collections.namedtuple("Card", ["rank", "suit"]) | ||
|
||
|
||
class FrenchDeck: | ||
ranks = [str(n) for n in range(2, 11)] + list("JQKA") | ||
suits = "spades diamonds clubs hearts".split() | ||
|
||
def __init__(self): | ||
self._cards = [Card(rank, suit) for suit in self.suits for rank in self.ranks] | ||
|
||
def __len__(self): | ||
return len(self._cards) | ||
|
||
def __getitem__(self, position): | ||
return self._cards[position] | ||
|
||
|
||
suit_values = dict(spades=3, hearts=2, diamonds=1, clubs=0) | ||
|
||
|
||
def spades_high(card): | ||
rank_value = FrenchDeck.ranks.index(card.rank) | ||
return rank_value * len(suit_values) + suit_values[card.suit] |
40 changes: 40 additions & 0 deletions
40
python_sandbox/python_sandbox/fluent_python/python_data_model/tests/test_cards.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from python_data_model.cards import Card, FrenchDeck, spades_high | ||
from random import choice | ||
import sys | ||
|
||
|
||
def test_cards(): | ||
print(f"sys.path: {sys.path}") | ||
beer_card = Card("7", "diamonds") | ||
assert beer_card.rank == "7" | ||
assert beer_card.suit == "diamonds" | ||
|
||
|
||
def test_deck(): | ||
deck = FrenchDeck() | ||
assert len(deck) == 52 | ||
assert deck[0] == Card("2", "spades") | ||
assert deck[-1] == Card("A", "hearts") | ||
# choice works with a sequence and FrenchDeck is a sequence because it implements | ||
# both __len__ and __getitem__ | ||
print(f"Pick a random card: {choice(deck)}") | ||
print(f"Pick a random card: {choice(deck)}") | ||
|
||
# slicing works | ||
assert deck[:3] == [Card("2", "spades"), Card("3", "spades"), Card("4", "spades")] | ||
assert deck[-3:] == [Card("Q", "hearts"), Card("K", "hearts"), Card("A", "hearts")] | ||
|
||
# iteration works too because of __getitem__ | ||
print("iterating cards") | ||
for card in deck: | ||
print(card) | ||
|
||
# in works too | ||
# If a collection has no __contains__ method, the "in" operator does a sequential scan | ||
assert Card("Q", "hearts") in deck | ||
assert Card("7", "beasts") not in deck | ||
|
||
# sort cards | ||
print("sorted cards") | ||
for card in sorted(deck, key=spades_high): | ||
print(card) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
hydra-core==0.11.3 | ||
pyyaml==5.3.1 | ||
pytest==7.1.3 |