Skip to content

Commit

Permalink
fluent python
Browse files Browse the repository at this point in the history
  • Loading branch information
jduan committed Oct 9, 2022
1 parent a344298 commit 8fa6b4e
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 67 deletions.
2 changes: 2 additions & 0 deletions python_sandbox/python_sandbox/fluent_python/README.md
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 python_sandbox/python_sandbox/fluent_python/chapter1/cards.py

This file was deleted.

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]
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)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def __init__(self, x=0, y=0):
self.y = y

def __repr__(self):
return 'Vector(%r, %r)' % (self.x, self.y)
return "Vector(%r, %r)" % (self.x, self.y)

def __abs__(self):
return hypot(self.x, self.y)
Expand All @@ -30,13 +30,13 @@ def __mul__(self, scalar):
def main():
v1 = Vector(2, 4)
v2 = Vector(2, 1)
print('v1 + v2:', v1 + v2)
print("v1 + v2:", v1 + v2)

v = Vector(3, 4)
print('abs(v):', abs(v))
print("abs(v):", abs(v))

print('v * 3:', v * 3)
print("v * 3:", v * 3)


if __name__ == '__main__':
if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions python_sandbox/requirements.txt
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

0 comments on commit 8fa6b4e

Please sign in to comment.