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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: rank_choicer-1.0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 447880f8ce5270a7e6bcb0fd28b94daf060cc515157b4503ea82d3eaa0cc14c2
MD5 92eb6d5e9eaf2512c413e39fc95241f4
BLAKE2b-256 1860e6729751988a374af327ae677fdb38f71b90cd617d8fcfef72a462fcbc48

See more details on using hashes here.

File details

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

File metadata

  • Download URL: rank_choicer-1.0.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4b242a9b056ceb63f6b3c52b98e09a39c18b56c08bdb47577dd6d05cd712cbd4
MD5 f017254a484c0b8a7dbf604ff643f32c
BLAKE2b-256 3e89b136a0146dd34f4cf845ef9411d9bdf38dffcd62a2e6c59e8f25c060d953

See more details on using hashes here.

Supported by

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