Skip to main content

treys is a pure Python poker hand evaluation library

Project description

A pure Python poker hand evaluation library

[ 3 ❤ ] , [ 3 ♠ ]

Installation

$ pip install treys

Implementation notes

Treys is a Python 3 port of Deuces based on the initial work in msaindon’s fork. Deuces was written by Will Drevo for the MIT Pokerbots Competition.

Treys is lightweight and fast. All lookups are done with bit arithmetic and dictionary lookups. That said, Treys won’t beat a C implemenation (~250k eval/s) but it is useful for situations where Python is required or where bots are allocated reasonable thinking time (human time scale).

Treys handles 5, 6, and 7 card hand lookups. The 6 and 7 card lookups are done by combinatorially evaluating the 5 card choices.

Usage

Treys is easy to set up and use.

>>> from treys import Card
>>> card = Card.new('Qh')

Card objects are represented as integers to keep Treys performant and lightweight.

Now let’s create the board and an example Texas Hold’em hand:

>>> board = [
>>>     Card.new('Ah'),
>>>     Card.new('Kd'),
>>>     Card.new('Jc')
>>> ]
>>> hand = [
>>>    Card.new('Qs'),
>>>    Card.new('Th')
>>> ]

Pretty print card integers to the terminal:

>>> Card.print_pretty_cards(board + hand)
  [ A ❤ ] , [ K ♦ ] , [ J ♣ ] , [ Q ♠ ] , [ T ❤ ]

If you have termcolor installed, they will be colored as well.

Otherwise move straight to evaluating your hand strength:

>>> from treys import Evaluator
>>> evaluator = Evaluator()
>>> print(evaluator.evaluate(board, hand))
1600

Hand strength is valued on a scale of 1 to 7462, where 1 is a Royal Flush and 7462 is unsuited 7-5-4-3-2, as there are only 7642 distinctly ranked hands in poker.

If you want to deal out cards randomly from a deck, you can also do that with Treys:

>>> from treys import Deck
>>> deck = Deck()
>>> board = deck.draw(5)
>>> player1_hand = deck.draw(2)
>>> player2_hand = deck.draw(2)

and print them:

>>> Card.print_pretty_cards(board)
  [ 4 ♣ ] , [ A ♠ ] , [ 5 ♦ ] , [ K ♣ ] , [ 2 ♠ ]
>>> Card.print_pretty_cards(player1_hand)
  [ 6 ♣ ] , [ 7 ❤ ]
>>> Card.print_pretty_cards(player2_hand)
  [ A ♣ ] , [ 3 ❤ ]

Let’s evaluate both hands strength, and then bin them into classes, one for each hand type (High Card, Pair, etc)

>>> p1_score = evaluator.evaluate(board, player1_hand)
>>> p2_score = evaluator.evaluate(board, player2_hand)
>>> p1_class = evaluator.get_rank_class(p1_score)
>>> p2_class = evaluator.get_rank_class(p2_score)

or get a human-friendly string to describe the score,

>>> print("Player 1 hand rank = %d (%s)\n" % (p1_score, evaluator.class_to_string(p1_class)))
Player 1 hand rank = 6330 (High Card)

>>> print("Player 2 hand rank = %d (%s)\n" % (p2_score, evaluator.class_to_string(p2_class)))
Player 2 hand rank = 1609 (Straight)

or, coolest of all, get a blow-by-blow analysis of the stages of the game with relation to hand strength:

>>> hands = [player1_hand, player2_hand]
>>> evaluator.hand_summary(board, hands)

========== FLOP ==========
Player 1 hand = High Card, percentage rank among all hands = 0.893192
Player 2 hand = Pair, percentage rank among all hands = 0.474672
Player 2 hand is currently winning.

========== TURN ==========
Player 1 hand = High Card, percentage rank among all hands = 0.848298
Player 2 hand = Pair, percentage rank among all hands = 0.452292
Player 2 hand is currently winning.

========== RIVER ==========
Player 1 hand = High Card, percentage rank among all hands = 0.848298
Player 2 hand = Straight, percentage rank among all hands = 0.215626

========== HAND OVER ==========
Player 2 is the winner with a Straight

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

treys-0.1.8.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

treys-0.1.8-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file treys-0.1.8.tar.gz.

File metadata

  • Download URL: treys-0.1.8.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.4

File hashes

Hashes for treys-0.1.8.tar.gz
Algorithm Hash digest
SHA256 a486a42b899e91985b4da4fdac9a30e638275648977104487acb90a2dd7cd73b
MD5 b3f5b577dd31ca8956520fe3138f9768
BLAKE2b-256 3ba61712340dc1ac96d40afe162d43ce146c7781ba59cde5efc988aaee35ada4

See more details on using hashes here.

File details

Details for the file treys-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: treys-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.4

File hashes

Hashes for treys-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 9ba3460ff2ed597510fb535af6280f115254b0b70699ea362f8f1ee067378063
MD5 0a3f0185c0bc502936c1fbff101bbeee
BLAKE2b-256 46dfe6b3b1cc98c3e00c5b146113f998dfe0de47358277648df235a6ae571143

See more details on using hashes here.

Supported by

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