Skip to content

Commit e051743

Browse files
committed
Clarify expression alias logic
Instead of using tuple indexing use a namedtuple mapped from the expression aliases. This makes readability / understanding of this logic a little easier.
1 parent dcd8b88 commit e051743

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

src/croniter/croniter.py

+21-15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from __future__ import absolute_import, print_function, division
55

6+
from collections import namedtuple
67
import traceback as _traceback
78
import copy
89
import math
@@ -76,6 +77,19 @@ def is_32bit():
7677

7778
EPOCH = datetime.datetime.fromtimestamp(0)
7879

80+
ExpressionAlias = namedtuple("ExpressionAlias", "regular, hashed")
81+
82+
# Non-standard expression aliases
83+
EXPR_ALIASES = {
84+
"@midnight": ExpressionAlias("0 0 * * *", "h h(0-2) * * * h"),
85+
"@hourly": ExpressionAlias("0 * * * *", "h * * * * h"),
86+
"@daily": ExpressionAlias("0 0 * * *", "h h * * * h"),
87+
"@weekly": ExpressionAlias("0 0 * * 0", "h h * * h h"),
88+
"@monthly": ExpressionAlias("0 0 1 * *", "h h h * * h"),
89+
"@yearly": ExpressionAlias("0 0 1 1 *", "h h h h * h"),
90+
"@annually": ExpressionAlias("0 0 1 1 *", "h h h h * h"),
91+
}
92+
7993
# fmt: off
8094
M_ALPHAS = {
8195
"jan": 1, "feb": 2, "mar": 3, "apr": 4, # noqa: E241
@@ -823,22 +837,14 @@ def _expand(
823837
# Split the expression in components, and normalize L -> l, MON -> mon,
824838
# etc. Keep expr_format untouched so we can use it in the exception
825839
# messages.
826-
expr_aliases = {
827-
"@midnight": ("0 0 * * *", "h h(0-2) * * * h"),
828-
"@hourly": ("0 * * * *", "h * * * * h"),
829-
"@daily": ("0 0 * * *", "h h * * * h"),
830-
"@weekly": ("0 0 * * 0", "h h * * h h"),
831-
"@monthly": ("0 0 1 * *", "h h h * * h"),
832-
"@yearly": ("0 0 1 1 *", "h h h h * h"),
833-
"@annually": ("0 0 1 1 *", "h h h h * h"),
834-
}
835-
836840
efl = expr_format.lower()
837-
hash_id_expr = hash_id is not None and 1 or 0
838-
try:
839-
efl = expr_aliases[efl][hash_id_expr]
840-
except KeyError:
841-
pass
841+
842+
# Expand expression aliases like `@midnight`
843+
if efl in EXPR_ALIASES:
844+
if hash_id is None:
845+
efl = EXPR_ALIASES[efl].regular
846+
else:
847+
efl = EXPR_ALIASES[efl].hashed
842848

843849
expressions = efl.split()
844850

0 commit comments

Comments
 (0)