From e2f89982cbb1006796211e0442717f8f5d5297e8 Mon Sep 17 00:00:00 2001 From: Jim Paris <jim@jtan.com> Date: Wed, 16 Feb 2011 22:30:26 +0000 Subject: [PATCH] Update tests etc git-svn-id: https://bucket.mit.edu/svn/nilm/nilmdb@9268 ddd99763-3ecb-0310-9145-efcb8ce7c51f --- src/nilmdb/__init__.py | 2 +- src/nilmdb/__init__.pyc | Bin 210 -> 0 bytes src/nilmdb/interval.py | 64 +++++++++++++++++++++++++++++++++--- src/nilmdb/interval.pyc | Bin 1072 -> 0 bytes src/nilmdb/test_interval.py | 41 +++++++++++++++++++++++ 5 files changed, 102 insertions(+), 5 deletions(-) delete mode 100644 src/nilmdb/__init__.pyc delete mode 100644 src/nilmdb/interval.pyc create mode 100644 src/nilmdb/test_interval.py diff --git a/src/nilmdb/__init__.py b/src/nilmdb/__init__.py index ab90bb1..f70d8d1 100644 --- a/src/nilmdb/__init__.py +++ b/src/nilmdb/__init__.py @@ -1,3 +1,3 @@ -from interval import Interval +from interval import * del interval diff --git a/src/nilmdb/__init__.pyc b/src/nilmdb/__init__.pyc deleted file mode 100644 index 5df0764847a6dcf38d6260c816a63cb0751695cd..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 210 zcmcckiI<B<JH|Je0ScIbv;z<qivx)iAYx?5W?;x+WQby9V2lRxnHYjKK#DT|1Azu3 zkX6C~Bs}v<Qj5wGbNn=zU_zNtp&+0Dg9a*5YzyS;XXNLm>Stx<>ZhcZ<>=?6rWWfb zl_qDWmgwhY=H!BjlqCJ)qGT8|K0Y%qvm`!Vub>iat4(fxN@-529mpBQAd3Z<0EA62 A2><{9 diff --git a/src/nilmdb/interval.py b/src/nilmdb/interval.py index 9352749..dee0f40 100644 --- a/src/nilmdb/interval.py +++ b/src/nilmdb/interval.py @@ -1,4 +1,7 @@ -import datetime +from datetime import datetime + +class IntervalException(Exception): + pass class Interval(object): """Represents an interval of time""" @@ -7,9 +10,62 @@ class Interval(object): self.start = start self.end = end + def __repr__(self): + return "Interval(" + repr(self.start) + ", " + repr(self.end) + ")" + + def __str__(self): + return "[" + str(self.start) + " -> " + str(self.end) + "]" + def __getattr__(self, name): return { } - def __setattr__(self, name): - raise Exception("__setattr__ " + name + "called") - + def __setattr__(self, name, value): + if (type(value).__name__ != 'datetime'): + raise IntervalException("Must set datetime values") + + self.__dict__[name] = value + if (type(self.start) is type(self.end)): + if (self.start > self.end): + raise IntervalException("Interval start must precede interval end") + + def intersects(self, other): + if (self.end <= other.start or + self.start >= other.end): + return False; + else: + return True; + + """hi + if (self.start < other.start && + self.end > other.start && + self.end < other.end) + + start-other.start start-other.end end-other.start end-other.end + + + ---- < < > < + ---- + + ---- < < < < n + ----- + + --- > < > < + -------- + + ------- < < > > + --- + + ---- > < > > + ----- + + ----------- > > > > n + ----- + """ + + +class IntervalSet(object): + """A non-intersecting set of intervals""" + + def __init__(self, value): + print "hello" + value + diff --git a/src/nilmdb/interval.pyc b/src/nilmdb/interval.pyc deleted file mode 100644 index 050fac0591d5562c1ba6fca07745e5a49aa38a5d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1072 zcmcIi&2H2{40f{HrCZu7<%DuVK|<sO03i?u4(wriVuduy&Uk6s{H!u=1yWD6FVsij zRd@mTJjw1p040hYdt!h7?D4PPCr9@$zkYT!K2v<(LBew=k2j%9q9u%!IH7eyenNLd zw?to-ss298HK+Q6XCa3$KefXBby@E)6CA#Wgcnc@G=M<IY(On13a-X<1YaJI8DPUR zq!`}x9JrN`m2Fmy@L9Lcq-xkr5TcSoK0v|~sGEdth%AgW8nNVAu2dGeM#_PPO4*BO zGPge0uV^!Y>$TBPqmemeP~i=%;=F6PxTu=K^L1U+%%NCaxl5L!t?DK!zbZoSw&s@c ztIdx%AZx3(5^HBb3tEqodE&H`iq?&7=$~`Kd<W9-C^9!Yo^9FigR*;!8rFVeDMfl~ zkKhL7M0<K#Jkom#L*jy9)~C3^)7yl8j&s(iML34GHf-TC(T-hN*X(0cGKc^1yW@>i zUAu42=-vE>#&%3jfIl{tE2r^Vbe`6Vwry?G`KwypN7412v)1UH7|k$BeNQo3|Gpju wpmCSOA&glvOXt)1WLQi+@ujftd>mxAx?m?U<-v)>fxI3@@K*OPPU|@R3&W-AKL7v# diff --git a/src/nilmdb/test_interval.py b/src/nilmdb/test_interval.py new file mode 100644 index 0000000..2774dd5 --- /dev/null +++ b/src/nilmdb/test_interval.py @@ -0,0 +1,41 @@ +from nilmdb import Interval, IntervalSet, IntervalException +from datetime import datetime +from nose.tools import assert_raises +import itertools + +def test_interval(): + """Test the Interval class""" + start = datetime.strptime("19801205","%Y%m%d") + end = datetime.strptime("20110216","%Y%m%d") + + # basic construction + i = Interval(start, end) + assert(i.start == start) + assert(i.end == end) + + # end before start + assert_raises(Exception, Interval, end, start) + +def test_interval_intersect(): + """Test Interval intersections""" + dates = [ datetime.strptime(year, "%y") for year in [ "00", "01", "02", "03" ] ] + perm = list(itertools.permutations(dates, 2)) + prod = list(itertools.product(perm, perm)) + should_intersect = { + False: [4, 5, 8, 20, 48, 56, 60, 96, 97, 100], + True: [0, 1, 2, 12, 13, 14, 16, 17, 24, 25, 26, 28, 29, + 32, 49, 50, 52, 53, 61, 62, 64, 65, 68, 98, 101, 104]} + for i,((a,b),(c,d)) in enumerate(prod): + try: + i1 = Interval(a, b) + i2 = Interval(c, d) + assert(i1.intersects(i2) == i2.intersects(i1)) + assert(i in should_intersect[i1.intersects(i2)]) + except IntervalException: + assert(i not in should_intersect[True] and + i not in should_intersect[False]) + +def test_intervalset(): + """Test interval sets""" + #iset = IntervalSet("hi") +