Skip to content

Instantly share code, notes, and snippets.

@txomon
Created June 11, 2014 16:26
Show Gist options
  • Save txomon/4a592aaca14ff09c9b0e to your computer and use it in GitHub Desktop.
Save txomon/4a592aaca14ff09c9b0e to your computer and use it in GitHub Desktop.
Mock's _Call's __eq__ method
def __eq__(self, other):
if other is ANY:
return True
try:
len_other = len(other)
except TypeError:
return False
self_name = ''
if len(self) == 2:
self_args, self_kwargs = self
else:
self_name, self_args, self_kwargs = self
other_name = ''
if len_other == 0:
other_args, other_kwargs = (), {}
elif len_other == 3:
other_name, other_args, other_kwargs = other
elif len_other == 1:
value, = other
if isinstance(value, tuple):
other_args = value
other_kwargs = {}
elif isinstance(value, basestring):
other_name = value
other_args, other_kwargs = (), {}
else:
other_args = ()
other_kwargs = value
else:
# len 2
# could be (name, args) or (name, kwargs) or (args, kwargs)
first, second = other
if isinstance(first, basestring):
other_name = first
if isinstance(second, tuple):
other_args, other_kwargs = second, {}
else:
other_args, other_kwargs = (), second
else:
other_args, other_kwargs = first, second
if self_name and other_name != self_name:
return False
for other_one, self_one in zip(other_args, self_args):
if not isinstance(other_one, self_one.__class__):
print 'In args'
print other_one, self_one.__class__
return False
for (other_key, other_value), (self_key, self_value) in\
zip(other_kwargs.iteritems(), self_kwargs.iteritems()):
if not isinstance(other_key, self_key.__class__):
print 'In kwargs key'
print other_key, self_key.__class__
return False
if not isinstance(other_value, self_value.__class__):
print 'In kwargs values'
print other_value,type(other_value), self_value.__class__
return False
# this order is important for ANY to work!
print (other_args, other_kwargs), (self_args, self_kwargs)
return (other_args, other_kwargs) == (self_args, self_kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment