python 版本号比较 重载运算符
# -*- 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 版本号比较 重载运算符的更多相关文章
- Python 正确重载运算符
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px Helvetica } 有些事情让我不安,比如运算符重载.我决定不支持运算符重载,这完全是个人 ...
- python学习笔记之运算符
目录 前言 软件环境 身份运算符 算术运算符 比较运算符 位移运算符 自变运算符 位运算符 逻辑运算符 成员关系运算符 Python真值表 最后 前言 在前面的博文介绍了Python的数据结构之后,接 ...
- Python基本语法_运算符详解
目录 目录 前言 软件环境 身份运算符 算术运算符 比较运算符 位移运算符 自变运算符 位运算符 逻辑运算符 成员关系运算符 Python真值表 最后 前言 在前面的博文介绍了Python的数据结构之 ...
- c++的重载运算符
c++中允许重载运算符: 这是我辛苦的结果 #include"iostream"using namespace std;class aaa{ int x;public: aaa() ...
- C# 重载运算符
如果你想让自己定义的类型可以用运算符进行运算,那么可以通过重载运算符来实现: 示例: class Salary { public int RMB { get; set; } public static ...
- 【STL】重载运算符
重载运算符 为什么要重载运算符: C++中预定义的运算符的操作对象只能是基本数据类型.但实际上,对于许多用户自定义类型(例如结构体),也需要类似的运算操作.这时就必须在C++中重新定义这些运算符,赋予 ...
- c++中有些重载运算符为什么要返回引用
事实上,我们的重载运算符返回void.返回对象本身.返回对象引用都是可以的,并不是说一定要返回一个引用,只不过在不同的情况下需要不同的返回值. 那么什么情况下要返回对象的引用呢? 原因有两个: 允许进 ...
- C++ Primer : : 第十四章 : 重载运算符与类型转换之类型转换运算符和重载匹配
类型转换运算符 class SmallInt { public: SmallInt(int i = 0) : val(i) { if (i < 0 || i > 255) throw st ...
- C++ Primer : 第十四章 : 重载运算与类型转换之重载运算符
重载前须知 重载运算符是特殊的函数,它们的名字由operator和其后要重载的运算符号共同组成. 因为重载运算符时函数, 因此它包含返回值.参数列表和函数体. 对于重载运算符是成员函数时, 它的第一个 ...
随机推荐
- excel矩阵运算操作-转置 行列式 相乘 逆阵
excel矩阵运算操作-转置 行列式 相乘 逆阵 https://jingyan.baidu.com/article/154b463128e13928ca8f41a4.html Excel中矩阵的相关 ...
- maven引入CDH依赖包
1. POM文件加入仓库 注意:id要和仓库配置中的id一致. <repositories> <repository> <id>cloudera</id> ...
- git pull push 所有分支
因为远端 git 服务器上有很多分支,一个个分支pull太麻烦,所以找了 pull 所有分支的方法,如下: git branch -r | grep -v '\->' | while read ...
- Flutter Android 正式打包、以及升级应用 重新打包
Flutter Android 正式打包 打开androidStudio软件 1.修改版本号. android->app->src->main->AndroidManifest ...
- redis连接时报错:Could not connect to Redis at 127.0.0.1:6379: Connection refused
在/etc目录下找到redis.conf 将daemonize no 修改为 daemonize yes 再输入 redis-server /usr/local/etc/redis.conf redi ...
- [LeetCode] 61. Rotate List 旋转链表
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
- 使用命令行操控vmware--安装
在虚拟化出现之前,服务器的使用率很低,一般只有15%-20%,其余的资源处于闲置状态,但vmware出现之后,首先,可以充分的利用资源,从而达到节约成本的目的,其次,使得虚机便于维护.一般小型公司使用 ...
- Azure上部署Barracuda WAF集群 --- 2
前面一篇文章讲了如何在Azure上部署Barracuda.这篇文章聊一聊如何配置Barracuda. License 向Barracuda的销售人员申请WAF的License.得到License后打开 ...
- [转帖]Linux教程(8)-Linux中的进程和日志㐇、
Linux教程(8)-Linux中的进程和日志 2018-08-20 23:42:23 钱婷婷 阅读数 3554更多 分类专栏: Linux教程与操作 Linux教程与使用 版权声明:本文为博主原 ...
- SpringBoot自动配置原理学习
介绍 构建Springboot项目时我们会创建一个启动类 @SpringBootApplication public class DemoApplication { public static voi ...