-
Notifications
You must be signed in to change notification settings - Fork 194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix generator support in fromdicts - use file cache instead of iterto… #625
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -3,15 +3,17 @@ | |||||
|
||||||
# standard library dependencies | ||||||
import io | ||||||
import itertools | ||||||
import json | ||||||
import inspect | ||||||
from json.encoder import JSONEncoder | ||||||
from os import unlink | ||||||
from tempfile import NamedTemporaryFile | ||||||
|
||||||
from petl.compat import PY2 | ||||||
from petl.compat import pickle | ||||||
from petl.io.sources import read_source_from_arg, write_source_from_arg | ||||||
# internal dependencies | ||||||
from petl.util.base import data, Table, dicts as _dicts, iterpeek | ||||||
from petl.util.base import data, Table, dicts as _dicts, iterpeek, iterchunk | ||||||
|
||||||
|
||||||
def fromjson(source, *args, **kwargs): | ||||||
|
@@ -140,6 +142,24 @@ def fromdicts(dicts, header=None, sample=1000, missing=None): | |||||
| 'c' | 2 | | ||||||
+-----+-----+ | ||||||
|
||||||
Argument `dicts` can also be a generator, the output of generator | ||||||
is iterated and cached using a temporary file to support further | ||||||
transforms and multiple passes of the table: | ||||||
|
||||||
>>> import petl as etl | ||||||
>>> dicts = ({"foo": chr(ord("a")+i), "bar":i+1} for i in range(3)) | ||||||
>>> table1 = etl.fromdicts(dicts, header=['foo', 'bar']) | ||||||
>>> table1 | ||||||
+-----+-----+ | ||||||
| foo | bar | | ||||||
+=====+=====+ | ||||||
| 'a' | 1 | | ||||||
+-----+-----+ | ||||||
| 'b' | 2 | | ||||||
+-----+-----+ | ||||||
| 'c' | 3 | | ||||||
+-----+-----+ | ||||||
|
||||||
If `header` is not specified, `sample` items from `dicts` will be | ||||||
inspected to discovery dictionary keys. Note that the order in which | ||||||
dictionary keys are discovered may not be stable, | ||||||
|
@@ -156,6 +176,16 @@ def fromdicts(dicts, header=None, sample=1000, missing=None): | |||||
:func:`petl.transform.headers.sortheader` on the resulting table to | ||||||
guarantee stability. | ||||||
|
||||||
.. versionchanged:: 1.7.5 | ||||||
|
||||||
Full support of generators passed as `dicts` has been added, leveraging | ||||||
`itertools.tee`. | ||||||
|
||||||
.. versionchanged:: 1.7.11 | ||||||
|
||||||
Generator support has been modified to use temporary file cache | ||||||
instead of `itertools.tee` due to high memory usage. | ||||||
|
||||||
""" | ||||||
view = DictsGeneratorView if inspect.isgenerator(dicts) else DictsView | ||||||
return view(dicts, header=header, sample=sample, missing=missing) | ||||||
|
@@ -175,9 +205,43 @@ def __iter__(self): | |||||
|
||||||
class DictsGeneratorView(DictsView): | ||||||
|
||||||
def __init__(self, dicts, header=None, sample=1000, missing=None): | ||||||
super(DictsGeneratorView, self).__init__(dicts, header, sample, missing) | ||||||
self._filecache = None | ||||||
|
||||||
def __iter__(self): | ||||||
self.dicts, dicts = itertools.tee(self.dicts) | ||||||
return iterdicts(dicts, self._header, self.sample, self.missing) | ||||||
if not self._header: | ||||||
self._determine_header() | ||||||
yield self._header | ||||||
|
||||||
if not self._filecache: | ||||||
self._filecache = NamedTemporaryFile(delete=False, mode='wb') | ||||||
Check notice Code scanning Consider using 'with' for resource-allocating operations (consider-using-with)
Consider using 'with' for resource-allocating operations (consider-using-with)
|
||||||
it = iter(self.dicts) | ||||||
for o in it: | ||||||
row = tuple(o[f] if f in o else self.missing for f in self._header) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Commited in the branch that has superseded this one: 662b785 |
||||||
pickle.dump(row, self._filecache, protocol=-1) | ||||||
self._filecache.flush() | ||||||
self._filecache.close() | ||||||
|
||||||
for row in iterchunk(self._filecache.name): | ||||||
yield row | ||||||
|
||||||
def _determine_header(self): | ||||||
it = iter(self.dicts) | ||||||
header = list() | ||||||
peek, it = iterpeek(it, self.sample) | ||||||
self.dicts = it | ||||||
if isinstance(peek, dict): | ||||||
peek = [peek] | ||||||
for o in peek: | ||||||
if hasattr(o, 'keys'): | ||||||
header += [k for k in o.keys() if k not in header] | ||||||
self._header = tuple(header) | ||||||
return it | ||||||
|
||||||
def __del__(self): | ||||||
if self._filecache: | ||||||
unlink(self._filecache.name) | ||||||
|
||||||
|
||||||
def iterjlines(f, header, missing): | ||||||
|
Check notice
Code scanning
Consider using Python 3 style super() without arguments (super-with-arguments)