|
|
@@ -392,6 +392,7 @@ class Table(object): |
|
|
|
def append(self, data): |
|
|
|
"""Append the data and flush it to disk. |
|
|
|
data is a nested Python list [[row],[row],[...]]""" |
|
|
|
raise Exception("don't call me") |
|
|
|
remaining = len(data) |
|
|
|
dataiter = iter(data) |
|
|
|
while remaining: |
|
|
@@ -476,10 +477,9 @@ class Table(object): |
|
|
|
# Success, so update self.nrows accordingly |
|
|
|
self.nrows = tot_rows |
|
|
|
|
|
|
|
def _get_data(self, start, stop, as_string): |
|
|
|
def get_data(self, start, stop): |
|
|
|
"""Extract data corresponding to Python range [n:m], |
|
|
|
and returns a numeric list or formatted string, |
|
|
|
depending on as_string.""" |
|
|
|
and returns a formatted string""" |
|
|
|
if (start is None or |
|
|
|
stop is None or |
|
|
|
start > stop or |
|
|
@@ -495,42 +495,27 @@ class Table(object): |
|
|
|
if count > remaining: |
|
|
|
count = remaining |
|
|
|
f = self.file_open(subdir, filename) |
|
|
|
if as_string: |
|
|
|
ret.append(f.extract_string(offset, count)) |
|
|
|
else: |
|
|
|
ret.extend(f.extract_list(offset, count)) |
|
|
|
ret.append(f.extract_string(offset, count)) |
|
|
|
remaining -= count |
|
|
|
row += count |
|
|
|
if as_string: |
|
|
|
return "".join(ret) |
|
|
|
return ret |
|
|
|
return "".join(ret) |
|
|
|
|
|
|
|
def get_as_text(self, start, stop): |
|
|
|
"""Extract data corresponding to Python range [n:m], |
|
|
|
and returns a formatted string""" |
|
|
|
return self._get_data(start, stop, True) |
|
|
|
def get_timestamp(self, row): |
|
|
|
"""Return the timestamp corresponding to the given row""" |
|
|
|
if (row is None or row < 0 or row >= self.nrows): |
|
|
|
raise IndexError("Index out of range") |
|
|
|
return int(self[row].split(' ')[0]) |
|
|
|
|
|
|
|
def __getitem__(self, key): |
|
|
|
"""Extract data and return it. Supports simple indexing |
|
|
|
(table[n]) and range slices (table[n:m]). Returns a nested |
|
|
|
Python list [[row],[row],[...]]""" |
|
|
|
(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']. |
|
|
|
|
|
|
|
# Handle simple slices |
|
|
|
This is inefficient; use .get_data instead.""" |
|
|
|
if isinstance(key, slice): |
|
|
|
# Fall back to brute force if the slice isn't simple |
|
|
|
try: |
|
|
|
if (key.step is not None and key.step != 1): |
|
|
|
raise IndexError |
|
|
|
return self._get_data(key.start, key.stop, False) |
|
|
|
except IndexError: |
|
|
|
return [ self[x] for x in xrange(*key.indices(self.nrows)) ] |
|
|
|
|
|
|
|
# Handle single points (inefficiently!) |
|
|
|
if key < 0 or key >= self.nrows: |
|
|
|
raise IndexError("Index out of range") |
|
|
|
(subdir, filename, offset, count) = self._offset_from_row(key) |
|
|
|
f = self.file_open(subdir, filename) |
|
|
|
return f.extract_list(offset, 1)[0] |
|
|
|
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 |
|
|
@@ -628,4 +613,4 @@ class TimestampOnlyTable(object): |
|
|
|
def __init__(self, table): |
|
|
|
self.table = table |
|
|
|
def __getitem__(self, index): |
|
|
|
return self.table[index][0] |
|
|
|
return self.table.get_timestamp(index) |