Coverage for physioblocks / base / registers.py: 100%

13 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-09 16:40 +0100

1# SPDX-FileCopyrightText: Copyright INRIA 

2# 

3# SPDX-License-Identifier: LGPL-3.0-only 

4# 

5# Copyright INRIA 

6# 

7# This file is part of PhysioBlocks, a library mostly developed by the 

8# [Ananke project-team](https://team.inria.fr/ananke) at INRIA. 

9# 

10# Authors: 

11# - Colin Drieu 

12# - Dominique Chapelle 

13# - François Kimmig 

14# - Philippe Moireau 

15# 

16# PhysioBlocks is free software: you can redistribute it and/or modify it under the 

17# terms of the GNU Lesser General Public License as published by the Free Software 

18# Foundation, version 3 of the License. 

19# 

20# PhysioBlocks is distributed in the hope that it will be useful, but WITHOUT ANY 

21# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 

22# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 

23# 

24# You should have received a copy of the GNU Lesser General Public License along with 

25# PhysioBlocks. If not, see <https://www.gnu.org/licenses/>. 

26 

27"""Define functions to update two-way :type:`dict` using common :type:`dict`""" 

28 

29from typing import Any 

30 

31 

32def register( 

33 register: dict[Any, Any], registered_key: Any, registered_value: Any 

34) -> None: 

35 """ 

36 Register the key and value to the provided register. 

37 

38 :param register: the register to fill. 

39 :type dict[Any, Any]: 

40 

41 :param registered_key: the key to register. 

42 :type Any: 

43 

44 :param registered_value: the value to register. 

45 :type Any: 

46 

47 :raise ValueError: Raises a ValueError if either the key or value 

48 is already registered in the provided register. 

49 """ 

50 if registered_key in register or registered_value in register: 

51 raise RegisterError( 

52 str.format( 

53 "{0}: {1} key or value is already registered.", 

54 registered_key, 

55 registered_value, 

56 ) 

57 ) 

58 

59 register[registered_key] = registered_value 

60 register[registered_value] = registered_key 

61 

62 

63def check_key_value_type( 

64 key: Any, key_type: type[Any], value: Any, value_type: type[Any] 

65) -> None: 

66 """ 

67 Check if key and params are of the expected types. 

68 

69 If not, it raises a type error. 

70 

71 :param key: the key value 

72 :type key: Any 

73 

74 :param key_type: the expected type for the key 

75 :type key_type: type 

76 

77 :param value: the value 

78 :type value: Any 

79 

80 :param value_type: the expected type for the value 

81 :type value_type: type 

82 

83 :raises TypeError: raise a type error if key or value param is not of the expected 

84 types 

85 """ 

86 if isinstance(key, key_type) is False: 

87 raise TypeError( 

88 str.format( 

89 "Expected type for key is {0} but got {1}.", 

90 key_type.__name__, 

91 type(key).__name__, 

92 ) 

93 ) 

94 

95 elif isinstance(value, value_type) is False: 

96 raise TypeError( 

97 str.format( 

98 "Expected type for value is {0} but got {1}.", 

99 value_type.__name__, 

100 type(value).__name__, 

101 ) 

102 ) 

103 

104 

105class RegisterError(Exception): 

106 """Error Raised when a key or value has already been registered.""" 

107 

108 pass