# -*- coding: utf-8 -*-

class VersionNum(object):
"""
版本号比较
默认版本以“.”分割,各位版本位数不超过3
例一:
235.458.95 由“.”分割版本,235、458、95都不能大于999
如果有版本要超过3位,需指定max_digit,但注意比较时,两个对比的max_digit必须相等!
例二:
if VersionNum('1.20.3') > VersionNum('1.9.3'): ...
"""
def __init__(self, version, delimiter='.', max_digit=3):
v_num = 0
v_list = version.split(delimiter)
for i, num in enumerate(v_list[-1::-1]):
if len(num) > max_digit:
raise ValueError('version can not greater than max_digit')
v_num += int(num) * (10 ** (i*max_digit))
self.max_digit = max_digit
self.data = v_num def __repr__(self):
"""
自我描述
"""
return "VersionNum(%d)" % self.data def __eq__(self, other):
self.check_max_digit(other)
return self.data == other.data def __ne__(self, other):
self.check_max_digit(other)
return self.data != other.data def __lt__(self, other):
self.check_max_digit(other)
return self.data < other.data def __le__(self, other):
self.check_max_digit(other)
return self.data <= other.data def __gt__(self, other):
self.check_max_digit(other)
return self.data > other.data def __ge__(self, other):
self.check_max_digit(other)
return self.data >= other.data def check_max_digit(self, other):
if self.max_digit != other.max_digit:
raise ValueError('The max_digit of the two versions of the comparison should be equal')

python 版本号比较 重载运算符的更多相关文章

  1. Python 正确重载运算符

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px Helvetica } 有些事情让我不安,比如运算符重载.我决定不支持运算符重载,这完全是个人 ...

  2. python学习笔记之运算符

    目录 前言 软件环境 身份运算符 算术运算符 比较运算符 位移运算符 自变运算符 位运算符 逻辑运算符 成员关系运算符 Python真值表 最后 前言 在前面的博文介绍了Python的数据结构之后,接 ...

  3. Python基本语法_运算符详解

    目录 目录 前言 软件环境 身份运算符 算术运算符 比较运算符 位移运算符 自变运算符 位运算符 逻辑运算符 成员关系运算符 Python真值表 最后 前言 在前面的博文介绍了Python的数据结构之 ...

  4. c++的重载运算符

    c++中允许重载运算符: 这是我辛苦的结果 #include"iostream"using namespace std;class aaa{ int x;public: aaa() ...

  5. C# 重载运算符

    如果你想让自己定义的类型可以用运算符进行运算,那么可以通过重载运算符来实现: 示例: class Salary { public int RMB { get; set; } public static ...

  6. 【STL】重载运算符

    重载运算符 为什么要重载运算符: C++中预定义的运算符的操作对象只能是基本数据类型.但实际上,对于许多用户自定义类型(例如结构体),也需要类似的运算操作.这时就必须在C++中重新定义这些运算符,赋予 ...

  7. c++中有些重载运算符为什么要返回引用

    事实上,我们的重载运算符返回void.返回对象本身.返回对象引用都是可以的,并不是说一定要返回一个引用,只不过在不同的情况下需要不同的返回值. 那么什么情况下要返回对象的引用呢? 原因有两个: 允许进 ...

  8. C++ Primer : : 第十四章 : 重载运算符与类型转换之类型转换运算符和重载匹配

    类型转换运算符 class SmallInt { public: SmallInt(int i = 0) : val(i) { if (i < 0 || i > 255) throw st ...

  9. C++ Primer : 第十四章 : 重载运算与类型转换之重载运算符

    重载前须知 重载运算符是特殊的函数,它们的名字由operator和其后要重载的运算符号共同组成. 因为重载运算符时函数, 因此它包含返回值.参数列表和函数体. 对于重载运算符是成员函数时, 它的第一个 ...

随机推荐

  1. AIX 系统参数配置

    AIX 系统参数配置 原创 Linux操作系统 作者:fanhongjie 时间:2008-05-08 22:46:37 540 0 AIX内核属于动态内核,核心参数基本上可以自动调整,因此当系统安装 ...

  2. 【转载】 tensorflow的单层静态与动态RNN比较

    原文地址: https://www.jianshu.com/p/1b1ea45fab47 yanghedada -------------------------------------------- ...

  3. 数据分析入门——pandas之数据合并

    主要分为:级联:pd.concat.pd.append 合并:pd.merge 一.numpy级联的回顾 详细参考numpy章节 https://www.cnblogs.com/jiangbei/p/ ...

  4. 数据分析入门——pandas之DataFrame多层/多级索引与聚合操作

    一.行多层索引 1.隐式创建 在构造函数中给index.colunms等多个数组实现(datafarme与series都可以) df的多级索引创建方法类似: 2.显式创建pd.MultiIndex 其 ...

  5. centos 安装 libiconv

    安装方法如下: cd /usr/local/src wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz tar -zxvf li ...

  6. vagrant报错处理

    vagrant up报错 Warning: Authentication failure. Retrying...解决方案 http://www.cnblogs.com/zqifa/p/vagrant ...

  7. webpack打包完成,复制,打包,移动,删除已生成的文件插件

    const FileManagerPlugin = require('filemanager-webpack-plugin'); 详情请到 https://www.npmjs.com/ 一看究竟 贴个 ...

  8. LeetCode 227. 基本计算器 II(Basic Calculator II)

    227. 基本计算器 II 227. Basic Calculator II 题目描述 实现一个基本的计算器来计算一个简单的字符串表达式的值. 字符串表达式仅包含非负整数,+,-,*,/ 四种运算符和 ...

  9. [转帖]ARM A77+G77最强公版架构:联发科5G SoC计划11月26日发布

    ARM A77+G77最强公版架构:联发科5G SoC计划11月26日发布 https://www.cnbeta.com/articles/tech/909025.htm 主流的手机SoC厂商已经纷纷 ...

  10. c++修改打印机名称

    public static bool SetPrinterName(string OldName, string newName) { IntPtr hPrinter; PrintAPI.struct ...