Skip to content

Commit 13d43d2

Browse files
authored
Merge pull request #7 from DesignBuilderSoftware/split-writer
split-writer
2 parents 94f704a + cb2f4cf commit 13d43d2

File tree

4 files changed

+56
-26
lines changed

4 files changed

+56
-26
lines changed

db_temperature_distribution/parser.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Define functions to extract and parse EnergyPlus temperature distribution."""
22

33

4-
import csv
54
import re
65
from collections import defaultdict
76
from pathlib import Path
@@ -100,36 +99,25 @@ def format_time_bins(all_time_bins: Dict[str, Dict[str, Table]]) -> Dict[str, Ta
10099
return formatted_time_bins
101100

102101

103-
def write_tables(all_time_bins: Dict[str, Table], directory: Path) -> List[Path]:
104-
"""Write time bins to a given directory."""
105-
output_paths = []
106-
for temperature, time_bins in all_time_bins.items():
107-
filename = f"Distribution - {temperature}.csv"
108-
path = Path(directory, filename)
109-
with open(path, mode="w", newline="", encoding="utf-8") as csv_file:
110-
writer = csv.writer(csv_file)
111-
writer.writerows(time_bins)
112-
output_paths.append(path)
113-
return output_paths
114-
115-
116-
def process_time_bins(html_path: Path, destination_dir: Path) -> List[Path]:
102+
def process_time_bins(html_path: Path) -> Dict[str, Table]:
117103
"""
118104
Read source html file and write parsed time bins to .csv.
119105
120106
Arguments
121107
---------
122108
html_path : Path
123109
A path to energyplus html summary file.
124-
destination_dir : Path
125-
A path to directory to place output file / files.
110+
111+
Returns
112+
-------
113+
dict of {str, Table}
114+
Processed time bins for all temperature types.
126115
127116
"""
128117
tables = read_html(html_path)
129118
time_bins_dict = find_time_bins(tables)
130119
if time_bins_dict:
131-
formatted_time_bins = format_time_bins(time_bins_dict)
132-
return write_tables(formatted_time_bins, destination_dir)
120+
return format_time_bins(time_bins_dict)
133121
raise NoTemperatureDistribution(
134122
f"File '{html_path}' does not include temperature distribution time bins."
135123
)

db_temperature_distribution/writer.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""Define functions to writer parsed tables to various formats."""
2+
import csv
3+
from pathlib import Path
4+
from typing import Dict, List
5+
6+
from db_temperature_distribution.parser import Table
7+
8+
9+
def write_tables(all_time_bins: Dict[str, Table], directory: Path) -> List[Path]:
10+
"""
11+
Write time bins to a given directory.
12+
13+
Arguments
14+
---------
15+
all_time_bins : Dict of {str, Table}
16+
Parsed time bins for all temperature ypes.
17+
directory : Path
18+
A path to directory to place output file / files.
19+
20+
Returns
21+
-------
22+
list of Path
23+
Paths of all output files.
24+
25+
"""
26+
output_paths = []
27+
for temperature, time_bins in all_time_bins.items():
28+
filename = f"Distribution - {temperature}.csv"
29+
path = Path(directory, filename)
30+
with open(path, mode="w", newline="", encoding="utf-8") as csv_file:
31+
writer = csv.writer(csv_file)
32+
writer.writerows(time_bins)
33+
output_paths.append(path)
34+
return output_paths

main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from db_temperature_distribution.logger import Logger
66
from db_temperature_distribution.parser import process_time_bins
7+
from db_temperature_distribution.writer import write_tables
78

89
if __name__ == "__main__":
910
energyplus_folder = os.path.expandvars(r"%LOCALAPPDATA%\DesignBuilder\EnergyPlus")
@@ -37,7 +38,8 @@
3738
args = parser.parse_args()
3839
html_path = Path(args.source_path)
3940
with Logger(show_dialogs=not args.no_dialogs) as logger:
40-
output_paths = process_time_bins(html_path, Path(args.dest_dir))
41+
parsed_time_bins = process_time_bins(html_path)
42+
output_paths = write_tables(parsed_time_bins, Path(args.dest_dir))
4143
file_list = "\t-".join(map(str, output_paths))
4244
message = f"Temperature distribution time bins are stored in:\n{file_list}"
4345
logger.print_message("Success!", message)

tests/test_db_temperature_distribution.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
NoTemperatureDistribution,
88
process_time_bins,
99
)
10+
from db_temperature_distribution.writer import write_tables
1011

1112

1213
def read_csv(path):
@@ -16,12 +17,17 @@ def read_csv(path):
1617

1718

1819
@pytest.fixture(scope="module", autouse=True)
19-
def parse_timebins(html_path, test_tempdir):
20-
return process_time_bins(html_path, test_tempdir)
20+
def parse_timebins(html_path):
21+
return process_time_bins(html_path)
2122

2223

23-
def test_file_paths(parse_timebins):
24-
for path in parse_timebins:
24+
@pytest.fixture(scope="module", autouse=True)
25+
def write_timebins(parse_timebins, test_tempdir):
26+
return write_tables(parse_timebins, test_tempdir)
27+
28+
29+
def test_file_paths(write_timebins):
30+
for path in write_timebins:
2531
assert path.exists()
2632

2733

@@ -39,6 +45,6 @@ def test_parsed_timebins(filename, expected_outputs, test_tempdir):
3945
assert content == expected_outputs[filename]
4046

4147

42-
def test_missing_timebins(html_path_no_bins, test_tempdir):
48+
def test_missing_timebins(html_path_no_bins):
4349
with pytest.raises(NoTemperatureDistribution):
44-
process_time_bins(html_path_no_bins, test_tempdir)
50+
process_time_bins(html_path_no_bins)

0 commit comments

Comments
 (0)