本文整理汇总了Python中pymarc.Record类的典型用法代码示例。如果您正苦于以下问题:Python Record类的具体用法?Python Record怎么用?Python Record使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Record类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_add_title
def test_add_title(self):
edition = self._edition()
edition.title = "The Good Soldier"
edition.sort_title = "Good Soldier, The"
edition.subtitle = "A Tale of Passion"
record = Record()
Annotator.add_title(record, edition)
[field] = record.get_fields("245")
self._check_field(
record, "245", {
"a": edition.title,
"b": edition.subtitle,
"c": edition.author,
}, ["0", "4"])
# If there's no subtitle or no author, those subfields are left out.
edition.subtitle = None
edition.author = None
record = Record()
Annotator.add_title(record, edition)
[field] = record.get_fields("245")
self._check_field(
record, "245", {
"a": edition.title,
}, ["0", "4"])
eq_([], field.get_subfields("b"))
eq_([], field.get_subfields("c"))
示例2: test_add_simplified_genres
def test_add_simplified_genres(self):
work = self._work(with_license_pool=True)
fantasy, ignore = Genre.lookup(self._db, "Fantasy", autocreate=True)
romance, ignore = Genre.lookup(self._db, "Romance", autocreate=True)
work.genres = [fantasy, romance]
record = Record()
Annotator.add_simplified_genres(record, work)
fields = record.get_fields("650")
[fantasy_field, romance_field] = sorted(fields, key=lambda x: x.get_subfields("a")[0])
eq_(["0", "7"], fantasy_field.indicators)
eq_("Fantasy", fantasy_field.get_subfields("a")[0])
eq_("Library Simplified", fantasy_field.get_subfields("2")[0])
eq_(["0", "7"], romance_field.indicators)
eq_("Romance", romance_field.get_subfields("a")[0])
eq_("Library Simplified", romance_field.get_subfields("2")[0])
# It also works with a materialized work.
self.add_to_materialized_view([work])
# The work is in the materialized view twice since it has two genres,
# but we can use either one.
[mw, ignore] = self._db.query(MaterializedWorkWithGenre).all()
record = Record()
Annotator.add_simplified_genres(record, mw)
fields = record.get_fields("650")
[fantasy_field, romance_field] = sorted(fields, key=lambda x: x.get_subfields("a")[0])
eq_(["0", "7"], fantasy_field.indicators)
eq_("Fantasy", fantasy_field.get_subfields("a")[0])
eq_("Library Simplified", fantasy_field.get_subfields("2")[0])
eq_(["0", "7"], romance_field.indicators)
eq_("Romance", romance_field.get_subfields("a")[0])
eq_("Library Simplified", romance_field.get_subfields("2")[0])
示例3: test_add_series
def test_add_series(self):
edition = self._edition()
edition.series = self._str
edition.series_position = 5
record = Record()
Annotator.add_series(record, edition)
self._check_field(record, "490", {
"a": edition.series,
"v": str(edition.series_position),
}, ["0", " "])
# If there's no series position, the same field is used without
# the v subfield.
edition.series_position = None
record = Record()
Annotator.add_series(record, edition)
self._check_field(record, "490", {
"a": edition.series,
}, ["0", " "])
[field] = record.get_fields("490")
eq_([], field.get_subfields("v"))
# If there's no series, the field is left out.
edition.series = None
record = Record()
Annotator.add_series(record, edition)
eq_([], record.get_fields("490"))
示例4: test_add_contributors
def test_add_contributors(self):
author = "a"
author2 = "b"
translator = "c"
# Edition with one author gets a 100 field and no 700 fields.
edition = self._edition(authors=[author])
edition.sort_author = "sorted"
record = Record()
Annotator.add_contributors(record, edition)
eq_([], record.get_fields("700"))
self._check_field(record, "100", {"a": edition.sort_author}, ["1", " "])
# Edition with two authors and a translator gets three 700 fields and no 100 fields.
edition = self._edition(authors=[author, author2])
edition.add_contributor(translator, Contributor.TRANSLATOR_ROLE)
record = Record()
Annotator.add_contributors(record, edition)
eq_([], record.get_fields("100"))
fields = record.get_fields("700")
for field in fields:
eq_(["1", " "], field.indicators)
[author_field, author2_field, translator_field] = sorted(fields, key=lambda x: x.get_subfields("a")[0])
eq_(author, author_field.get_subfields("a")[0])
eq_(Contributor.PRIMARY_AUTHOR_ROLE, author_field.get_subfields("e")[0])
eq_(author2, author2_field.get_subfields("a")[0])
eq_(Contributor.AUTHOR_ROLE, author2_field.get_subfields("e")[0])
eq_(translator, translator_field.get_subfields("a")[0])
eq_(Contributor.TRANSLATOR_ROLE, translator_field.get_subfields("e")[0])
示例5: test_add_physical_description
def test_add_physical_description(self):
book = self._edition()
book.medium = Edition.BOOK_MEDIUM
audio = self._edition()
audio.medium = Edition.AUDIO_MEDIUM
record = Record()
Annotator.add_physical_description(record, book)
self._check_field(record, "300", {"a": "1 online resource"})
self._check_field(record, "336", {
"a": "text",
"b": "txt",
"2": "rdacontent",
})
self._check_field(record, "337", {
"a": "computer",
"b": "c",
"2": "rdamedia",
})
self._check_field(record, "338", {
"a": "online resource",
"b": "cr",
"2": "rdacarrier",
})
self._check_field(record, "347", {
"a": "text file",
"2": "rda",
})
self._check_field(record, "380", {
"a": "eBook",
"2": "tlcgt",
})
record = Record()
Annotator.add_physical_description(record, audio)
self._check_field(record, "300", {
"a": "1 sound file",
"b": "digital",
})
self._check_field(record, "336", {
"a": "spoken word",
"b": "spw",
"2": "rdacontent",
})
self._check_field(record, "337", {
"a": "computer",
"b": "c",
"2": "rdamedia",
})
self._check_field(record, "338", {
"a": "online resource",
"b": "cr",
"2": "rdacarrier",
})
self._check_field(record, "347", {
"a": "audio file",
"2": "rda",
})
eq_([], record.get_fields("380"))
示例6: test_unicode
def test_unicode(self):
record = Record()
record.add_field(Field(245, ['1', '0'], ['a', unichr(0x1234)]))
writer = MARCWriter(open('test/foo', 'w'))
writer.write(record)
writer.close()
reader = MARCReader(open('test/foo'))
record = reader.next()
self.assertEqual(record['245']['a'], unichr(0x1234))
示例7: create_record
def create_record(cls, work, annotator, force_create=False, integration=None):
"""Build a complete MARC record for a given work."""
if callable(annotator):
annotator = annotator()
if isinstance(work, BaseMaterializedWork):
pool = work.license_pool
else:
pool = work.active_license_pool()
if not pool:
return None
edition = pool.presentation_edition
identifier = pool.identifier
_db = Session.object_session(work)
record = None
existing_record = getattr(work, annotator.marc_cache_field)
if existing_record and not force_create:
record = Record(data=existing_record.encode('utf-8'), force_utf8=True)
if not record:
record = Record(leader=annotator.leader(work), force_utf8=True)
annotator.add_control_fields(record, identifier, pool, edition)
annotator.add_isbn(record, identifier)
# TODO: The 240 and 130 fields are for translated works, so they can be grouped even
# though they have different titles. We do not group editions of the same work in
# different languages, so we can't use those yet.
annotator.add_title(record, edition)
annotator.add_contributors(record, edition)
annotator.add_publisher(record, edition)
annotator.add_physical_description(record, edition)
annotator.add_audience(record, work)
annotator.add_series(record, edition)
annotator.add_system_details(record)
annotator.add_ebooks_subject(record)
data = record.as_marc()
if isinstance(work, BaseMaterializedWork):
setattr(pool.work, annotator.marc_cache_field, data)
else:
setattr(work, annotator.marc_cache_field, data)
# Add additional fields that should not be cached.
annotator.annotate_work_record(work, pool, edition, identifier, record, integration)
return record
示例8: sort_6_subs
def sort_6_subs(rec):
msg = ''
new_rec = Record(to_unicode=True, force_utf8=True)
new_rec_fields = []
rec_fields = rec.get_fields()
for field in rec_fields:
script_field = False
if not field.is_control_field() and (len(field.get_subfields('6')) > 0): # the field contains a subfield $6
script_field = True
ind1 = field.indicator1
ind2 = field.indicator2
tag = field.tag
first_sub = True # variable to keep track of whether you're on the first subfield in the field
needs_sorted = True # variable to keep track of whether the field needs sorted or if the $6 is already correctly the first subfield
field_subs = [] # list variable to capture all the subfields in the field *except* for the subfield $6
for subfield in field:
# check if $6 is the first subfield - if so, the field is OK and does *not* need to be sorted
if needs_sorted and first_sub and subfield[0] == '6':
needs_sorted = False
elif needs_sorted:
if first_sub:
# this is the first subfield and is *not* $6, so the field needs sorted - creates one instance of a new_field object only when the 1st subfield is encountered
new_field = Field(tag=tag, indicators=[ind1,ind2], subfields=[])
# when subfield $6 is finally encountered in the field (not the 1st), add it to the new_field object now so it becomes the first subfield
# Note: subfield[0] is the subfield code and subfield[1] is the subfield content for this subfield
if subfield[0]=='6':
new_field.add_subfield(subfield[0],subfield[1])
# if the subfield is *not* $6, add it to the list of subfields to be added later to the new_field
else:
field_subs.append([subfield[0],subfield[1]])
first_sub = False
if needs_sorted:
# then the $6 was *not* the 1st subfield and we need to now add the remaining subfields to the new_field object
for sub in field_subs:
# add the remaining subfields to the new_field object
new_field.add_subfield(sub[0],sub[1])
new_rec_fields.append(new_field) # add the new field to the record
if not script_field or not needs_sorted:
new_rec_fields.append(field)
for new_f in new_rec_fields:
new_rec.add_field(new_f)
return new_rec
示例9: test_writing_unicode
def test_writing_unicode(self):
record = Record()
record.add_field(Field(245, ['1', '0'], ['a', unichr(0x1234)]))
record.leader = ' a '
writer = MARCWriter(open('test/foo', 'wb'))
writer.write(record)
writer.close()
reader = MARCReader(open('test/foo', 'rb'), to_unicode=True)
record = next(reader)
self.assertEqual(record['245']['a'], unichr(0x1234))
reader.close()
os.remove('test/foo')
示例10: writeMetadataToMarc
def writeMetadataToMarc(data, MARCMapping, saveLocation):
record = Record()
for key in data:
if key in MARCMapping:
if(key == u'UUID'):
field = Field(
tag = MARCMapping[key],
data = data[key])
else:
field = Field(
tag = MARCMapping[key][:3],
subfields = [MARCMapping[key][3], data[key]],
indicators=['0', '0'])
record.add_field(field)
writeRecordToFile(record, filename)
示例11: faulty015
def faulty015(record: Record) -> bool:
found = False
for f in record.get_fields("015"):
if "a" in f:
if len(f["a"].split(' ')) > 1:
found = True
return found
示例12: empty020a
def empty020a(record: Record) -> bool:
sf020a = []
if "020" in record:
fields = record.get_fields("020")
for f in fields:
if "a" in f:
sf020a.append(f.get_subfields("a").pop())
return sf020a == [""]
示例13: __next__
def __next__(self):
jobj = next(self.iter)
rec = Record()
rec.leader = jobj['leader']
for field in jobj['fields']:
k,v = list(field.items())[0]
if 'subfields' in v and hasattr(v,'update'):
# flatten m-i-j dict to list in pymarc
subfields = []
for sub in v['subfields']:
for code,value in sub.items():
subfields.extend((code,value))
fld = Field(tag=k,subfields=subfields,indicators=[v['ind1'], v['ind2']])
else:
fld = Field(tag=k,data=v)
rec.add_field(fld)
return rec
示例14: test_add_formats
def test_add_formats(self):
edition, pool = self._edition(with_license_pool=True)
epub_no_drm, ignore = DeliveryMechanism.lookup(
self._db, Representation.EPUB_MEDIA_TYPE, DeliveryMechanism.NO_DRM)
pool.delivery_mechanisms[0].delivery_mechanism = epub_no_drm
LicensePoolDeliveryMechanism.set(
pool.data_source, pool.identifier, Representation.PDF_MEDIA_TYPE,
DeliveryMechanism.ADOBE_DRM, RightsStatus.IN_COPYRIGHT)
record = Record()
Annotator.add_formats(record, pool)
fields = record.get_fields("538")
eq_(2, len(fields))
[pdf, epub] = sorted(fields, key=lambda x: x.get_subfields("a")[0])
eq_("Adobe PDF eBook", pdf.get_subfields("a")[0])
eq_([" ", " "], pdf.indicators)
eq_("EPUB eBook", epub.get_subfields("a")[0])
eq_([" ", " "], epub.indicators)
示例15: periodsMissing
def periodsMissing(record: Record) -> bool:
fields = record.get_fields("100", "110", "700", "710")
for f in fields:
if "e" in f:
functions = f.get_subfields("e")
for func in functions:
if func[-1].isalpha():
return True
return False