Best Python code snippet using sure_python
core.py
Source:core.py
...96 def is_simple(self, obj):97 return isinstance(obj, (98 string_types, integer_types99 ))100 def compare_complex_stuff(self, X, Y):101 kind = type(X)102 mapping = {103 float: self.compare_floats,104 dict: self.compare_dicts,105 list: self.compare_iterables,106 tuple: self.compare_iterables,107 }108 return mapping.get(kind, self.compare_generic)(X, Y)109 def compare_generic(self, X, Y):110 c = self.get_context()111 if X == Y:112 return True113 else:114 m = 'X%s != Y%s' % (red(c.current_X_keys), green(c.current_Y_keys))115 return DeepExplanation(m)116 def compare_floats(self, X, Y):117 c = self.get_context()118 if self.epsilon is None:119 return self.compare_generic(X, Y)120 if abs(X - Y) <= self.epsilon:121 return True122 else:123 m = 'X%s±%s != Y%s±%s' % (red(c.current_X_keys), self.epsilon, green(c.current_Y_keys), self.epsilon)124 return DeepExplanation(m)125 def compare_dicts(self, X, Y):126 c = self.get_context()127 x_keys = list(sorted(X.keys()))128 y_keys = list(sorted(Y.keys()))129 diff_x = list(set(x_keys).difference(set(y_keys)))130 diff_y = list(set(y_keys).difference(set(x_keys)))131 if diff_x:132 msg = "X%s has the key %%r whereas Y%s does not" % (133 red(c.current_X_keys),134 green(c.current_Y_keys),135 ) % diff_x[0]136 return DeepExplanation(msg)137 elif diff_y:138 msg = "X%s does not have the key %%r whereas Y%s has it" % (139 red(c.current_X_keys),140 green(c.current_Y_keys),141 ) % diff_y[0]142 return DeepExplanation(msg)143 elif X == Y:144 return True145 else:146 for key_X, key_Y in zip(x_keys, y_keys):147 self.key_X = key_X148 self.key_Y = key_Y149 value_X = X[key_X]150 value_Y = Y[key_Y]151 child = DeepComparison(152 value_X,153 value_Y,154 epsilon=self.epsilon,155 parent=self,156 ).compare()157 if isinstance(child, DeepExplanation):158 return child159 def get_context(self):160 if self._context:161 return self._context162 X_keys = []163 Y_keys = []164 comp = self165 while comp.parent:166 X_keys.insert(0, comp.parent.key_X)167 Y_keys.insert(0, comp.parent.key_Y)168 comp = comp.parent169 def get_keys(i):170 if not i:171 return ''172 return '[%s]' % ']['.join(map(safe_repr, i))173 class ComparisonContext:174 current_X_keys = get_keys(X_keys)175 current_Y_keys = get_keys(Y_keys)176 parent = comp177 self._context = ComparisonContext()178 return self._context179 def compare_iterables(self, X, Y):180 len_X, len_Y = map(len, (X, Y))181 if len_X > len_Y:182 msg = "X has %d items whereas Y has only %d" % (len_X, len_Y)183 return DeepExplanation(msg)184 elif len_X < len_Y:185 msg = "Y has %d items whereas X has only %d" % (len_Y, len_X)186 return DeepExplanation(msg)187 elif X == Y:188 return True189 else:190 for i, (value_X, value_Y) in enumerate(zip(X, Y)):191 self.key_X = self.key_Y = i192 child = DeepComparison(193 value_X,194 value_Y,195 epsilon=self.epsilon,196 parent=self,197 ).compare()198 if isinstance(child, DeepExplanation):199 return child200 def compare(self):201 X, Y = self.operands202 if isinstance(X, mock._CallList):203 X = list(X)204 if isinstance(Y, mock._CallList):205 X = list(Y)206 c = self.get_context()207 if self.is_simple(X) and self.is_simple(Y): # both simple208 if X == Y:209 return True210 c = self.get_context()211 m = "X%s is %%r whereas Y%s is %%r"212 msg = m % (red(c.current_X_keys), green(c.current_Y_keys)) % (X, Y)213 return DeepExplanation(msg)214 elif type(X) is not type(Y): # different types215 xname, yname = map(lambda _: type(_).__name__, (X, Y))216 msg = 'X%s is a %%s and Y%s is a %%s instead' % (217 red(c.current_X_keys),218 green(c.current_Y_keys),219 ) % (xname, yname)220 exp = DeepExplanation(msg)221 else:222 exp = self.compare_complex_stuff(X, Y)223 if isinstance(exp, DeepExplanation):224 original_X, original_Y = c.parent.operands225 raise exp.as_assertion(original_X, original_Y)226 return exp227 def explanation(self):228 return self._explanation229def _get_file_name(func):230 try:231 name = inspect.getfile(func)232 except AttributeError:233 name = get_function_code(func).co_filename234 return os.path.abspath(name)235def _get_line_number(func):236 try:...
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!!