Lib: MethodMappingProxy to perform the same method call on multiple objects

This commit is contained in:
Eric Teunis de Boone 2023-11-14 15:17:08 +01:00
parent 869ef6dc29
commit 4462d4ef2e

View file

@ -8,6 +8,23 @@ try:
except ImportError:
import numpy.fft as ft
class MethodMappingProxy():
def __init__(self, *elements):
self.elements = elements
def __getattr__(self, name):
# Test name exists on all elements
for el in self.elements:
attr = getattr(el, name)
if not callable(attr):
raise AttributeError("Attribute `{name}` is not callable on `{el}`.")
def mapping_func(*args, **kwargs):
return [ getattr(el, name)(*args, **kwargs) for el in self.elements ]
return mapping_func
def sampled_time(sample_rate=1, start=0, end=1, offset=0):
return offset + np.arange(start, end, 1/sample_rate)