Skip to content

Commit

Permalink
rewrote set_interval, and added test where same interval set twice in…
Browse files Browse the repository at this point in the history
… a row. Closes #222
  • Loading branch information
stringertheory committed Feb 5, 2024
1 parent 3c56b07 commit ffbdf57
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
13 changes: 13 additions & 0 deletions tests/test_traces.py
Expand Up @@ -123,6 +123,9 @@ def test_set_interval():
tsd = TimeSeries()
pytest.raises(ValueError, tsd.set_interval, 4, 4, 4)

tsd = TimeSeries()
pytest.raises(ValueError, tsd.set_interval, 4, 3, 4)


def test_set_interval_datetime():
ts = TimeSeries(default=400)
Expand Down Expand Up @@ -231,3 +234,13 @@ def value_parse(value):
assert ts[datetime(2000, 1, 1, 9)] is None
assert ts[datetime(2000, 1, 1, 10, 30)] == "15"
assert ts[datetime(2000, 1, 1, 20)] == "nan"


def test_set_same_interval_twice():
tr = TimeSeries({0: 10, 100: 10})

tr[17:42] = 0
assert list(tr.items()) == [(0, 10), (17, 0), (42, 10), (100, 10)]

tr[17:42] = 0
assert list(tr.items()) == [(0, 10), (17, 0), (42, 10), (100, 10)]
23 changes: 11 additions & 12 deletions traces/timeseries.py
Expand Up @@ -180,19 +180,18 @@ def set_interval(self, start, end, value, compact=False):
be anyway.
"""
# for each interval to render
for i, (s, _e, v) in enumerate(list(self.iterperiods(start, end))): # noqa: B007
# look at all intervals included in the current interval
# (always at least 1)
if i == 0:
# if the first, set initial value to new value of range
self.set(s, value, compact)
else:
# otherwise, remove intermediate key
del self[s]
if start >= end:
msg = f"start must be less than end, got start={start!r} and end={end!r}"
raise ValueError(msg)

end_value = self[end]

# delete all intermediate items between start and end
for t in list(self._d.irange(start, end, inclusive=(False, False))):
del self[t]

# finish by setting the end of the interval to the previous value
self.set(end, v, compact)
self.set(start, value, compact)
self.set(end, end_value, compact)

def compact(self):
"""Convert this instance to a compact version: the value will be the
Expand Down

0 comments on commit ffbdf57

Please sign in to comment.