Coverage for src/convert_lists.py: 98%

81 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-06-27 06:36 +0000

1""" Convert lists to a string and format it 

2 The module takes a list of values and converts them to a string format. 

3 And then compare two lists of tuples and find matching indices based on the first three elements of each tuple. 

4 And then create a new lists with teg for the first list and remove the elements from the second list us 

5 and make list_norm_del_add 

6 

7Returns: 

8 list: list_norm_del_add of tuple with three sort - add, del, norm ready to create html 

9 """ 

10 

11import re 

12from src.settings import lg 

13 

14def converter(list_for_convert): 

15 """Convert the list to a string and format it. 

16 The function takes a list of values and converts them to a string format. 

17 

18 Args: 

19 list_for_convert (list): list of values to be converted to a string 

20 

21 Returns: 

22 list: list of strings 

23 """ 

24 def convert_to_string(data): 

25 """Convert the data to a string and format it. 

26 The function takes a value and converts it to a string format.  

27 It removes leading and trailing whitespace, replaces multiple spaces with a single space, 

28 and removes the word 'None' if it appears in the string. 

29 If the input is None or an empty string, it returns an empty string. 

30 

31 Args: 

32 data (str): value to be converted to a string 

33 

34 Returns: 

35 list: list of strings 

36 """ 

37 if not data: 

38 return "" 

39 try: 

40 data = str(data) 

41 data = data.strip() 

42 data = re.sub(r"\s+", " ", data) 

43 data = re.sub(r"\s*\bNone\b", "", data) 

44 return data 

45 except (TypeError, ValueError): 

46 return "" 

47 

48 def convert_to_true_false(data): 

49 """Convert the data to a boolean value. 

50 

51 Args: 

52 data (str): value to be converted to a boolean 

53 

54 Returns: 

55 boolen: True if the data is not empty and not equal to '501', False otherwise 

56 """ 

57 if not data: 

58 return False 

59 if '501' in data: 

60 return False 

61 return True 

62 

63 

64 

65 

66 metres, times, firm, name, uwagi, przebieg, tel, wenz, pomp, sort = list_for_convert 

67 

68 times = times.strftime("%H:%M") 

69 if tel: 

70 if isinstance(tel, float): 

71 tel = str(int(tel)).strip() 

72 elif isinstance(tel, str): 

73 tel = tel.strip() 

74 else: 

75 tel = "" 

76 

77 przebieg = convert_to_string(przebieg) 

78 firm = convert_to_string(firm) 

79 name = convert_to_string(name) 

80 tel = convert_to_string(tel) 

81 uwagi = convert_to_string(uwagi) 

82 metres = convert_to_string(metres) 

83 pomp = convert_to_true_false(convert_to_string(pomp)) 

84 

85 return [metres, times, firm, name, uwagi, przebieg, tel, wenz, pomp, sort] 

86 

87def compare_lists_by_tuples(del_lista, add_lista): 

88 """Compare two lists of tuples and find matching indices based on the first three elements of each tuple. 

89 And then create a new lists with teg for the first list and remove the elements from the second list us 

90 and make_list_with_teg 

91 

92 Args: 

93 del_lista (list): del_lista from get_lista 

94 add_lista (list): add_lista from get_lista 

95 

96 Returns: 

97 tuple: del_lista with teg and add_lista without elements that are in del_lista 

98 """ 

99 matching_indices = [] 

100 

101 for index1, tuple1 in enumerate(del_lista): 

102 for index2, tuple2 in enumerate(add_lista): 

103 if tuple1[:3] == tuple2[:3]: 

104 matching_indices.append((index1, index2)) 

105 

106 del_lista, add_lista = make_list_with_teg(del_lista, add_lista, matching_indices) 

107 return del_lista, add_lista 

108 

109def make_list_with_teg(del_lista, add_lista, matching_indices): 

110 """Create a new list with HTML tags for the del_lista list and remove the elements from the add_lista. 

111 The function takes two lists and a list of matching indices. It creates a new list with HTML tags for the del_lista 

112 and removes the elements from the add_lista that are in the del_lista. 

113 

114 Args: 

115 del_lista (list): del_lista from get_lista 

116 add_lista (list): add_lista from get_lista 

117 matching_indices (list of tuple): list of tuples with indices of matching elements in del_lista and add_lista 

118 

119 Returns: 

120 tuple: del_liata_with_teg HTML tags and add_lista_without_change  

121 """ 

122 del_lista_with_teg = del_lista 

123 add_lista_without_change = add_lista 

124 del_elem_from_add_lista = [tup[1] for tup in matching_indices] 

125 

126 

127 for matching in matching_indices: 

128 item_del = del_lista[matching[0]] 

129 item_add = add_lista[matching[1]] 

130 

131 for index, (elem1, elem2) in enumerate(zip(item_del[:7], item_add[:7])): 

132 if elem1 != elem2: 

133 change_elem = (f'<span style="color: rgb(238, 36, 36); font-weight: bold; text-decoration: line-through;">{elem1}</span>' 

134 f' <span style="color: rgb(0, 139, 7); font-weight: bold;">{elem2}</span>') 

135 del_lista_with_teg[matching[0]][index] = change_elem 

136 del_lista_with_teg[matching[0]][9] = 0 

137 

138 

139 del_elem_from_add_lista.sort(reverse=True) 

140 for index in del_elem_from_add_lista: 

141 if 0 <= index < len(add_lista_without_change): 

142 del add_lista_without_change[index] 

143 

144 lg(f'this is del_lista with teg in {__name__}') 

145 lg(del_lista_with_teg) 

146 lg(f'this is add lista without change in {__name__}') 

147 lg(add_lista_without_change) 

148 

149 return del_lista_with_teg, add_lista_without_change 

150 

151def get_list_from_three_norm_del_add(lista_norm, lista_del, lista_add): 

152 """It creates a comprehensive list from elements that existed, were removed, 

153 or are new, adding an index at the end of each element: 0 for 

154 existing, 1 for removed, and 2 for new 

155 

156 Args: 

157 lista_norm (list): The list has not changed since the last time 

158 lista_del (list): The list with removed items 

159 lista_add (list): The list with new items, sorted items by time> metrs> firm 

160 

161 Returns: 

162 list: list of tuple with three sort - add, del, norm 

163 """ 

164 

165 # Add the index of the list to which the element belongs at the end of the element 

166 # 0-normal 1-del 2 - add (SORT) 

167 lista_norm = [tup + (0,) for tup in lista_norm] 

168 lista_del = [tup + (1,) for tup in lista_del] 

169 lista_add = [tup + (2,) for tup in lista_add] 

170 

171 lista_norm = list(map(converter, lista_norm)) 

172 lista_del = list(map(converter, lista_del)) 

173 lista_add = list(map(converter, lista_add)) 

174 

175 lista_del, lista_add = compare_lists_by_tuples(lista_del, lista_add) 

176 

177 # If we do not find it in the list of new ones, then we add from the list of those that were already there. 

178 replacement_dict = {tuple(tup[:8]): tup for tup in lista_add} 

179 lista_norm_add = [replacement_dict.get(tuple(tup[:8]), tup) for tup in lista_norm] 

180 

181 # add elements from lista_norm_add to lista_norm_del_add if they are not already in lista_del 

182 lista_norm_del_add = lista_del.copy() 

183 for sublist2 in lista_norm_add: 

184 if not any(l1[:3] == sublist2[:3] for l1 in lista_del): 

185 lista_norm_del_add.append(sublist2) 

186 

187 

188 # sorted by time, meters, name of firm 

189 lista_norm_del_add = sorted(lista_norm_del_add, key=lambda event: (event[1], event[2], event[3])) 

190 

191 return lista_norm_del_add 

192