pumpwood_communication.misc

Miscelaneus function to help in development.

 1"""Miscelaneus function to help in development."""
 2import pandas as pd
 3import copy
 4from typing import List, Literal
 5from pumpwood_communication.exceptions import PumpWoodException
 6
 7
 8def unpack_dict_columns(df: pd.DataFrame, columns: List[str]) -> pd.DataFrame:
 9    """Unpack dictinary columns at a dataframe.
10
11    Return a copy of the dataframe with 'columns' unpacked and removed
12    from result.
13
14    Args:
15        df (pd.DataFrame):
16            Dataframe to unpack the columns.
17        columns (List[str]):
18            List of columns to unpack in columns.
19
20    Return:
21        Return a dataframe with dict columns unpacked.
22    """
23    list_unpacked_results = []
24    for c in columns:
25        list_unpacked_results.append(df[c].apply(pd.Series))
26    return pd.concat(
27        [df.drop(columns=columns)] + list_unpacked_results,
28        axis=1)
29
30
31def extract_dict_subset(data: dict, keys: List[str],
32                        raise_not_present: Literal[
33                            'raise', 'ignore', 'add_none'] = 'raise'):
34    """Extract a subset of data from dictonary.
35
36    Args:
37        data (dict):
38            Dictionary data from which the subset will be extracted.
39        keys (List[str]):
40            Keys that will be extracted.
41        raise_not_present (str):
42            Control if an erros should be raised when key is not present.
43            - 'raise': Raise error
44            - 'ignore': Does not return the missing key on function result.
45            - 'add_none': Return key with None value.
46
47    Returns:
48        Return a dictonary with a copy of subset of the keys of original
49        dictonary.
50    """
51    if raise_not_present not in ['raise', 'ignore', 'add_none']:
52        msg = (
53            'raise_not_present must be in [raise, ignore, add_none].'
54            'raise_not_present={raise_not_present}')
55        raise PumpWoodException(
56            message=msg, payload={
57                'raise_not_present': raise_not_present})
58
59    return_dict = {}
60    for key in keys:
61        temp_data = data.get(key)
62        if temp_data is None:
63            if raise_not_present == 'raise':
64                msg = (
65                    'key [{key}] not found on dictonary and raise_not_present '
66                    'arg is set as [{raise_not_present}]')
67                raise PumpWoodException(
68                    message=msg, payload={
69                        'key': key, 'raise_not_present': raise_not_present})
70            if raise_not_present == 'ignore':
71                continue
72            if raise_not_present == 'add_none':
73                return_dict[key] = None
74        else:
75            return_dict[key] = copy.deepcopy(temp_data)
76    return return_dict
def unpack_dict_columns( df: pandas.core.frame.DataFrame, columns: List[str]) -> pandas.core.frame.DataFrame:
 9def unpack_dict_columns(df: pd.DataFrame, columns: List[str]) -> pd.DataFrame:
10    """Unpack dictinary columns at a dataframe.
11
12    Return a copy of the dataframe with 'columns' unpacked and removed
13    from result.
14
15    Args:
16        df (pd.DataFrame):
17            Dataframe to unpack the columns.
18        columns (List[str]):
19            List of columns to unpack in columns.
20
21    Return:
22        Return a dataframe with dict columns unpacked.
23    """
24    list_unpacked_results = []
25    for c in columns:
26        list_unpacked_results.append(df[c].apply(pd.Series))
27    return pd.concat(
28        [df.drop(columns=columns)] + list_unpacked_results,
29        axis=1)

Unpack dictinary columns at a dataframe.

Return a copy of the dataframe with 'columns' unpacked and removed from result.

Arguments:
  • df (pd.DataFrame): Dataframe to unpack the columns.
  • columns (List[str]): List of columns to unpack in columns.
Return:

Return a dataframe with dict columns unpacked.

def extract_dict_subset( data: dict, keys: List[str], raise_not_present: Literal['raise', 'ignore', 'add_none'] = 'raise'):
32def extract_dict_subset(data: dict, keys: List[str],
33                        raise_not_present: Literal[
34                            'raise', 'ignore', 'add_none'] = 'raise'):
35    """Extract a subset of data from dictonary.
36
37    Args:
38        data (dict):
39            Dictionary data from which the subset will be extracted.
40        keys (List[str]):
41            Keys that will be extracted.
42        raise_not_present (str):
43            Control if an erros should be raised when key is not present.
44            - 'raise': Raise error
45            - 'ignore': Does not return the missing key on function result.
46            - 'add_none': Return key with None value.
47
48    Returns:
49        Return a dictonary with a copy of subset of the keys of original
50        dictonary.
51    """
52    if raise_not_present not in ['raise', 'ignore', 'add_none']:
53        msg = (
54            'raise_not_present must be in [raise, ignore, add_none].'
55            'raise_not_present={raise_not_present}')
56        raise PumpWoodException(
57            message=msg, payload={
58                'raise_not_present': raise_not_present})
59
60    return_dict = {}
61    for key in keys:
62        temp_data = data.get(key)
63        if temp_data is None:
64            if raise_not_present == 'raise':
65                msg = (
66                    'key [{key}] not found on dictonary and raise_not_present '
67                    'arg is set as [{raise_not_present}]')
68                raise PumpWoodException(
69                    message=msg, payload={
70                        'key': key, 'raise_not_present': raise_not_present})
71            if raise_not_present == 'ignore':
72                continue
73            if raise_not_present == 'add_none':
74                return_dict[key] = None
75        else:
76            return_dict[key] = copy.deepcopy(temp_data)
77    return return_dict

Extract a subset of data from dictonary.

Arguments:
  • data (dict): Dictionary data from which the subset will be extracted.
  • keys (List[str]): Keys that will be extracted.
  • raise_not_present (str): Control if an erros should be raised when key is not present.
    • 'raise': Raise error
    • 'ignore': Does not return the missing key on function result.
    • 'add_none': Return key with None value.
Returns:

Return a dictonary with a copy of subset of the keys of original dictonary.