Coverage for src/gentrie/trie/mutation.py: 100%

1# -*- coding: utf-8 -*-

2"""Mutation operations for the trie."""

3

4 from typing import Any

5

6 from ..protocols import GeneralizedKey

7 from ..types import TrieId

8

9 from .trie_mixins import TrieMixinsInterface

10

11

12 class TrieMutationMixin:

13 """Mixin providing mutation operations."""

14

15 def __setitem__(self: TrieMixinsInterface, key: GeneralizedKey, value: Any) -> None:

16 """Adds or updates an entry in the trie using subscript notation.

17

18 This is a convenience wrapper for the :meth:`update` method.

19

20 Example:

21 trie[['ape', 'green', 'apple']] = "A green apple"

22

23 Args:

24 key (GeneralizedKey): The key for the entry.

25 value (Any): The value to associate with the key.

26

27 Raises:

28 InvalidGeneralizedKeyError: If key is not a valid :class:`GeneralizedKey`.

29 """

30 self.update(key, value)

31

32 def __delitem__(self: TrieMixinsInterface, key: TrieId | GeneralizedKey) -> None:

33 """Removes an entry from the trie using subscript notation.

34

35 This is a convenience wrapper for the :meth:`remove` method.

36

37 Example:

38 del trie[['ape', 'green', 'apple']]

39

40 Args:

41 key (TrieId | GeneralizedKey): The identifier for the entry to remove.

42

43 Raises:

44 KeyError: if the key does not exist in the trie.

45 TypeError: if the key is not a :class:`TrieId` or a valid :class:`GeneralizedKey`.

46 """

47 self.remove(key)