Coverage for hdl_registers/constant.py: 100%

24 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-01-29 22:03 +0000

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

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

3# 

4# This file is part of the hdl_registers project, a HDL register generator fast enough to be run 

5# in real time. 

6# https://hdl-registers.com 

7# https://gitlab.com/hdl_registers/hdl_registers 

8# -------------------------------------------------------------------------------------------------- 

9 

10 

11class Constant: 

12 is_boolean = False 

13 is_integer = False 

14 is_float = False 

15 

16 def __init__(self, name, value, description=None): 

17 """ 

18 Arguments: 

19 name (str): The name of the constant. 

20 value (bool, int, str): The constant value. 

21 description (str): Textual description for the constant. 

22 """ 

23 self.name = name 

24 self.description = "" if description is None else description 

25 

26 self._value = None 

27 # Assign self._value via setter 

28 self.value = value 

29 

30 @property 

31 def value(self): 

32 """ 

33 Getter for value. 

34 """ 

35 return self._value 

36 

37 @value.setter 

38 def value(self, value): 

39 """ 

40 Setter for value that performs sanity checks. 

41 """ 

42 self._value = value 

43 

44 if isinstance(value, int): 

45 self.is_boolean = isinstance(value, bool) 

46 self.is_integer = not self.is_boolean 

47 

48 elif isinstance(value, float): 

49 self.is_float = True 

50 

51 if sum([self.is_boolean, self.is_integer, self.is_float]) != 1: 

52 raise ValueError( 

53 f'Constant "{self.name}" has invalid data type "{type(value)}". Value: "{value}"' 

54 ) 

55 

56 def __repr__(self): 

57 return f"""{self.__class__.__name__}(\ 

58name={self.name},\ 

59value={self.value},\ 

60description={self.description}, 

61)"""