Skip to main content

multi-district electoral systems

Project description

L. Cassius Longinus. 60 BC. AR Denarius"{ width="330px" margin="0 auto" align="center" }

interregnum

This package provides electoral apportionment methods and it allows to build electoral systems based on multiple constituencies/districts.

Features:

  • proportional, preferential and bi-proportional methods
  • compensatory systems
  • district based systems
  • constraints definition system (for example: vote threshold)

Installation

interregnum is available on PyPI. It can be installed using pip:

pip install interregnum

Python $\ge 3.10$ is required.

Quickstart

Command line interface

:::{seealso} For details, read the {doc}CLI documentation <cli>. :::

The CLI app allows to calculate some data using an electoral system.

Let's create a sample electoral system: use the Highest Averages method using the D'Hondt divisor.

# freedonia.yaml
name: Freedonia - Local elections 1933
method: highest_averages
method_params:
    divisor_f: dhondt
seats: 3
candidates:
- name: Freedonia for Rufus T. Firefly
  votes: 8950
- name: Sylvania for Louis Clahern
  votes: 5203

Now, let's calculate:

interregnum-cli calc freedonia.yaml results.json --results seats.tsv

The original YAML file has been saved to a JSON file named results.json, with the same information, plus a field results containing the allocated seats and additional information about what the method internally did (event log, random state, etc.):

{
    "resume_allocation": false,
    "initial_seats": "min_seats",
    "method": "highest_averages",
    "method_params": {
        "divisor_f": "dhondt"
    },
    "type": "district",
    "name": "Freedonia - Local elections 1933",
    "result": {
        "allocation": [
            {
                "name": {
                    "name": "Freedonia for Rufus T. Firefly",
                    "alliance": "Freedonia for Rufus T. Firefly"
                },
                "votes": 8950,
                "seats": 2
            },
            {
                "name": {
                    "name": "Sylvania for Louis Clahern",
                    "alliance": "Sylvania for Louis Clahern"
                },
                "votes": 5203,
                "seats": 1
            }
        ],
        "data": {
            "log": [
                {
                    "EVENT": "winner",
                    "target": {
                        "name": "Freedonia for Rufus T. Firefly",
                        "alliance": "Freedonia for Rufus T. Firefly"
                    },
                    "criterion": "best_quotient",
                    "quota": 8950
                },
                {
                    "EVENT": "winner",
                    "target": {
                        "name": "Sylvania for Louis Clahern",
                        "alliance": "Sylvania for Louis Clahern"
                    },
                    "criterion": "best_quotient",
                    "quota": 5203
                },
                {
                    "EVENT": "winner",
                    "target": {
                        "name": "Freedonia for Rufus T. Firefly",
                        "alliance": "Freedonia for Rufus T. Firefly"
                    },
                    "criterion": "best_quotient",
                    "quota": 4475
                }
            ],
            "max_quota": 8950,
            "min_quota": 4475,
            "remaining_seats": 0
        },
        "deterministic": true,
        "random_state": {}
    },
    "candidates": [
        {
            "name": "Freedonia for Rufus T. Firefly",
            "votes": 8950,
            "groups": []
        },
        {
            "name": "Sylvania for Louis Clahern",
            "votes": 5203,
            "groups": []
        }
    ],
    "seats": 3
}

Additionally, we saved a tabular file seats.tsv including only the allocation result:

node    district        name    alliance        votes   seats   min_seats       max_seats       groups  meta
Freedonia - Local elections 1933                Freedonia for Rufus T. Firefly  Freedonia for Rufus T. Firefly  8950    2       0
Freedonia - Local elections 1933                Sylvania for Louis Clahern      Sylvania for Louis Clahern      5203    1       0

Python library

:::{seealso} For details, read the {doc}API documentation <modules/modules>. :::

We will calculate the same data using the HighestAveragesAllocator class:

from interregnum.methods.singlevote import HighestAveragesAllocator

# create a d'Hondt allocator
dhondt = HighestAveragesAllocator("dhondt")
# allocate 3 seats for given set of votes using the created allocator
result = dhondt(
    candidates=[("Freedonia for Rufus T. Firefly", 8950),
                ("Sylvania for Louis Clahern", 5203)],
    seats=3
)

# content: result.allocation
[Candidate(name='Freedonia for Rufus T. Firefly', votes=8950, seats=2),
 Candidate(name='Sylvania for Louis Clahern', votes=5203, seats=1)]

# content: result.data.log
[QuotaWinnerEvent(target='Freedonia for Rufus T. Firefly', criterion='best_quotient', quota=Fraction(8950, 1)),
 QuotaWinnerEvent(target='Sylvania for Louis Clahern', criterion='best_quotient', quota=Fraction(5203, 1)),
 QuotaWinnerEvent(target='Freedonia for Rufus T. Firefly', criterion='best_quotient', quota=Fraction(4475, 1))]

Electoral systems based on districts

It is possible to create complex electoral systems whose districts can be expressed as a directed acyclic graph. A district can depend on another district's elements: tallied votes or results.

Changelog

0.2.0

  • Command line: generate a dependency graph in Graphviz DOT format from an electoral system

0.1.0

  • Initial public release

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

interregnum-0.2.0.tar.gz (2.5 MB view details)

Uploaded Source

Built Distribution

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

interregnum-0.2.0-py3-none-any.whl (2.4 MB view details)

Uploaded Python 3

File details

Details for the file interregnum-0.2.0.tar.gz.

File metadata

  • Download URL: interregnum-0.2.0.tar.gz
  • Upload date:
  • Size: 2.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.18

File hashes

Hashes for interregnum-0.2.0.tar.gz
Algorithm Hash digest
SHA256 804c224c1606f9dc8a8d277389cf116501cd1a6845a36b376dcf97850f37806d
MD5 9ef4b1abd361bc58b274f09ee3918879
BLAKE2b-256 bc4b42a87140b244feab49f8ca0664fc39a1a1efae7359654cbbb4dda37555c9

See more details on using hashes here.

File details

Details for the file interregnum-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: interregnum-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 2.4 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.18

File hashes

Hashes for interregnum-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 39f81d37e2f477a464d6924f7ed373281d88c6b7658422f10eea9f84d1b58116
MD5 ed28d6a9eb7a5862c3647d83c26e1fde
BLAKE2b-256 c7a597d4b9e48fb119735eab5b636e8c3801a92269c628ffb88e1317354d0490

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