Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add types for Prefix and PrefixMap #134

Merged
merged 23 commits into from
Dec 19, 2024
Merged

Add types for Prefix and PrefixMap #134

merged 23 commits into from
Dec 19, 2024

Conversation

cthoyt
Copy link
Member

@cthoyt cthoyt commented Dec 1, 2024

This PR adds the Prefix class, which allows you to do the following:

import csv
from pydantic import BaseModel, ValidationError
from curies import Converter, Prefix

converter = Converter.from_prefix_map({
    "CHEBI": "http://purl.obolibrary.org/obo/CHEBI_",
})

class Row(BaseModel):
    prefix: Prefix
    identifier: str
    name: str
    smiles: str

records = [
    {"prefix": "CHEBI", "identifier": "16236", "name": "ethanol", "smiles": "CCO"},
    {"prefix": "CHEBI", "identifier": "28831", "name": "propanol", "smiles": "CCCO"},
    {"prefix": "CHOBI", "identifier": "44884", "name": "pentanol", "smiles": "CCCCCO"},
]
for record in records:
    try:
        model = Row.model_validate(record, context=converter)
    except ValidationError as e:
        print(f"Issue parsing record {record}: {e}")
        continue

@cthoyt cthoyt marked this pull request as ready for review December 18, 2024 14:54
@cthoyt cthoyt merged commit 1df957b into main Dec 19, 2024
11 checks passed
@cthoyt cthoyt deleted the add-prefix-type branch December 19, 2024 10:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant