def testComparisonMethods(self): v1 = vtk.vtkVariant(10) v2 = vtk.vtkVariant("10") # compare without regards to type self.assertEqual(vtk.vtkVariantEqual(v1, v2), True) self.assertEqual(vtk.vtkVariantLessThan(v1, v2), False) # compare with different types being non-equivalent self.assertEqual(vtk.vtkVariantStrictEquality(v1, v2), False) if sys.hexversion >= 0x03000000: self.assertEqual(vtk.vtkVariantStrictWeakOrder(v1, v2), True) else: # for Python 2, it worked like the cmp() function self.assertEqual(vtk.vtkVariantStrictWeakOrder(v1, v2), -1)
print("Valid cast result: %r" % i) # A failed cast returns None v = vtk.vtkVariant("hello") i = vtk.vtkVariantCast(v, 'int') print("Invalid cast result: %r" % i) # # Comparisons and sorting: See VTK docs for more info # # Special function vtk.vtkVariantStrictWeakOrder: # Compare variants by type first, and then by value. The return values # are -1, 0, 1 like the python cmp() method, for compatibility with the # python list sort() method. This is in contrast with the C++ version, # which returns true or false. v1 = vtk.vtkVariant(10) v2 = vtk.vtkVariant("10") i = vtk.vtkVariantStrictWeakOrder(v1, v2) print("Strict weak order (10, '10') -> %i" % i) # Check two variants for strict equality of type and value. b = vtk.vtkVariantStrictEquality(v1, v2) print("Strict equality (10, '10') -> %s" % b) # Two special-purpose methods, not generally useful in python # First is identical to (v1 < v2) b = vtk.vtkVariantLessThan(v1, v2) # Second is identical to (v1 == v2) b = vtk.vtkVariantEqual(v1, v2)
v = vtk.vtkVariant("hello") i = vtk.vtkVariantCast(v, 'int') print("Invalid cast result: %r" % i) # # Comparisons and sorting: See VTK docs for more info # # Special function vtk.vtkVariantStrictWeakOrder: # Compare variants by type first, and then by value. For Python 2, the # return values are -1, 0, 1 like the Python 2 "cmp()" method. This is # in contrast with the Python 3 and C++ versions of this function, which # check if (v1 < v2) and return True or False. v1 = vtk.vtkVariant(10) v2 = vtk.vtkVariant("10") r = vtk.vtkVariantStrictWeakOrder(v1, v2) print("Strict weak order (10, '10') ->", r) # Sorting by strict weak order, using a key function: unsorted = [1, 2.5, vtk.vtkVariant(), "0", unicodeEtre] l = [vtk.vtkVariant(x) for x in unsorted] l.sort(key=vtk.vtkVariantStrictWeakOrderKey) print("Sort by weak order ->", l) # Check two variants for strict equality of type and value. b = vtk.vtkVariantStrictEquality(v1, v2) print("Strict equality (10, '10') -> %s" % b) # Two special-purpose methods. # First is identical to (v1 < v2) b = vtk.vtkVariantLessThan(v1, v2)