Browse Source

Remove Table.__getitem__; used only by tests

tags/nilmdb-1.4.0
Jim Paris 10 years ago
parent
commit
28e72fd53e
2 changed files with 11 additions and 16 deletions
  1. +1
    -12
      nilmdb/server/bulkdata.py
  2. +10
    -4
      tests/test_bulkdata.py

+ 1
- 12
nilmdb/server/bulkdata.py View File

@@ -479,24 +479,13 @@ class Table(object):
return "".join(ret)

def get_timestamp(self, row):
"""Return the timestamp corresponding to the given row"""
"""Extract timestamps from a row, with table[n] notation."""
if (row is None or row < 0 or row >= self.nrows):
raise IndexError("Index out of range")
(subdir, filename, offset, count) = self._offset_from_row(row)
f = self.file_open(subdir, filename)
return f.extract_timestamp(offset)

def __getitem__(self, key):
"""Extract data and return it. Supports simple indexing
(table[n]) and range slices (table[n:m]). For simple
indexing, return 'row\n'. For ranges, return a list of rows
['row\n','row\n'].

This is inefficient; use .get_data instead."""
if isinstance(key, slice):
return [ self[x] for x in xrange(*key.indices(self.nrows)) ]
return self.get_data(key, key+1)

def _remove_rows(self, subdir, filename, start, stop):
"""Helper to mark specific rows as being removed from a
file, and potentially remove or truncate the file itself."""


+ 10
- 4
tests/test_bulkdata.py View File

@@ -49,10 +49,16 @@ class TestBulkData(object):
nodes.append(data.getnode("/foo/baz/quux"))
del nodes

def get_node_slice(key):
if isinstance(key, slice):
return [ node.get_data(x, x+1) for x in
xrange(*key.indices(node.nrows)) ]
return node.get_data(key, key+1)

# Test node
node = data.getnode("/foo/bar")
with assert_raises(IndexError):
x = node[0]
x = get_node_slice(0)
raw = []
for i in range(1000):
raw.append("%d 1 2 3 4 5 6 7 8\n" % (10000 + i))
@@ -67,14 +73,14 @@ class TestBulkData(object):

# Extract slices
for s in misc_slices:
eq_(node[s], raw[s])
eq_(get_node_slice(s), raw[s])

# Extract misc slices while appending, to make sure the
# data isn't being added in the middle of the file
for s in [2, slice(1,5), 2, slice(1,5)]:
node.append_string("0 0 0 0 0 0 0 0 0\n", 0, 50000)
raw.append("0 0 0 0 0 0 0 0 0\n")
eq_(node[s], raw[s])
eq_(get_node_slice(s), raw[s])

# Get some coverage of remove; remove is more fully tested
# in cmdline
@@ -92,7 +98,7 @@ class TestBulkData(object):

# Extract slices
for s in misc_slices:
eq_(node[s], raw[s])
eq_(get_node_slice(s), raw[s])

# destroy
with assert_raises(ValueError):


Loading…
Cancel
Save