How to use jsonlines - 10 common examples

To help you get started, we’ve selected a few jsonlines examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github alexwarstadt / data_generation / utils / data_generator.py View on Github external
if field in new_data:
                            new_data[field] = string_beautify(new_data[field])
                            new_data.update(constant_data)
                    new_data["pairID"] = str(pairID)
                    pairID += 1
                    if pairID % 100 == 0:
                        print("%d sentences generated" % pairID)
                    output_writer.write(new_data)
            except Exception as e:
                self.log_exception(e)
                print(self.get_stack_trace(e))
                error_counter += 1
                if error_counter > number_to_generate // 5:
                    pass
                    # raise Exception("Over 20\% of samples result in errors. You should fix this.")
        jsonlines.Writer(output).write_all(generated_data)
github alexwarstadt / data_generation / utils / data_generator.py View on Github external
past_sentences.append(track_sentence)


                    for C in new_data:
                        for field in self.data_fields:
                            if field in C:
                                C[field] = string_beautify(C[field])
                                C.update(constant_data)
                        generated_data.append(C)
            except Exception as e:
                self.log_exception(e)
                print(self.get_stack_trace(e))
                # error_counter += 1
                # if error_counter >= number_to_generate // 10:
                #     raise Exception("Over 10\% of samples result in errors. You should fix this.")
        jsonlines.Writer(output).write_all(generated_data)
github chainer / chainerrl-visualizer / tests / worker_jobs / test_rollout_job.py View on Github external
step_count = 15
    obs_list = MagicMock()
    render_img_list = MagicMock()

    os.makedirs(os.path.join(tmpdir, 'images'))

    if test_case == 'Unsupported':
        with pytest.raises(Exception):
            rollout(agent, gymlike_env, rollout_dir, step_count, obs_list, render_img_list)
            agent.stop_episode.assert_called_once()
        return

    rollout(agent, gymlike_env, rollout_dir, step_count, obs_list, render_img_list)
    agent.stop_episode.assert_called_once()

    with jsonlines.open('{}/{}'.format(tmpdir, ROLLOUT_LOG_FILE_NAME)) as reader:
        lines_num = 0
        for log_line in reader.iter(type=dict):
            lines_num += 1
        assert lines_num == 15

        # Common log entries for each test case
        assert 'step' in log_line
        assert 'reward' in log_line
        assert 'image_path' in log_line
        assert 'action' in log_line

        if test_case == 'A3C':
            assert 'state_value' in log_line
            assert len(log_line['action_probs']) == 4
        elif test_case == 'PPO':
            assert 'state_value' in log_line
github wbolster / jsonlines / tests / test_jsonlines.py View on Github external
def test_custom_dumps():
    fp = io.BytesIO()
    writer = jsonlines.Writer(fp, dumps=lambda obj: 'oh hai')
    with writer:
        writer.write({})
    assert fp.getvalue() == b'oh hai\n'
github wbolster / jsonlines / tests / test_jsonlines.py View on Github external
def test_reading_from_iterable():
    with jsonlines.Reader(['1', b'{}']) as reader:
        assert list(reader) == [1, {}]
    assert 'wrapping 
github wbolster / jsonlines / tests / test_jsonlines.py View on Github external
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
github wbolster / jsonlines / tests / test_jsonlines.py View on Github external
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()
github wbolster / jsonlines / tests / test_jsonlines.py View on Github external
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)
github wbolster / jsonlines / tests / test_jsonlines.py View on Github external
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]
github wbolster / jsonlines / tests / test_jsonlines.py View on Github external
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)

jsonlines

Library with helpers for the jsonlines file format

BSD-2-Clause
Latest version published 8 months ago

Package Health Score

73 / 100
Full package analysis

Similar packages