Apsheduler学习笔记之cron表达式

cron 表达式由一组键值对 field = expr 确定

field是时间类型如年月日
expr是时间表达式如*/2, 1-5或者1-3/2


hour='1-5/2',一点到五点,每隔两小时

Field类关键方法

    def compile_expression(self, expr):
    # 将表达式转化成AllExpression/RangeExpression 对象
        for compiler in self.COMPILERS:
            match = compiler.value_re.match(expr)
            if match:
                compiled_expr = compiler(**match.groupdict())  # expr -> obj of Compiler

                try:
                    compiled_expr.validate_range(self.name)   # validate
                except ValueError as e:
                    exc = ValueError('Error validating expression {!r}: {}'.format(expr, e))
                    six.raise_from(exc, None)

                self.expressions.append(compiled_expr)
                return

        raise ValueError('Unrecognized expression "%s" for field "%s"' % (expr, self.name))
    def get_value(self, dateval):
        return getattr(dateval, self.name)

    def get_next_value(self, dateval):
        # call expr.get_next_value
        smallest = None
        for expr in self.expressions:
            value = expr.get_next_value(dateval, self)
            if smallest is None or (value is not None and value < smallest):
                smallest = value

        return smallest

你可能感兴趣的:(Apsheduler学习笔记之cron表达式)