Skip to content

dict

CLASS DESCRIPTION
LeastRecentlyUsedDict

A dict with a maximum size, evicting the least recently written key when full.

FUNCTION DESCRIPTION
from_str_values

Convert a sequence of 'key=value' strings into a dictionary.

LeastRecentlyUsedDict(maxsize: int = 128, *args: Any, **kwargs: Any)

A dict with a maximum size, evicting the least recently written key when full.

Getting a key that is not present returns a default value of 0.

Setting a key marks it as most recently used and removes the oldest key if full.

May be useful for tracking the count of items where limited memory usage is needed even if the set of items can be unlimited.

Based on the example at https://docs.python.org/3/library/collections.html#ordereddict-examples-and-recipes

Source code in nava/platform/util/collections/dict.py
41
42
43
def __init__(self, maxsize: int = 128, *args: Any, **kwargs: Any) -> None:
    self.maxsize = maxsize
    super().__init__(*args, **kwargs)
from_str_values(value: Iterable[str] | None) -> dict[str, str] | None

Convert a sequence of 'key=value' strings into a dictionary.

Source code in nava/platform/util/collections/dict.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def from_str_values(value: Iterable[str] | None) -> dict[str, str] | None:
    """Convert a sequence of 'key=value' strings into a dictionary."""
    result = {}

    if value is None:
        return None

    for val in value:
        if not val.strip():
            continue

        k, v = val.split("=")

        if k in result:
            raise ValueError(f"Data {k} is specified twice")

        result[k] = v

    return result or None