diff --git a/concepts/__init__.py b/concepts/__init__.py index a5472e5..1cd7cd2 100644 --- a/concepts/__init__.py +++ b/concepts/__init__.py @@ -6,10 +6,11 @@ from ._example import EXAMPLE from .contexts import Context from .definitions import Definition +from .examples import load_dataset __all__ = ['EXAMPLE', 'Context', 'Definition', 'Shape', - 'load', 'load_cxt', 'load_csv', + 'load', 'load_cxt', 'load_csv', load_dataset, 'make_context'] __title__ = 'concepts' diff --git a/concepts/examples.py b/concepts/examples.py new file mode 100644 index 0000000..ac281e8 --- /dev/null +++ b/concepts/examples.py @@ -0,0 +1,41 @@ +"""Loading examplary formal contexts.""" + +import typing +from urllib.request import urlopen + +from .contexts import Context + +__all__ = ['load_dataset'] + +DATASET_SOURCE = "https://raw.githubusercontent.com/fcatools/contexts/main/contexts" + + +# inspired by https://github.com/mwaskom/seaborn/blob/master/seaborn/utils.py#L524 +def load_dataset(name: str, *, data_src: typing.Optional[str] = DATASET_SOURCE, + encoding: typing.Optional[str] = 'utf-8'): + """Load an example formal context from the online repository (requires internet). + + Args: + name (str): + Name of the dataset (``{name}.cxt`` on + https://github.com/fcatools/contexts/tree/main/contexts). + data_src (str, optional): + Base URL to the repository to download the dataset. + encoding (str, optional): + Encoding of the file (``'utf-8'``, ``'latin1'``, ``'ascii'``, ...). + + Returns: + Context: New :class:`.Context` instance. + + Example: + >>> import concepts + >>> concepts.load_dataset('livingbeings_en') + + """ + + url = f"{data_src}/{name}.cxt" + + # TODO: implement caching here? + + with urlopen(url) as data: + return Context.fromstring(data.read().decode(encoding), 'cxt') diff --git a/tests/test_examples.py b/tests/test_examples.py new file mode 100644 index 0000000..37c854f --- /dev/null +++ b/tests/test_examples.py @@ -0,0 +1,8 @@ +import concepts + + +def test_load_dataset(): + context = concepts.load_dataset('livingbeings_en') + + assert len(context.objects) == 8 + assert len(context.properties) == 9