Coverage for pySDC/helpers/pysdc_helper.py: 100%

9 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-04-29 09:02 +0000

1class FrozenClass(object): 

2 """ 

3 Helper class to freeze a class, i.e. to avoid adding more attributes 

4 

5 Attributes: 

6 __isfrozen: Flag to freeze a class 

7 """ 

8 

9 __isfrozen = False 

10 

11 def __setattr__(self, key, value): 

12 """ 

13 Function called when setting arttributes 

14 

15 Args: 

16 key: the attribute 

17 value: the value 

18 """ 

19 

20 # check if attribute exists and if class is frozen 

21 if self.__isfrozen and not hasattr(self, key): 

22 raise TypeError("%r is a frozen class" % self) 

23 object.__setattr__(self, key, value) 

24 

25 def _freeze(self): 

26 """ 

27 Function to freeze the class 

28 """ 

29 self.__isfrozen = True 

30 

31 def get(self, key, default=None): 

32 """ 

33 Wrapper for `__dict__.get` to use when reading variables that might not exist, depending on the configuration 

34 

35 Args: 

36 key (str): Name of the variable you wish to read 

37 default: Value to be returned if the variable does not exist 

38 

39 Returns: 

40 __dict__.get(key, default) 

41 """ 

42 return self.__dict__.get(key, default)