以下是使用Python解析crontab时间格式的一个类, 同时minute和hour支持了 + 的操作. 记录一下备忘.

其中的line参数是字符串分拆后的格式, 包含了 "week", "month", "day", "hour", "minute".

#!/usr/bin/env python3
# -*-coding:utf-8-*- """
支持crontab表达式的分解, 判断是否符合条件.
@author Felix Zhang
@contact cnscud@gmail.com
@since 2018.1.11
""" from datetime import datetime class MatchTimeExp(object):
TIMEUNIT_FORMAT = {"minute": "%M", "hour": "%H", "day": "%d", "month": "%m", "year": "%Y", "week": "%w"}
TIMEUNIT_SCALE = {"minute": 60, "hour": 24} @staticmethod
def fire(line, cur_time):
holders = ["week", "month", "day", "hour", "minute"]
for h in holders:
if line[h] is not None and line[h] != "":
ret = MatchTimeExp.matchtime(line[h], h, cur_time)
if ret != 0:
return ret return 0 @staticmethod
def matchtime(exp, timeunit, thetime):
"""支持的格式: * 5 */5 */5+2 1,2,3 5-22 5-8,11-15
@:type thetime datetime
@:return match=0 , not match =1 , error = -1
"""
assert isinstance(thetime, datetime) exp = exp.replace(" ", "") digit_exp = exp.replace(",", "").replace("-", "").replace("/", "").replace("*", "").replace("+", "")
if digit_exp != "" and not digit_exp.isdigit():
return -1 # 分解逗号
nodes = exp.split(",")
if len(nodes) > 1:
for node in nodes:
if node != "" and MatchTimeExp.__matchtime_one(node, timeunit, thetime) == 0:
return 0
return 1
else:
return MatchTimeExp.__matchtime_one(exp, timeunit, thetime) @staticmethod
def __check_plusexp(step, timeunit, curtimenode):
""" 支持+ 的特殊语法""" # 仅支持
if timeunit not in ("minute", "hour"):
return -1 parts = step.strip().split("+")
if len(parts) == 2 and parts[0].strip().isdigit() and parts[1].strip().isdigit():
mystep = int(parts[0])
plusvalue = int(parts[1])
if plusvalue >= mystep:
return -1 timenode = curtimenode - plusvalue
if timenode < 0:
timenode += MatchTimeExp.TIMEUNIT_SCALE.get(timeunit) if timenode % mystep == 0:
return 0
else:
return -1 return 1 @staticmethod
def __matchtime_one(exp, timeunit, thetime):
if exp == "*":
return 0 if exp == "" or exp is None:
return 1 curtimenode = int(thetime.strftime(MatchTimeExp.TIMEUNIT_FORMAT.get(timeunit))) if exp == str(curtimenode):
return 0 patternfind = False items = exp.split('/')
if len(items) == 2 and items[0] == "*":
patternfind = True
step = items[1]
if step.isdigit():
if curtimenode % int(step) == 0:
return 0
else:
return MatchTimeExp.__check_plusexp(step, timeunit, curtimenode)
elif len(items) > 1:
return -1 # # 逗号
# nodes = exp.split(",")
# if len(nodes) > 0:
# for node in nodes:
# if node.strip() == str(curtimenode):
# return 0 # 减号:表示范围
nodes = exp.split("-")
if len(nodes) > 1:
patternfind = True
if len(nodes) == 2 and nodes[0].strip().isdigit() and nodes[1].strip().isdigit():
if int(nodes[0].strip()) <= curtimenode <= int(nodes[1].strip()):
return 0
else:
return -1 if not patternfind and not exp.isdigit():
return -1 return 1 def main():
thetime = datetime.now()
thetime = thetime.replace(minute=5) # 测试分钟
test("*", thetime, 0)
test("*/5", thetime, 0)
test("*/3", thetime, 1)
test("5", thetime, 0)
test("6", thetime, 1)
test("5,10,15", thetime, 0)
test("2,4,6", thetime, 1)
test("2-6", thetime, 0)
test("12-25", thetime, 1) test("2-6,9-12", thetime, 0)
test("12-15, 20-23", thetime, 1) thetime = datetime.now()
thetime = thetime.replace(minute=15)
test("*/5", thetime, 0)
test("*/3", thetime, 0)
test("*/7", thetime, 1)
test("6", thetime, 1)
test("5,10,15", thetime, 0)
test("2-6", thetime, 1)
test("12-25", thetime, 0)
test("2-6,9-12", thetime, 1)
test("12-15, 20-23", thetime, 0) thetime = datetime.now()
thetime = thetime.replace(minute=5)
test("*/7+6", thetime, 1)
test("*/7+5", thetime, 0)
test("*/7+2", thetime, 1) thetime = datetime.now()
thetime = thetime.replace(minute=1)
test("*/7+6", thetime, 1)
test("*/7+5", thetime, 0)
test("*/7+2", thetime, 1) thetime = datetime.now()
thetime = thetime.replace(minute=12)
test("*/7+5", thetime, 0)
test("*/7+1", thetime, 1)
test("*/7+9", thetime, -1) # wrong exp
test("a-b", thetime, -1)
test("a,2", thetime, -1)
test("*/b", thetime, -1)
test("*/7+a", thetime, -1)
test("*/a+b", thetime, -1) # , + - / * thetime = datetime.now()
thetime = thetime.replace(minute=12) test("12,", thetime, 0)
test("11,", thetime, 1)
test(",2", thetime, 1) test("3-5-8", thetime, -1)
test("3-", thetime, -1)
test("-3", thetime, -1)
test("5+2", thetime, -1)
test("/2", thetime, -1)
test("2/", thetime, -1)
test("*5", thetime, -1) thetime = datetime.now()
thetime = thetime.replace(hour=8) test("*/3+2", thetime, 0, "hour")
test("*/3+1", thetime, 1, "hour")
test("*/3+5", thetime, -1, "hour") thetime = thetime.replace(hour=1) test("*/3+2", thetime, 1, "hour")
test("*/3+1", thetime, 0, "hour") thetime = thetime.replace(day=5) test("*/4", thetime, 1, "day")
test("*/5", thetime, 0, "day")
test("*/3+2", thetime, -1, "day") thetime = datetime.now()
thetime = thetime.replace(minute=6) test_all({"week": "*", "month": "*", "day": "*", "hour": "*", "minute": "*/3"}, thetime, 0)
test_all({"week": "*", "month": "*", "day": "*", "hour": "*", "minute": "*/4"}, thetime, 1) def test_all(line, curtime, okresult):
timeprint = curtime.strftime("%Y-%m-%d %H:%M:%S")
# minute hour day month week
lineprint = " ".join([line["minute"], line["hour"], line["day"], line["month"], line["week"]])
if MatchTimeExp.fire(line, curtime) == okresult:
print("pass: matchtime %s check: (%s) is %i in %s" % ("all", lineprint, okresult, timeprint))
else:
print("not pass: matchtime %s check (%s) is %i in %s" % ("all", lineprint, okresult, timeprint)) def test(exp, curtime, okresult, unit="minute"):
timeprint = curtime.strftime("%Y-%m-%d %H:%M:%S")
if MatchTimeExp.matchtime(exp, unit, curtime) == okresult:
print("pass: matchtime %s check %s is in %s" % (unit, exp, timeprint))
else:
print("not pass: matchtime %s check %s is in %s" % (unit, exp, timeprint)) if __name__ == '__main__':
main()

Python: 解析crontab正则,增加+操作的更多相关文章

  1. Python学习-列表的转换和增加操作

    列表的转换和增加操作 列表的转换操作:可以将一个元组,字符串等转换成列表. str = "liuwenhao"; print(list(str)); // ['l', 'i', ' ...

  2. python 解析json loads dumps

    认识 引用模块 重要函数 案例 排序 缩进参数 压缩 参考 认识 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript(Standa ...

  3. Python解析器源码加密系列之(二):一次使用标准c的FILE*访问内存块的尝试

    摘要:由于近期打算修改Python解释器以实现pyc文件的加密/解密,出于保密的要求,解密之后的数据只能放在内存中,不能写入到文件中.但是后续的解析pyc文件的代码又只能接受FILE*作为入参,所以就 ...

  4. python 解析json

    一.概念 序列化(Serialization):将对象的状态信息转换为可以存储或可以通过网络传输的过程,传输的格式可以是JSON.XML等.反序列化就是从存储区域(JSON,XML)读取反序列化对象的 ...

  5. python解析xml之lxml

    虽然python解析xml的库很多,但是,由于lxml在底层是用C语言实现的,所以lxml在速度上有明显优势.除了速度上的优势,lxml在使用方面,易用性也非常好.这里将以下面的xml数据为例,介绍l ...

  6. dom4j解析XML的CURD操作

    在开发JavaWeb项目中通常是利用XML配置文件来提高应用程序的访问效率,而在配置XML的同时,更多时候是对XML进行解析. 一.解析XML配置文件有两种机制: DOM和SAX的机制: DOM是将整 ...

  7. python 解析Excel

    python 解析Excel 公司背景:好吧LZ太懒了.略... 原由起因:公司老板发话要导出公司数据库中符合条件的数据,源数据有400万,符合条件的大概有70万左右吧. 最终目的:符合条件的数据并生 ...

  8. Python 解析构建数据大杂烩 -- csv、xml、json、excel

    Python 可以通过各种库去解析我们常见的数据.其中 csv 文件以纯文本形式存储表格数据,以某字符作为分隔值,通常为逗号:xml 可拓展标记语言,很像超文本标记语言 Html ,但主要对文档和数据 ...

  9. python入门学习:3.操作列表

    python入门学习:3.操作列表 关键点:列表 3.1 遍历整个列表3.2 创建数值列表3.3 使用列表3.4 元组 3.1 遍历整个列表   循环这种概念很重要,因为它是计算机自动完成重复工作的常 ...

随机推荐

  1. Mysql8关于hashjoin的代码处理方式

    Mysql8关于hashjoin的代码处理方式 目录 Mysql8关于hashjoin的代码处理方式 1 表的Schema如下所示: 2 HashJoin代码实现 3 总结 1 表的Schema如下所 ...

  2. 解决SpringMVC重复提交的问题

    方法一:通过重定向采取请求转发的方式完成表单内容的添加会造成内容的重复插入.当向Servlet发送一条增加记录的请求后,servlet首先向数据库增加一条记录,然后又从数据库中查询出所有数据,接着转发 ...

  3. 远程代码执行MS08-067漏洞复现失败过程

    远程代码执行MS08-067漏洞复现失败过程 漏洞描述: 如果用户在受影响的系统上收到特制的 RPC 请求,则该漏洞可能允许远程执行代码. 在微软服务器系统上,攻击者可能未经身份验证即可利用此漏洞运行 ...

  4. 二、RabbitMQ 进阶特性及使用场景 [.NET]

    前言 经过上一篇的介绍,相信大家对RabbitMQ 的各种概念有了一定的了解,及如何使用RabbitMQ.Client 去发送和消费消息. 特性及使用场景 1. TTL 过期时间 TTL可以用来指定q ...

  5. 关于Kubernetes(简称K8S)的开启及基本使用,基于Docker Desktop & WSL2

    背景介绍 Kubernetes(简称k8s)已成为目前业界容器编排的事实标准,其搭配Docker可建立非常高效便捷的高可扩展.高可用应用服务架构. Kubernetes的名字来自希腊语,意思是&quo ...

  6. 从DVWA靶场学代码审计

    DVWA是较为经典的一个传统漏洞的靶场 内置了low,medium,hight,impossible四个安全级别供安全人员去研究相关漏洞.今天就来对impossible这个级别进行代码审计,从中学习一 ...

  7. HDU 4292 Food 多源多汇入门题

    Food 有F种食物和D种饮料,每种食物或饮料只能供有限次,且每个人只享用一种食物和一种饮料.现在有n个人,每个人都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几个人同时享用到自己喜欢的食物和饮 ...

  8. Java运算中的类型转换

    类型转换 运算中,不同类型的数据先转化为同一类型,然后进行运算 public class Dome04 { public static void main(String[] args) { //int ...

  9. MySQL主从异常恢复

    说明 MySQL主从出现不同步的情况时,或者要添加新的从库时,可以使用以下方法进行操作回复主从. 停止业务应用 停止所有连接到主从库上的应用,在恢复主从期间禁止任何增删改等操作,否则恢复失败 停止主从 ...

  10. Laravel + Swoole 打造IM简易聊天室

    最近在学习Swoole,利用Swoole扩展让PHP生动了不少,本篇就来Swoole开发一款简易的IM聊天室 应用场景:实现简单的即时消息聊天室. (一)扩展安装 pecl install swool ...