Coverage for teaser/data/dataclass.py: 86%

65 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2025-04-29 16:01 +0000

1"""This module holds file paths and bindings for json data.""" 

2import os 

3import sys 

4import warnings 

5 

6import teaser.logic.utilities as utils 

7import json 

8import collections 

9 

10from teaser.data.utilities import ConstructionData 

11v = sys.version_info 

12if v >= (2, 7): 

13 try: 

14 FileNotFoundError 

15 except NameError: 

16 FileNotFoundError = IOError 

17 

18class DataClass(object): 

19 """Class for JSON data. 

20 

21 This class loads all JSON files with statistic or template data needed 

22 for statistical data enrichment. 

23 

24 Parameters 

25 ---------- 

26 construction_data : ConstructionData 

27 The prefix of this parameter indicates which statistical data about building 

28 elements should be used. Its type is the enum class ConstructionData. 

29 

30 Attributes 

31 ---------- 

32 element_bind : collections.OrderedDict 

33 Ordered dictionary of the TypeBuildingElements binding. 

34 path_tb : str 

35 Full path to TypeElements_IWU.json. Default is 

36 teaser/data/input/inputdata/TypeElements_IWU.json. 

37 material_bind : collections.OrderedDict 

38 Ordered dictionary of the Material binding. 

39 path_mat : str 

40 Full path to MaterialTemplates.json. Default is 

41 teaser/data/input/inputdata/MaterialTemplates.json. 

42 conditions_bind : collections.OrderedDict 

43 Ordered dictionary of the UseConditions binding. 

44 path_uc : str 

45 Full path to UseConditions.json. Default is 

46 teaser/data/input/inputdata/UseConditions.json 

47 

48 """ 

49 

50 def __init__(self, construction_data: ConstructionData) -> object: 

51 """Construct DataClass.""" 

52 self.element_bind = None 

53 if construction_data.is_iwu(): 

54 self.path_tb = utils.get_full_path( 

55 "data/input/inputdata/TypeElements_IWU.json" 

56 ) 

57 self.load_tb_binding() 

58 elif construction_data.is_tabula_de(): 

59 self.path_tb = utils.get_full_path( 

60 os.path.join( 

61 "data", "input", "inputdata", "TypeElements_TABULA_DE.json" 

62 ) 

63 ) 

64 self.load_tb_binding() 

65 elif construction_data.is_tabula_dk(): 

66 self.path_tb = utils.get_full_path( 

67 os.path.join( 

68 "data", "input", "inputdata", "TypeElements_TABULA_DK.json" 

69 ) 

70 ) 

71 self.load_tb_binding() 

72 elif construction_data.is_kfw(): 

73 self.path_tb = utils.get_full_path( 

74 os.path.join( 

75 "data", "input", "inputdata", "TypeElements_KFW.json" 

76 ) 

77 ) 

78 self.load_tb_binding() 

79 elif construction_data is None: 

80 pass 

81 self.material_bind = None 

82 self.path_mat = utils.get_full_path( 

83 "data/input/inputdata/MaterialTemplates.json" 

84 ) 

85 self.conditions_bind = None 

86 self.path_uc = utils.get_full_path("data/input/inputdata/UseConditions.json") 

87 

88 self.load_uc_binding() 

89 self.load_mat_binding() 

90 

91 def load_tb_binding(self): 

92 """Load TypeBuildingElement json into binding classes.""" 

93 if self.path_tb.endswith("json"): 

94 if os.path.isfile(self.path_tb): 

95 try: 

96 with open(self.path_tb, "r") as f: 

97 self.element_bind = json.load( 

98 f, object_pairs_hook=collections.OrderedDict 

99 ) 

100 except json.decoder.JSONDecodeError: 

101 print("Your TypeElements file seems to be broken.") 

102 else: 

103 with open(self.path_tb, "w") as f: 

104 self.element_bind = collections.OrderedDict() 

105 

106 def load_uc_binding(self): 

107 """Load UseConditions json into binding classes.""" 

108 if self.path_uc.endswith("json"): 

109 if os.path.isfile(self.path_uc): 

110 try: 

111 with open(self.path_uc, "r") as f: 

112 self.conditions_bind = json.load( 

113 f, object_pairs_hook=collections.OrderedDict 

114 ) 

115 except json.decoder.JSONDecodeError: 

116 raise IOError("Your UseConditions.json file seems to be broken.") 

117 else: 

118 with open(self.path_uc, "w") as f: 

119 self.conditions_bind = collections.OrderedDict() 

120 

121 def load_mat_binding(self): 

122 """Load MaterialTemplates json into binding classes.""" 

123 if self.path_mat.endswith("json"): 

124 if os.path.isfile(self.path_mat): 

125 try: 

126 with open(self.path_mat, "r") as f: 

127 self.material_bind = json.load( 

128 f, object_pairs_hook=collections.OrderedDict 

129 ) 

130 except json.decoder.JSONDecodeError: 

131 print("Your Materials file seems to be broken.") 

132 else: 

133 with open(self.path_mat, "w") as f: 

134 self.material_bind = collections.OrderedDict()