Browse Source

More timestamper tests.

Add "end" parameter to TimestamperRate, to force the file to end
before a specific time.  Will be good for loading in old files and
knowing that things don't overlap.


git-svn-id: https://bucket.mit.edu/svn/nilm/nilmdb@10644 ddd99763-3ecb-0310-9145-efcb8ce7c51f
tags/bxinterval-last
Jim Paris 10 years ago
parent
commit
b72e276eb8
4 changed files with 100 additions and 27 deletions
  1. +30
    -6
      nilmdb/timestamper.py
  2. +1
    -1
      setup.cfg
  3. +5
    -14
      tests/test_client.py
  4. +64
    -6
      tests/test_timestamper.py

+ 30
- 6
nilmdb/timestamper.py View File

@@ -24,9 +24,21 @@ class Timestamper(object):

def readline(self, *args):
line = self.file.readline(*args)
if line:
line = self.ts_iter.next() + line
return line
if not line:
return ""
try:
return self.ts_iter.next() + line
except StopIteration:
return ""

def readlines(self, size = None):
out = ""
while True:
line = self.readline()
out += line
if not line or (size and len(out) >= size):
break
return out

def __iter__(self):
return self
@@ -39,14 +51,26 @@ class Timestamper(object):

class TimestamperRate(Timestamper):
"""Timestamper that uses a start time and a fixed rate"""
def __init__(self, file, start_timestamp, rate_hz):
def iterator(start, rate):
def __init__(self, file, start, rate, end = None):
"""
file: file name or object
start: Unix timestamp for the first value

rate: 1/rate is added to the timestamp for each line

end: If specified, raise StopIteration before outputting a value
greater than this."""
def iterator(start, rate, end):
n = 0
rate = float(rate)
while True:
now = start + n / rate
if end and now >= end:
raise StopIteration
yield sprintf("%.6f ", start + n / rate)
n += 1
Timestamper.__init__(self, file, iterator(start_timestamp, rate_hz))
Timestamper.__init__(self, file, iterator(start, rate, end))

class TimestamperNow(Timestamper):
"""Timestamper that uses current time"""


+ 1
- 1
setup.cfg View File

@@ -9,7 +9,7 @@ cover-erase=1
##cover-branches=1 # need nose 1.1.3 for this
stop=1
verbosity=2
tests=tests/test_timestamper.py
#tests=tests/test_timestamper.py
#tests=tests/test_serializer.py
#tests=tests/test_client.py:TestClient.test_client_nilmdb
#with-profile=1


+ 5
- 14
tests/test_client.py View File

@@ -128,18 +128,9 @@ class TestClient(object):

datetime_tz.localtz_set("America/New_York")

with open("tests/data/prep-20120323T1000") as prepdata:
start = datetime_tz.datetime_tz.smartparse("20120323T1000")
client.stream_insert_nots("/newton/prep", prepdata,
start, rate)
testfile = "tests/data/prep-20120323T1000"
start = datetime_tz.datetime_tz.smartparse("20120323T1000")
rate = 120
data = nilmdb.timestamper.TimestamperRate(testfile, start, 120)
#client.stream_insert("/newton/prep", data)
# insert with start/end
with open("tests/data/prep-20120323T1002") as prepdata:
start = datetime_tz.datetime_tz.smartparse("20120323T1002")
end = start + datetime_tz.timedelta(minutes=2)
client.stream_insert_untimed("/newton/prep", prepdata,
start = start, end = end)

# TODO: Just start writing the client side of this as if it works.
# Then fill things in to make it so!
pass

+ 64
- 6
tests/test_timestamper.py View File

@@ -7,6 +7,7 @@ from nose.tools import *
from nose.tools import assert_raises
import os
import sys
import cStringIO

def eq_(a, b):
if not a == b:
@@ -18,13 +19,70 @@ def ne_(a, b):

class TestTimestamper(object):

# Not a very comprehensive test, but it's good enough.
def test_timestamper(self):
now = datetime_tz.datetime_tz.now().totimestamp()
def join(list):
return "\n".join(list) + "\n"

# ts = nilmdb.timestamper.TimestamperRate("/tmp/foo", now, 8000)
# for line in ts:
# print line.strip()
start = datetime_tz.datetime_tz.smartparse("03/24/2012").totimestamp()
lines_in = [ "hello", "world", "hello world" ]
lines_out = [ "1332561600.000000 hello",
"1332561600.000125 world",
"1332561600.000250 hello world" ]

ts = nilmdb.timestamper.TimestamperNow("/tmp/foo")
# full
input = cStringIO.StringIO(join(lines_in))
ts = nilmdb.timestamper.TimestamperRate(input, start, 8000)
foo = ts.readlines()
eq_(foo, join(lines_out))

# first 30 or so bytes means the first 2 lines
input = cStringIO.StringIO(join(lines_in))
ts = nilmdb.timestamper.TimestamperRate(input, start, 8000)
foo = ts.readlines(30)
eq_(foo, join(lines_out[0:2]))

# stop iteration early
input = cStringIO.StringIO(join(lines_in))
ts = nilmdb.timestamper.TimestamperRate(input, start, 8000,
1332561600.000200)
foo = ""
for line in ts:
foo += line
eq_(foo, join(lines_out[0:2]))

# stop iteration early (readlines)
input = cStringIO.StringIO(join(lines_in))
ts = nilmdb.timestamper.TimestamperRate(input, start, 8000,
1332561600.000200)
foo = ts.readlines()
eq_(foo, join(lines_out[0:2]))

# stop iteration really early
input = cStringIO.StringIO(join(lines_in))
ts = nilmdb.timestamper.TimestamperRate(input, start, 8000,
1332561600.000000)
foo = ts.readlines()
eq_(foo, "")

# use iterator
input = cStringIO.StringIO(join(lines_in))
ts = nilmdb.timestamper.TimestamperRate(input, start, 8000)
foo = ""
for line in ts:
foo += line
eq_(foo, join(lines_out))

# check that TimestamperNow gives similar result
input = cStringIO.StringIO(join(lines_in))
ts = nilmdb.timestamper.TimestamperNow(input)
foo = ts.readlines()
ne_(foo, join(lines_out))
eq_(len(foo), len(join(lines_out)))

# Test passing a file (should be empty)
ts = nilmdb.timestamper.TimestamperNow("/dev/null")
for line in ts:
print line.strip()
raise AssertionError
ts.close()

Loading…
Cancel
Save