mirror of
				https://gitlab.science.ru.nl/mthesis-edeboone/m-thesis-introduction.git
				synced 2025-10-31 03:46:44 +01:00 
			
		
		
		
	Lib: MethodMappingProxy to perform the same method call on multiple objects
This commit is contained in:
		
							parent
							
								
									869ef6dc29
								
							
						
					
					
						commit
						4462d4ef2e
					
				
					 1 changed files with 17 additions and 0 deletions
				
			
		
							
								
								
									
										17
									
								
								lib/util.py
									
										
									
									
									
								
							
							
						
						
									
										17
									
								
								lib/util.py
									
										
									
									
									
								
							|  | @ -8,6 +8,23 @@ try: | ||||||
| except ImportError: | except ImportError: | ||||||
|     import numpy.fft as ft |     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): | def sampled_time(sample_rate=1, start=0, end=1, offset=0): | ||||||
|     return offset + np.arange(start, end, 1/sample_rate) |     return offset + np.arange(start, end, 1/sample_rate) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue