Skip to main content

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.

GitHub PyPi

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

cardpy-1.0.2.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cardpy-1.0.2-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file cardpy-1.0.2.tar.gz.

File metadata

  • Download URL: cardpy-1.0.2.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.1

File hashes

Hashes for cardpy-1.0.2.tar.gz
Algorithm Hash digest
SHA256 94f273ec716ecee1d3a4a3874eb9331cc60a581114ebf170ea6584b80bdba7f3
MD5 40746a083434278df9e601f9fac842bb
BLAKE2b-256 df8b9f5a3afa41cb5a9851bfbead4dcaf51ce81e6109229f2a4da2d1d26f256e

See more details on using hashes here.

File details

Details for the file cardpy-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: cardpy-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.1

File hashes

Hashes for cardpy-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d2e0b71d715a0e466954881702c1aab1d536987f3989da5cc71a872e558f4295
MD5 3a6d065f796dfd7f0ab32bca4515256e
BLAKE2b-256 e799c4bbf590f20896b27de424f006edd4f80b50d0bb51dc520ed30829af5e16

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page