Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_reading_from_iterable():
with jsonlines.Reader(['1', b'{}']) as reader:
assert list(reader) == [1, {}]
assert 'wrapping
def test_invalid_lines():
data = u'[1, 2'
with jsonlines.Reader(io.StringIO(data)) as reader:
with pytest.raises(jsonlines.InvalidLineError) as excinfo:
reader.read()
exc = excinfo.value
assert "invalid json" in str(exc)
assert exc.line == data
def test_reader():
fp = io.BytesIO(SAMPLE_BYTES)
with jsonlines.Reader(fp) as reader:
it = iter(reader)
assert next(it) == {'a': 1}
assert next(it) == {'b': 2}
with pytest.raises(StopIteration):
next(it)
with pytest.raises(EOFError):
reader.read()
def test_invalid_utf8():
with jsonlines.Reader([b'\xff\xff']) as reader:
with pytest.raises(jsonlines.InvalidLineError) as excinfo:
reader.read()
assert 'line is not valid utf-8' in str(excinfo.value)
def test_empty_strings_in_iterable():
input = ['123', '', '456']
it = iter(jsonlines.Reader(input))
assert next(it) == 123
with pytest.raises(jsonlines.InvalidLineError):
next(it)
with pytest.raises(StopIteration):
next(it)
it = jsonlines.Reader(input).iter(skip_empty=True)
assert list(it) == [123, 456]
def unify_results(results_file, data_file):
results = np.load(os.path.join(presuppositions_dir, results_file))
data = [l for l in jsonlines.Reader(open(os.path.join(data_dir, data_file))).iter()]
for r, d in zip(results, data):
d["pred_entailment"] = r[0]
d["pred_neutral"] = r[1]
d["pred_contradiction"] = r[2]
return data
def __iter_extended_rows(self):
rows = jsonlines.Reader(self.__chars)
for row_number, row in enumerate(rows, start=1):
if isinstance(row, (tuple, list)):
yield row_number, None, list(row)
elif isinstance(row, dict):
keys, values = zip(*sorted(row.items()))
yield (row_number, list(keys), list(values))
else:
if not self.__force_parse:
raise exceptions.SourceError('JSON item has to be list or dict')
yield (row_number, None, [])
def _load(cls, readable_file):
reader = jsonlines.Reader(readable_file)
meta = reader.read()
meta.pop('top_hash', None) # Obsolete as of PR #130
pkg = cls()
pkg._meta = meta
for obj in reader:
path = cls._split_key(obj.pop('logical_key'))
subpkg = pkg._ensure_subpackage(path[:-1])
key = path[-1]
if not obj.get('physical_keys', None):
# directory-level metadata
subpkg.set_meta(obj['meta'])
continue
if key in subpkg._children:
raise PackageException("Duplicate logical key while loading package")
subpkg._children[key] = PackageEntry(
obj['physical_keys'][0],