Skip to main content

PH Evaluator Python package (phevaluator)

GitHub Workflow Status PyPI version PyPI downloads Apache_2.0

Description

PH Evaluator is designed for evaluating poker hands with more than 5 cards. Instead of traversing all the combinations, it uses a perfect hash algorithm to get the hand strength from a pre-computed hash table, which only costs very few CPU cycles and considerably small memory (~100kb for the 7 card evaluation). With slight modification, the same algorithm can be also applied to evaluating Omaha poker hands.

The full API documentation is published at https://henryrlee.github.io/PokerHandEvaluator/python.

Installation

The library requires Python 3.10 or newer.

The evaluator is implemented as a C extension (phevaluator._pheval) that wraps the C sources in the repository's cpp directory, so a C compiler and pip 21.3 or newer are required to build from source. Older pip versions build out-of-tree and cannot reach the cpp directory.

  • from release on PyPI

    pip install phevaluator
    
  • from source code

    Build from within the repository (the sibling cpp directory is used to compile the extension):

    pip install .
    

    If you are using pip older than 21.3, either upgrade pip:

    pip install --upgrade pip
    

    or pass the --use-feature=in-tree-build flag so the build can access the cpp sources:

    pip install --use-feature=in-tree-build .
    

Using the library

The main function is the evaluate_cards function.

from phevaluator.evaluator import evaluate_cards

p1 = evaluate_cards("9c", "4c", "4s", "9d", "4h", "Qc", "6c")
p2 = evaluate_cards("9c", "4c", "4s", "9d", "4h", "2c", "9h")

# The rank is an integer from 1 (strongest) to 7462 (weakest), so a smaller
# value means a stronger hand. Player 2 has a stronger hand here.
print(f"The rank of the hand in player 1 is {p1}") # 292
print(f"The rank of the hand in player 2 is {p2}") # 236

The returned value is the rank of the hand among all 7462 distinct five-card hands. It is identical to the return value of Cactus Kev's evaluator: 1 is the best possible hand (a Royal Straight Flush) and 7462 is the worst. A smaller value is always a stronger hand, so you can compare two hands directly (e.g. p2 < p1 means player 2 wins).

The function can take both numbers and card strings (with a format like: 'Ah' or '2C'). Usage examples can be seen in examples.py.

Omaha and Pot Limit Omaha

Omaha-style hands (five community cards followed by the hole cards) can be evaluated with the dedicated functions:

from phevaluator import evaluate_omaha_cards  # 4 hole cards
from phevaluator import evaluate_plo4_cards   # 4 hole cards (alias of Omaha)
from phevaluator import evaluate_plo5_cards   # 5 hole cards
from phevaluator import evaluate_plo6_cards   # 6 hole cards

# 5 community cards + 4 hole cards. The return value is a rank in the same
# 1 (strongest) to 7462 (weakest) scale as evaluate_cards, computed from the
# best five-card hand allowed by the Omaha rules.
evaluate_omaha_cards("4c", "5c", "6c", "7s", "8s", "2c", "9c", "As", "Kd")

evaluate_plo5_cards and evaluate_plo6_cards require the package to be built with PLO5/PLO6 support. Their lookup tables are very large (hundreds of MB of generated C source each), so they are opt-in: set the PHEVALUATOR_BUILD_PLO environment variable when installing from the repository.

# Build with PLO5 and PLO6 support (also accepts "5", "6", or "all")
PHEVALUATOR_BUILD_PLO=5,6 pip install .

Calling evaluate_plo5_cards/evaluate_plo6_cards on a package built without the corresponding support raises NotImplementedError. evaluate_plo4_cards (and evaluate_omaha_cards) are always available.

Card Id

We can use an integer to represent a card. The two least significant bits represent the 4 suits, ranging from 0-3. The rest of it represents the 13 ranks, ranging from 0-12.

More specifically, the ranks are:

deuce = 0, trey = 1, four = 2, five = 3, six = 4, seven = 5, eight = 6, nine = 7, ten = 8, jack = 9, queen = 10, king = 11, ace = 12.

And the suits are: club = 0, diamond = 1, heart = 2, spade = 3

So that you can use rank * 4 + suit to get the card ID.

Technically, it is fine to swap the suit values, as long as the suits are uniquely mapped to the values 0, 1, 2, and 3. If you do swap the suit values, make sure to update the mapping in the source code as well (suit_map in card.py).

The complete card Id mapping can be found below. The rows are the ranks from 2 to Ace, and the columns are the suits: club, diamond, heart and spade.

C D H S
2 0 1 2 3
3 4 5 6 7
4 8 9 10 11
5 12 13 14 15
6 16 17 18 19
7 20 21 22 23
8 24 25 26 27
9 28 29 30 31
T 32 33 34 35
J 36 37 38 39
Q 40 41 42 43
K 44 45 46 47
A 48 49 50 51

Test

  • The functionality of the evaluators is tested against CSV test data in the test_data/ folder (generated by the C++ evaluator), covering five-card, six-card, seven-card, and PLO4 (Omaha) evaluations.
  • The Card module is tested against the Card Id table documented above.

The tests import the compiled phevaluator._pheval extension, so build it first (for example with an editable install, pip install -e .) and run the tests from the repository root of the Python package:

python -m unittest discover -v

Contributing

Thank you for your interest in contributing to the Python package of PHEvaluator! To ensure a smooth contribution process, please follow the guidelines below.

Requirements

  • Python 3.10 or newer
  • A C compiler (the evaluator is built as a C extension)
  • Ruff for linting and formatting
  • mypy for type checking

Code style

Development Setup

Install development dependencies:

pip install '.[dev]'

Checking Changes

You can install the package from the source code in editable mode:

pip install -e .

This allows the installed package to automatically reflect changes made in the phevaluator folder.

Building the Package

To build the package, run the following command:

python -m build

This will create a dist folder containing the built package.

Install the built package for testing:

pip install dist/*.whl

Check whether your distribution's long description will render correctly on PyPI:

python -m twine check dist/*

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

phevaluator-0.6.0.tar.gz (8.0 MB view details)

Uploaded Source

Built Distributions

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

phevaluator-0.6.0-cp314-cp314-win_amd64.whl (3.7 MB view details)

Uploaded CPython 3.14Windows x86-64

phevaluator-0.6.0-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

phevaluator-0.6.0-cp314-cp314-macosx_11_0_arm64.whl (3.9 MB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

phevaluator-0.6.0-cp314-cp314-macosx_10_15_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.14macOS 10.15+ x86-64

phevaluator-0.6.0-cp313-cp313-win_amd64.whl (3.7 MB view details)

Uploaded CPython 3.13Windows x86-64

phevaluator-0.6.0-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

phevaluator-0.6.0-cp313-cp313-macosx_11_0_arm64.whl (3.9 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

phevaluator-0.6.0-cp313-cp313-macosx_10_13_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

phevaluator-0.6.0-cp312-cp312-win_amd64.whl (3.7 MB view details)

Uploaded CPython 3.12Windows x86-64

phevaluator-0.6.0-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

phevaluator-0.6.0-cp312-cp312-macosx_11_0_arm64.whl (3.9 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

phevaluator-0.6.0-cp312-cp312-macosx_10_13_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

phevaluator-0.6.0-cp311-cp311-win_amd64.whl (3.7 MB view details)

Uploaded CPython 3.11Windows x86-64

phevaluator-0.6.0-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

phevaluator-0.6.0-cp311-cp311-macosx_11_0_arm64.whl (3.9 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

phevaluator-0.6.0-cp311-cp311-macosx_10_9_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

phevaluator-0.6.0-cp310-cp310-win_amd64.whl (3.7 MB view details)

Uploaded CPython 3.10Windows x86-64

phevaluator-0.6.0-cp310-cp310-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

phevaluator-0.6.0-cp310-cp310-macosx_11_0_arm64.whl (3.9 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

phevaluator-0.6.0-cp310-cp310-macosx_10_9_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

Details for the file phevaluator-0.6.0.tar.gz.

File metadata

  • Download URL: phevaluator-0.6.0.tar.gz
  • Upload date:
  • Size: 8.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for phevaluator-0.6.0.tar.gz
Algorithm Hash digest
SHA256 3fba3a05c2ef82fab969dcc1ac8f623f4b0d0a00c36493ca1f60838d205403a7
MD5 6ef216f8da965cbacfe4b761ce38d261
BLAKE2b-256 1cfade863004b3f3c3ec5b3959b91e74a8fad94165717e88f13a8dc997f6424c

See more details on using hashes here.

File details

Details for the file phevaluator-0.6.0-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for phevaluator-0.6.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 f67868bd9a19ceb5d3019a8237d4062a7fcec0b890da875c35c2286e46c6d463
MD5 54d02ae74f46a47bce425739f6688bd3
BLAKE2b-256 261af55d5f819a4089516fd8cabf8107c98e7ca0a081f69b14e619d4de4fee19

See more details on using hashes here.

File details

Details for the file phevaluator-0.6.0-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for phevaluator-0.6.0-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 4e8e028a1a810bfa8ca80fab72ce33b5282928e79f0151934b7dcf52b37fb8c4
MD5 ded363cc681bc9b356112700a07841f3
BLAKE2b-256 f22e2aadf96b56b2230eae52fd8a34a309bd30e0dd6c2b8fbfff18c2c5bcf5d5

See more details on using hashes here.

File details

Details for the file phevaluator-0.6.0-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for phevaluator-0.6.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ebefef82fdd49ae6b0325e014880104bf4de7ff73ce1ec536a57f4c3e3ff2b17
MD5 63e7b62d34f94c826457b4ce49e9e20b
BLAKE2b-256 978f92c1ca3a71cb8bc8f5e3ab55d0986efd493af9b25a236dc6cf8c28ffca47

See more details on using hashes here.

File details

Details for the file phevaluator-0.6.0-cp314-cp314-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for phevaluator-0.6.0-cp314-cp314-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 0a438d85246f272f85aca1bbd989febc0fca0b65dbddfaf004c5cefbe6091fc9
MD5 201d6cc742caad7bbb4107c9f539f3ac
BLAKE2b-256 05d206ecc43db2a4dfc837a92c7d9606c2ae65cbe296d0974a42ab66d49976cf

See more details on using hashes here.

File details

Details for the file phevaluator-0.6.0-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for phevaluator-0.6.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 f20b3b8d03a47152b2a302727c2e67adf8b77ea2505db3d2eb85ba51d1e5ad51
MD5 181083bd6403397ed54a4e8f9b733ee4
BLAKE2b-256 62bf76eff5b0f16a5c0b53f1964e149df20fe30876dfc7963b2f7ac0a5b0157b

See more details on using hashes here.

File details

Details for the file phevaluator-0.6.0-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for phevaluator-0.6.0-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 2992597d529848a9abb2146b26cb80782f9be6a9584a1409385f4533e5044ffa
MD5 a69e0167186f4dff502d073e75191c64
BLAKE2b-256 45e92024f24fc4a0922019cfee2666d12c5fca72d2148d8846a07df32ca6a1c0

See more details on using hashes here.

File details

Details for the file phevaluator-0.6.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for phevaluator-0.6.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 12de81a114a007541f079ef8368f332b6d753abbff4fe1075057dd23967a48c1
MD5 5b5115a61ef0d693f7323a57f2f0f3c6
BLAKE2b-256 9bd3553634084a57a9f7be27bd8e05c291c14721739edd475877947e0496af7d

See more details on using hashes here.

File details

Details for the file phevaluator-0.6.0-cp313-cp313-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for phevaluator-0.6.0-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 568a53e9f1eb9595ad6372f9b23aea38968571d2c529d6e88e6140c4af07f6da
MD5 cb21227f6b804289d252fc0552c0da21
BLAKE2b-256 a8c15021d9cbd9504cc13d88e7b078db3e345cbea9c1752852ce5d7cc43eb83c

See more details on using hashes here.

File details

Details for the file phevaluator-0.6.0-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for phevaluator-0.6.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 840c07870f68a4e0d8415c16124c2e0bae794cc61ffd15109b2b7d6b270085c4
MD5 b91b62ad490d823917ff9f125ee2ae9a
BLAKE2b-256 50d872d182159747256e60d3e60a6c78aab61d22158ff913f4c0c2d0e7898e90

See more details on using hashes here.

File details

Details for the file phevaluator-0.6.0-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for phevaluator-0.6.0-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 4d6c26a0ea8846e8c694f71897ef42a611389dc93473057f37e3fb5c81ed1da3
MD5 bebbefd8a77384e2db1a36629c51bdb2
BLAKE2b-256 7eb5d23931fa85c63eaef4b7042fe79e64eaf67eedae1195002b7b84a007e75c

See more details on using hashes here.

File details

Details for the file phevaluator-0.6.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for phevaluator-0.6.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 347927744a49dd637453a54bd92991623626e527f5eff0c29cae4bbbe3faf879
MD5 e9adc40f17f73f7127d774866ef4f426
BLAKE2b-256 59de22b4ecb0b0cf2883f799513c06aa0b095068c57859037d8a05cda5802120

See more details on using hashes here.

File details

Details for the file phevaluator-0.6.0-cp312-cp312-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for phevaluator-0.6.0-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 8de9990979a23c0d22f33cb6a750f3c72bed3f374298d40ee5ca4696785e5098
MD5 d5904c671352cbbc15c97db757e45072
BLAKE2b-256 8619f8707a60127ec017bf14157220800bf4e792f46124d9eec977a4d766e287

See more details on using hashes here.

File details

Details for the file phevaluator-0.6.0-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for phevaluator-0.6.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 b215938f069b979af1b5a142fecc5f70b86e9be7b10b051ee6b0fcbc63ea6483
MD5 605c17e6eab6e951f00bcb4ac75f322d
BLAKE2b-256 0e0ad632c620172532492343e32d8b3d92ad8eedd8de8715895ef41f5c66b3bd

See more details on using hashes here.

File details

Details for the file phevaluator-0.6.0-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for phevaluator-0.6.0-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 cd9079cdabc38b0cf3f7226660e1ca571623c51ddc8536bb84a4af745091f70e
MD5 22bbce021a76a09cf12747dca07882d1
BLAKE2b-256 243058a38c2e062cec5ce4a5f2ca14cb36213e343d3c5ae9eeea513e013d3315

See more details on using hashes here.

File details

Details for the file phevaluator-0.6.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for phevaluator-0.6.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 16130428fd5406fdba24bf39135a0dcfbbefade32051553cf2813110602e57d6
MD5 d32c53178febbf74a7adc5977ed27079
BLAKE2b-256 7ffe584c640490d5383a9ef5600ae1b4754260c64a501cfaff1cf9bb8b62509d

See more details on using hashes here.

File details

Details for the file phevaluator-0.6.0-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for phevaluator-0.6.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 229ed1e81278fb1eeb2bac1559baee4b40d3fc2949f5f9c23ad44f67431595c1
MD5 a40e778d8db8f27e1e6641713deca7f0
BLAKE2b-256 bdab49568616efc2c91f859815923a641873c29eec3f4fdd90e9ecc9a37f354f

See more details on using hashes here.

File details

Details for the file phevaluator-0.6.0-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for phevaluator-0.6.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 887ba0446cb6991d22cf3b499f097e7075cbdb2090b743eb9ab4979851de2ff9
MD5 8815f7af4aaaf8f7a85ea3b4dae1be31
BLAKE2b-256 fb28f13ec6ce526b4d1afe897870765078dc6d89cc46245359796ceb0fe62475

See more details on using hashes here.

File details

Details for the file phevaluator-0.6.0-cp310-cp310-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for phevaluator-0.6.0-cp310-cp310-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 bb46d29640fe25488521a3fbd338bfd53f57286f61e1c7fbf0bb0f74d8d4cd7f
MD5 5cf42a433cb986bf95ac8d5dcdf2b688
BLAKE2b-256 c15374abfdc5fadb271025da93799fe51ccd31909444801d4fb91e057723b2d5

See more details on using hashes here.

File details

Details for the file phevaluator-0.6.0-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for phevaluator-0.6.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4f1d1b64c692e95a8ac62bb3b2dd380a92c2197571b1174dc62a40966273f5a5
MD5 d81735d1132e1a1bc65b2825d2a8ca10
BLAKE2b-256 299a4b8633950ad7a386dd3d6f7e9a0c98a3274072dafdb6baff3dc0942ed513

See more details on using hashes here.

File details

Details for the file phevaluator-0.6.0-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for phevaluator-0.6.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 1f31e89dc310fd24bee7b4789c84f4b64093f14c81482b99663a3469b84b742c
MD5 df1142d5ffa2114528aa614de70fcf48
BLAKE2b-256 bddc88195b46bd56c77886e03a4793ee9b75e053eff6aecfe060f4da832e8601

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 Sentry Error logging StatusPage Status page