From 96d393dfb757afc61ffb319c34035d8d2ce7c33d Mon Sep 17 00:00:00 2001 From: KV Date: Fri, 27 Nov 2020 22:20:45 +0100 Subject: [PATCH] Use a generator expressions and raise exception if failing 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. --- src/wireviz/wv_bom.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index d21b96ef..b7dda90c 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -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