From 7409bc8b8e3b47bb7789728de3b99143ed054991 Mon Sep 17 00:00:00 2001 From: Yaman Umuroglu <maltanar@gmail.com> Date: Tue, 18 Aug 2020 20:26:21 +0200 Subject: [PATCH] [Util] catch duplicate annotations in get_by_name --- src/finn/util/basic.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/finn/util/basic.py b/src/finn/util/basic.py index 62d5947e3..cc759bebb 100644 --- a/src/finn/util/basic.py +++ b/src/finn/util/basic.py @@ -156,13 +156,19 @@ def make_build_dir(prefix=""): def get_by_name(container, name, name_field="name"): - """Return item from container by .name field if it exists, None otherwise""" + """Return item from container by .name field if it exists, None otherwise. + Will throw an Exception if multiple items are found, since this violates the + ONNX standard.""" names = [getattr(x, name_field) for x in container] - try: - ind = names.index(name) - return container[ind] - except ValueError: + + inds = [i for i, e in enumerate(names) if e == name] + if len(inds) > 1: + raise Exception("Found multiple get_by_name matches, undefined behavior") + elif len(inds) == 0: return None + else: + ind = inds[0] + return container[ind] def remove_by_name(container, name, name_field="name"): -- GitLab