Skip to content

Commit 8a37d82

Browse files
committed
Server: generalize iterate_over_range boundary inclusion
1 parent 8ec310d commit 8a37d82

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/server/utils/dates.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
)
99
from datetime import date, timedelta
1010
from epiweeks import Week, Year
11+
from operator import lt, le
1112
import logging
1213

1314
def time_value_to_date(value: int) -> date:
@@ -140,16 +141,21 @@ def _to_ranges(values: Sequence[Union[Tuple[int, int], int]], value_to_date: Cal
140141
logging.info('bad input to date ranges', input=values, exception=e)
141142
return values
142143

143-
def iterate_over_range(start: int, end: int) -> Iterator[int]:
144+
def iterate_over_range(start: int, end: int, inclusive: bool = False) -> Iterator[int]:
144145
"""Iterate over ints corresponding to dates in a time range.
145146
146-
Left inclusive, right exclusive to mimic behavior of Python's built-in range.
147+
By default left inclusive, right exclusive to mimic the behavior of the built-in range.
147148
"""
149+
if inclusive:
150+
op = le
151+
else:
152+
op = lt
153+
148154
if start > end:
149155
return
150156

151157
current_date, final_date = time_value_to_date(start), time_value_to_date(end)
152-
while current_date < final_date:
158+
while op(current_date, final_date):
153159
yield date_to_time_value(current_date)
154160
current_date = current_date + timedelta(days=1)
155161

0 commit comments

Comments
 (0)