Skip to content

Commit 0d4cb80

Browse files
Simplify merge method.
Simplify merge method by using list and dictionary comprehensions instead of nested for loops. This also avoids the need to check if some of the elements of the reports arguments are None, and the need to check if reports contains more than one report. This should also be a small performance improvement. This change also adds a bit more detail to the docstring for the merge method. The creation of a new empty report is also modified to no longer use an intermediary list of keys.
1 parent 91c6a55 commit 0d4cb80

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

pvlib/bifacial.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ def build(report, pvarray):
159159
back surface of center pvrow (index=1)"""
160160
# Initialize the report as a dictionary
161161
if report is None:
162-
list_keys = ['total_inc_back', 'total_inc_front']
163-
report = {key: [] for key in list_keys}
162+
report = {'total_inc_back': [], 'total_inc_front': []}
164163
# Add elements to the report
165164
if pvarray is not None:
166165
pvrow = pvarray.pvrows[1] # use center pvrow
@@ -177,13 +176,11 @@ def build(report, pvarray):
177176

178177
@staticmethod
179178
def merge(reports):
180-
"""Works for dictionary reports"""
181-
report = reports[0]
182-
# Merge only if more than 1 report
183-
if len(reports) > 1:
184-
keys_report = list(reports[0].keys())
185-
for other_report in reports[1:]:
186-
if other_report is not None:
187-
for key in keys_report:
188-
report[key] += other_report[key]
179+
"""Works for dictionary reports. Merges the reports list of
180+
dictionaries by flattening the lists for each key into a single
181+
super list. Returns a dictionary with two list values."""
182+
# Dictionary comprehension obviates the need to check if there are more
183+
# than one report, and if one of the elements in reports is None.
184+
report = {k:[item for d in reports for item in d[k]]
185+
for k in reports[0].keys()}
189186
return report

0 commit comments

Comments
 (0)