Skip to content

idstore

IDStore

Store for ids. Used to create unique IDs on a page.

Source code in mkreports/md/idstore.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class IDStore:
    """
    Store for ids. Used to create unique IDs on a page.
    """

    _count: Dict[str, int]
    _used: Set[str]
    _start_with: int

    def __init__(self, start_with: int = 0, used_ids: Set[str] = set()) -> None:
        """
        Initialize the IDStore.

        Args:
            start_with (int): First value of the counter.
            used_ids (set[str]): Set of IDs that have to be avoided as they are
                otherwise used.
        """
        self._count = defaultdict(partial(identity, start_with - 1))
        self._used = copy(used_ids)
        self._start_with = start_with

    def _increment(self, prefix: str) -> int:
        """
        Returns the next value of the counter (and increments).
        """
        self._count[prefix] += 1
        return self._count[prefix]

    def next_id(self, prefix: str) -> str:
        """
        Returns an id with a counted number at the end.

        Args:
            prefix (str): Prefix to be used for the ID.

        Returns:
            str: ID as a string.
        """
        # get the next id until it has not been used yet
        while (next_id := f"{prefix}-{self._increment(prefix)}") in self._used:
            pass
        self._used.add(next_id)
        return next_id

    def __eq__(self, other):
        if type(self) != type(other):
            return False
        return self.__dict__ == other.__dict__

__init__(start_with=0, used_ids=set())

Initialize the IDStore.

Parameters:

Name Type Description Default
start_with int

First value of the counter.

0
used_ids set[str]

Set of IDs that have to be avoided as they are otherwise used.

set()
Source code in mkreports/md/idstore.py
20
21
22
23
24
25
26
27
28
29
30
31
def __init__(self, start_with: int = 0, used_ids: Set[str] = set()) -> None:
    """
    Initialize the IDStore.

    Args:
        start_with (int): First value of the counter.
        used_ids (set[str]): Set of IDs that have to be avoided as they are
            otherwise used.
    """
    self._count = defaultdict(partial(identity, start_with - 1))
    self._used = copy(used_ids)
    self._start_with = start_with

_increment(prefix)

Returns the next value of the counter (and increments).

Source code in mkreports/md/idstore.py
33
34
35
36
37
38
def _increment(self, prefix: str) -> int:
    """
    Returns the next value of the counter (and increments).
    """
    self._count[prefix] += 1
    return self._count[prefix]

next_id(prefix)

Returns an id with a counted number at the end.

Parameters:

Name Type Description Default
prefix str

Prefix to be used for the ID.

required

Returns:

Name Type Description
str str

ID as a string.

Source code in mkreports/md/idstore.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def next_id(self, prefix: str) -> str:
    """
    Returns an id with a counted number at the end.

    Args:
        prefix (str): Prefix to be used for the ID.

    Returns:
        str: ID as a string.
    """
    # get the next id until it has not been used yet
    while (next_id := f"{prefix}-{self._increment(prefix)}") in self._used:
        pass
    self._used.add(next_id)
    return next_id