Skip to main content

A Python package for calculating the winner of a poll using rank choice voting (Instant-runoff voting).

Project description

rank-choicer

A Python package for calculating the winner of a poll using rank choice voting (Instant-runoff voting).

Install

Use pip to install:

pip install rank-choicer

Usage

First, set up the counter with the available options:

from rank_choicer import RankChoiceCounter
counter = RankChoiceCounter(["A", "B", "C"])

Then, you can pass in the votes to its count_votes method which returns the winner:

votes = {
    "voter1": ["A", "B", "C"],
    "voter2": ["B", "A", "C"],
    "voter3": ["C", "A", "B"],
    "voter4": ["A", "C", "B"],
    "voter5": ["B", "C", "A"],
}
winner = counter.count_votes(votes)
print(f"Winner is: {winner}")
Winner is A

Viewing Specific Rounds

Sometimes, you may want to review the different rounds of elimination for either analysis or visualization. You can do so by calling the get_round_results method:

results = counter.get_round_results()
print(f"Eliminated in the first round: {results[0].eliminated_options}")
print(results[0].vote_counts)
Eliminated in the first round: [C]

Handling Ties In Elimination

In rare cases, you may have more than one option with the lowest votes. In those cases, you can handle what to eliminate in two ways:

  • Randomly eliminate one of the options with lowest votes
  • Eliminate all options tied for lowest votes

The RankChoiceCounter defaults to random but you can change the strategy used using the elimination_strategy parameter:

from rank_choicer import EliminationStrategy 
counter = RankChoiceCounter(
    ["A", "B", "C", "D"], elimination_strategy=EliminationStrategy.BATCH
)

Contributing

Community made feature requests, patches, bug reports, and contributions are always welcome.

Please review our contributing guidelines if you decide to make a contribution.

License

This project is licensed under the MIT License. See LICENSE for more details.

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

rank_choicer-1.0.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

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

rank_choicer-1.0.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file rank_choicer-1.0.0.tar.gz.

File metadata

  • Download URL: rank_choicer-1.0.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.10.9 Windows/10

File hashes

Hashes for rank_choicer-1.0.0.tar.gz
Algorithm Hash digest
SHA256 12144a6f16ddf2f4ea25c8173f30b8722f1a26772d65bd86eb77fb5e52f77bfb
MD5 a6c0c3fc8f840430e621e23a530bb8f5
BLAKE2b-256 c86c72af5aa541b730d9c86f7893a35fa9dbcec9758052fb8b03b4a170eedef7

See more details on using hashes here.

File details

Details for the file rank_choicer-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: rank_choicer-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.10.9 Windows/10

File hashes

Hashes for rank_choicer-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9d48ac1382981725ad7d0b66ee5fc28a8662849cf1e91c4d955909519843c015
MD5 3211a066d3c218a5d62e57622ef3ea15
BLAKE2b-256 edc2b16ec85590195b0cb9165863abd12765b96d716e96c033ef59a5de7a1b3f

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