Skip to main content

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.4.0

  • cli: export candidates, preferences or district seats
  • cli: '-' is interpreted as stdin/stdout
  • cli: added result filtering options to calculate subcommand
  • added write method to PreferencesFile
  • test with pypy3

0.3.0

  • Use slots for dataclasses
  • Fix __str__ for sparse and graphs
  • Move preference types to methods
  • Fix type-hint errors (mypy --strict)
  • Fix urls for source and bug reports
  • Fix belgian imperiali divisor iterator

0.2.0

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

0.1.0

  • Initial public release

Metadata

Release files for interregnum 0.4.1

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

Source distribution (sdist)

Source distribution for interregnum 0.4.1
File Size Uploaded Provenance
interregnum-0.4.1.tar.gz 2.6 MB Without provenance Details

Built distribution (wheel)

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

Total release size: 5.0 MB

Release files / interregnum-0.4.1.tar.gz

Download URL interregnum-0.4.1.tar.gz
Size 2.6 MB
Tags Source
SHA-256 checksum
How to use checksums
d643bfd23d35d630f13d45a0c9f618af42226701a74e0fbd76bf070e5c80e00d
BLAKE2b-256 checksum
How to use checksums
1e0b23520a39de7a8778eb5009dfb2a1a0fd24587583216c6c94ea57b42c2fa6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.2.0 CPython/3.10.20

Release files / interregnum-0.4.1-py3-none-any.whl

Download URL interregnum-0.4.1-py3-none-any.whl
Size 2.4 MB
Tags Python 3
SHA-256 checksum
How to use checksums
2e884185fdab93b5e5bfe6aafda1afd29387e898f7c9ee19d043a93916d112f3
BLAKE2b-256 checksum
How to use checksums
bb7a6fafd388d5fdf67ab2826d252f876de50f4307db3f931982dfe3ccfd4e0b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.2.0 CPython/3.10.20

Security for interregnum 0.4.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

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