Skip to content

Commit

Permalink
Merge pull request #507 from jfreissmann/dev
Browse files Browse the repository at this point in the history
Improve writing JSON file method.
  • Loading branch information
fwitte committed Apr 29, 2024
2 parents f9757d0 + f678521 commit b77e844
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docs/scripts/generate_tespy_data_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def get_char_data(filename):
path = resource_filename('tespy.data', filename + '.json')

with open(path) as f:
data = json.loads(f.read())
data = json.load(f)

return data

Expand Down
10 changes: 5 additions & 5 deletions src/tespy/networks/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -2711,7 +2711,7 @@ def export_network(self, fn):
Path/filename for the network configuration file.
"""
with open(fn + 'network.json', 'w') as f:
f.write(json.dumps(self._serialize(), indent=4))
json.dump(self._serialize(), f, indent=4)

logger.debug('Network information saved to %s.', fn)

Expand Down Expand Up @@ -2758,7 +2758,7 @@ def save_busses(self, fn):
bus_data[label] = self.results[label]["design value"].to_dict()
fn = fn + 'busses.json'
with open(fn, "w", encoding="utf-8") as f:
f.write(json.dumps(bus_data, indent=4))
json.dump(bus_data, f, indent=4)
logger.debug('Bus information saved to %s.', fn)

def export_connections(self, fn):
Expand All @@ -2768,7 +2768,7 @@ def export_connections(self, fn):

fn = fn + "connections.json"
with open(fn, "w", encoding="utf-8") as f:
f.write(json.dumps(connections, indent=4).replace("NaN", "null"))
json.dump(connections, f, indent=4)
logger.debug('Connection information exported to %s.', fn)

def export_components(self, fn):
Expand All @@ -2779,7 +2779,7 @@ def export_components(self, fn):

fname = f"{fn}{c}.json"
with open(fname, "w", encoding="utf-8") as f:
f.write(json.dumps(components, indent=4).replace("NaN", "null"))
json.dump(components, f, indent=4)
logger.debug('Component information exported to %s.', fname)

def export_busses(self, fn):
Expand All @@ -2789,5 +2789,5 @@ def export_busses(self, fn):
busses.update(bus._serialize())
fn = fn + 'busses.json'
with open(fn, "w", encoding="utf-8") as f:
f.write(json.dumps(busses, indent=4).replace("NaN", "null"))
json.dump(busses, f, indent=4)
logger.debug('Bus information exported to %s.', fn)
8 changes: 4 additions & 4 deletions src/tespy/networks/network_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def load_network(path):
logger.debug(msg)

with open(path_comps + f, "r", encoding="utf-8") as c:
data = json.loads(c.read())
data = json.load(c)

comps.update(construct_components(component, data))

Expand All @@ -270,7 +270,7 @@ def load_network(path):
logger.debug(msg)

with open(fn, "r", encoding="utf-8") as c:
data = json.loads(c.read())
data = json.load(c)

conns = construct_connections(data, comps)

Expand All @@ -289,7 +289,7 @@ def load_network(path):
logger.debug(msg)

with open(fn, "r", encoding="utf-8") as c:
data = json.loads(c.read())
data = json.load(c)

busses = construct_busses(data, comps)
# add busses to network
Expand Down Expand Up @@ -364,7 +364,7 @@ def construct_network(path):
"""
# read network .json-file
with open(path + 'network.json', 'r') as f:
data = json.loads(f.read())
data = json.load(f)

# create network object with its properties
return Network(**data)
Expand Down
4 changes: 2 additions & 2 deletions src/tespy/tools/characteristics.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ def load_default_char(component, parameter, function_name, char_type):
path = os.path.join(__datapath__, 'char_maps.json')

with open(path) as f:
data = json.loads(f.read())
data = json.load(f)

if char_type == CharLine:
x = data[component][parameter][function_name]['x']
Expand Down Expand Up @@ -546,7 +546,7 @@ def load_custom_char(name, char_type):
if os.path.isfile(path):

with open(path) as f:
data = json.loads(f.read())
data = json.load(f)

if char_type == CharLine:
x = data[name]['x']
Expand Down
4 changes: 2 additions & 2 deletions tests/test_tools/test_characteristics.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_custom_CharLine_import():
shutil.copy(src=path + '/' + f, dst=tmp_path)

with open(data_path) as f:
raw_data = json.loads(f.read())
raw_data = json.load(f)

data = raw_data['heat exchanger']['kA_char2']
with open(os.path.join(path, 'char_lines.json'), 'w') as outfile:
Expand Down Expand Up @@ -84,7 +84,7 @@ def test_custom_CharMap_import():
shutil.copy(src=path + '/' + f, dst=tmp_path)

with open(data_path) as f:
raw_data = json.loads(f.read())
raw_data = json.load(f)

data = raw_data['compressor']['char_map_pr']
with open(os.path.join(path, 'char_maps.json'), 'w') as outfile:
Expand Down

0 comments on commit b77e844

Please sign in to comment.