Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
Fixed parsers.Native reset (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
roll authored Sep 13, 2016
1 parent afc6e2c commit 63f848a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
6 changes: 6 additions & 0 deletions tabulator/parsers/native.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import absolute_import
from __future__ import unicode_literals

import six
from .. import exceptions
from . import api

Expand All @@ -26,6 +27,9 @@ def closed(self):
return True

def open(self, source, encoding, loader):
if hasattr(source, '__next__' if six.PY3 else 'next'):
message = 'Only callable returning an iterator is supported'
raise exceptions.ParsingError(message)
self.close()
self.__source = source
self.reset()
Expand All @@ -44,6 +48,8 @@ def extended_rows(self):

def __iter_extended_rows(self):
items = self.__source
if not hasattr(items, '__iter__'):
items = items()
for number, item in enumerate(items, start=1):
if isinstance(item, (tuple, list)):
yield (number, None, list(item))
Expand Down
36 changes: 34 additions & 2 deletions tests/test_topen.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,27 @@ def test_native_iterator():
assert table.read() == [['id', 'name'], ['1', 'english'], ['2', '中国人']]


def test_native_iterator():

# Get table
def generator():
yield ['id', 'name']
yield ['1', 'english']
yield ['2', '中国人']
with pytest.raises(exceptions.ParsingError) as excinfo:
iterator = generator()
topen(iterator)
assert 'callable' in str(excinfo.value)


def test_native_generator():

# Get table
def generator():
yield ['id', 'name']
yield ['1', 'english']
yield ['2', '中国人']
source = generator()
table = topen(source)
table = topen(generator)

# Make assertions
assert table.headers is None
Expand Down Expand Up @@ -433,6 +445,26 @@ def test_reset_and_sample_size():
(7, ['id', 'name'], ['6', 'f'])]


def test_reset_generator():

# Generator
def generator():
yield [1]
yield [2]

# Get table
table = topen(generator, sample_size=0)

# Make assertions
assert table.read() == [[1], [2]]

# Reset
table.reset()

# Make assertions
assert table.read() == [[1], [2]]


# Tests [processors]

def test_processors_headers():
Expand Down

0 comments on commit 63f848a

Please sign in to comment.