A Python module for playing cards.
Project description
cardpy
A comprehensive Python module for playing cards, providing flexible card and deck management with type hints and modern Python features.
Features
- Standard 52-card deck management
- Support for multiple decks (e.g., for Blackjack)
- Card comparison and sorting
- Unicode suit symbols (♠, ♥, ♦, ♣)
- Rich deck operations (shuffle, cut, deal, etc.)
- Type hints and thorough error checking
- Chainable methods for fluid syntax
- Hand management for card games
- Custom sorting options
- Face-up/face-down card tracking
Installation
pip install cardpy
Quick Start
from cardpy import Card, Deck, Hand, Rank, Suit
# Create and shuffle a deck
deck = Deck(init=True) # Creates standard 52-card deck
deck.shuffle()
# Create a hand and deal cards
hand = Hand()
hand.extend(deck.draw_multiple(5))
print(f"Your hand: {hand}")
# Create specific cards
ace_spades = Card(Rank.ACE, Suit.SPADES)
king_hearts = Card('K', '♥') # String representations work too
# Compare cards
if ace_spades > king_hearts:
print("Ace beats King!")
Core Classes
Card
Represents a playing card with rank, suit, color and face-up status:
# Multiple ways to create cards
ace = Card(Rank.ACE, Suit.SPADES)
king = Card('K', 'H') # Using string shortcuts
ten = Card('10', '♦') # Using Unicode symbols
# Card properties
print(f"{ace}") # "ACE of SPADES â™ "
print(f"{ace:rank}") # "A"
print(f"{ace:suit}") # "â™ "
print(f"{ace:color}") # "black"
# Face-up/down handling
ace.face_up = True
ace.flip() # Flips the card over
Deck
Manages collections of cards with rich operations:
# Different deck types
standard = Deck(init=True) # 52-card deck
empty = Deck() # Empty deck
blackjack = Deck(init=True, deck_count=6) # 6 decks combined
# Chain operations
deck.shuffle().cut().reverse()
# Card operations
card = deck.draw()
hand = deck.draw_multiple(5)
top_three = deck.peek_multiple(3)
# Combine decks
big_deck = deck1 + deck2
deck1 *= 2 # Double the deck
Hand
Manages player hands with similar operations to Deck:
# Create and manage hands
hand = Hand()
hand.extend(deck.draw_multiple(5))
hand.sort() # Sort by rank and suit
# Play cards
played_card = hand.play()
played_multiple = hand.play_multiple(2)
# Combine hands
merged_hand = hand1 + hand2
Advanced Features
Custom Sorting
Sort cards using custom rank and suit orders:
# Define custom ordering
ace_low = [Rank.ACE] + Card.RANKS[:-1] # Ace-low ordering
hearts_high = [Suit.DIAMONDS, Suit.CLUBS, Suit.SPADES, Suit.HEARTS]
# Sort using custom order
deck.sort(ranks=ace_low, suits=hearts_high)
hand.sort(ranks=ace_low, suits=hearts_high)
Multiple Deck Games
# Create a 6-deck shoe for Blackjack
shoe = Deck(init=True, deck_count=6)
shoe.shuffle()
# Deal cards to multiple players
hands = shoe.deal(4, 2) # 2 cards each to 4 players
# Track remaining cards
remaining = len(shoe)
print(f"Cards remaining: {remaining}")
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Author
Risheet Lenka - GitHub Profile
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cardpy-2.0.3.tar.gz.
File metadata
- Download URL: cardpy-2.0.3.tar.gz
- Upload date:
- Size: 10.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
85abcc8e2c7acace2235c08cd2b72237ba72c2d647f5b06a5e8894307f713f8e
|
|
| MD5 |
f1272a9eefded3206af520b7beb98ae7
|
|
| BLAKE2b-256 |
abacb56dfe4b2391cd28448a89a1f7719d1889120bce44db55dc5138de8b30c9
|
File details
Details for the file cardpy-2.0.3-py3-none-any.whl.
File metadata
- Download URL: cardpy-2.0.3-py3-none-any.whl
- Upload date:
- Size: 10.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d4de8265614f9dd04a1ec9c0ae03b702920b4f9a3472fcde021f25a59d503638
|
|
| MD5 |
b73452925d94d4f7f07090ff971b3454
|
|
| BLAKE2b-256 |
4669284cfa918cea431a367f9fdd264afa516b6e4758aeba7ce6b2b0833a12d4
|