Skip to content

comicbox.readinglists.compare

Compare two lists: the same list, or a different one.

Comparison dataclass

How two lists compare.

overlap is the shared issues over all issues. contains is the share of a found in b, then of b found in a. order_agreement is the longest common subsequence of the shared issues over their count, or None when either list is marked unordered, since such a list has no order to agree with.

Source code in comicbox/readinglists/compare.py
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
@dataclass(frozen=True, slots=True)
class Comparison:
    """
    How two lists compare.

    ``overlap`` is the shared issues over all issues. ``contains`` is the
    share of ``a`` found in ``b``, then of ``b`` found in ``a``.
    ``order_agreement`` is the longest common subsequence of the shared
    issues over their count, or None when either list is marked unordered,
    since such a list has no order to agree with.
    """

    verdict: Verdict
    overlap: float
    contains: tuple[float, float]
    order_agreement: float | None

Verdict

Bases: StrEnum

What two lists are to each other, decided in this order.

Source code in comicbox/readinglists/compare.py
48
49
50
51
52
53
54
55
56
57
class Verdict(StrEnum):
    """What two lists are to each other, decided in this order."""

    # Each contains the other and the orders agree. Safe to combine.
    SAME = "same"
    # Each contains the other, in different orders: two takes on one event.
    VARIANT = "variant"
    # One contains the other, as tags do a curated list.
    SUBSET = "subset"
    UNRELATED = "unrelated"

compare(a, b)

Compare two lists by the issues they share and the order they share.

When either list is marked unordered, the verdict rests on what each holds of the other alone.

Source code in comicbox/readinglists/compare.py
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
def compare(a: Mapping[str, Any], b: Mapping[str, Any]) -> Comparison:
    """
    Compare two lists by the issues they share and the order they share.

    When either list is marked unordered, the verdict rests on what each
    holds of the other alone.
    """
    lists = [load_model(reading_list) for reading_list in (a, b)]
    sequences = group_rows(
        [reading_list.get(ENTRIES_KEY) or [] for reading_list in lists]
    ).sequences
    a_groups, b_groups = (set(sequence) for sequence in sequences)
    shared = a_groups & b_groups
    union = a_groups | b_groups
    contains = (_share(shared, a_groups), _share(shared, b_groups))
    order_agreement = (
        _order_agreement(sequences, shared) if all(map(is_ordered, lists)) else None
    )
    return Comparison(
        _verdict(contains, order_agreement),
        len(shared) / len(union) if union else 0.0,
        contains,
        order_agreement,
    )