Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def compare_generic(self, X, Y, msg_format='X{0} != Y{1}'):
c = self.get_context()
if X == Y:
return True
else:
m = msg_format.format(red(c.current_X_keys), green(c.current_Y_keys))
return DeepExplanation(m)
y_keys = list(Y.keys())
diff_x = list(set(x_keys).difference(set(y_keys)))
diff_y = list(set(y_keys).difference(set(x_keys)))
if diff_x:
msg = "X{0} has the key {1!r} whereas Y{2} does not".format(
red(c.current_X_keys),
safe_repr(diff_x[0]),
green(c.current_Y_keys))
return DeepExplanation(msg)
elif diff_y:
msg = "X{0} does not have the key {1!r} whereas Y{2} has it".format(
red(c.current_X_keys),
safe_repr(diff_y[0]),
green(c.current_Y_keys))
return DeepExplanation(msg)
elif X == Y:
return True
else:
for key_X in x_keys:
self.key_X = key_X
self.key_Y = key_X
value_X = X[key_X]
value_Y = Y[key_X]
child = DeepComparison(
value_X,
value_Y,
epsilon=self.epsilon,
parent=self,
def compare_floats(self, X, Y):
c = self.get_context()
if self.epsilon is None:
return self.compare_generic(X, Y)
if abs(X - Y) <= self.epsilon:
return True
else:
m = 'X{0}±{1} != Y{2}±{3}'.format(
red(c.current_X_keys), self.epsilon, green(c.current_Y_keys), self.epsilon)
return DeepExplanation(m)
def compare_ordereddict(self, X, Y):
"""Compares two instances of an OrderedDict."""
# check if OrderedDict instances have the same keys and values
child = self.compare_dicts(X, Y)
if isinstance(child, DeepExplanation):
return child
# check if the order of the keys is the same
for i, j in zip(X.items(), Y.items()):
if i[0] != j[0]:
c = self.get_context()
msg = "X{0} and Y{1} are in a different order".format(
red(c.current_X_keys), green(c.current_Y_keys)
)
return DeepExplanation(msg)
return True
def compare_dicts(self, X, Y):
c = self.get_context()
x_keys = list(X.keys())
y_keys = list(Y.keys())
diff_x = list(set(x_keys).difference(set(y_keys)))
diff_y = list(set(y_keys).difference(set(x_keys)))
if diff_x:
msg = "X{0} has the key {1!r} whereas Y{2} does not".format(
red(c.current_X_keys),
safe_repr(diff_x[0]),
green(c.current_Y_keys))
return DeepExplanation(msg)
elif diff_y:
msg = "X{0} does not have the key {1!r} whereas Y{2} has it".format(
red(c.current_X_keys),
safe_repr(diff_y[0]),
green(c.current_Y_keys))
return DeepExplanation(msg)
elif X == Y:
return True
else:
for key_X in x_keys:
self.key_X = key_X
self.key_Y = key_X
def safe_repr(val):
try:
if isinstance(val, dict):
# We special case dicts to have a sorted repr. This makes testing
# significantly easier
val = _obj_with_safe_repr(val)
ret = repr(val)
if six.PY2:
ret = ret.decode('utf-8')
except UnicodeEncodeError:
ret = red('a %r that cannot be represented' % type(val))
else:
ret = green(ret)
return ret