def test_read_write_bytes_lines(self, tmpdir, spacy_doc): expected = [{ "idx": i, "sent": sent.text } for i, sent in enumerate(spacy_doc.sents)] for ext in (".json", ".json.gz", ".json.bz2", ".json.xz"): filepath = str( tmpdir.join("test_read_write_json_lines_bytes" + ext)) if compat.PY2 is True: if ext == ".json.xz": with pytest.raises(ValueError): io.open_sesame(filepath, mode="wb", encoding="utf-8", make_dirs=True) else: io.write_json(expected, filepath, mode="wb", make_dirs=True, lines=True) observed = list( io.read_json(filepath, mode="rb", lines=True)) assert observed == expected else: with pytest.raises(TypeError): io.write_json( expected, filepath, mode="wb", encoding=None, make_dirs=True, lines=True, )
def test_read_write_json_lines_str(tmpdir, spacy_doc): to_write = [{'idx': i, 'sent': sent.text} for i, sent in enumerate(spacy_doc.sents)] for subfield in ('idx', 'sent'): expected = [item[subfield] for item in to_write] filename = str(tmpdir.join('test_read_write_json_lines.json')) io.write_json(to_write, filename, make_dirs=True) observed = list(io.read_json(filename, lines='item.' + subfield)) assert observed == expected
def test_read_write_unicode(self, tmpdir, spacy_doc): expected = [{ "idx": i, "sent": sent.text } for i, sent in enumerate(spacy_doc.sents)] for ext in (".json", ".json.gz", ".json.bz2", ".json.xz"): filepath = str(tmpdir.join("test_read_write_json_unicode" + ext)) io.write_json(expected, filepath, mode="wt", make_dirs=True) observed = next(io.read_json(filepath, mode="rt", lines=False)) assert observed == expected
def test_read_write_unicode(self, tmpdir, spacy_doc): expected = [{"idx": i, "sent": sent.text} for i, sent in enumerate(spacy_doc.sents)] for ext in (".json", ".json.gz", ".json.bz2", ".json.xz"): filepath = str(tmpdir.join("test_read_write_json_unicode" + ext)) if compat.PY2 is True and ext != ".json": with pytest.raises(ValueError): io.open_sesame(filepath, mode="wt", encoding=None, make_dirs=True) else: io.write_json(expected, filepath, mode="wt", make_dirs=True) observed = next(io.read_json(filepath, mode="rt", lines=False)) assert observed == expected
def test_read_write_json_lines_str(tmpdir, spacy_doc): to_write = [{ "idx": i, "sent": sent.text } for i, sent in enumerate(spacy_doc.sents)] for subfield in ("idx", "sent"): expected = [item[subfield] for item in to_write] filename = str(tmpdir.join("test_read_write_json_lines.json")) io.write_json(to_write, filename, make_dirs=True) observed = list(io.read_json(filename, lines="item." + subfield)) assert observed == expected
def test_read_write_json_lines_unicode(tmpdir, spacy_doc): expected = [{'idx': i, 'sent': sent.text} for i, sent in enumerate(spacy_doc.sents)] for ext in ('.json', '.json.gz', '.json.bz2', '.json.xz'): filename = str(tmpdir.join('test_read_write_json_lines_unicode' + ext)) if compat.is_python2 is True and ext != '.json': with pytest.raises(ValueError): io.open_sesame( filename, mode='wt', encoding=None, make_dirs=True) else: io.write_json(expected, filename, mode='wt', make_dirs=True, lines=True) observed = list(io.read_json(filename, mode='rt', lines=True)) assert observed == expected
def test_read_write_json_bytes(tmpdir, spacy_doc): expected = [{'idx': i, 'sent': sent.text} for i, sent in enumerate(spacy_doc.sents)] for ext in ('.json', '.json.gz', '.json.bz2', '.json.xz'): filename = str(tmpdir.join('test_read_write_json_bytes' + ext)) if compat.is_python2 is True: if ext == '.json.xz': with pytest.raises(ValueError): io.open_sesame( filename, mode='wb', encoding='utf-8', make_dirs=True) else: io.write_json(expected, filename, mode='wb', make_dirs=True) observed = next(io.read_json(filename, mode='rb', lines=False)) assert observed == expected else: with pytest.raises(TypeError): io.write_json(expected, filename, 'wb', make_dirs=True)