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:
parent
96d393dfb7
commit
1d653c44ed
@ -103,14 +103,10 @@ def generate_bom(harness):
|
|||||||
# deduplicate bom
|
# deduplicate bom
|
||||||
bom = []
|
bom = []
|
||||||
for _, group in groupby(sorted(bom_entries, key=bom_types_group), key=bom_types_group):
|
for _, group in groupby(sorted(bom_entries, key=bom_types_group), key=bom_types_group):
|
||||||
last_entry = None
|
group_entries = list(group)
|
||||||
total_qty = 0
|
designators = sum((make_list(entry.get('designators')) for entry in group_entries), [])
|
||||||
designators = []
|
total_qty = sum(entry['qty'] for entry in group_entries)
|
||||||
for group_entry in group:
|
bom.append({**group_entries[0], 'qty': round(total_qty, 3), 'designators': sorted(set(designators))})
|
||||||
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))})
|
|
||||||
|
|
||||||
# add an incrementing id to each bom item
|
# add an incrementing id to each bom item
|
||||||
return [{**entry, 'id': index} for index, entry in enumerate(bom, 1)]
|
return [{**entry, 'id': index} for index, entry in enumerate(bom, 1)]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user