How to use the pymarc.JSONWriter function in pymarc

To help you get started, we’ve selected a few pymarc 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 edsu / pymarc / test / test_writer.py View on Github external
def test_close_true(self):
        """If close_fh is true, then the file handle is also closed."""
        file_handle = StringIO()
        self.assertFalse(file_handle.closed, "The file handle should be open")
        writer = pymarc.JSONWriter(file_handle)
        self.assertFalse(file_handle.closed, "The file handle should still be open")
        writer.close()
        self.assertTrue(
            file_handle.closed, "The file handle should close when the writer closes"
        )
github edsu / pymarc / test / test_writer.py View on Github external
"ind1": "0",
                                "ind2": "0",
                                "subfields": [
                                    { "a": "Foo /" },
                                    { "c": "by me." }
                                ]
                            }
                        }
                    ]
                }
            ]
        """
        )
        file_handle = StringIO()
        try:
            writer = pymarc.JSONWriter(file_handle)
            record = pymarc.Record()
            record.add_field(pymarc.Field("100", ["0", "0"], ["a", "me"]))
            record.add_field(
                pymarc.Field("245", ["0", "0"], ["a", "Foo /", "c", "by me."])
            )
            writer.write(record)
            writer.close(close_fh=False)
            actual = json.loads(file_handle.getvalue())
            self.assertEquals(actual, expected)
        finally:
            file_handle.close()
github edsu / pymarc / test / test_writer.py View on Github external
def test_writing_empty_record(self):
        expected = json.loads(
            r"""
            [
                {
                    "leader" : "          22        4500",
                    "fields" : []
                }
            ]
        """
        )
        file_handle = StringIO()
        try:
            writer = pymarc.JSONWriter(file_handle)
            record = pymarc.Record()
            writer.write(record)
            writer.close(close_fh=False)
            actual = json.loads(file_handle.getvalue())
            self.assertEquals(actual, expected)
        finally:
            file_handle.close()
github edsu / pymarc / test / test_writer.py View on Github external
def test_writing_0_records(self):
        expected = json.loads(
            r"""
            []
        """
        )
        file_handle = StringIO()
        try:
            writer = pymarc.JSONWriter(file_handle)
            writer.close(close_fh=False)
            actual = json.loads(file_handle.getvalue())
            self.assertEquals(actual, expected)
        finally:
            file_handle.close()
github edsu / pymarc / test / test_writer.py View on Github external
def test_close_false(self):
        """If close_fh is false, then the file handle is NOT closed."""
        file_handle = StringIO()
        self.assertFalse(file_handle.closed, "The file handle should be open")
        writer = pymarc.JSONWriter(file_handle)
        self.assertFalse(file_handle.closed, "The file handle should still be open")
        writer.close(close_fh=False)
        self.assertFalse(
            file_handle.closed,
            "The file handle should NOT close when the writer closes",
        )
github edsu / pymarc / test / test_writer.py View on Github external
"ind1": "0",
                                "ind2": "0",
                                "subfields": [
                                    { "a": "Foo /" },
                                    { "c": "by me." }
                                ]
                            }
                        }
                    ]
                }
            ]
        """
        )
        file_handle = StringIO()
        try:
            writer = pymarc.JSONWriter(file_handle)
            record = pymarc.Record()
            record.add_field(
                pymarc.Field("008", data="090227s2009    mau                 chi d")
            )
            record.add_field(pymarc.Field("100", ["0", "0"], ["a", "me"]))
            record.add_field(
                pymarc.Field("245", ["0", "0"], ["a", "Foo /", "c", "by me."])
            )
            writer.write(record)
            record = pymarc.Record()
            record.add_field(pymarc.Field("100", ["0", "0"], ["a", "me"]))
            record.add_field(
                pymarc.Field("245", ["0", "0"], ["a", "Foo /", "c", "by me."])
            )
            writer.write(record)
            record = pymarc.Record()