Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions petl/io/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,13 @@ def fromdicts(dicts, header=None, sample=1000, missing=None):
class DictsView(Table):

def __init__(self, dicts, header=None, sample=1000, missing=None):
self.dicts = dicts
self._dicts = dicts
self._header = header
self.sample = sample
self.missing = missing

def __iter__(self):
return iterdicts(self.dicts, self._header, self.sample, self.missing)
return iterdicts(self._dicts, self._header, self.sample, self.missing)


class DictsGeneratorView(DictsView):
Expand All @@ -222,7 +222,7 @@ def __iter__(self):
self._filecache = NamedTemporaryFile(delete=False, mode='wb+', buffering=0)

position = 0
it = iter(self.dicts)
it = iter(self._dicts)
while True:
if position < self._cached:
self._filecache.seek(position)
Expand All @@ -241,10 +241,10 @@ def __iter__(self):
yield row

def _determine_header(self):
it = iter(self.dicts)
it = iter(self._dicts)
header = list()
peek, it = iterpeek(it, self.sample)
self.dicts = it
self._dicts = it
if isinstance(peek, dict):
peek = [peek]
for o in peek:
Expand Down
13 changes: 12 additions & 1 deletion petl/test/io/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pytest

from petl.test.helpers import ieq
from petl import fromjson, fromdicts, tojson, tojsonarrays
from petl import dummytable, fromjson, fromdicts, tojson, tojsonarrays


def test_fromjson_1():
Expand Down Expand Up @@ -339,3 +339,14 @@ def generator():
('b', 2, "x"),
('c', "x", 2))
ieq(expect, actual)


def test_fromdicts_dicts_method():
# fromdicts() returns a DictsView whose internal data attribute was named
# 'dicts', shadowing the inherited Table.dicts() method. Calling .dicts()
# on the result should return row dicts, not raise TypeError.
dummy = dummytable(numrows=3, seed=42)
data = list(dummy.dicts())
actual = fromdicts(dummy.dicts())
result = list(actual.dicts())
assert result == data
Loading