Skip to content

Commit

Permalink
Memory Footprint Resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
fahadsiddiqui committed Sep 24, 2020
1 parent 704cbe4 commit fec465e
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions petl/io/json.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, division


# standard library dependencies
import io
import json
from json.encoder import JSONEncoder
from petl.compat import PY2


from petl.compat import PY2
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.io.sources import read_source_from_arg, write_source_from_arg


def fromjson(source, *args, **kwargs):
Expand Down Expand Up @@ -91,7 +89,6 @@ def fromjson(source, *args, **kwargs):


class JsonView(Table):

def __init__(self, source, *args, **kwargs):
self.source = source
self.missing = kwargs.pop('missing', None)
Expand All @@ -109,12 +106,13 @@ def __iter__(self):
write_through=True)
try:
if self.lines:
dicts = [json.loads(jline) for jline in f.read().splitlines()]
for row in iterjlines(f, self.header, self.missing):
yield row
else:
dicts = json.load(f, *self.args, **self.kwargs)
for row in iterdicts(dicts, self.header, self.sample,
self.missing):
yield row
for row in iterdicts(dicts, self.header, self.sample,
self.missing):
yield row
finally:
if not PY2:
f.detach()
Expand Down Expand Up @@ -173,6 +171,22 @@ def __iter__(self):
return iterdicts(self.dicts, self.header, self.sample, self.missing)


def iterjlines(f, header, missing):
it = iter(f)

if header is None:
header = list()
peek, it = iterpeek(it, 1)
json_obj = json.loads(peek)
if hasattr(json_obj, 'keys'):
header += [k for k in json_obj.keys() if k not in header]
yield tuple(header)

for o in it:
json_obj = json.loads(o)
yield tuple(json_obj[f] if f in json_obj else missing for f in header)


def iterdicts(dicts, header, sample, missing):
it = iter(dicts)

Expand Down

0 comments on commit fec465e

Please sign in to comment.