Replace accumulation loop with sum expressions

Make a list from the group iterator for reusage in sum expressions
and to pick first group entry. The expected group sizes are very small,
so performance loss by creating a temporary list should be neglectable.

Alternativly, itertools.tee(group, 3) could be called to triplicate
the iterator, but it was not chosen for readability reasons.
This commit is contained in:
KV 2020-11-30 18:10:24 +01:00
parent 96d393dfb7
commit 1d653c44ed

View File

@ -103,14 +103,10 @@ def generate_bom(harness):
# deduplicate bom
bom = []
for _, group in groupby(sorted(bom_entries, key=bom_types_group), key=bom_types_group):
last_entry = None
total_qty = 0
designators = []
for group_entry in group:
designators.extend(make_list(group_entry.get('designators')))
total_qty += group_entry['qty']
last_entry = group_entry
bom.append({**last_entry, 'qty': round(total_qty, 3), 'designators': sorted(set(designators))})
group_entries = list(group)
designators = sum((make_list(entry.get('designators')) for entry in group_entries), [])
total_qty = sum(entry['qty'] for entry in group_entries)
bom.append({**group_entries[0], 'qty': round(total_qty, 3), 'designators': sorted(set(designators))})
# add an incrementing id to each bom item
return [{**entry, 'id': index} for index, entry in enumerate(bom, 1)]