python类cmp()的实例源码

test_builtins.py 文件源码 项目:packaging 作者: blockstack 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def test_cmp(self):
        self.assertEqual(cmp(-1, 1), -1)
        self.assertEqual(cmp(1, -1), 1)
        self.assertEqual(cmp(1, 1), 0)
        # verify that circular objects are not handled
        a = []; a.append(a)
        b = []; b.append(b)
        from UserList import UserList
        c = UserList(); c.append(c)
        self.assertRaises(RuntimeError, cmp, a, b)
        self.assertRaises(RuntimeError, cmp, b, c)
        self.assertRaises(RuntimeError, cmp, c, a)
        self.assertRaises(RuntimeError, cmp, a, c)
       # okay, now break the cycles
        a.pop(); b.pop(); c.pop()
        self.assertRaises(TypeError, cmp)
test_builtins.py 文件源码 项目:packaging 作者: blockstack 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def test_basic(self):
        data = range(100)
        copy = data[:]
        random.shuffle(copy)
        self.assertEqual(data, sorted(copy))
        self.assertNotEqual(data, copy)

        data.reverse()
        random.shuffle(copy)
        self.assertEqual(data, sorted(copy, cmp=lambda x, y: cmp(y,x)))
        self.assertNotEqual(data, copy)
        random.shuffle(copy)
        self.assertEqual(data, sorted(copy, key=lambda x: -x))
        self.assertNotEqual(data, copy)
        random.shuffle(copy)
        self.assertEqual(data, sorted(copy, reverse=1))
        self.assertNotEqual(data, copy)
test_futurize.py 文件源码 项目:packaging 作者: blockstack 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def test_cmp(self):
        before = """
        assert cmp(1, 2) == -1
        assert cmp(2, 1) == 1
        """
        after = """
        from past.builtins import cmp
        assert cmp(1, 2) == -1
        assert cmp(2, 1) == 1
        """
        self.convert_check(before, after, stages=(1, 2), ignore_imports=False)
kron.py 文件源码 项目:kron 作者: qtfkwk 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def __cmp__(self, y):
        """Compare two duration objects"""
        if isinstance(y, duration):
            return cmp(self.value, y.value)
        elif isinstance(y, (int, float)):
            return cmp(self.value, y)
        else:
            raise DurationComparisonError
kron.py 文件源码 项目:kron 作者: qtfkwk 项目源码 文件源码 阅读 15 收藏 0 点赞 0 评论 0
def __cmp__(self, y):
        """Compare two timestamps"""
        if isinstance(y, timestamp):
            return cmp(self.value, y.value)
        else:
            raise TimestampComparisonError
optics.py 文件源码 项目:idealoom 作者: conversence 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def base_cmp(cls, a, b):
        x = cmp(a.start, b.start)
        return x if x != 0 else -cmp(a.end, b.end)


问题


面经


文章

微信
公众号

扫码关注公众号