Skip to content

Commit

Permalink
Use a generator expressions and raise exception if failing
Browse files Browse the repository at this point in the history
Seems to be the most popular search alternative:
 https://stackoverflow.com/questions/8653516/python-list-of-dictionaries-search

Raising StopIteration if not found is better than returning None
to detect such an internal error more easily.
  • Loading branch information
kvid committed Nov 27, 2020
1 parent 6bd202b commit 6e0750b
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/wireviz/wv_bom.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,10 @@ def generate_bom(harness):
return [{**entry, 'id': index} for index, entry in enumerate(bom, 1)]

def get_bom_index(bom: List[dict], extra: AdditionalComponent) -> int:
"""Return id of BOM entry or raise StopIteration if not found."""
# Remove linebreaks and clean whitespace of values in search
target = tuple(clean_whitespace(v) for v in bom_types_group({**asdict(extra), 'item': extra.description}))
for entry in bom:
if bom_types_group(entry) == target:
return entry['id']
return None
return next(entry['id'] for entry in bom if bom_types_group(entry) == target)

def bom_list(bom):
keys = ['id', 'item', 'qty', 'unit', 'designators'] # these BOM columns will always be included
Expand Down

0 comments on commit 6e0750b

Please sign in to comment.