Skip to content

Decorators

df(func)

This decorator checks if the output is a JSON-like structure and converts it to a DataFrame. If the output is None, it returns None. If the output is already a DataFrame, it returns it as is. If the output is a JSON-like structure (dict or list of dicts), it flattens it and converts it to a DataFrame.

Parameters:

Name Type Description Default
df(boolean)

True to convert output to DataFrame, False to keep as is. Default is True.

required

Returns:

Name Type Description
function

The wrapped function that returns a DataFrame or None.

Examples:

list_capacities(df=True)
Source code in src/pyfabricops/utils/decorators.py
10
11
12
13
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
def df(func):
    """
    This decorator checks if the output is a JSON-like structure and converts it to a DataFrame.
    If the output is None, it returns None. If the output is already a DataFrame, it returns it as is.
    If the output is a JSON-like structure (dict or list of dicts), it flattens it and converts it to a DataFrame.

    Args:
        df(boolean): True to convert output to DataFrame, False to keep as is. Default is True.

    Returns:
        function: The wrapped function that returns a DataFrame or None.

    Examples:
        ```python
        list_capacities(df=True)
        ```
    """

    @wraps(func)
    def _wrapper(*args, **kwargs):
        df = kwargs.pop('df', True)
        result = func(*args, **kwargs)

        if result is None:
            return None

        if df:
            return _json_df(result)
        else:
            return result

    return _wrapper