From 45370e2c672322503631f26c61b4b45e9737ba23 Mon Sep 17 00:00:00 2001 From: Darius Arnold Date: Wed, 8 Aug 2018 19:25:11 +0200 Subject: [PATCH] [bugfix] PickingResults raised incorrect error on missing attribute Accessing a non existing attribute raised a KeyError instead of an AttributeError, breaking methods that relied on the correct error type. Maybe this fixes the __setstate__ bug during event picking from GUI. --- pylot/core/pick/autopick.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/pylot/core/pick/autopick.py b/pylot/core/pick/autopick.py index 7b9aaca3..4b820e37 100644 --- a/pylot/core/pick/autopick.py +++ b/pylot/core/pick/autopick.py @@ -192,7 +192,8 @@ class PickingParameters(object): class PickingResults(dict): """ - Store picking results + Used to store picking results. + PickingResults is a dict like class that adds attribute (dot) access to the dictionaries values. """ def __init__(self): @@ -229,11 +230,21 @@ class PickingResults(dict): self.spe = None # symmetrized picking error self.weight = 4 # weight of onset - def __setattr__(self, key, value): - self[key] = value + # to correctly provide dot access to dictionary attributes, all attribute access of the class is forwarded to the + # dictionary + def __getattr__(self, item): + """Override getattr to return an AttributeError instead of a KeyError when the instance doesn't have the + attribute. + """ + try: + attr = dict.__getitem__(self, item) + except KeyError: + raise AttributeError('{classname} has no attribute {attrname}'.format(classname=self.__class__.__name__, + attrname=item)) + return attr - def __getattr__(self, key): - return self[key] + __setattr__ = dict.__setitem__ + __delattr__ = dict.__delitem__ class PickingContainer: