Deck

Playing Cards

Deck

 Deck ()

Represents a deck of cards

A Deck of cards is a collection of Card objects:

deck = Deck()
deck
A♣️; 2♣️; 3♣️; 4♣️; 5♣️; 6♣️; 7♣️; 8♣️; 9♣️; 10♣️; J♣️; Q♣️; K♣️; A♦️; 2♦️; 3♦️; 4♦️; 5♦️; 6♦️; 7♦️; 8♦️; 9♦️; 10♦️; J♦️; Q♦️; K♦️; A❤️; 2❤️; 3❤️; 4❤️; 5❤️; 6❤️; 7❤️; 8❤️; 9❤️; 10❤️; J❤️; Q❤️; K❤️; A♠️; 2♠️; 3♠️; 4♠️; 5♠️; 6♠️; 7♠️; 8♠️; 9♠️; 10♠️; J♠️; Q♠️; K♠️

There are 52 cards in a deck.

test_eq(len(deck), 52)

Deck.pop

 Deck.pop (index=-1)

Removes and returns card index from the deck

Type Default Details
index int -1 Card number to pop
deck.pop()
K♠️

There are 51 cards left in the deck now.

test_eq(len(deck), 51)

You can show the docs for methods not created with patch by calling show_doc. For example, the code show_doc(Deck.remove) produces the following documentation:


Deck.remove

 Deck.remove (card:cards_deck.card.Card)

Removes card from the deck or raises exception if it is not there

Type Details
card Card Card to remove

If we remove a card from the Deck we can verify that it no longer exists:

card23 = Card(2, 3)
deck.remove(card23)

assert card23 not in deck

However, another card that we haven’t removed, such as the 10 of hearts will still be in the Deck of cards because we haven’t removed it:

assert Card(2,10) in deck

Hand

 Hand ()

Represents a deck of cards

hand = Hand()
test_eq(len(hand), 0)

move_cards

 move_cards (source:__main__.Deck, dest:__main__.Hand, num:int)

Pop the given number of cards from the deck and move to dest.

Type Details
source Deck deck to move cards from
dest Hand destination to move cards to
num int number of cards to move
Note

You might be wondering: “what are these comments are following each parameter?” These are called docments, a concise way of documenting your code that also renders beautifully in nbdev. nbdev also supports rendering numpy-style docstrings as well.

deck = Deck()
deck.shuffle()
move_cards(deck, hand, 7)
hand
7♣️; 5♠️; Q♠️; A♣️; 4❤️; J♣️; 3❤️
test_eq(len(deck), 52-7)
test_eq(len(hand), 7)

Drawing Cards With Replacement

Let’s try something fun with our deck of cards, drawing a card with replacement:


draw_n

 draw_n (n:int, replace:bool=True)

Draw n cards, with replacement iif replace

Type Default Details
n int number of cards to draw
replace bool True whether or not draw with replacement
sample = draw_n(10)
sample
[5♣️, 3♣️, 8♣️, 9❤️, 10♦️, 7♠️, 8♣️, 6❤️, 5❤️, A❤️]

Visualizing the results

This isn’t terribly interesting from a statistical perspective. However, its an example of how you can include visualizations in your nbdev projects!

Note

Notice how we are hiding just the input with #|echo: false, so readers can see the output but hide the code. You can also fold the code using the #|code-fold: true directive.

Create a CLI (Advanced)

We can create a CLI with @call parse


draw_cards

 draw_cards (n:int, replace:bool=True, outfile:str=None)

Draw n cards optionally with replacement

Type Default Details
n int number of cards to draw
replace bool True whether or not draw with replacement
outfile str None output file, defaults to stdout
Tip

We normally wouldn’t repeat all of these arguments when one function is wrapping another one. Instead, we would use delegates. However, we wanted to keep this tutorial simple, so we didn’t use that here.

fname = 'sample.txt'
draw_cards(10, outfile=fname)
print(Path(fname).read_text())
10❤️
9❤️
3♣️
Q♣️
J♣️
4♠️
2♠️
2❤️
9♦️
5♠️