Skip to content

Commit

Permalink
Replace accumulation loop with sum expressions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kvid committed Nov 30, 2020
1 parent 6e0750b commit e56c5bd
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/wireviz/wv_bom.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit e56c5bd

Please sign in to comment.