field.format_field(): should it return tag, indicators, and subfield indicators as the docstring says?

56 views
Skip to first unread message

Ben W.

unread,
Feb 6, 2024, 10:05:39 AM2/6/24
to pymarc Discussion
I recently noticed that the docstring for the format_field() method in pymarc.Field says it will return "the field as a string w/ tag, indicators, and subfield indicators", but the code clearly doesn't return anything other than the Subfield.values for the input field.

Is this the desired behavior, or does the docstring indicate the desired behavior? I've updated format_field to show the missing items with examples shown below:

# Two test fields
record = Record()
f = Field(
    tag="245",
    indicators=["0", "1"],
    subfields=[
        Subfield(code="a", value="The pragmatic programmer : "),
        Subfield(code="b", value="from journeyman to master /"),
        Subfield(code="c", value="Andrew Hunt, David Thomas."),
    ],
)
fs = Field(
    tag="650",
    indicators=["0", "0"],
    subfields=[
        Subfield(code="a", value="Fentanyl"),
        Subfield(code="x", value="Overdose"),
        Subfield(code="z", value="United States"),
        Subfield(code="x", value="Prevention"),
    ],
)

Title field @5.1.1:
The pragmatic programmer :  from journeyman to master / Andrew Hunt, David Thomas.

Updated:
245  01$a The pragmatic programmer : $b from journeyman to master /$c Andrew Hunt, David Thomas.

Subject field @5.1.1:
Fentanyl -- Overdose -- United States -- Prevention

Updated:
650  00$a Fentanyl -- $x Overdose -- $z United States -- $x Prevention

What's the desired behavior?

Regards,

Ben W.

Ben W.

unread,
Feb 7, 2024, 10:18:10 AM2/7/24
to pymarc Discussion
Meant to include the updated version of format_field that I used for the examples: 

    def updated_format_field(self) -> str:
        """Returns the field as a string w/ tag, indicators, and subfield indicators.

        Like :func:`Field.value() <pymarc.field.Field.value>`, but prettier
        (adds spaces, formats subject headings).
        """
        if self.is_control_field():
            return self.data

        field_data: str = ""

        field_data += f"{self.tag}  {self.indicator1}{self.indicator2}"

        for subfield in self.subfields:
            if subfield.code == "6":
                continue

            if not self.is_subject_field():
                field_data += f"${subfield.code} {subfield.value}"
            else:
                if subfield.code not in ("v", "x", "y", "z"):
                    field_data += f"${subfield.code} {subfield.value}"
                else:
                    field_data += f" -- ${subfield.code} {subfield.value}"
        return field_data.strip()

Ed Summers

unread,
Feb 12, 2024, 10:22:14 AM2/12/24
to pym...@googlegroups.com
Sorry for the delay in getting to this Ben. I’ve updated the documentation [1] to reflect what the method actually does, rather than change the implementation, which would break backwards compatibility. If it’s useful to have format_field() optionally return additional information that would be good to have as a merge request on Gitlab.

Thanks for noticing the discrepancy!

//Ed

[1] https://pymarc.readthedocs.io/en/latest/#pymarc.field.Field.format_field

B W

unread,
Feb 14, 2024, 10:05:55 AM2/14/24
to pym...@googlegroups.com
Thanks for the update, Ed, happy to help!

B

--
You received this message because you are subscribed to the Google Groups "pymarc Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pymarc+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pymarc/F7154723-52EB-4049-A579-80180DC1DD05%40pobox.com.
Reply all
Reply to author
Forward
0 new messages