Browse Source

Start to be a little more strict about how intervals are half-open.

tags/before-insert-rework
Jim Paris 9 years ago
parent
commit
17c329fd6d
4 changed files with 51 additions and 49 deletions
  1. +1
    -0
      .gitignore
  2. +3
    -2
      nilmdb/interval.pyx
  3. +1
    -1
      setup.cfg
  4. +46
    -46
      tests/test_interval.py

+ 1
- 0
.gitignore View File

@@ -1,2 +1,3 @@
tests/*testdb/
.coverage
*.pyc

+ 3
- 2
nilmdb/interval.pyx View File

@@ -2,7 +2,8 @@

Represents an interval of time, and a set of such intervals.

Intervals are closed, ie. they include timestamps [start, end]
Intervals are half-open, ie. they include data points with timestamps
[start, end)
"""

# First implementation kept a sorted list of intervals and used
@@ -45,7 +46,7 @@ cdef class Interval:
return self.__class__.__name__ + "(" + s + ")"

def __str__(self):
return "[" + str(self.start) + " -> " + str(self.end) + "]"
return "[" + str(self.start) + " -> " + str(self.end) + ")"

def __cmp__(self, Interval other):
"""Compare two intervals. If non-equal, order by start then end"""


+ 1
- 1
setup.cfg View File

@@ -14,7 +14,7 @@ verbosity=2
#tests=tests/test_layout.py
#tests=tests/test_rbtree.py
#tests=tests/test_interval.py
tests=tests/test_rbtree.py,tests/test_interval.py
#tests=tests/test_rbtree.py,tests/test_interval.py
#tests=tests/test_interval.py
#tests=tests/test_client.py
#tests=tests/test_timestamper.py


+ 46
- 46
tests/test_interval.py View File

@@ -25,7 +25,7 @@ def makeset(string):

Each character is 1 second
[ = interval start
| = interval end + adjacent start
| = interval end + next start
] = interval end
. = zero-width interval (identical start and end)
anything else is ignored
@@ -38,7 +38,7 @@ def makeset(string):
elif (c == "|"):
iset += Interval(start, day)
start = day
elif (c == "]"):
elif (c == ")"):
iset += Interval(start, day)
del start
elif (c == "."):
@@ -88,14 +88,14 @@ class TestInterval:

# big integers and floats
x = Interval(5000111222, 6000111222)
eq_(str(x), "[5000111222.0 -> 6000111222.0]")
eq_(str(x), "[5000111222.0 -> 6000111222.0)")
x = Interval(123.45, 234.56)
eq_(str(x), "[123.45 -> 234.56]")
eq_(str(x), "[123.45 -> 234.56)")

# misc
i = Interval(d1, d2)
eq_(repr(i), repr(eval(repr(i))))
eq_(str(i), "[1332561600.0 -> 1332648000.0]")
eq_(str(i), "[1332561600.0 -> 1332648000.0)")

def test_interval_intersect(self):
# Test Interval intersections
@@ -175,59 +175,59 @@ class TestInterval:

# misc
eq_(repr(iset), repr(eval(repr(iset))))
eq_(str(iset), "[[100.0 -> 200.0], [200.0 -> 300.0]]")
eq_(str(iset), "[[100.0 -> 200.0), [200.0 -> 300.0)]")

def test_intervalset_geniset(self):
# Test basic iset construction
eq_(makeset(" [----] "),
makeset(" [-|--] "))
eq_(makeset(" [----) "),
makeset(" [-|--) "))

eq_(makeset("[] [--] ") +
makeset(" [] [--]"),
makeset("[|] [-----]"))
eq_(makeset("[) [--) ") +
makeset(" [) [--)"),
makeset("[|) [-----)"))

eq_(makeset(" [-------]"),
eq_(makeset(" [-------)"),
makeset(" [-|-----|"))


def test_intervalset_intersect(self):
# Test intersection (&)
with assert_raises(TypeError): # was AttributeError
x = makeset("[--]") & 1234
x = makeset("[--)") & 1234

# Intersection with interval
eq_(makeset("[---|---][]") &
list(makeset(" [------] "))[0],
makeset(" [-----] "))
eq_(makeset("[---|---)[)") &
list(makeset(" [------) "))[0],
makeset(" [-----) "))

# Intersection with sets
eq_(makeset("[---------]") &
makeset(" [---] "),
makeset(" [---] "))
eq_(makeset("[---------)") &
makeset(" [---) "),
makeset(" [---) "))

eq_(makeset(" [---] ") &
makeset("[---------]"),
makeset(" [---] "))
eq_(makeset(" [---) ") &
makeset("[---------)"),
makeset(" [---) "))

eq_(makeset(" [-----]") &
makeset(" [-----] "),
makeset(" [--] "))
eq_(makeset(" [-----)") &
makeset(" [-----) "),
makeset(" [--) "))

eq_(makeset(" [--] [--]") &
makeset(" [------] "),
makeset(" [-] [-] "))
eq_(makeset(" [--) [--)") &
makeset(" [------) "),
makeset(" [-) [-) "))

eq_(makeset(" [---]") &
makeset(" [--] "),
eq_(makeset(" [---)") &
makeset(" [--) "),
makeset(" "))

eq_(makeset(" [-|---]") &
makeset(" [-----|-] "),
makeset(" [----] "))
eq_(makeset(" [-|---)") &
makeset(" [-----|-) "),
makeset(" [----) "))

eq_(makeset(" [-|-] ") &
makeset(" [-|--|--] "),
makeset(" [---] "))
eq_(makeset(" [-|-) ") &
makeset(" [-|--|--) "),
makeset(" [---) "))

# Border cases -- will give different results if intervals are
# half open or fully closed. Right now, they are half open,
@@ -235,19 +235,19 @@ class TestInterval:
# often contain a data point at the endpoint.
half_open = True
if half_open:
eq_(makeset(" [---]") &
makeset(" [----] "),
eq_(makeset(" [---)") &
makeset(" [----) "),
makeset(" "))
eq_(makeset(" [----][--]") &
makeset("[-] [--] []"),
makeset(" [] [-] []"))
eq_(makeset(" [----)[--)") &
makeset("[-) [--) [)"),
makeset(" [) [-) [)"))
else:
eq_(makeset(" [---]") &
makeset(" [----] "),
eq_(makeset(" [---)") &
makeset(" [----) "),
makeset(" . "))
eq_(makeset(" [----][--]") &
makeset("[-] [--] []"),
makeset(" [] [-]. []"))
eq_(makeset(" [----)[--)") &
makeset("[-) [--) [)"),
makeset(" [) [-). [)"))

class TestIntervalDB:
def test_dbinterval(self):


Loading…
Cancel
Save