Python类

时间:2016-11-07 15:01:09

标签: python python-3.x

有一个代码,其中主要版本,次要版本,构建版本,修订版本

class VersionComparatorException(Exception):
    pass


class VersionParseException(Exception):
    pass


class Version(object):

    def __init__(self, version, major=0):
        self.major = major
        self.version = version
        ver = version.split('.')
        new_list = [re.sub('\D+', '', i) for i in ver]

        while len(new_list) < 4:
            new_list.append('0')
        print(new_list)
        major = new_list[0]
        minor = new_list[1]
        build = new_list[2]
        revision = new_list[3]
        for i in new_list:
            if len(i)==0:
                raise VersionParseException()


    def __lt__(self, test):
        return self.version < test.version

    def __eq__(self, test):
        return self.version == test.version

    def __ne__(self, test):
        return self.version != test.version

print(Version('6.4.2') < Version ('5.5hh.5'))

比较版本只能版本如何实现与下面的表格相比抛出VersionComparatorException

Version("1.0") == 1
Version("2.0") == "2.0"
Version("3.1.2") < [3, 1, 2]
Version("1.5.5") > dict()

1 个答案:

答案 0 :(得分:0)

您必须添加以下检查:

if not isinstance(test, Version):
    raise VersionComparatorException()

在每个比较方法或某个辅助函数中。