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-2.0.0.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

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

cardpy-2.0.0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for cardpy-2.0.0.tar.gz
Algorithm Hash digest
SHA256 a10956ef375cf2e043b745972f3d3ce594f829b406ea1ee19449f2188eaf04ac
MD5 2d7d9d7801ed48744e1ee0acc6fd3644
BLAKE2b-256 b50069b5c61003f4652481c78ca8dae9bf6e7997db7494269fbaa8592c4c47ac

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for cardpy-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ea9581260b5d27df5afb3017836b79af9a0f463fcefdf0fc63e2452a3f517e82
MD5 b77346b8fdd5ae9172cd719f5d68002a
BLAKE2b-256 67e4447cf9d648755b322549a501c1ec9b75ca8a310ab2d4dde77a3fae7824a9

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