Reference

class Reference(*, prefix: PrefixType_co, identifier: Annotated[str, AfterValidator(func=_validate_identifier)])[source]

Bases: BaseModel, Generic[PrefixType_co]

A reference to an entity in a given identifier space.

This class uses Pydantic to make it easier to build other more complex data types with Pydantic that also uses a first- class notion of parsed reference (instead of merely stringified CURIEs). Instances of this class can also be hashed because of the “frozen” configuration from Pydantic (see https://docs.pydantic.dev/latest/usage/model_config/ for more details).

A reference can be constructed several ways:

>>> Reference(prefix="chebi", identifier="1234")
Reference(prefix='chebi', identifier='1234')
>>> Reference.from_curie("chebi:1234")
Reference(prefix='chebi', identifier='1234')

A reference can also be constructued using Pydantic’s parsing utilities, but keep in mind if you’re using Pydantic v1 or Pydantic v2.

A reference can be formatted as a CURIE string with the curie attribute

>>> Reference.from_curie("chebi:1234").curie
'chebi:1234'

References can’t be sliced like reference tuples, but they can still be accessed through attributes

>>> t = Reference.from_curie("chebi:1234")
>>> t.prefix
'chebi'
>>> t.identifier
'1234'

If you need a performance gain, you can get a ReferenceTuple using the pair attribute:

>>> reference = Reference.from_curie("chebi:1234")
>>> reference.pair
ReferenceTuple(prefix='chebi', identifier='1234')

If you want to extend the reference class with your own custom prefix, then it can be done with generics

from curies import Reference, Prefix
from pydantic import BaseModel
from pydantic_core.core_schema import ValidationInfo


class DerivedPrefix(Prefix):
    @classmethod
    def validate(cls, value: str, info: ValidationInfo) -> str:
        if value != value.lower():
            raise ValueError
        return value


class DerivedReference(Reference[DerivedPrefix]):
    pass

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Attributes Summary

curie

Get the reference as a CURIE string.

model_config

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

pair

Get the reference as a 2-tuple of prefix and identifier.

Methods Summary

from_curie(curie, *[, sep, converter])

Parse a CURIE string and populate a reference.

from_reference(reference, *[, converter])

Parse a CURIE string and populate a reference.

with_name(name)

Return this reference, with a name.

without_name()

Return this reference, since it already has no name.

Attributes Documentation

curie

Get the reference as a CURIE string.

Returns:

A string representation of a compact URI (CURIE).

>>> Reference(prefix="chebi", identifier="1234").curie
'chebi:1234'
model_config = {'frozen': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

pair

Get the reference as a 2-tuple of prefix and identifier.

Methods Documentation

classmethod from_curie(curie: str, *, sep: str = ':', converter: Converter | None = None) → Self[source]

Parse a CURIE string and populate a reference.

Parameters:
  • curie – A string representation of a compact URI (CURIE)

  • sep – The separator

  • converter – The converter to use as context when parsing

Returns:

A reference object

>>> Reference.from_curie("chebi:1234")
Reference(prefix='chebi', identifier='1234')
classmethod from_reference(reference: Reference | ReferenceTuple, *, converter: Converter | None = None) → Self[source]

Parse a CURIE string and populate a reference.

Parameters:
  • reference – A pre-parsed reference

  • converter – The converter to use as context when parsing

Returns:

A reference object

with_name(name: str) → NamableReference[source]

Return this reference, with a name.

without_name() → Reference[source]

Return this reference, since it already has no name.