Skip to content

Remove

RemoveExtract

Bases: ExtractionMethod

remove keys from body.

Method name: remove

Example Configuration

.. code-block:: yaml

- method: remove
  inputs:
    keys:
      - hello
      - world
Source code in extraction_methods/plugins/remove.py
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
class RemoveExtract(ExtractionMethod):
    """
    remove keys from body.

    **Method name:** ``remove``

    Example Configuration:
        .. code-block:: yaml

            - method: remove
              inputs:
                keys:
                  - hello
                  - world
    """

    input_class = RemoveInput

    def matching_keys(self, keys: KeysView[str], key_regex: str) -> list[str]:
        """
        Find all keys that match regex

        :param keys: dictionary keys to test
        :type keys: KeysView
        :param key_regex: regex to test against
        :type key_regex: str

        :return: matching keys
        :rtype: list
        """

        regex = re.compile(key_regex)

        return list(filter(regex.match, keys))

    def remove_key(self, body: dict[str, Any], key_parts: list[str]) -> dict[str, Any]:
        """
        Remove nested terms

        :param body: current body
        :type body: dict
        :param key_parts: key parts seperated by delimiter
        :type key_parts: list

        :return: dict
        :rtype: update body
        """

        for key in self.matching_keys(body.keys(), key_parts[0]):

            if len(key_parts) > 1:
                body[key] = self.remove_key(body[key], key_parts[1:])

            else:
                del body[key]

        return body

    @update_input
    def run(self, body: dict[str, Any]) -> dict[str, Any]:

        for key in self.input.keys:
            body = self.remove_key(body, key.split(self.input.delimiter))

        return body

matching_keys(keys, key_regex)

Find all keys that match regex

:param keys: dictionary keys to test :type keys: KeysView :param key_regex: regex to test against :type key_regex: str

:return: matching keys :rtype: list

Source code in extraction_methods/plugins/remove.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def matching_keys(self, keys: KeysView[str], key_regex: str) -> list[str]:
    """
    Find all keys that match regex

    :param keys: dictionary keys to test
    :type keys: KeysView
    :param key_regex: regex to test against
    :type key_regex: str

    :return: matching keys
    :rtype: list
    """

    regex = re.compile(key_regex)

    return list(filter(regex.match, keys))

remove_key(body, key_parts)

Remove nested terms

:param body: current body :type body: dict :param key_parts: key parts seperated by delimiter :type key_parts: list

:return: dict :rtype: update body

Source code in extraction_methods/plugins/remove.py
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def remove_key(self, body: dict[str, Any], key_parts: list[str]) -> dict[str, Any]:
    """
    Remove nested terms

    :param body: current body
    :type body: dict
    :param key_parts: key parts seperated by delimiter
    :type key_parts: list

    :return: dict
    :rtype: update body
    """

    for key in self.matching_keys(body.keys(), key_parts[0]):

        if len(key_parts) > 1:
            body[key] = self.remove_key(body[key], key_parts[1:])

        else:
            del body[key]

    return body

RemoveInput

Bases: Input

Model for Remove Input.

Parameters:

Name Type Description Default
keys list[str]

list of keys to remove.

required
delimiter str

delimiter for nested term.

'.'
Source code in extraction_methods/plugins/remove.py
23
24
25
26
27
28
29
30
31
32
33
34
class RemoveInput(Input):
    """
    Model for Remove Input.
    """

    keys: list[str] = Field(
        description="list of keys to remove.",
    )
    delimiter: str = Field(
        default=".",
        description="delimiter for nested term.",
    )