|
|
@@ -1,5 +1,6 @@ |
|
|
|
"""Interval. Like nilmdb.server.interval, but re-implemented here |
|
|
|
in plain Python so clients have easier access to it. |
|
|
|
in plain Python so clients have easier access to it, and with a few |
|
|
|
helper functions. |
|
|
|
|
|
|
|
Intervals are half-open, ie. they include data points with timestamps |
|
|
|
[start, end) |
|
|
@@ -104,3 +105,20 @@ def set_difference(a, b): |
|
|
|
b_interval = None |
|
|
|
if a_interval: |
|
|
|
out_start = ts |
|
|
|
|
|
|
|
def optimize(it): |
|
|
|
""" |
|
|
|
Given an iterable 'it' with intervals, optimize them by joining |
|
|
|
together intervals that are adjacent in time, and return a generator |
|
|
|
that yields the new intervals. |
|
|
|
""" |
|
|
|
saved_int = None |
|
|
|
for interval in it: |
|
|
|
if saved_int is not None: |
|
|
|
if saved_int.end == interval.start: |
|
|
|
interval.start = saved_int.start |
|
|
|
else: |
|
|
|
yield saved_int |
|
|
|
saved_int = interval |
|
|
|
if saved_int is not None: |
|
|
|
yield saved_int |