Skip to main content

Functionalities for playing cards

Project description

cardspy

A Python library for modeling and manipulating playing cards, decks, suits, and ranks. Provides utilities for games, simulations, and card-based logic.

Features

  • Full 52-card deck modeling
  • Card, Suit, and Rank objects with rich properties
  • Deck operations: shuffle, deal, add/remove cards, reset, clear
  • Bitmask utilities for efficient card set operations
  • 100% test coverage

Installation

pip install cardspy

Quick Start

from cardspy.deck import Deck
from cardspy.card import Card, C2C, C3D, sort_cards, cards_mask, extract_cards, rank_mask_from_cards
from cardspy.suit import CLUB, DIAMOND, HEART, SPADE
from cardspy.rank import R2, R3, R4, R5, R6, R7, R8, R9, RT, RJ, RQ, RK, RA

# Create a new deck
deck = Deck()
print(f"Deck has {deck.count()} cards")  # 52

# Shuffle and deal 5 cards
deck.shuffle()
cards_mask_val = deck.deal_cards(5)  # Bitmask of 5 cards
dealt_cards = extract_cards(cards_mask_val)
print("Dealt:", [str(card) for card in dealt_cards])

# Remove a card
deck.remove_card(C2C.key)
print(deck.contains(C2C.key))  # False

# Add a card back
deck.add_card(C2C.key)
print(deck.contains(C2C.key))  # True

# Sort cards
sorted_cards = sort_cards(dealt_cards)
print("Sorted:", [str(card) for card in sorted_cards])

# Bitmask utilities
mask = cards_mask([C2C, C3D])
print(mask)
print(extract_cards(mask))
print(rank_mask_from_cards(mask))

API Reference

Card

  • Card(key, rank, suit, code, name, symbol)
  • Predefined constants: C2C, C2D, ..., CAS (all 52 cards)
  • Methods: __str__(), __repr__()

Suit

  • Suit(key, code, name, symbol)
  • Predefined: CLUB, DIAMOND, HEART, SPADE

Rank

  • Rank(key, code, name)
  • Predefined: R2, R3, ..., RA

Deck

  • Deck() — creates a new deck (full 52 cards)
  • deck.shuffle() — shuffle deck
  • deck.clear() — remove all cards
  • deck.reset() — restore to full deck
  • deck.count() — number of cards
  • deck.get_cards() — list of Card objects in deck
  • deck.add_card(card_key) — add a card by key
  • deck.add_cards(cards_key) — add multiple cards by bitmask
  • deck.remove_card(card_key) — remove a card by key
  • deck.remove_cards(cards_key) — remove multiple cards by bitmask
  • deck.deal_cards(count, shuffle=True) — deal N cards, returns bitmask
  • deck.deal_specific_card(card_key) — deal a specific card
  • deck.deal_specific_cards(cards_key) — deal specific cards by bitmask
  • deck.contains(card_key) — check if card is present

Utility Functions (in cardspy.card)

  • sort_cards(cards) — sort by rank
  • cards_mask(cards) — get bitmask from list of cards
  • extract_cards(mask) — get list of cards from bitmask
  • extract_cards_key(mask) — get list of card keys from bitmask
  • rank_mask_from_cards(mask) — get rank bitmask from card bitmask

Example: Poker Hand Evaluation

from cardspy.card import C2C, C3C, C4C, C5C, C6C, cards_mask, extract_cards

# Create a straight flush
hand = [C2C, C3C, C4C, C5C, C6C]
mask = cards_mask(hand)
print("Hand mask:", mask)
print("Extracted:", extract_cards(mask))

Testing

To run the tests and check coverage:

pytest --cov=cardspy --cov-report=term-missing

License

MIT


For more details, see the source code and tests in the cardspy and tests directories.

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

cardspy-1.0.2.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

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

cardspy-1.0.2-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cardspy-1.0.2.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.10.12 Darwin/24.5.0

File hashes

Hashes for cardspy-1.0.2.tar.gz
Algorithm Hash digest
SHA256 2746ddf2ef997d6410d79c16d1b44e56196aa5d6832017b917551cf65f353b97
MD5 74977de1e0bd1f532d4b0b10bdb955bc
BLAKE2b-256 955322d5dca96e6576b433100812b513c2b8e7a83d2e3e000e4ef54f56cd1de2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cardspy-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.10.12 Darwin/24.5.0

File hashes

Hashes for cardspy-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 68e802baf3597eb4bd4205466f7d2a14c6d645486b7e3ee027cbd90f51209d15
MD5 3955f4ef5a9284ab3a531bc2122f1e0c
BLAKE2b-256 1c0476b594f499f3f2aac06f2c75c4261d7811ffc8575e8ba3cbaa8c8e3e991f

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