Mapping Module

Module: piethorn.collections.mapping

Overview

This module provides:

  • Pair, a small immutable key/value holder

  • Map, an ordered mutable mapping implemented with parallel key and value lists

Pair

class Pair(key, value)

Frozen dataclass used when a lightweight key/value pair object is needed.

Map

class Map(keys=None, values=None, *, loop_fill=False)

Dictionary-like container that preserves insertion order and exposes list-style index helpers.

Construction rules

  • keys and values must either both be None or both be sequences

  • the sequences must have the same length

  • when loop_fill=False, mutable sequences may be stored directly

Example

from piethorn.collections.mapping import Map

mapping = Map(["a", "b"], [1, 2])
mapping["a"] = 3
mapping["c"] = 4

Key methods

has_key(key)

Return whether the key exists.

has_value(value)

Return whether an equal value exists.

key_index(key, start=0, stop=sys.maxsize)

Return the insertion index of a key.

value_index(value, start=0, stop=sys.maxsize)

Return the insertion index of a value.

key_at_index(index)

Return the key stored at an insertion index.

value_at_index(index)

Return the value stored at an insertion index.

Example sequence of calls

mapping.key_index("c")     # 2
mapping.value_index(2)     # 1
mapping.key_at_index(0)    # "a"
mapping.value_at_index(2)  # 4
list(mapping)              # ["a", "b", "c"]
list(reversed(mapping))    # ["c", "b", "a"]

Autodoc

class piethorn.collections.mapping.Map(keys: Sequence | None = None, values: Sequence | None = None, *, loop_fill=False)

A Map is like a dictionary, but allows for any value type as a key.

has_key(key)

Return whether key exists in the map.

has_value(key)

Return whether a value equal to key exists in the map.

key_at_index(index)

Return the key stored at index.

key_index(key, start=0, stop=9223372036854775807)

Return the index of key in insertion order.

value_at_index(index)

Return the value stored at index.

value_index(value, start=0, stop=9223372036854775807)

Return the index of value in insertion order.

class piethorn.collections.mapping.Pair(key: KT, value: VT)

Lightweight (key, value) holder used to keep key->value pairs.

key: KT
value: VT