def _enum_rows(self, fh: IO[str], _slice: slice = None) -> Iterator[Tuple[int, Row]]: """Enumerate on-disk and in-memory rows.""" if _slice is None: _slice = slice(None) indices = range(*_slice.indices(len(self._rows))) fields = self.fields field_index = self._field_index file_exhausted = False for i, row in enumerate(self._rows): # always read next line until EOF to keep in sync if not file_exhausted: line: Optional[str] = None try: line = next(fh) except StopIteration: file_exhausted = True # now skip if it's not a requested index if i not in indices: continue # proceed only if we have a row in memory or on disk if row is None: if line is not None: row = Row(fields, tsdb.split(line), field_index=field_index) else: continue yield (i, row)
def test_split(empty_testsuite): assert tsdb.split('') == (None, ) assert tsdb.split('one') == ('one', ) assert tsdb.split(u'あ') == (u'あ', ) assert tsdb.split('one@two') == ('one', 'two') assert tsdb.split('one@@three') == ('one', None, 'three') assert (tsdb.split('one\\s@\\\\two\\nabc') == ('one@', '\\two\nabc')) rels = tsdb.read_schema(empty_testsuite) assert tsdb.split('10@one', fields=rels['item']) == (10, 'one')
def _getitem(self, index: int) -> Row: """Get a single non-slice index.""" row = self._rows[index] if row is None: # need to handle negative indices manually if index < 0: index = len(self._rows) + index with tsdb.open(self.dir, self.name, encoding=self.encoding) as lines: for i, line in enumerate(lines): if i == index: row = Row(self.fields, tsdb.split(line), field_index=self._field_index) break if row is None: raise ITSDBError('could not retrieve row {}'.format(index)) return row