How to use the pymarc.reader.MARCReader 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_reader.py View on Github external
def setUp(self):
        fh = open("test/test.dat", "rb")
        raw = fh.read()
        fh.close()

        self.reader = pymarc.reader.MARCReader(raw)
github edsu / pymarc / test / test_record.py View on Github external
def test_copy(self):
        from copy import deepcopy

        with open("test/one.dat", "rb") as fh:
            r1 = next(MARCReader(fh))
            r2 = deepcopy(r1)
            r1.add_field(Field("999", [" ", " "], subfields=["a", "foo"]))
            r2.add_field(Field("999", [" ", " "], subfields=["a", "bar"]))
            self.assertEqual(r1["999"]["a"], "foo")
            self.assertEqual(r2["999"]["a"], "bar")
github edsu / pymarc / test / test_xml.py View on Github external
def test_xml_namespaces(self):
        """Tests the 'namespace' parameter of the record_to_xml() method."""
        # get a test record
        fh = open("test/test.dat", "rb")
        record = next(pymarc.reader.MARCReader(fh))
        # record_to_xml() with quiet set to False should generate errors
        #   and write them to sys.stderr
        xml = pymarc.record_to_xml(record, namespace=False)
        # look for the xmlns in the written xml, should be -1
        self.assertFalse(b'xmlns="http://www.loc.gov/MARC21/slim"' in xml)

        # record_to_xml() with quiet set to True should not generate errors
        xml = pymarc.record_to_xml(record, namespace=True)
        # look for the xmlns in the written xml, should be >= 0
        self.assertTrue(b'xmlns="http://www.loc.gov/MARC21/slim"' in xml)

        fh.close()
github edsu / pymarc / test / test_record.py View on Github external
def test_multiple_isbn(self):
        with open("test/multi_isbn.dat", "rb") as fh:
            reader = MARCReader(fh)
            record = next(reader)
            self.assertEqual(record.isbn(), "0914378287")
github edsu / pymarc / pymarc / reader.py View on Github external
def map_records(f, *files):
    """Applies a given function to each record in a batch.

    You can pass in multiple batches.

    .. code-block:: python

        def print_title(r):
            print(r['245'])
        map_records(print_title, file('marc.dat'))
    """
    for file in files:
        list(map(f, MARCReader(file)))
github edsu / pymarc / pymarc / reader.py View on Github external
def __init__(
        self,
        marc_target,
        to_unicode=True,
        force_utf8=False,
        hide_utf8_warnings=False,
        utf8_handling="strict",
        file_encoding="iso8859-1",
        permissive=False,
    ):
        """The constructor to which you can pass either raw marc or a file-like object.

        Basically the argument you pass in should be raw MARC in transmission format or
        an object that responds to read().
        """
        super(MARCReader, self).__init__()
        self.to_unicode = to_unicode
        self.force_utf8 = force_utf8
        self.hide_utf8_warnings = hide_utf8_warnings
        self.utf8_handling = utf8_handling
        self.file_encoding = file_encoding
        self.permissive = permissive
        if hasattr(marc_target, "read") and callable(marc_target.read):
            self.file_handle = marc_target
        else:
            self.file_handle = BytesIO(marc_target)