Skip to content

comicbox.readinglists.combine

Combine several lists of one reading order into one list and a report.

Pure: no files, no network. The inputs are in rank order, the first being primary. Guarantees:

  • The same inputs in the same order give the same output.
  • Combining a list with itself returns it unchanged, when every entry has a key.
  • Adding an input never reorders the backbone’s own entries.
  • A caller that wants to keep an existing order passes it first.

An entry with no key, no usable issue id and no series name and issue with a start year, volume or cover year, cannot be matched, so it is never joined with another, not even with itself in a copy of its list: combining such a list with itself gives that entry twice. A join on the series, issue and cover year alone, the weakest key, is reported with the name joins, and never brings together rows whose series years differ. Nor does a join on the series, volume number and issue: Z-Men (1963) and Z-Men (1991) volume 1 stay apart.

CombineResult dataclass

The combined list and what combining found.

provenance is parallel to the list’s entries: the rows each came from, in rank order. only_in is indexed by input: the entries no other input has. name_joins are the entries some row joined on a series name alone, with a start year, volume or cover year: the joins most likely to be wrong. shared_ids are the ids left on more than one entry that no input repeats.

Source code in comicbox/readinglists/combine/__init__.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
@dataclass(frozen=True, slots=True)
class CombineResult:
    """
    The combined list and what combining found.

    ``provenance`` is parallel to the list's entries: the rows each came
    from, in rank order. ``only_in`` is indexed by input: the entries no
    other input has. ``name_joins`` are the entries some row joined on a
    series name alone, with a start year, volume or cover year: the joins
    most likely to be wrong. ``shared_ids`` are the ids left on more than
    one entry that no input repeats.
    """

    reading_list: dict[str, Any]
    provenance: tuple[tuple[EntryOrigin, ...], ...]
    only_in: tuple[tuple[int, ...], ...]
    order_conflicts: tuple[OrderConflict, ...]
    id_conflicts: tuple[IdConflict, ...]
    name_joins: tuple[int, ...]
    shared_ids: tuple[SharedId, ...] = ()

EntryOrigin dataclass

A row of an input: input_index ranks the input, row its entry.

Source code in comicbox/readinglists/combine/__init__.py
46
47
48
49
50
51
@dataclass(frozen=True, slots=True)
class EntryOrigin:
    """A row of an input: ``input_index`` ranks the input, ``row`` its entry."""

    input_index: int
    row: int

IdConflict dataclass

Two values for one source on one issue: one of the lists is wrong.

Combined entry entry keeps kept for source, a database; the row at input_index and row, joined to it, had dropped. A barcode that differs is a printing or an edition, so it is never one.

Source code in comicbox/readinglists/combine/__init__.py
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
@dataclass(frozen=True, slots=True)
class IdConflict:
    """
    Two values for one source on one issue: one of the lists is wrong.

    Combined entry ``entry`` keeps ``kept`` for ``source``, a database; the
    row at ``input_index`` and ``row``, joined to it, had ``dropped``. A
    barcode that differs is a printing or an edition, so it is never one.
    """

    entry: int
    source: str
    kept: str
    dropped: str
    input_index: int
    row: int

OrderConflict dataclass

An input puts combined entry entry somewhere the backbone does not.

Source code in comicbox/readinglists/combine/__init__.py
54
55
56
57
58
59
60
@dataclass(frozen=True, slots=True)
class OrderConflict:
    """An input puts combined entry ``entry`` somewhere the backbone does not."""

    entry: int
    input_index: int
    row: int

SharedId dataclass

A database id the combined list holds on more than one entry.

No input repeats it, so it names one issue, but the rows that carry it could not all be joined: one input gave the issue twice, with a different id each time, and a row of one input joins at most one row of another. That is bad data worth showing. entries are the positions.

Source code in comicbox/readinglists/combine/__init__.py
81
82
83
84
85
86
87
88
89
90
91
92
93
94
@dataclass(frozen=True, slots=True)
class SharedId:
    """
    A database id the combined list holds on more than one entry.

    No input repeats it, so it names one issue, but the rows that carry it
    could not all be joined: one input gave the issue twice, with a
    different id each time, and a row of one input joins at most one row of
    another. That is bad data worth showing. ``entries`` are the positions.
    """

    source: str
    key: str
    entries: tuple[int, ...]

combine(reading_lists)

Combine lists of one reading order, given in rank order.

Rows are the same issue when they share an entry_keys key. The first ordered input is the backbone of the order; an entry’s fields are the union of ids and otherwise the first value in rank order.

Source code in comicbox/readinglists/combine/__init__.py
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
def combine(reading_lists: Sequence[Mapping[str, Any]]) -> CombineResult:
    """
    Combine lists of one reading order, given in rank order.

    Rows are the same issue when they share an ``entry_keys`` key. The
    first ordered input is the backbone of the order; an entry's fields are
    the union of ids and otherwise the first value in rank order.
    """
    if not reading_lists:
        reason = "Nothing to combine."
        raise ReadingListError(reason)
    lists = [load_model(reading_list) for reading_list in reading_lists]
    inputs = [reading_list.get(ENTRIES_KEY) or [] for reading_list in lists]
    grouping = group_rows(inputs)
    entries = _merge_entries(inputs, grouping)
    flags = [is_ordered(reading_list) for reading_list in lists]
    order, order_conflicts = _order(grouping, entries, flags)
    backbone = flags.index(True) if True in flags else None
    # Built only from model-loaded values, so it needs no second load.
    reading_list = merge_header(lists, backbone)
    if order:
        reading_list[ENTRIES_KEY] = [entries[group] for group in order]
    result = _result(reading_list, grouping, order, order_conflicts, len(lists))
    shared = _shared_ids(inputs, reading_list.get(ENTRIES_KEY) or [])
    return replace(result, shared_ids=shared)