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. Most of work is taken from msaindon’s fork.

Treys (originally Deuces) was written by Will Drevo for the MIT Pokerbots Competition. It 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. Once again, refer to my blog post for a more mathematically complete explanation of why this is so.

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

Uploaded Source

Built Distribution

treys-0.1.3-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: treys-0.1.3.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.1.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.6

File hashes

Hashes for treys-0.1.3.tar.gz
Algorithm Hash digest
SHA256 98a3511546cb00a7852a5114ba0754159dfa191a7b8a03fa52564933fbff7d25
MD5 d871bc5aafc640a62ca41e675bafe2de
BLAKE2b-256 8eb9b537686078c938e0c5d59c707d002d3ffa843f0cf1a7578a2872b56cb2cd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: treys-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.1.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.6

File hashes

Hashes for treys-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5e37886499b039a8fc536b7dcedbcee6fbf057bb83576cea7e214810e0eb7145
MD5 ccbe05f2fc6b664d133d5a80b1fcd99a
BLAKE2b-256 6dbd62a04aa40980ff9f63eda8addcbeb75752064953111d5dbc2ed1ec386989

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