Skip to content

Commit a3c7ed6

Browse files
committed
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.
1 parent a4f5a4b commit a3c7ed6

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

imas/ids_structure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def __init__(self, parent: IDSBase, metadata: IDSMetadata):
5757
def __getattr__(self, name):
5858
if name not in self._children:
5959
raise AttributeError(
60-
f"IDS structure '{self._path}' has no attribute '{name}'"
60+
f"IDS structure '{self.metadata.path_string}' has no attribute '{name}'"
6161
)
6262
# Create child node
6363
child_meta = self._children[name]

0 commit comments

Comments
 (0)