Skip to content

PPF-1707: Add a __repr__ method to the Widget base class #1711

@annaswims

Description

@annaswims

Version

PyPDFForm=4.7.10

Issue Description

Add a __repr__ method to the Widget base class so that individual widgets and the pdf.widgets dictionary render meaningfully in interactive sessions and debug output.

Currently, inspecting pdf.widgets or a single widget produces an unhelpful default object repr. After this change, each widget displays its class name, name, value, and all non-None public attributes, omitting internal bookkeeping fields (SET_ATTR_TRIGGER_HOOK_MAP, attr_set_tracker, hooks_to_trigger).

Code Snippet

from pprint import pprint
from PyPDFForm import PdfWrapper

pdf = PdfWrapper("docs/pdfs/sample_template.pdf")

# inspect the full widgets dict
pprint(pdf.widgets)

# inspect a single widget
print(repr(pdf.widgets["test"]))

Example output:
before changes

{'check': <PyPDFForm.lib.middleware.checkbox.Checkbox object at 0x10b6086e0>,
 'check_2': <PyPDFForm.lib.middleware.checkbox.Checkbox object at 0x10b5fcb90>,
 'check_3': <PyPDFForm.lib.middleware.checkbox.Checkbox object at 0x10b5fce10>,
 'test': <PyPDFForm.lib.middleware.text.Text object at 0x10b4af8c0>,
 'test_2': <PyPDFForm.lib.middleware.text.Text object at 0x10b5fca50>,
 'test_3': <PyPDFForm.lib.middleware.text.Text object at 0x10b5fccd0>}

desired output:

  {'check': Checkbox(name='check', value=None, readonly=False, required=False, hidden=False, page_number=1, x=358.874, y=664.717, width=18.47999999999996, height=18.480000000000018),
 'check_2': Checkbox(name='check_2', value=None, readonly=False, required=False, hidden=False, page_number=2, x=349.637, y=673.954, width=18.478999999999985, height=18.480000000000018),
 'check_3': Checkbox(name='check_3', value=None, readonly=False, required=False, hidden=False, page_number=3, x=349.305, y=667.344, width=18.480000000000018, height=18.479999999999905),
 'test': Text(name='test', value=None, readonly=False, required=False, hidden=False, page_number=1, x=73.3365, y=662.692, width=232.4235, height=21.067999999999984, comb=False, multiline=False),
 'test_2': Text(name='test_2', value=None, readonly=False, required=False, hidden=False, page_number=2, x=71.4095, y=671.626, width=232.42350000000005, height=21.067999999999984, comb=False, multiline=False),
 'test_3': Text(name='test_3', value=None, readonly=False, required=False, hidden=False, page_number=3, x=70.5919, y=665.349, width=232.42309999999998, height=21.067999999999984, comb=False, multiline=False)}

PDF Form Template

sample_template.pdf

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions