Consolidated handling of single variables vs arrays of variables
Opened this issue · 0 comments
alhom commented
The following pattern is repeated often, used to handle cases where both single cellids and lists of cellids are supported as function arguments. Vectorized operations are preferred for speed, so (often) single cellid arguments are expanded into numpy arrays to be able to handle the computations in vectorized form without duplicate code.
It might be possible to handle these cases with a function decorator-wrapper, and would simplify the code quite a bit (and help with handling e.g. several return paths).
stack = True
if not hasattr(cellid,"__len__"):
cellid = np.atleast_1d(cellid)
stack = False
# .
# .
# .
if stack:
return AMR_count - 1
else:
return (AMR_count - 1)[0]
Example:
Start:
analysator/pyVlsv/vlsvreader.py
Line 1818 in a184153
End:
analysator/pyVlsv/vlsvreader.py
Line 1836 in a184153