Skip to content

comicbox.readinglists.losses ΒΆ

Name what a format changes: the model paths a round trip does not keep.

lost_fields writes a list, reads it back and compares, so its answer is exact by construction: every path on which the list read back differs from the list written is named, and nothing else. A flag at its default is the same as a flag left out.

describe_losses(before, after, skipped) ΒΆ

Name the paths on which after differs from before.

Header keys come first, then entry paths such as entries.date.cover_date for any entry, then entries #2, #5 for the entries skipped names, which were not written at all.

Source code in comicbox/readinglists/losses.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
def describe_losses(
    before: Mapping[str, Any], after: Mapping[str, Any], skipped: Sequence[int]
) -> tuple[str, ...]:
    """
    Name the paths on which ``after`` differs from ``before``.

    Header keys come first, then entry paths such as
    ``entries.date.cover_date`` for any entry, then ``entries #2, #5`` for
    the entries ``skipped`` names, which were not written at all.
    """
    lost = sorted(_different(_header(before), _header(after)))
    kept = [
        entry
        for position, entry in enumerate(before.get(ENTRIES_KEY) or ())
        if position not in skipped
    ]
    paths: set[str] = set()
    for entry, entry_back in zip_longest(
        kept, after.get(ENTRIES_KEY) or (), fillvalue={}
    ):
        paths |= _different(
            _leaves(entry, ENTRIES_KEY), _leaves(entry_back, ENTRIES_KEY)
        )
    lost.extend(sorted(paths))
    if skipped:
        lost.append(skipped_entries(skipped))
    return tuple(lost)

skipped_entries(positions) ΒΆ

Name entries by their position from 0, as a person counts them.

Source code in comicbox/readinglists/losses.py
26
27
28
def skipped_entries(positions: Iterable[int]) -> str:
    """Name entries by their position from 0, as a person counts them."""
    return f"{ENTRIES_KEY} " + ", ".join(f"#{position + 1}" for position in positions)