PyXR

c:\python24\lib \ test \ test_compare.py



0001 import sys
0002 
0003 class Empty:
0004     def __repr__(self):
0005         return '<Empty>'
0006 
0007 class Coerce:
0008     def __init__(self, arg):
0009         self.arg = arg
0010 
0011     def __repr__(self):
0012         return '<Coerce %s>' % self.arg
0013 
0014     def __coerce__(self, other):
0015         if isinstance(other, Coerce):
0016             return self.arg, other.arg
0017         else:
0018             return self.arg, other
0019 
0020 class Cmp:
0021     def __init__(self,arg):
0022         self.arg = arg
0023 
0024     def __repr__(self):
0025         return '<Cmp %s>' % self.arg
0026 
0027     def __cmp__(self, other):
0028         return cmp(self.arg, other)
0029 
0030 
0031 candidates = [2, 2.0, 2L, 2+0j, [1], (3,), None, Empty(), Coerce(2), Cmp(2.0)]
0032 
0033 def test():
0034     for a in candidates:
0035         for b in candidates:
0036             try:
0037                 x = a == b
0038             except:
0039                 print 'cmp(%s, %s) => %s' % (a, b, sys.exc_info()[0])
0040             else:
0041                 if x:
0042                     print "%s == %s" % (a, b)
0043                 else:
0044                     print "%s != %s" % (a, b)
0045     # Ensure default comparison compares id() of args
0046     L = []
0047     for i in range(10):
0048         L.insert(len(L)//2, Empty())
0049     for a in L:
0050         for b in L:
0051             if cmp(a, b) != cmp(id(a), id(b)):
0052                 print "ERROR:", cmp(a, b), cmp(id(a), id(b)), id(a), id(b)
0053 
0054 test()
0055 

Generated by PyXR 0.9.4
SourceForge.net Logo