美文网首页
python3比较版本号方法封装

python3比较版本号方法封装

作者: 写给明天的自己 | 来源:发表于2021-07-07 17:05 被阅读0次

    上学不是你的唯一出路,但是上学是你最好的出路。

    下一篇:python3检查列表、元组、字符串中的重复元素

    废话不多说,自己封装的方法,代码自己看,有注释,本人感觉已经很好了,如有还需优化的地方请评论留言,谢谢。

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @File      : compare.py
    
    import re
    
    
    class CompareSize:
        only_point_num = "\.{2,100}"
        ver_format = "^\d+(\.\d+){0,100}$"
        VERSION_ERR = "Please check the version number, example '6.2.3'."
        VERSION_TYPE_ERR = "Wrong type, please input string."
    
        @classmethod
        def compare_version(cls, old, new):
            """
            Compare the size of the version number.
    
            Compare the size of the two incoming version numbers (Format: 6.0.1),
            first judge whether the format and type of the version number are correct,
            then split the version number, and finally compare the size.
    
            :param old: Version number. type:str
            :param new: Version number. type:str
            :return: True: new > old, False: old > new, equal: old = new
            :rtype: str
            """
            
            try:
                result_old = re.search(cls.only_point_num, old)
                result_new = re.search(cls.only_point_num, new)
    
            except TypeError:
                return cls.VERSION_TYPE_ERR
    
            else:
                if not (result_old and result_new) is None:
                    return cls.VERSION_ERR
    
                else:
                    ver_format_old = re.search(cls.ver_format, old)
                    ver_format_new = re.search(cls.ver_format, new)
    
                    if not (ver_format_old and ver_format_new):
                        return cls.VERSION_ERR
                    else:
                        try:
                            list_old = old.split('.')
                            list_new = new.split('.')
    
                        except AttributeError:
                            return cls.VERSION_ERR
    
                        else:
                            if len(list_old) > len(list_new):
                                version_len = len(list_old)
                            else:
                                version_len = len(list_new)
    
                            for i in range(version_len):
                                try:
                                    if int(list_old[i]) > int(list_new[i]):
                                        return 'False'
                                    elif int(list_old[i]) == int(list_new[i]):
                                        continue
                                    else:
                                        return 'True'
    
                                except IndexError:
                                    if len(list_old) > len(list_new):
                                        return 'False'
                                    else:
                                        return 'True'
    
                            return 'equal'
    
    

    觉得可以的话点个赞呗,谢谢。


    如果感觉本文对您有帮助可以点个赞哦

    本文为学习笔记,转载请标明出处

    本文仅供交流学习,请勿用于非法途径

    仅是个人意见,如有想法,欢迎留言

    相关文章

      网友评论

          本文标题:python3比较版本号方法封装

          本文链接:https://www.haomeiwen.com/subject/ahooultx.html