Best Python code snippet using autotest_python
compare.py
Source:compare.py
...27 else:28 all += 129 event_p = event_prediction[batch_index][i]30 event_r = event_real[batch_index][i]31 if get_max(event_p) == get_max(event_r):32 same_event += 133 type_p = type_prediction[batch_index][i]34 type_r = type_real[batch_index][i]35 if get_max(type_p) == get_max(type_r):36 same_type += 137 polarity_p = polarity_prediction[batch_index][i]38 polarity_r = polarity_real[batch_index][i]39 if get_max(polarity_p) == get_max(polarity_r):40 same_polarity += 141 degree_p = degree_prediction[batch_index][i]42 degree_r = degree_real[batch_index][i]43 if get_max(degree_p) == get_max(degree_r):44 same_degree += 145 modality_p = modality_prediction[batch_index][i]46 modality_r = modality_real[batch_index][i]47 if get_max(modality_p) == get_max(modality_r):48 same_modality += 149 i += 150 batch_index += 151 return same_event / all, same_type / all, same_polarity / all, same_degree / all, same_modality / all52def compare_five_p_and_r(event_prediction, event_real, type_prediction, type_real, polarity_prediction, polarity_real,53 degree_prediction, degree_real, modality_prediction, modality_real):54 # batch size55 batch_size = len(event_prediction)56 all = 057 same_event = 058 same_type = 059 same_polarity = 060 same_degree = 061 same_modality = 062 prediction_event = 063 prediction_event_type = 064 prediction_event_polarity = 065 prediction_event_degree = 066 prediction_event_modality = 067 real_event = 068 real_type = 069 real_polarity = 070 real_degree = 071 real_modality = 072 # wrong_event = 073 #74 tp_event = 075 fp_event = 076 fn_event = 077 tn_event = 078 #79 # tp_type = 080 # fp_type = 081 # fn_type = 082 # tn_type = 083 #84 # tp_polarity = 085 # fp_polarity = 086 # fn_polarity = 087 # tn_polarity = 088 #89 # tp_degree = 090 # fp_degree = 091 # fn_degree = 092 # tn_degree = 093 #94 # tp_modality = 095 # fp_modality = 096 # fn_modality = 097 # tn_modality = 098 batch_index = 099 while batch_index < batch_size:100 i = 0101 step_size = len(event_prediction[batch_index])102 while i < step_size:103 event_p = event_prediction[batch_index][i]104 event_r = event_real[batch_index][i]105 type_p = type_prediction[batch_index][i]106 type_r = type_real[batch_index][i]107 polarity_p = polarity_prediction[batch_index][i]108 polarity_r = polarity_real[batch_index][i]109 degree_p = degree_prediction[batch_index][i]110 degree_r = degree_real[batch_index][i]111 modality_p = modality_prediction[batch_index][i]112 modality_r = modality_real[batch_index][i]113 # is no word114 if string.atof(event_r[0]) == 1:115 break116 # is event117 pre_pos = get_max_expect_first(event_p)118 real_pos = get_max_expect_first(event_r)119 if pre_pos == 1:120 prediction_event += 1121 if real_pos == 1:122 real_event += 1123 if pre_pos == 1 & real_pos == 1:124 same_event += 1125 if get_max(type_p) == get_max(type_r):126 same_type += 1127 if get_max(polarity_p) == get_max(polarity_r):128 same_polarity += 1129 if get_max(degree_p) == get_max(degree_r):130 same_degree += 1131 if get_max(modality_p) == get_max(modality_r):132 same_modality += 1133 i += 1134 batch_index += 1135 print ('event', same_event, prediction_event, real_event)136 print ('type', same_type, prediction_event, real_event)137 print ('polarity', same_polarity, prediction_event, real_event)138 print ('degree', same_degree, prediction_event, real_event)139 print ('modality', same_modality, prediction_event, real_event)140 precision_event = same_event / prediction_event141 recall_event = same_event / real_event142 combine_event = '(' + str(precision_event) + ',' + str(recall_event) + ')'143 precision_type = same_type / prediction_event144 recall_type = same_type / real_event145 combine_type = '(' + str(precision_type) + ',' + str(recall_type) + ')'146 precision_polarity = same_polarity / prediction_event147 recall_polarity = same_polarity / real_event148 combine_polarity = '(' + str(precision_polarity) + ',' + str(recall_polarity) + ')'149 precision_degree = same_degree / prediction_event150 recall_degree = same_degree / real_event151 combine_degree = '(' + str(precision_degree) + ',' + str(recall_degree) + ')'152 precision_modality = same_modality / prediction_event153 recall_modality = same_modality / real_event154 combine_modality = '(' + str(precision_modality) + ',' + str(recall_modality) + ')'155 return combine_event, combine_type, combine_polarity, combine_degree, combine_modality # def compare_event(prediction, real):156# len = len(prediction)157#158# all = 0159# same = 0160#161# i = 0162# while i < len:163# index_need = get_max(real[i])164# index_prediction = get_max(prediction[i])165#166# if index_need != 0:167# all += 1168# if (index_need == index_prediction):169# same += 1170# i += 1171#172# return173def get_max(nums):174 max = -2000000175 pos = -1176 i = 0177 while i < len(nums):178 if (nums[i] > max):179 pos = i180 max = nums[i]181 i += 1182 return pos183def get_max_expect_first(nums):184 max = -2000000185 pos = -1186 i = 1187 while i < len(nums):188 if (nums[i] > max):189 pos = i190 max = nums[i]191 i += 1192 return pos193# nums = [-0.2, 0, 0.1, -0.4]...
g_stack_max_effective.py
Source:g_stack_max_effective.py
...12 self.stack = []13 self.max = Stack()14 def push(self, value):15 self.stack.append(value)16 max_value = self.get_max()17 max_value = float('-inf') if max_value is None else max_value18 if value >= max_value:19 self.max.push(value)20 def pop(self):21 try:22 saved = self.stack.pop()23 except IndexError:24 raise IndexError('error')25 if saved == self.get_max():26 self.max.pop()27 def get_max(self):28 try:29 return self.max.top()30 except IndexError:31 return None32def run_commands(stack, commands):33 result = []34 for command in commands:35 if 'push' in command:36 stack.push(int(command.split()[1]))37 elif command == 'pop':38 try:39 stack.pop()40 except IndexError as error:41 result.append(error)42 else:43 result.append(stack.get_max())44 return '\n'.join(str(line) for line in result)45def test_run_commands():46 stack = StackMaxEffective()47 result = run_commands(stack, commands=[48 'get_max', 'push 7', 'pop', 'push -2', 'push -1', 'pop', 'get_max',49 'get_max',50 ])51 assert result == 'None\n-2\n-2', f'Wrong answer: {result}'52 stack = StackMaxEffective()53 result = run_commands(stack, commands=[54 'get_max', 'pop', 'pop', 'push 10', 'get_max', 'push -9', 'push 333',55 'get_max', 'pop', 'pop', 'pop', 'pop', 'get_max',56 ])57 assert result == 'None\nerror\nerror\n10\n333\nerror\nNone', (...
heap.py
Source:heap.py
...45 else:46 break47 self._heap[j] = x48 print(self._heap, self.length - 1)49 def get_max(self):50 print(self._heap)51 if self.length == 1:52 self.length -= 153 return self._heap.pop(0)54 if self.length > 1:55 rr = self._heap[0]56 self._heap[0] = self._heap[self.length - 1]57 self.length -= 158 self._heap.pop()59 self._siftup(0)60 return rr61 else:62 raise Exception("no nums")63 @staticmethod64 def left_child(i):65 return 2 * i + 166 @staticmethod67 def parent_pos(i):68 return (i - 1) // 269if __name__ == '__main__':70 heap = Heap([1, 3, 10, 22, 4, 665, 23, 5, 77, 46])71 # print(heap._heap)72 print(heap.get_max())73 # print(heap.get_max())74 # print(heap.get_max())75 # print(heap.get_max())76 # print(heap.get_max())77 # print(heap.get_max())78 # print(heap.get_max())79 # print(heap.get_max())80 # print(heap.get_max())81 # heap.push(2)82 # heap.push(3)83 # heap.push(4)84 # heap.push(5)85 # heap.push(6)86 # heap.push(4)87 # heap.push(3)88 # heap.push(2)89 # print(heap.get_max())90 # print(heap.get_max())91 # print(heap.get_max())92 # print(heap.get_max())93 import heapq94 haha = [1, 3, 10, 22, 4, 665, 23, 5, 77, 46]95 heapq.heapify(haha)96 print(heapq.heappop(haha))...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!