Skip to content

func

Module to work with retrieving arguments functions

FuncDefaultParser

Bases: SourceParser

Builds the parser to extract default arguments from a function signature

This is the lowest-ranked parser.

Parameters:

  • label (str) –

    The debugging label to indicate an argument was set at the CLI.

  • rank (int) –

    The priority of the parser. Generally, we aim between [0,100] for human-readabilty.

Source code in src/argmerge/func.py
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
60
61
62
63
64
65
66
67
68
69
class FuncDefaultParser(SourceParser):
    """Builds the parser to extract default arguments from a function signature

    This is the lowest-ranked parser.

    params:
        label (str): The debugging label to indicate an argument was set at the CLI.
        rank (int): The priority of the parser. Generally, we aim between [0,100] for
            human-readabilty.

    """

    label: str = "Python Function Default"
    rank: int = 0

    def __call__(
        cls,
        threshold_kwargs: dict,
        change_ledger: dict,
        f: Callable,
        debug: bool = False,
    ) -> tuple[dict, dict]:
        """Lowest level parser - retrieve function defaults as fallback arguments.

        Args:
            threshold_kwargs (dict[str, Any]): kwargs passed around the
                @threshold decorator.
            change_ledger (dict[str, dict[str, str  |  int]]): Tracks when kwargs are
                updated inside the @threshold decorator.
            f (Callable): The function we wrap.
            debug (bool, optional): Flag to turn on more logging. Defaults to False.


        Returns:
            tuple[dict, dict]: an updated `threshold_kwargs` and `change_ledger`.
        """

        if debug:
            LOGGER.remove()
            LOGGER.add(sys.stderr, level="DEBUG")

        _sig = signature(f)
        LOGGER.debug(f"Function {signature=}")
        _default: dict = {}

        for k, v in _sig.parameters.items():
            if v.default is not Parameter.empty:
                _default[k] = v.default

        LOGGER.debug(f"{_default=}")
        for k in _default:
            change_ledger[k] = {"label": cls.label, "rank": cls.rank}

        threshold_kwargs.update(**_default)

        return threshold_kwargs, change_ledger

__call__

__call__(threshold_kwargs, change_ledger, f, debug=False)

Lowest level parser - retrieve function defaults as fallback arguments.

Parameters:

  • threshold_kwargs (dict[str, Any]) –

    kwargs passed around the @threshold decorator.

  • change_ledger (dict[str, dict[str, str | int]]) –

    Tracks when kwargs are updated inside the @threshold decorator.

  • f (Callable) –

    The function we wrap.

  • debug (bool, default: False ) –

    Flag to turn on more logging. Defaults to False.

Returns:

  • tuple[dict, dict]

    tuple[dict, dict]: an updated threshold_kwargs and change_ledger.

Source code in src/argmerge/func.py
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
60
61
62
63
64
65
66
67
68
69
def __call__(
    cls,
    threshold_kwargs: dict,
    change_ledger: dict,
    f: Callable,
    debug: bool = False,
) -> tuple[dict, dict]:
    """Lowest level parser - retrieve function defaults as fallback arguments.

    Args:
        threshold_kwargs (dict[str, Any]): kwargs passed around the
            @threshold decorator.
        change_ledger (dict[str, dict[str, str  |  int]]): Tracks when kwargs are
            updated inside the @threshold decorator.
        f (Callable): The function we wrap.
        debug (bool, optional): Flag to turn on more logging. Defaults to False.


    Returns:
        tuple[dict, dict]: an updated `threshold_kwargs` and `change_ledger`.
    """

    if debug:
        LOGGER.remove()
        LOGGER.add(sys.stderr, level="DEBUG")

    _sig = signature(f)
    LOGGER.debug(f"Function {signature=}")
    _default: dict = {}

    for k, v in _sig.parameters.items():
        if v.default is not Parameter.empty:
            _default[k] = v.default

    LOGGER.debug(f"{_default=}")
    for k in _default:
        change_ledger[k] = {"label": cls.label, "rank": cls.rank}

    threshold_kwargs.update(**_default)

    return threshold_kwargs, change_ledger

FuncUpdater

Bases: SourceParser

Builds the parser to extract default arguments from a function signature

This is the highest-ranked parser.

Parameters:

  • label (str) –

    The debugging label to indicate an argument was set at Runtime by the developer.

  • rank (int) –

    The priority of the parser. Generally, we aim between [0,100] for human-readabilty.

Source code in src/argmerge/func.py
 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
class FuncUpdater(SourceParser):
    """Builds the parser to extract default arguments from a function signature

    This is the highest-ranked parser.

    params:
        label (str): The debugging label to indicate an argument was set at Runtime by
            the developer.
        rank (int): The priority of the parser. Generally, we aim between [0,100] for
            human-readabilty.
    """

    label: str = "Developer-provided"
    rank: int = 100

    def __call__(
        cls,
        threshold_kwargs: dict[str, Any],
        change_ledger: dict[str, dict[str, str | int]],
        func_kwargs: dict[str, Any],
        debug: bool = False,
    ) -> tuple[dict, dict]:
        """Update the external values with the function's runtime arguments.

        Args:
            threshold_kwargs (dict[str, Any]): kwargs passed around the
                @threshold decorator.
            change_ledger (dict[str, dict[str, str  |  int]]): Tracks when kwargs are
                updated inside the @threshold decorator.
            func_kwargs (dict[str, Any]): The Runtime kwargs of the function.
            debug (bool, optional): Flag to turn on more logging. Defaults to False.

        Returns:
            Returns:
            tuple[dict, dict]: an updated `threshold_kwargs` and `change_ledger`.
        """
        if debug:
            LOGGER.remove()
            LOGGER.add(sys.stderr, level="DEBUG")

        LOGGER.debug(f"{threshold_kwargs=}")
        LOGGER.debug(f"{func_kwargs=}")

        threshold_kwargs.update(**func_kwargs)

        for key in func_kwargs:
            change_ledger[key] = {"label": cls.label, "rank": cls.rank}

        return threshold_kwargs, change_ledger

__call__

__call__(threshold_kwargs, change_ledger, func_kwargs, debug=False)

Update the external values with the function's runtime arguments.

Parameters:

  • threshold_kwargs (dict[str, Any]) –

    kwargs passed around the @threshold decorator.

  • change_ledger (dict[str, dict[str, str | int]]) –

    Tracks when kwargs are updated inside the @threshold decorator.

  • func_kwargs (dict[str, Any]) –

    The Runtime kwargs of the function.

  • debug (bool, default: False ) –

    Flag to turn on more logging. Defaults to False.

Returns:

  • Returns ( dict ) –
  • dict

    tuple[dict, dict]: an updated threshold_kwargs and change_ledger.

Source code in src/argmerge/func.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
def __call__(
    cls,
    threshold_kwargs: dict[str, Any],
    change_ledger: dict[str, dict[str, str | int]],
    func_kwargs: dict[str, Any],
    debug: bool = False,
) -> tuple[dict, dict]:
    """Update the external values with the function's runtime arguments.

    Args:
        threshold_kwargs (dict[str, Any]): kwargs passed around the
            @threshold decorator.
        change_ledger (dict[str, dict[str, str  |  int]]): Tracks when kwargs are
            updated inside the @threshold decorator.
        func_kwargs (dict[str, Any]): The Runtime kwargs of the function.
        debug (bool, optional): Flag to turn on more logging. Defaults to False.

    Returns:
        Returns:
        tuple[dict, dict]: an updated `threshold_kwargs` and `change_ledger`.
    """
    if debug:
        LOGGER.remove()
        LOGGER.add(sys.stderr, level="DEBUG")

    LOGGER.debug(f"{threshold_kwargs=}")
    LOGGER.debug(f"{func_kwargs=}")

    threshold_kwargs.update(**func_kwargs)

    for key in func_kwargs:
        change_ledger[key] = {"label": cls.label, "rank": cls.rank}

    return threshold_kwargs, change_ledger