Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# -------------------------------------------------------------------------------------------------- 

2# Copyright (c) Lukas Vik. All rights reserved. 

3# 

4# This file is part of the tsfpga project. 

5# https://tsfpga.com 

6# https://gitlab.com/tsfpga/tsfpga 

7# -------------------------------------------------------------------------------------------------- 

8 

9 

10class ModuleList: 

11 """ 

12 Wrapper for a list of modules, with convenience functions. 

13 """ 

14 

15 def __init__(self): 

16 self._modules = [] 

17 

18 def append(self, module): 

19 """ 

20 Append a module to the list. 

21 """ 

22 self._modules.append(module) 

23 

24 def get(self, module_name): 

25 """ 

26 Get the module with the specified name. If no module matched, an exception is raised. 

27 """ 

28 for module in self._modules: 

29 if module.name == module_name: 

30 return module 

31 

32 raise ValueError(f'No module "{module_name}" available') 

33 

34 def __iter__(self): 

35 return iter(self._modules) 

36 

37 def __getitem__(self, index): 

38 return self._modules[index] 

39 

40 def __len__(self): 

41 return len(self._modules) 

42 

43 def __add__(self, other): 

44 if not isinstance(other, self.__class__): 

45 raise TypeError(f"Can only concatenate with another {self.__class__.__name__}") 

46 

47 # Note that the list concatenation implies a shallow copy of the lists 

48 result = self.__class__() 

49 result._modules = self._modules + other._modules 

50 return result 

51 

52 def __copy__(self): 

53 result = self.__class__() 

54 result._modules = self._modules.copy() 

55 return result 

56 

57 def copy(self): 

58 """ 

59 Create a shallow copy of the module list. This public function is available as a 

60 convenience and to mimic the interface of a regular python list. 

61 """ 

62 return self.__copy__() 

63 

64 def __str__(self): 

65 return str(self._modules)