Coverage for hdl_registers/constant/integer_constant.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-19 20:51 +0000

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

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

3# 

4# This file is part of the hdl-registers project, an HDL register generator fast enough to run 

5# in real time. 

6# https://hdl-registers.com 

7# https://github.com/hdl-registers/hdl-registers 

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

9 

10# Standard libraries 

11from typing import Optional 

12 

13# Local folder libraries 

14from .constant import Constant 

15 

16 

17class IntegerConstant(Constant): 

18 def __init__(self, name: str, value: int, description: Optional[str] = None): 

19 """ 

20 Arguments: 

21 name: The name of the constant. 

22 value: The constant value. 

23 description: Textual description for the constant. 

24 """ 

25 self.name = name 

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

27 

28 self._value = 0 

29 # Assign self._value via setter 

30 self.value = value 

31 

32 @property 

33 def value(self) -> int: 

34 """ 

35 Getter for value. 

36 """ 

37 return self._value 

38 

39 @value.setter 

40 def value(self, value: int) -> None: 

41 """ 

42 Setter for value that performs sanity checks. 

43 """ 

44 if not isinstance(value, int): 

45 raise ValueError( 

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

47 ) 

48 

49 self._value = value 

50 

51 def __repr__(self) -> str: 

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

53name={self.name},\ 

54value={self.value},\ 

55description={self.description},\ 

56)"""