-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add types for Prefix and PrefixMap (#134)
- Loading branch information
Showing
5 changed files
with
384 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,3 +67,4 @@ for updating your code. | |
struct | ||
api | ||
services/index | ||
typing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
Typing | ||
====== | ||
This package comes with utilities for better typing other resources. | ||
|
||
Let's say you have a table like this: | ||
|
||
====== ========== ======== ====== | ||
prefix identifier name smiles | ||
====== ========== ======== ====== | ||
CHEBI 16236 ethanol CCO | ||
CHEBI 28831 propanol CCCO | ||
CHOBI 44884 pentanol CCCCCO | ||
====== ========== ======== ====== | ||
|
||
Note that there's a typo in the prefix on the fourth row in the prefix because it | ||
uses ``CHOBI`` instead of ``CHEBI``. In the following code, we simulate reading that | ||
file and show where the error shows up: | ||
|
||
.. code-block:: python | ||
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 | ||
Note that :meth:`pydantic.BaseModel.model_validate` allows for passing a "context". | ||
The :class:`curies.Prefix` class implements custom context handling, so if you pass | ||
a converter, it knows how to check using prefixes in the converter. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.