Ranking Module

class hivemind.ranking.Ranking[source]

Bases: object

A class for managing ranked choice voting.

This class handles both fixed and automatic ranking of options in the Hivemind protocol. It supports different ranking strategies including fixed rankings and automatic rankings based on proximity to a preferred choice.

Variables:
  • fixed (List[str] | None) – List of fixed ranked choices

  • auto (str | None) – The preferred choice for automatic ranking

  • type (str | None) – The type of ranking (‘fixed’, ‘auto_high’, or ‘auto_low’)

__init__() None[source]

Initialize a new Ranking instance.

Returns:

None

get(options: List[HivemindOption] | None = None) List[str][source]

Get the ranked choices.

Parameters:

options (List[HivemindOption] | None) – List of HivemindOptions, required for auto ranking

Returns:

A list of option cids in ranked order

Return type:

List[str]

Raises:

Exception – If ranking is not set or options are invalid for auto ranking

set_auto_high(choice: str) None[source]

Set the ranking to auto high.

The ranking will be calculated at runtime by given options, ordered by the values closest to preferred choice. In case 2 options are equally distant to preferred choice, the higher option has preference.

Parameters:

choice (str) – Option cid of the preferred choice

Raises:

Exception – If choice is invalid

Returns:

None

set_auto_low(choice: str) None[source]

Set the ranking to auto low.

The ranking will be calculated at runtime by given options, ordered by the values closest to preferred choice. In case 2 options are equally distant to preferred choice, the lower option has preference.

Parameters:

choice (str) – Option cid of the preferred choice

Raises:

Exception – If choice is invalid

Returns:

None

set_fixed(ranked_choice: List[str]) None[source]

Set a fixed ranked choice.

Parameters:

ranked_choice (List[str]) – A list of option cids in order of preference

Raises:

Exception – If ranked_choice is invalid

Returns:

None

to_dict() Dict[str, Any][source]

Convert ranking settings to dict for IPFS storage.

Returns:

Ranking settings as a Dict

Return type:

Dict[str, Any]