Coverage for backend/ahuora-compounds/ahuora_compounds/PropertyPackage.py: 47%

30 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2026-03-26 20:57 +0000

1from typing import Set 

2 

3class PropertyPackage(): 

4 

5 def __init__(self, name: str): 

6 # Unique name for the package 

7 self.__name = name 

8 # Dictionary of registered compounds and their associated phases (e.g. {"H2O": ["Liq","Vap"]}) 

9 self.__compounds : dict[str, list[str]] = {} 

10 

11 # Read only properties 

12 @property 

13 def compounds(self) -> Set[str]: 

14 return set(self.__compounds.keys()) 

15 

16 @property 

17 def name(self) -> str: 

18 return self.__name 

19 

20 # Generic methods 

21 def register_compound(self, name: str, phases: list[str] = ["Liq","Vap"]): 

22 self.__compounds[name] = phases 

23 

24 def unregister_compound(self, name: str): 

25 if name in self.compounds: 

26 del self.__compounds[name] 

27 else: 

28 raise ValueError(f"Compound {name} is not registered in package {self.name}") 

29 

30 def check_registered(self, name: str) -> bool: 

31 """ 

32 Check if a compound is registered in this package. 

33  

34 Args: 

35 name (str): Name of the compound to check. 

36  

37 Returns: 

38 bool: True if the compound is registered, False otherwise. 

39 """ 

40 return name in self.compounds 

41 

42 def check_supported_compounds(self, compounds: Set[str], strict: bool = True): 

43 """ 

44 Check if a set of compounds are supported by this package. 

45  

46 Args: 

47 compounds (Set[str]): Name of the compound to check. 

48 strict (bool): If True, all compounds must be supported. 

49 If False, only a single compound must be supported. 

50  

51 Returns: 

52 bool: True if the compounds are supported, False otherwise. 

53 """ 

54 

55 num_supported = len([c for c in compounds if self.check_supported_compound(c)]) 

56 

57 if strict: 

58 # Checks all compounds are supported 

59 return num_supported == len(compounds) 

60 else: 

61 # Checks at least one is supported 

62 return num_supported > 0 

63 

64 def check_supported_compound(self, compound: str, strict: bool = True) -> bool: 

65 """ 

66 Check if a single compound is supported by this package. 

67 Defalt behaviour assumes all "registered" compounds are supported. 

68  

69 Args: 

70 compound (str): Name of the compound to check. 

71 strict (bool): If True, all compounds must be supported. 

72 If False, only a single compound must be supported. 

73  

74 Returns: 

75 bool: True if the compound is supported, False otherwise. 

76 """ 

77 return self.check_registered(compound) 

78 

79 def get_compound_phases(self, compound: str) -> list[str]: 

80 """ 

81 Get the supported phases for a given compound in this package. 

82  

83 Args: 

84 compound (str): Name of the compound to check. 

85  

86 Returns: 

87 list[str]: List of supported phases for the compound. Empty list if compound is not supported. 

88 """ 

89 if self.check_registered(compound): 

90 return self.__compounds[compound] 

91 else: 

92 return []