Skip to content

Source Provenance

SourceProvenance

Where a value came from in the source data, for use in the provenance parameter of the add_... methods.

row is 1-based and counts the header row, matching what a researcher sees in the spreadsheet program.

Invalid input never raises an error:

  • An empty source_file becomes an empty string and emits a warning.
  • An empty sheet, row or cell (for example None, pd.NA or "") becomes None.
  • A sheet or cell that is not a string is converted to a string.
  • A row that is not an integer (for example "abc" or 5.5) becomes None and emits a warning.

Examples:

provenance = xmllib.SourceProvenance(
    source_file="data.xlsx",
    sheet="Sheet1",
    row=5,
    cell="C",
)
Source code in dsp/dsp-tools/src/dsp_tools/xmllib/models/provenance.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
@dataclass(frozen=True)
class SourceProvenance:
    """
    Where a value came from in the source data, for use in the `provenance` parameter of the `add_...` methods.

    `row` is 1-based and counts the header row, matching what a researcher sees in the spreadsheet program.

    Invalid input never raises an error:

    - An empty `source_file` becomes an empty string and emits a warning.
    - An empty `sheet`, `row` or `cell` (for example `None`, `pd.NA` or `""`) becomes `None`.
    - A `sheet` or `cell` that is not a string is converted to a string.
    - A `row` that is not an integer (for example `"abc"` or `5.5`) becomes `None` and emits a warning.

    Examples:
        ```python
        provenance = xmllib.SourceProvenance(
            source_file="data.xlsx",
            sheet="Sheet1",
            row=5,
            cell="C",
        )
        ```
    """

    source_file: str
    sheet: str | None = None
    row: int | None = None
    cell: str | None = None

    def __post_init__(self) -> None:
        # The dataclass is frozen, so the normalised values are set with object.__setattr__.
        object.__setattr__(self, "source_file", _normalise_source_file(self.source_file))
        object.__setattr__(self, "sheet", _normalise_optional_str(self.sheet))
        object.__setattr__(self, "row", _normalise_row(self.row))
        object.__setattr__(self, "cell", _normalise_optional_str(self.cell))