Skip to content

Commit caf0a5c

Browse files
committed
moderated input validation based on sample solution
1 parent 0d07a9b commit caf0a5c

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

test_times.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,10 @@ def test_input_ends_at_start():
4040
def test_input_validation():
4141
"""input validation, does code raise error when start time > end time?"""
4242
with raises(ValueError):
43-
time_range("2010-01-12 12:00:00", "2010-01-12 10:00:00")
43+
time_range("2010-01-12 12:00:00", "2010-01-12 10:00:00")
44+
"""
45+
sample solution is
46+
with pytest.raises(ValueError) as e:
47+
time_range("2010-01-12 10:00:00", "2010-01-12 09:30:00")
48+
assert e.match("The end of the time range has to come strictly after its start)
49+
"""

times.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
def time_range(start_time, end_time, number_of_intervals=1, gap_between_intervals_s=0):
44
start_time_s = datetime.datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")
55
end_time_s = datetime.datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S")
6-
if end_time_s < start_time_s:
7-
raise ValueError ("end_time is smaller than start_time")
6+
if end_time_s <= start_time_s:
7+
raise ValueError ("the end_time has to come after the start_time")
88
d = (end_time_s - start_time_s).total_seconds() / number_of_intervals + gap_between_intervals_s * (1 / number_of_intervals - 1)
99
sec_range = [(start_time_s + datetime.timedelta(seconds=i * d + i * gap_between_intervals_s),
1010
start_time_s + datetime.timedelta(seconds=(i + 1) * d + i * gap_between_intervals_s))

0 commit comments

Comments
 (0)