Skip to main content

In pure python

27 January 2011, Alvin Liang

Introduction

This is a pure python library to calculate the rank of the best poker hand out of 5, 6, or 7 cards. It does not run the board for you, or calculate winning percentage, EV, or anything like that. But if you give it two hands and the same board, you will be able to tell which hand wins.

It is nowhere near as fast as pypoker-eval, but it works if you can’t use C for some reason (the early stages of the first MIT pokerbot competition come to mind). The core algorithm is slower, and you obviously don’t have the speed of C.

Quick Start

from pokereval.card import Card
from pokereval.hand_evaluator import HandEvaluator

hole = [Card(2, 1), Card(2, 2)]
board = []
score = HandEvaluator.evaluate_hand(hole, board)

Rank is 2-14 representing 2-A, while suit is 1-4 representing spades, hearts, diamonds, clubs.

The Card constructor accepts two arguments, rank, and suit.

aceOfSpades = Card(14, 1)
twoOfDiamonds = Card(2, 3)

Algorithm

The algorithm for 5 cards is just a port of the algorithm that used to be at the following URL. (I purposely broke the link because it now hosts a malware site.) httx://wwx.suffecool.net/poker/evaluator.html

I came up with the 6 and 7 card evaluators myself, using a very similar card representation and applying some of the same ideas with prime numbers. The idea was to strike a balance between lookup table size and speed.

Also, I haven’t included the code I used to generate the lookup tables, but you should be able to do that with a simpler, slower algorithm. Maybe I’ll add that later as well.

There is also a two-card ranking/percentile algorithm that is unrelated to the rest and may get cleaned up later. We used it at one point for some pre-flop evaluation. Credit to Zach Wissner-Gross for developing this.

Documentation is sparse at the moment, sorry about that, and obviously I did not really bother to package it or clean it up. I may or may not work on this in the future. Basically, I made it, so why not release it?

Contributors

  • Me! Go me!

  • Zach Wissner-Gross (2-card algorithm)

  • arslr (Fixes for other Python versions)

  • Jim Kelly (Help with packaging, additional documentation)

  • hwmrocker (Improvements to Card constructor, Python 3 compatibility)

  • radekj (Tests, Python 3 compatibility)

Release files for pokereval 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pokereval 0.2.0
File Size Uploaded
pokereval-0.2.0.tar.gz 568.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pokereval 0.2.0
File Interpreter ABI Platform
pokereval-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 1.1 MB

Release files / pokereval-0.2.0.tar.gz

Download URL pokereval-0.2.0.tar.gz
Size 568.8 kB
Tags Source
SHA-256 checksum
How to use checksums
b85ab5cc10f9392da474b5538803ea1a89ff41a02c231f498497b3bba95f2e3a
BLAKE2b-256 checksum
How to use checksums
ed4f987a9edbad5b35eb7a6eede7834e39c4605e2f29bcae0e212545fbcb8a90
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.7.5

Release files / pokereval-0.2.0-py3-none-any.whl

Download URL pokereval-0.2.0-py3-none-any.whl
Size 573.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cbcdac21a2cbd329998c2ab39cd2e30ad4ec4215c1bf7e0ff2ff017c076472f4
BLAKE2b-256 checksum
How to use checksums
305ab20017b8963dcc1e8a38cd74a832266ed6ea0f215d3903218358dfcc0327
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.7.5

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page