From a3c7ed69186576c5277434a77f251064822404cd Mon Sep 17 00:00:00 2001 From: Maarten Sebregts Date: Wed, 5 Aug 2026 15:56:56 +0200 Subject: [PATCH] Improve speed of `hasattr` on IDS structures Replace `_path` (which constructs the path including AoS indices) with the static `metadata.path_string` when an attribute cannot be found. Speeds up the following sample from 110ms to 0.5ms (200x gain): ```python import timeit import imas cp = imas.IDSFactory().core_profiles() cp.profiles_1d.resize(1000) def totime(): hasattr(cp.profiles_1d[999], "xyz") print(timeit.repeat(totime, number=1000)) ``` **Changed behaviour:** Before: `AttributeError: IDS structure 'profiles_1d[999]' has no attribute 'xyz'` After: `AttributeError: IDS structure 'profiles_1d' has no attribute 'xyz'` Since the index of an AoS is not relevant for whether an attribute exists or not, I believe this is not a problem. --- imas/ids_structure.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/imas/ids_structure.py b/imas/ids_structure.py index fbc3042e..4818ae04 100644 --- a/imas/ids_structure.py +++ b/imas/ids_structure.py @@ -57,7 +57,7 @@ def __init__(self, parent: IDSBase, metadata: IDSMetadata): def __getattr__(self, name): if name not in self._children: raise AttributeError( - f"IDS structure '{self._path}' has no attribute '{name}'" + f"IDS structure '{self.metadata.path_string}' has no attribute '{name}'" ) # Create child node child_meta = self._children[name]