Browse Source

Add nilmdb.utils.interval.optimize function

tags/nilmdb-1.8.1
Jim Paris 10 years ago
parent
commit
c7c3aff0fb
1 changed files with 19 additions and 1 deletions
  1. +19
    -1
      nilmdb/utils/interval.py

+ 19
- 1
nilmdb/utils/interval.py View File

@@ -1,5 +1,6 @@
"""Interval. Like nilmdb.server.interval, but re-implemented here """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 Intervals are half-open, ie. they include data points with timestamps
[start, end) [start, end)
@@ -104,3 +105,20 @@ def set_difference(a, b):
b_interval = None b_interval = None
if a_interval: if a_interval:
out_start = ts 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

Loading…
Cancel
Save