Skip to content

Commit

Permalink
Add overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt committed Oct 13, 2023
1 parent ca9e1e1 commit bda2c64
Showing 1 changed file with 48 additions and 13 deletions.
61 changes: 48 additions & 13 deletions src/curies/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import itertools as itt
import json
from collections import defaultdict
from functools import partial
from pathlib import Path
from typing import (
TYPE_CHECKING,
Expand All @@ -16,6 +17,7 @@
Dict,
Iterable,
List,
Literal,
Mapping,
NamedTuple,
Optional,
Expand All @@ -25,6 +27,7 @@
TypeVar,
Union,
cast,
overload,
)

import requests
Expand Down Expand Up @@ -862,20 +865,34 @@ def format_curie(self, prefix: str, identifier: str) -> str:
"""Format a prefix and identifier into a CURIE string."""
return f"{prefix}{self.delimiter}{identifier}"

def compress_strict(self, uri: str) -> str:
"""Compress a URI to a CURIE, and raise an error of not possible."""
rv = self.compress(uri)
if rv is None:
raise CompressionError(uri)
return rv
@overload
def compress(self, uri: str, *, strict: Literal[True], passthrough: bool) -> str:
...

@overload
def compress(self, uri: str, *, strict: Literal[False], passthrough: Literal[True]) -> str:
...

@overload
def compress(
self, uri: str, *, strict: Literal[False], passthrough: Literal[False]
) -> Optional[str]:
...

def compress(self, uri: str) -> Optional[str]:
def compress(
self, uri: str, *, strict: bool = False, passthrough: bool = False
) -> Optional[str]:
"""Compress a URI to a CURIE, if possible.
:param uri:
A string representing a valid uniform resource identifier (URI)
:param strict: If true and the URI can't be compressed, returns an error
:param passthrough: If true, strict is false, and the URI can't be compressed, return the input.
:returns:
A compact URI if this converter could find an appropriate URI prefix, otherwise none.
:raises CompressionError:
If strict is set to true and the URI can't be compressed
>>> from curies import Converter
>>> converter = Converter.from_prefix_map({
Expand All @@ -888,9 +905,13 @@ def compress(self, uri: str) -> Optional[str]:
>>> converter.compress("http://example.org/missing:0000000")
"""
prefix, identifier = self.parse_uri(uri)
if prefix is None or identifier is None:
return None
return self.format_curie(prefix, identifier)
if prefix and identifier:
return self.format_curie(prefix, identifier)
if strict:
raise CompressionError(uri)
if passthrough:
return uri
return None

def parse_uri(self, uri: str) -> Union[ReferenceTuple, Tuple[None, None]]:
"""Compress a URI to a CURIE pair.
Expand Down Expand Up @@ -1133,14 +1154,19 @@ def pd_compress(
df: "pandas.DataFrame",
column: Union[str, int],
target_column: Union[None, str, int] = None,
strict: bool = False,
passthrough: bool = False,
) -> None:
"""Convert all URIs in the given column to CURIEs.
:param df: A pandas DataFrame
:param column: The column in the dataframe containing URIs to convert to CURIEs.
:param target_column: The column to put the results in. Defaults to input column.
:param strict: If true and the URI can't be compressed, returns an error
:param passthrough: If true, strict is false, and the URI can't be compressed, return the input.
"""
df[column if target_column is None else target_column] = df[column].map(self.compress)
func = partial(self.compress, strict=strict, passthrough=passthrough)
df[column if target_column is None else target_column] = df[column].map(func)

def pd_expand(
self,
Expand Down Expand Up @@ -1223,16 +1249,25 @@ def pd_standardize_uri(
)

def file_compress(
self, path: Union[str, Path], column: int, sep: Optional[str] = None, header: bool = True
self,
path: Union[str, Path],
column: int,
sep: Optional[str] = None,
header: bool = True,
strict: bool = False,
passthrough: bool = False,
) -> None:
"""Convert all URIs in the given column of a CSV file to CURIEs.
:param path: A pandas DataFrame
:param column: The column in the dataframe containing URIs to convert to CURIEs.
:param sep: The delimiter of the CSV file, defaults to tab
:param header: Does the file have a header row?
:param strict: If true and the URI can't be compressed, returns an error
:param passthrough: If true, strict is false, and the URI can't be compressed, return the input.
"""
self._file_helper(self.compress, path=path, column=column, sep=sep, header=header)
func = partial(self.compress, strict=strict, passthrough=passthrough)
self._file_helper(func, path=path, column=column, sep=sep, header=header)

def file_expand(
self, path: Union[str, Path], column: int, sep: Optional[str] = None, header: bool = True
Expand Down

0 comments on commit bda2c64

Please sign in to comment.