# Fake a number that implements numeric methods through __coerce__
return '<CoerceNumber %s>' % repr(self
.arg
)
def __coerce__(self
, other
):
if isinstance(other
, CoerceNumber
):
return self
.arg
, other
.arg
# Fake a number that implements numeric ops through methods.
return '<MethodNumber %s>' % repr(self
.arg
)
def __radd__(self
,other
):
def __rsub__(self
,other
):
def __rmul__(self
,other
):
def __rdiv__(self
,other
):
def __rpow__(self
,other
):
def __rmod__(self
,other
):
def __cmp__(self
, other
):
return cmp(self
.arg
, other
)
candidates
= [ 2, 4.0, 2L, 2+0j
, [1], (2,), None,
MethodNumber(2), CoerceNumber(2)]
infix_binops
= [ '+', '-', '*', '/', '**', '%' ]
prefix_binops
= [ 'divmod' ]
# avoid testing platform fp quirks
def format_result(value
):
if isinstance(value
, complex):
return '(%s + %sj)' % (format_float(value
.real
),
format_float(value
.imag
))
elif isinstance(value
, float):
return format_float(value
)
print '%s %s %s' % (a
, op
, b
),
error
= sys
.exc_info()[:2]
print '... %s' % error
[0]
print '=', format_result(x
)
z
= a
# assume it has no inplace ops
print '%s %s= %s' % (a
, op
, b
),
error
= sys
.exc_info()[:2]
print '... %s' % error
[0]
print '=>', format_result(z
)
print '%s(%s, %s)' % (op
, a
, b
),
x
= eval('%s(a, b)' % op
)
error
= sys
.exc_info()[:2]
print '... %s' % error
[0]
print '=', format_result(x
)
warnings
.filterwarnings("ignore",
r
'complex divmod\(\), // and % are deprecated',