Skip to content

comicbox.readinglists.identity ¶

Identity: when two reading-list entries are the same issue.

entry_keys is the one definition. combine, compare, the CLI matcher and codex’s database matcher all use it, so they agree on what counts as the same issue.

Discriminators dataclass ¶

What a row tells its volume by: up to one discriminator of each kind.

year is the year-like one: the start year or, failing that, a volume number of 1900 or more, as a CBL’s Volume="2006" is. volume is a volume number under 1900. Z-Men (1991) volume 2 has both.

Source code in comicbox/readinglists/identity.py
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
@dataclass(frozen=True, slots=True)
class Discriminators:
    """
    What a row tells its volume by: up to one discriminator of each kind.

    ``year`` is the year-like one: the start year or, failing that, a volume
    number of 1900 or more, as a CBL's ``Volume="2006"`` is. ``volume`` is a
    volume number under 1900. Z-Men (1991) volume 2 has both.
    """

    year: int | None = None
    volume: int | None = None

    def shares_a_kind(self, other: "Discriminators") -> bool:
        """Tell whether two rows have a discriminator of one kind each."""
        return (self.year is not None and other.year is not None) or (
            self.volume is not None and other.volume is not None
        )

    def conflicts(self, other: "Discriminators") -> bool:
        """
        Tell whether two rows are of two volumes.

        They conflict when they have a discriminator of the same kind and
        those differ, in either kind: the 2011 and 2016 volumes, or volumes 1
        and 2. A start year and volume 2 cannot be compared.
        """
        return _differ(self.year, other.year) or _differ(self.volume, other.volume)

conflicts(other) ¶

Tell whether two rows are of two volumes.

They conflict when they have a discriminator of the same kind and those differ, in either kind: the 2011 and 2016 volumes, or volumes 1 and 2. A start year and volume 2 cannot be compared.

Source code in comicbox/readinglists/identity.py
171
172
173
174
175
176
177
178
179
def conflicts(self, other: "Discriminators") -> bool:
    """
    Tell whether two rows are of two volumes.

    They conflict when they have a discriminator of the same kind and
    those differ, in either kind: the 2011 and 2016 volumes, or volumes 1
    and 2. A start year and volume 2 cannot be compared.
    """
    return _differ(self.year, other.year) or _differ(self.volume, other.volume)

shares_a_kind(other) ¶

Tell whether two rows have a discriminator of one kind each.

Source code in comicbox/readinglists/identity.py
165
166
167
168
169
def shares_a_kind(self, other: "Discriminators") -> bool:
    """Tell whether two rows have a discriminator of one kind each."""
    return (self.year is not None and other.year is not None) or (
        self.volume is not None and other.volume is not None
    )

ParsedIssue dataclass ¶

An issue name, normalized, and the number and suffix read out of it.

Source code in comicbox/readinglists/identity.py
61
62
63
64
65
66
67
@dataclass(frozen=True, slots=True)
class ParsedIssue:
    """An issue name, normalized, and the number and suffix read out of it."""

    name: str
    number: Decimal | None
    suffix: str

any_conflict(values) ¶

Tell whether any two of some rows’ discriminators conflict.

Source code in comicbox/readinglists/identity.py
224
225
226
227
228
229
def any_conflict(values: Iterable[Discriminators]) -> bool:
    """Tell whether any two of some rows' discriminators conflict."""
    values = tuple(values)
    years = {value.year for value in values if value.year is not None}
    volumes = {value.volume for value in values if value.volume is not None}
    return len(years) > 1 or len(volumes) > 1

discriminators(entry) ¶

Get what an entry tells its volume by.

Source code in comicbox/readinglists/identity.py
186
187
188
189
190
191
192
193
def discriminators(entry: Mapping[str, Any]) -> Discriminators:
    """Get what an entry tells its volume by."""
    series = entry.get(SERIES_KEY) or {}
    start_year = _whole_number(series.get(SERIES_START_YEAR_KEY))
    number = _whole_number((entry.get(VOLUME_KEY) or {}).get(NUMBER_KEY))
    if number is not None and number < YEAR_LIKE_VOLUME:
        return Discriminators(start_year, number)
    return Discriminators(number if start_year is None else start_year)

entry_date(entry) ¶

Get the cover date, or the first day the year and month allow.

Source code in comicbox/readinglists/identity.py
351
352
353
354
355
356
357
358
359
360
361
362
363
def entry_date(entry: Mapping[str, Any]) -> date | None:
    """Get the cover date, or the first day the year and month allow."""
    entry_date_ = entry.get(DATE_KEY) or {}
    if cover_date := entry_date_.get(COVER_DATE_KEY):
        return cover_date
    if not (year := entry_date_.get(YEAR_KEY)):
        return None
    try:
        return date(
            year, entry_date_.get(MONTH_KEY) or 1, entry_date_.get(DAY_KEY) or 1
        )
    except (TypeError, ValueError):
        return None

entry_keys(entry) ¶

List the keys that identify an entry’s issue, strongest first.

("id", source, key) for each issue id, best ranked source first, then ("name", series, "year", year, number, suffix) by the start year or a year-like volume, then ("name", series, "volume", volume, number, suffix) by a volume number under 1900, then the weakest, ("name-year", series, cover year, number, suffix). Two entries are the same issue when they share a key and their discriminators do not conflict (Discriminators.conflicts): Z-Men (1963) and Z-Men (1991) may both be volume 1. An entry with none, no id and no series and issue with a start year, volume or cover year, matches nothing.

Source code in comicbox/readinglists/identity.py
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
def entry_keys(entry: Mapping[str, Any]) -> tuple[EntryKey, ...]:
    """
    List the keys that identify an entry's issue, strongest first.

    ``("id", source, key)`` for each issue id, best ranked source first,
    then ``("name", series, "year", year, number, suffix)`` by the start
    year or a year-like volume, then ``("name", series, "volume", volume,
    number, suffix)`` by a volume number under 1900, then the weakest,
    ``("name-year", series, cover year, number, suffix)``. Two entries are
    the same issue when they share a key and their discriminators do not
    conflict (``Discriminators.conflicts``): Z-Men (1963) and Z-Men (1991)
    may both be volume 1. An entry with none, no id and no series and issue
    with a start year, volume or cover year, matches nothing.
    """
    keys = [*_id_keys(entry), *_name_keys(entry)]
    if (name_year_key := _name_year_key(entry)) is not None:
        keys.append(name_year_key)
    return tuple(keys)

entry_sort_key(entry) ¶

Sort by date, series and issue; anything missing sorts last.

Source code in comicbox/readinglists/identity.py
366
367
368
369
370
371
372
373
374
375
376
377
378
def entry_sort_key(entry: Mapping[str, Any]) -> tuple[Any, ...]:
    """Sort by date, series and issue; anything missing sorts last."""
    when = entry_date(entry)
    series = entry.get(SERIES_KEY) or {}
    number, suffix = _issue_parts(entry) or (None, "")
    return (
        when is None,
        when or date.min,
        normalize_series(str(series.get(NAME_KEY) or "")),
        number is None,
        number or Decimal(0),
        suffix,
    )

name_variants(entry) ¶

List the series names to try for an entry, normalized, in order.

The name as given; the name without a trailing year or volume in parentheses (Bladehawk (2016), X (Vol. 2)), when it has one; the name without a leading article; and the name without a reprint suffix such as TPB or Omnibus. A year or volume is never added: Ivory Knight is not the series Ivory Knight 2020. For matching a list against comics only: entry_keys stays strict, so combine never joins on a variant.

Source code in comicbox/readinglists/identity.py
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
def name_variants(entry: Mapping[str, Any]) -> tuple[str, ...]:
    """
    List the series names to try for an entry, normalized, in order.

    The name as given; the name without a trailing year or volume in
    parentheses (``Bladehawk (2016)``, ``X (Vol. 2)``), when it has one; the
    name without a leading article; and the name without a reprint suffix
    such as TPB or Omnibus. A year or volume is never added: ``Ivory Knight``
    is not the series ``Ivory Knight 2020``. For matching a list against
    comics only: ``entry_keys`` stays strict, so ``combine`` never joins on
    a variant.
    """
    raw = str((entry.get(SERIES_KEY) or {}).get(NAME_KEY) or "").strip()
    if not raw:
        return ()
    normalized = [normalize_series(name) for name in _spelled_variants(raw)]
    normalized += [_without_reprint_suffix(name) for name in normalized]
    return tuple(dict.fromkeys(name for name in normalized if name))

parse_issue(text) ¶

Normalize an issue name and split it into its number and suffix.

The comicbox rules: #004AU is 4AU, number 4 and suffix AU; ½ is .5. 3 and 3.0 have equal numbers. -1 is a real issue number: IssueField.split_issue reads its sign.

Source code in comicbox/readinglists/identity.py
79
80
81
82
83
84
85
86
87
88
89
def parse_issue(text: Any) -> ParsedIssue:
    """
    Normalize an issue name and split it into its number and suffix.

    The comicbox rules: ``#004AU`` is ``4AU``, number 4 and suffix ``AU``;
    ``½`` is ``.5``. ``3`` and ``3.0`` have equal numbers. ``-1`` is a real
    issue number: ``IssueField.split_issue`` reads its sign.
    """
    name = IssueField.parse_issue(str(text).strip()) if text is not None else ""
    number_text, suffix = IssueField.split_issue(name)
    return ParsedIssue(name, _to_decimal(number_text), suffix.strip())

variant_keys(entry) ¶

Build the name keys of an entry’s variants, its own name key first.

Each keeps one of the entry’s discriminators and its issue, so an entry with no name key has no variants either: it still matches by id alone. For each name, the year key comes before the volume key.

Source code in comicbox/readinglists/identity.py
316
317
318
319
320
321
322
323
324
325
326
327
328
def variant_keys(entry: Mapping[str, Any]) -> tuple[EntryKey, ...]:
    """
    Build the name keys of an entry's variants, its own name key first.

    Each keeps one of the entry's discriminators and its issue, so an entry
    with no name key has no variants either: it still matches by id alone.
    For each name, the year key comes before the volume key.
    """
    return tuple(
        (NAME_KEY_KIND, name, *rest)
        for name in name_variants(entry)
        for _, _, *rest in _name_keys(entry)
    )