Browse Source

Need to fill out __and__ and __or__... tricky

git-svn-id: https://bucket.mit.edu/svn/nilm/nilmdb@9280 ddd99763-3ecb-0310-9145-efcb8ce7c51f
tags/bxinterval-last
Jim Paris 13 years ago
parent
commit
b22ebb6fa4
2 changed files with 90 additions and 12 deletions
  1. +8
    -0
      nilmdb/interval.py
  2. +82
    -12
      nilmdb/test_interval.py

+ 8
- 0
nilmdb/interval.py View File

@@ -21,6 +21,9 @@ class Interval(object):
return { }

def __setattr__(self, name, value):
"""Set attribute"""
# TODO: If we need to manipulate file names, offsets, lengths, etc,
# based on start and end time changing, do it here.
if (type(value) is not datetime):
raise IntervalError("Must set datetime values")
self.__dict__[name] = value
@@ -118,6 +121,11 @@ class IntervalSet(object):
return new

def __or__(self, other):
"""Compute a new IntervalSet from the union of two others"""
pass

def __and__(self, other):
"""Compute a new IntervalSet from the intersection of two others"""
pass
def _add_intervals(self, iterable):


+ 82
- 12
nilmdb/test_interval.py View File

@@ -115,18 +115,88 @@ def test_intervalset_construct():
# misc
assert(repr(iset) == repr(eval(repr(iset).replace("datetime.",""))))

def test_intervalset_intersect():
"""Test interval set intersections"""
dates = [ datetime.strptime(year, "%y") for year in [ "00", "01", "02", "03", "04", "05" ] ]

a = Interval(dates[0], dates[1]) # ---
b = Interval(dates[0], dates[1]) # ---
c = Interval(dates[0], dates[1]) # ---
d = Interval(dates[0], dates[1]) # ---
e = Interval(dates[0], dates[1]) # ---
f = Interval(dates[0], dates[1]) # -
g = Interval(dates[0], dates[1]) # -------

def iset(string):
"""Build an IntervalSet from a string, for testing purposes

Each character is a year
[ = interval start
| = interval end + adjacent start
] = interval end
anything else is ignored
"""
iset = IntervalSet()
for i, c in enumerate(string):
day = datetime.strptime("{0:04d}".format(i+2000), "%Y")
if (c == "["):
start = day
elif (c == "|"):
iset += Interval(start, day)
start = day
elif (c == "]"):
iset += Interval(start, day)
del start
return iset

def test_intervalset_iset():
"""test basic iset construction"""
assert(iset(" [----] ") ==
iset(" [-|--] "))

assert(iset("[] [--] ") +
iset(" [] [--]") ==
iset("[|] [-----]"))

def test_intervalset_intsersect():
"""test intersection (&)"""
assert(iset("[---------]") &
iset(" [---] ") ==
iset(" [---] "))

assert(iset(" [-----]") &
iset(" [-----] ") ==
iset(" [--] "))

assert(iset(" [---]") &
iset(" [--] ") ==
iset(" "))

assert(iset(" [-|---]") &
iset(" [-----|-] ") ==
iset(" [----] "))

assert(iset(" [-|-] ") &
iset(" [-|--|--] ") ==
iset(" [---] "))

assert(iset(" [----][--]") &
iset("[-] [--] []") ==
iset(" [] [-] []"))

def test_intervalset_union():
"""test union (|)"""
assert(iset("[---------]") |
iset(" [---] ") ==
iset("[---------]"))

assert(iset(" [-----]") |
iset(" [-----] ") ==
iset(" [--------]"))

assert(iset(" [---]") |
iset(" [--] ") ==
iset(" [--] [---]"))

assert(iset(" [-|---]") |
iset(" [-----|-] ") ==
iset(" [--------]"))

assert(iset(" [-|-] ") |
iset(" [-|--|--] ") ==
iset(" [-------] "))

assert(iset(" [----][--]") |
iset("[-] [--] []") ==
iset("[---------]"))

# start-other.start start-other.end end-other.start end-other.end
# --- < < < < n


Loading…
Cancel
Save