A Python module for playing cards.
Project description
cardpy
A comprehensive Python module for playing cards, providing flexible card and deck management.
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
Installation
Install using pip:
pip install cardpy
Quick Start
from cardpy import Card, Deck, Rank, Suit
# Create and shuffle a standard deck
deck = Deck(init=True)
deck.shuffle()
# Deal 5 cards each to 4 players
hands = deck.deal(4, 5)
for i, hand in enumerate(hands, 1):
print(f"Player {i}'s hand: {hand}")
# Create specific cards
ace_spades = Card(Rank.ACE, Suit.SPADES)
king_hearts = Card('K', '♥') # String representations also work
Core Classes
Card
Represents a playing card with rank, suit, and color:
# Create cards using enum members or strings
ace = Card(Rank.ACE, Suit.SPADES)
king = Card('K', 'H') # 'H' for Hearts
# Compare cards
if ace > king:
print("Ace is higher than King")
# Format card display
print(f"{ace}") # "ACE of SPADES â™ "
print(f"{ace:rank}") # "A"
print(f"{ace:suit}") # "â™ "
Deck
Manages a collection of cards with various operations:
# Create different types of decks
standard = Deck(init=True) # 52-card deck
empty = Deck() # Empty deck
multiple = Deck(init=True, deck_count=6) # 6 decks for Blackjack
# Chain operations
deck.shuffle().cut().reverse()
# Draw cards
card = deck.draw()
hand = deck.draw_multiple(5)
# Peek at cards
top_cards = deck.peek(3) # Look at top 3 cards
# Combine decks
big_deck = deck1 + deck2
deck1 *= 2 # Double the deck
Advanced Features
Custom Sorting
Sort cards using custom rank and suit orders:
# Define custom ordering
custom_ranks = [Rank.ACE] + Card.ranks[:-1] # Ace-low ordering
custom_suits = [Suit.HEARTS, Suit.DIAMONDS, Suit.CLUBS, Suit.SPADES]
# Sort using custom order
deck.sort(ranks=custom_ranks, suits=custom_suits)
Deck Operations
# Deal cards to multiple players
hands = deck.deal(4, 5) # 5 cards each to 4 players
# Cut the deck
deck.cut() # Cut in half
deck.cut(15) # Cut at specific position
# Count specific cards
aces = deck.count(Card(Rank.ACE, Suit.SPADES))
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. Code
License
This project is licensed under the MIT License - see the LICENSE file for details.
Author
Risheet Lenka - GitHub
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-1.0.1.tar.gz.
File metadata
- Download URL: cardpy-1.0.1.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dbccc7f9ae01ff9b2022f4127d84abf299defd39a6d3f62bd6f93fb0a9a2f7fb
|
|
| MD5 |
5d058f3e12ff3d269684c0e5f0af2490
|
|
| BLAKE2b-256 |
96b3e32d3bb3e2ada24d4b2cae8f1225d590e7d3b2f9fece4c85b5e0ab54a673
|
File details
Details for the file cardpy-1.0.1-py3-none-any.whl.
File metadata
- Download URL: cardpy-1.0.1-py3-none-any.whl
- Upload date:
- Size: 7.3 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 |
19f71e5f1fa457cb8f36163bc74b218a68642f99d3b95953c5a06e9d3c81fb0a
|
|
| MD5 |
dc1b868bdb6dee98e77789a10b06f306
|
|
| BLAKE2b-256 |
3c6756e1e9dadbe3836fd7bd933000365e57341712a417a2383a86841304b01c
|