Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__/

6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ deck_obj.add_deck()
print(len(deck_obj.deck))
```

**index and iterate over a deck object**
```
first_card = deck_obj[0]
card_names = [card.name for card in deck_obj]
```

### Test coverage
```
============================= test session starts =============================
Expand Down
39 changes: 33 additions & 6 deletions deck_of_cards/deck_of_cards.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def __eq__(self, other):
:param other: other Card instance to compare
:return:
"""
return self.rank == other.rank and self.suit == other.suit
if isinstance(other, Card):
return self.rank == other.rank and self.suit == other.suit
return False

def __lt__(self, other):
"""
Expand Down Expand Up @@ -146,6 +148,19 @@ def __init__(self):
]
logging.debug("_Deck initialized_")

def __getitem__(self, index):
"""
Allows bracket notation to be used to index into self.cards
:return: Card
"""
return self.deck[index]

def __iter__(self):
"""
Allows deck to be iterable
"""
yield from self.deck

def _deck_empty(self):
"""
Checks if the deck is empty
Expand All @@ -168,7 +183,7 @@ def add_deck(self):
]
self.deck += deck
logging.debug("Deck of cards added")
return self.deck
return self

def order_deck(self):
"""
Expand All @@ -182,7 +197,7 @@ def order_deck(self):
for card in self.deck
]
))
return self.deck
return self

def shuffle_deck(self):
"""
Expand All @@ -196,7 +211,7 @@ def shuffle_deck(self):
for card in self.deck
]
))
return self.deck
return self

def print_deck(self):
"""
Expand All @@ -215,7 +230,7 @@ def reset_deck(self):
"""
self.__init__()
logging.debug("_Object reinitialized_")
return self.deck
return self

def _give_card(self, method_name, operation):
"""
Expand Down Expand Up @@ -294,7 +309,7 @@ def add_jokers(self):
joker_color = Card((4, 15))
self.deck.append(joker_bw)
self.deck.append(joker_color)
return self.deck
return self


def main():
Expand Down Expand Up @@ -333,6 +348,18 @@ def main():
deck_obj.take_card(card)
print(len(deck_obj.deck))

# deck is indexable:
print("Deck is indexable:")
print("First card is: " + deck_obj[0].name)
# deck is iterable:
print("Deck is iterable and each item is a card:")
for c in deck_obj:
if (c.rank == 1):
print(c.name)

print("You can also do functional operations on the deck object:")
aces = filter(lambda c: c.rank == 1, deck_obj)
print([a.name for a in aces])

if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion deck_of_cards/test_deck_of_cards.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import pytest

from deck_of_cards.deck_of_cards import Card, DeckOfCards
from deck_of_cards import Card, DeckOfCards


class TestCard:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="deck_of_cards",
version="0.0.10",
version="0.0.11",
author="Simon Lachaîne",
author_email="simonthechain@gmail.com",
description="A module to create and interact with a deck of cards object",
Expand Down
2 changes: 1 addition & 1 deletion source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# The short X.Y version
version = ''
# The full version, including alpha/beta/rc tags
release = '0.0.9'
release = '0.0.11'


# -- General configuration ---------------------------------------------------
Expand Down
10 changes: 9 additions & 1 deletion source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,12 @@ add a second deck of cards to the first one

print(len(deck_obj.deck))
deck_obj.add_deck()
print(len(deck_obj.deck))
print(len(deck_obj.deck))

index and iterate over a deck object
------------------------------------
.. code-block:: python
:emphasize-lines: 2

first_card = deck_obj[0]
card_names = [card.name for card in deck_obj]