SemanticallyStandardizable
- class SemanticallyStandardizable[source]
Bases:
ABCAn object that can be standardized.
In the following example, a simple object is constructed:
from typing_extensions import Self from curies import Converter, Reference, SemanticallyStandardizable class ReferenceHolder(SemanticallyStandardizable): def __init__(self, reference): self.reference = reference def standardize(self, converter: Converter) -> Self: return ReferenceHolder(converter.standardize_reference(self.reference, strict=True))
It’s good form to make these operations return new objects, but there’s no reason you couldn’t update the object in place like in :
from typing_extensions import Self from curies import Converter, Reference, SemanticallyStandardizable class ReferenceHolder(SemanticallyStandardizable): def __init__(self, reference): self.reference = reference def standardize(self, converter: Converter) -> Self: self.reference = converter.standardize_reference(self.reference, strict=True) return self
In the following example, the
pydantic.BaseModel.model_copy()is used to automatically reuse all other fields that aren’t updated, which creates a new object.import datetime from typing_extensions import Self from curies import Converter, Reference, SemanticallyStandardizable from pydantic import BaseModel class Triple(BaseModel, SemanticallyStandardizable): subject: Reference predicate: Reference object: Reference date_asserted: datetime.date def standardize(self, converter: Converter) -> Self: return self.model_copy( update={ "subject": converter.standardize_reference(self.subject, strict=True), "predicate": converter.standardize_reference(self.predicate, strict=True), "object": converter.standardize_reference(self.object, strict=True), } )
curiesprovides a high-level interface for standardizing classes incuries.standardize().from curies import Converter converter = Converter() converter.add_prefix("CHEBI", "http://purl.obolibrary.org/obo/CHEBI_") converter.add_synonym("CHEBI", "chebi") r1 = ReferenceHolder(Reference.from_curie("chebi:1")) r2 = ReferenceHolder(Reference.from_curie("chebi:2")) # can be used directly on an object assert ReferenceHolder(Reference.from_curie("CHEBI:1")) == curies.standardize(r1, converter) # can also be used on an iterable/collection assert [ ReferenceHolder(Reference.from_curie("CHEBI:1")), ReferenceHolder(Reference.from_curie("CHEBI:2")), ] == curies.standardize((r1, r2), converter)
Methods Summary
standardize(converter)Standardize all references in the object.
Methods Documentation