Skip to content

Lambda

LambdaExtract

Bases: ExtractionMethod

Description: Accepts a dictionary. String values are popped from the dictionary and are put back into the dictionary with the key specified.

Method name: lambda

Example Configuration

.. code-block:: yaml

- method: lambda
  inputs:
    function: 'lambda x: x * x'
    args:
      - hello
      - $world
    kwargs:
      hello: world
      goodbye: all
Source code in extraction_methods/plugins/lambda.py
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
class LambdaExtract(ExtractionMethod):
    """
    Description: Accepts a dictionary. String values are popped from the dictionary and
    are put back into the dictionary with the ``key`` specified.

    **Method name:** ``lambda``

    Example Configuration:
        .. code-block:: yaml

            - method: lambda
              inputs:
                function: 'lambda x: x * x'
                args:
                  - hello
                  - $world
                kwargs:
                  hello: world
                  goodbye: all
    """

    input_class = LambdaInput

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

        output_body = body.copy()

        function = eval(self.input.function)  # nosec B307

        result = function(*self.input.args, **self.input.kwargs)

        if self.input.output_key:
            output_body[self.input.output_key] = result

        elif isinstance(result, dict):
            output_body |= result

        return output_body

LambdaInput

Bases: Input

Model for Lambda Input.

Parameters:

Name Type Description Default
function str

lambda function to be run.

required
args list[Any]

list of arguments for function.

[]
kwargs dict[str, Any]

dictionary of key word arguments for function.

{}
output_key str

key to output to.

'label'
Source code in extraction_methods/plugins/lambda.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class LambdaInput(Input):  # type: ignore[no-redef]
    """
    Model for Lambda Input.
    """

    function: str = Field(
        description="lambda function to be run.",
    )
    args: list[Any] = Field(
        default=[],
        description="list of arguments for function.",
    )
    kwargs: dict[str, Any] = Field(
        default={},
        description="dictionary of key word arguments for function.",
    )
    output_key: str = Field(
        default="label",
        description="key to output to.",
    )