e2nIEE / pandapipes
Showing 1 of 1 files from the diff.

@@ -9,7 +9,7 @@
Loading
9 9
10 10
from pandapower.io_utils import PPJSONEncoder, to_dict_with_coord_transform, \
11 11
    get_raw_data_from_pickle, transform_net_with_df_and_geo, PPJSONDecoder
12 -
from pandapower.io_utils import pp_hook
12 +
from pandapower.io_utils import pp_hook, encrypt_string, decrypt_string
13 13
14 14
from pandapipes.io.convert_format import convert_format
15 15
from pandapipes.io.io_utils import isinstance_partial, FromSerializableRegistryPpipe
@@ -42,7 +42,7 @@
Loading
42 42
        pickle.dump(save_net, f, protocol=2)  # use protocol 2 for py2 / py3 compatibility
43 43
44 44
45 -
def to_json(net, filename=None):
45 +
def to_json(net, filename=None, encryption_key=None):
46 46
    """
47 47
    Saves a pandapipes Network in JSON format. The index columns of all pandas DataFrames will be
48 48
    saved in ascending order. net elements which name begins with "_" (internal elements) will not
@@ -53,6 +53,8 @@
Loading
53 53
    :param filename: The absolute or relative path to the output file or a writable file-like \
54 54
            object. If None, a JSON string is returned.
55 55
    :type filename: str, file-object, default None
56 +
    :param encryption_key: If given, the pandapipes network is stored as an encrypted json string
57 +
    :type encryption_key: str, default None
56 58
    :return: JSON string of the Network (only if filename is None)
57 59
58 60
    :Example:
@@ -60,13 +62,16 @@
Loading
60 62
        >>> pandapipes.to_json(net, "example.json")
61 63
62 64
    """
65 +
    json_string = json.dumps(net, cls=PPJSONEncoder, indent=2, isinstance_func=isinstance_partial)
66 +
    if encryption_key is not None:
67 +
        json_string = encrypt_string(json_string, encryption_key)
63 68
    if filename is None:
64 -
        return json.dumps(net, cls=PPJSONEncoder, indent=2, isinstance_func=isinstance_partial)
69 +
        return json_string
65 70
    if hasattr(filename, 'write'):
66 -
        json.dump(net, fp=filename, cls=PPJSONEncoder, indent=2, isinstance_func=isinstance_partial)
71 +
        filename.write(json_string)
67 72
    else:
68 73
        with open(filename, "w") as fp:
69 -
            json.dump(net, fp=fp, cls=PPJSONEncoder, indent=2, isinstance_func=isinstance_partial)
74 +
            fp.write(json_string)
70 75
71 76
72 77
def from_pickle(filename):
@@ -89,7 +94,7 @@
Loading
89 94
    return net
90 95
91 96
92 -
def from_json(filename, convert=True):
97 +
def from_json(filename, convert=True, encryption_key=None):
93 98
    """
94 99
    Load a pandapipes network from a JSON file or string.
95 100
    The index of the returned network is not necessarily in the same order as the original network.
@@ -99,6 +104,8 @@
Loading
99 104
    :type filename: str, file-object
100 105
    :param convert: whether or not to convert the format from earlier versions
101 106
    :type convert: bool
107 +
    :param encryption_key: if given, key to decrypt an encrypted pandapower network
108 +
    :type encryption_key: str
102 109
    :return: net - The pandapipes network that was saved as JSON
103 110
    :rtype: pandapipesNet
104 111
@@ -114,10 +121,10 @@
Loading
114 121
    else:
115 122
        with open(filename) as fp:
116 123
            json_string = fp.read()
117 -
    return from_json_string(json_string, convert=convert)
124 +
    return from_json_string(json_string, convert=convert, encryption_key=encryption_key)
118 125
119 126
120 -
def from_json_string(json_string, convert=False):
127 +
def from_json_string(json_string, convert=False, encryption_key=None):
121 128
    """
122 129
    Load a pandapipes network from a JSON string.
123 130
    The index of the returned network is not necessarily in the same order as the original network.
@@ -127,6 +134,8 @@
Loading
127 134
    :type json_string: str
128 135
    :param convert: whether or not to convert the format from earlier versions
129 136
    :type convert: bool
137 +
    :param encryption_key: if given, key to decrypt an encrypted pandapower network
138 +
    :type encryption_key: str
130 139
    :return: net - The pandapipes network that was contained in the JSON string
131 140
    :rtype: pandapipesNet
132 141
@@ -135,6 +144,9 @@
Loading
135 144
        >>> net = pandapipes.from_json_string(json_str)
136 145
137 146
    """
147 +
    if encryption_key is not None:
148 +
        json_string = decrypt_string(json_string, encryption_key)
149 +
138 150
    net = json.loads(json_string, cls=PPJSONDecoder, object_hook=partial(pp_hook,
139 151
                                                                         registry_class=FromSerializableRegistryPpipe))
140 152
Files Coverage
pandapipes 90.96%
setup.py 0.00%
Project Totals (74 files) 90.63%

No yaml found.

Create your codecov.yml to customize your Codecov experience

Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading