How to use pymarc - 10 common examples

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 jermnelson / aristotle-library-apps / bibframe / ingesters / test_MARC.py View on Github external
def test_date2(self):
        # Authority Record
        record = pymarc.Record()
        record.add_field(pymarc.Field(tag='130',
                                      indicators=[' ', '0'],
                                      subfields=['f', '1902']))
        title_parser = MARC21toTitleEntity(redis_datastore=TEST_REDIS,
                                           record=record)
        title_parser.ingest()
        self.assertEquals(title_parser.title_entity.date,
                          ['1902'])
github edsu / pymarc / test / test_writer.py View on Github external
def test_writing_1_record(self):
        expected = r"""
            =LDR            22        4500
            =100  00$ame
            =245  00$aFoo /$cby me.
        """
        expected = textwrap.dedent(expected[1:])
        file_handle = StringIO()
        try:
            writer = pymarc.TextWriter(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)
            self.assertEquals(file_handle.getvalue(), expected)
        finally:
            file_handle.close()
github KnowledgeLinks / bibcat / tests / test_marc_ingester.py View on Github external
def setUp(self):
        self.marc_record = pymarc.Record()
        self.marc_record.add_field(
            pymarc.Field('245', 
                ['1', '0'],
                ['a', 'This is a test:',
                 'b', 'and subtitle']))
        self.ingester = marc.MARCIngester(record=self.marc_record)
github edsu / pymarc / test / test_marc8.py View on Github external
self.assertEqual(type(utitle), bytes)
            self.assertEqual(utitle, b"De la solitude a\xcc\x80 la communaute\xcc\x81.")

        with open("test/utf8_without_leader_flag.dat", "rb") as fh:
            reader = MARCReader(fh, to_unicode=True, hide_utf8_warnings=True)
            record = next(reader)
            self.assertEqual(type(record), Record)
            utitle = record["240"]["a"]
            self.assertEqual(type(utitle), str)
            # unless you force utf-8 characters will get lost and
            # warnings will appear in the terminal
            self.assertEqual(utitle, "De la solitude a   la communaute .")

        # force reading as utf-8
        with open("test/utf8_without_leader_flag.dat", "rb") as fh:
            reader = MARCReader(
                fh, to_unicode=True, force_utf8=True, hide_utf8_warnings=True
            )
            record = next(reader)
            self.assertEqual(type(record), Record)
            utitle = record["240"]["a"]
            self.assertEqual(type(utitle), str)
            self.assertEqual(
                utitle,
                u"De la solitude a"
                + chr(0x0300)
                + " la communaute"
                + chr(0x0301)
                + ".",
            )
github edsu / pymarc / test / test_record.py View on Github external
def test_location(self):
        record = Record()
        loc1 = "=852  \\\\$aAmerican Institute of Physics.$bNiels Bohr Library and Archives.$eCollege Park, MD"
        loc2 = "=852  01$aCtY$bMain$hLB201$i.M63"
        loclist = [loc1, loc2]
        self.assertEqual(record.location(), [])
        record.add_field(Field("040", [" ", " "], subfields=["a", "DLC", "c", "DLC"]))
        record.add_field(
            Field(
                "852",
                [" ", " "],
                subfields=[
                    "a",
                    "American Institute of Physics.",
                    "b",
                    "Niels Bohr Library and Archives.",
                    "e",
                    "College Park, MD",
                ],
            )
        )
        record.add_field(
            Field(
                "852",
                [0, 1],
github edsu / pymarc / test / test_record.py View on Github external
def test_added_entries(self):
        record = Record()
        ae1 = "=730  0\\$aTosefta.$lEnglish.$f1977."
        ae2 = "=700  10$aLe Peu, Pepe."
        aelist = [ae1, ae2]
        self.assertEqual(record.addedentries(), [])
        record.add_field(
            Field(
                "730",
                [0, " "],
                subfields=["a", "Tosefta.", "l", "English.", "f", "1977."],
            )
        )
        record.add_field(Field("700", [1, 0], subfields=["a", "Le Peu, Pepe."]))
        record.add_field(Field("245", [0, 0], subfields=["a", "Le Peu's Tosefa."]))
        self.assertEqual(len(record.addedentries()), 2)
        self.assertEqual(record.addedentries()[0].__str__(), ae1)
        self.assertEqual(record.addedentries()[1].__str__(), ae2)
        raelist = [rae.__str__() for rae in record.addedentries()]
        self.assertEqual(aelist, raelist)
github edsu / pymarc / test / test_record.py View on Github external
def test_author(self):
        record = Record()
        self.assertEqual(record.author(), None)
        record.add_field(
            Field("100", [1, 0], subfields=["a", "Bletch, Foobie,", "d", "1979-1981."])
        )
        self.assertEqual(record.author(), "Bletch, Foobie, 1979-1981.")

        record = Record()
        record.add_field(
            Field("130", [0, " "], subfields=["a", "Bible.", "l", "Python."])
        )
        self.assertEqual(record.author(), None)
github edsu / pymarc / test / test_record.py View on Github external
def test_issn(self):
        record = Record()
        self.assertEqual(record.issn(), None)
        record.add_field(
            Field(tag="022", indicators=["0", ""], subfields=["a", "0395-2037"])
        )
        self.assertEqual(record.issn(), "0395-2037")
github edsu / pymarc / test / test_record.py View on Github external
def test_multi_find(self):
        record = Record()
        subject1 = Field(
            tag="650", indicators=["", "0"], subfields=["a", "Programming Language"]
        )
        record.add_field(subject1)
        subject2 = Field(
            tag="651", indicators=["", "0"], subfields=["a", "Object Oriented"]
        )
        record.add_field(subject2)
        found = record.get_fields("650", "651")
        self.assertEqual(len(found), 2)
github edsu / pymarc / test / test_writer.py View on Github external
def test_write(self):
        """Write a record off to a file."""
        file_handle = open("test/writer-test.dat", "wb")
        writer = pymarc.MARCWriter(file_handle)
        record = pymarc.Record()
        field = pymarc.Field("245", ["0", "0"], ["a", "foo"])
        record.add_field(field)
        writer.write(record)
        writer.close()
        self.assertTrue(
            file_handle.closed, "The file handle should close when the writer closes"
        )

        # read it back in
        reader = pymarc.MARCReader(open("test/writer-test.dat", "rb"))
        next(reader)
        reader.close()

        # remove it
        os.remove("test/writer-test.dat")