• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rl-institut / multi-vector-simulator / 4084410217

pending completion
4084410217

push

github

pierre-francois.duc
Fix failing test

5928 of 7724 relevant lines covered (76.75%)

0.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.11
/src/multi_vector_simulator/utils/helpers.py
1
"""
2
Helper functions
3
================
4

5
Util functions that are useful throughout the MVS
6

7
Including:
8
- find_valvue_by_key(): Finds value of a key in a nested dictionary.
9
"""
10

11
import os
1✔
12

13
from multi_vector_simulator.utils.constants_json_strings import (
1✔
14
    DSO_FEEDIN_CAP,
15
    AUTO_CREATED_HIGHLIGHT,
16
    DSO_CONSUMPTION,
17
    DSO_FEEDIN,
18
    DSO_PEAK_DEMAND_PERIOD,
19
    DSO_PEAK_DEMAND_SUFFIX,
20
)
21

22

23
def find_value_by_key(data, target, result=None):
1✔
24
    """
25
    Finds value of a key in a nested dictionary.
26

27
    Parameters
28
    ----------
29
    data: dict
30
        Dict to be searched for target key
31

32
    target: str
33
        Key for which the value should be found in data
34

35
    result: None, value or list
36
        Only provided if function loops in itself
37

38
    Returns
39
    -------
40
    value if the key is only once in data
41
    list of values if it appears multiple times.
42
    """
43
    # check each item-value pair in the level
44
    for k, v in data.items():
1✔
45
        # if target in keys of level
46
        if k == target:
1✔
47
            if result is None:
1✔
48
                result = v
1✔
49
            elif isinstance(result, list):
1✔
50
                # Expands list of key finds
51
                result.append(v)
1✔
52
            else:
53
                # creates list for multiple key finds
54
                previous_result = result
1✔
55
                result = []
1✔
56
                result.append(previous_result)
1✔
57
                result.append(v)
1✔
58
        # Check next level for target
59
        if isinstance(v, dict):
1✔
60
            result = find_value_by_key(data=v, target=target, result=result)
1✔
61
    return result
1✔
62

63

64
def translates_epa_strings_to_mvs_readable(folder_name, file_name):
1✔
65
    """
66
    This function translates the json file generated by the EPA to a file readable by the MVS.
67
    This is necessary as there are some parameter names whose string representative differs in both tools.
68

69
    Parameters
70
    ----------
71
    folder_name: path
72
        Path to the folder with the json file
73
    file_name: json file name with extension
74
        Json to be converted
75

76
    Returns
77
    -------
78
    Stores converted json file to current dict
79

80
    Usage:
81
        `import multi_vector_simulator.utils.helpers as helpers`
82
        `helpers.translates_epa_strings_to_mvs_readable("./epa_benchmark", "epa_benchmark.json-original")`
83
    """
84
    import json
×
85
    from multi_vector_simulator.utils.data_parser import convert_epa_params_to_mvs
×
86

87
    with open(os.path.join(folder_name, file_name)) as json_file:
×
88
        epa_dict = json.load(json_file)
×
89

90
    dict_values = convert_epa_params_to_mvs(epa_dict)
×
91

92
    with open(os.path.join(folder_name, "mvs_config.json"), "w") as json_file:
×
93
        json.dump(dict_values, json_file, indent=4)
×
94

95

96
def get_item_if_list(list_or_float, index):
1✔
97
    if isinstance(list_or_float, list):
1✔
98
        answer = list_or_float[index]
1✔
99
    else:
100
        answer = list_or_float
1✔
101
    return answer
1✔
102

103

104
def get_length_if_list(list_or_float):
1✔
105
    if isinstance(list_or_float, list):
1✔
106
        answer = len(list_or_float)
1✔
107
    else:
108
        answer = 0
1✔
109
    return answer
1✔
110

111

112
def peak_demand_bus_name(dso_name: str, feedin: bool = False):
1✔
113
    """Name for auto created bus related to peak demand pricing period"""
114

115
    if feedin is False:
1✔
116
        dso_direction = DSO_CONSUMPTION
1✔
117
    else:
118
        dso_direction = DSO_FEEDIN
1✔
119

120
    return (
1✔
121
        f"{dso_name}{dso_direction}_{DSO_PEAK_DEMAND_SUFFIX} {AUTO_CREATED_HIGHLIGHT}"
122
    )
123

124

125
def peak_demand_transformer_name(
1✔
126
    dso_name: str, peak_number: int = None, feedin: bool = False
127
):
128
    """Name for auto created bus related to peak demand pricing period"""
129
    if feedin is False:
1✔
130
        dso_direction = DSO_CONSUMPTION
1✔
131
    else:
132
        dso_direction = DSO_FEEDIN
1✔
133
    transformer_name = f"{dso_name}{dso_direction}{DSO_PEAK_DEMAND_PERIOD}"
1✔
134
    if peak_number is not None:
1✔
135
        transformer_name = f"{transformer_name}_{str(peak_number)}"
1✔
136

137
    return f"{transformer_name} {AUTO_CREATED_HIGHLIGHT}"
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc