Source code for aoptk.literature.metadata

from __future__ import annotations
from dataclasses import dataclass
from aoptk.literature.id import DOI
from aoptk.literature.id import ID
from aoptk.literature.id import PMCID
from aoptk.literature.id import PMID


@dataclass
[docs] class Metadata: """Data structure representing a publication. Attributes: id (ID): Identifier by which the publication was found. Can be PMID, PMCID, DOI, or other such as PPR. pmcid (PMCID | None): PubMed Central ID, if available. pmid (PMID | None): PubMed ID, if available. doi (DOI | None): Digital Object Identifier, if available. year (str | None): Year of publication. title (str | None): Title of the publication. authors (list[str] | None): Authors of the publication. """
[docs] id: ID
[docs] pmcid: PMCID | None = None
[docs] pmid: PMID | None = None
[docs] doi: DOI | None = None
[docs] year: int | None = None
[docs] title: str | None = None
[docs] authors: list[str] | None = None
[docs] def __str__(self) -> ID: return self.id
[docs] def __eq__(self, other: object) -> bool: """Compare Metadata by their identifiers.""" attributes_to_check = ("id", "doi", "pmid", "pmcid") if isinstance(other, Metadata): for attr in attributes_to_check: a = getattr(self, attr) b = getattr(other, attr) if a is not None and b is not None and a == b: return True return False if isinstance(other, ID): for attr in attributes_to_check: a = getattr(self, attr) if a is not None and a == other: return True return False
[docs] def __hash__(self) -> int: """Hash based on the identifier used by __eq__ for use in sets/dicts.""" if self.doi: return hash(self.doi) if self.pmid: return hash(self.pmid) if self.pmcid: return hash(self.pmcid) return hash(self.id)