PrefixMap

class PrefixMap(root: RootModelRootType = PydanticUndefined)[source]

Bases: RootModel[dict[TypeVar, str]]

A simple prefix map.

This can be used to validate dictionaries:

from curies import PrefixMap

prefix_map_model = PrefixMap.model_validate(
    {
        "CHEBI": "http://purl.obolibrary.org/obo/CHEBI_",
    }
)

# note that you have to unpack it
prefix_map_dict = prefix_map_model.root

Similarly, a prefix map can be used as part of another Pydantic model like in:

from pydantic import BaseModel
from curies import PrefixMap


class RDFContent(BaseModel):
    prefix_map: PrefixMap
    triples: list[tuple[str, str, str]]


rdf_content = RDFContent.model_validate(
    {
        "prefix_map": {
            "CHEBI": "http://purl.obolibrary.org/obo/CHEBI_",
        },
        "triples": [
            ("CHEBI:1234", "RO:0000001", "CHEBI:5678"),
        ],
    }
)

# note that you have to unpack the resulting prefix map
prefix_map = rdf_content.prefix_map.root

If you want to inject a non-standard Prefix type, then you can annotate the prefix map with an optional generic. In the following example, the derived prefix checks that it’s always lowercase.

from curies import Prefix, PrefixMap
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 PrefixMapContainer(BaseModel):
    prefix_map: PrefixMap[DerivedPrefix]

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

model_config

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

Attributes Documentation

model_config = {}

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