Skip to content

General function

GeneralFunctionExtract

Bases: ExtractionMethod

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

Method name: general_function

Example Configuration

.. code-block:: yaml

- method: general_function
  inputs:
    funtion:import.path.to.the.fuction
    args:
      - hello
      - world
    kwargs:
      hello: world
      foo: bar
Source code in extraction_methods/plugins/general_function.py
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
class GeneralFunctionExtract(ExtractionMethod):
    """
    Accepts a dictionary. String values are popped from the dictionary and
    are put back into the dictionary with the ``key`` specified.

    **Method name:** ``general_function``

    Example Configuration:
        .. code-block:: yaml

            - method: general_function
              inputs:
                funtion:import.path.to.the.fuction
                args:
                  - hello
                  - world
                kwargs:
                  hello: world
                  foo: bar
    """

    input_class = GeneralFunctionInput

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

        module_name, function_name = self.input.function.rsplit(self.input.delimiter, 1)

        module = importlib.import_module(module_name)

        function = getattr(module, function_name)

        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

GeneralFunctionInput

Bases: Input

Model for General Fuction Input.

Parameters:

Name Type Description Default
function str

Function to be run name can be seperatated by delimieter.

required
delimiter str

text delimiter to put between module/function names.

'.'
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, else response will be merged with body.

''
Source code in extraction_methods/plugins/general_function.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class GeneralFunctionInput(Input):
    """
    Model for General Fuction Input.
    """

    function: str = Field(
        description="Function to be run name can be seperatated by delimieter.",
    )
    delimiter: str = Field(
        default=".",
        description="text delimiter to put between module/function names.",
    )
    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="",
        description="key to output to, else response will be merged with body.",
    )