Skip to content

Commit

Permalink
Implement typing support in "colour_datasets" package.
Browse files Browse the repository at this point in the history
  • Loading branch information
KelSolaar committed Feb 5, 2022
1 parent a8bc176 commit 43f8040
Show file tree
Hide file tree
Showing 25 changed files with 1,568 additions and 983 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*.pyo
.DS_Store
.coverage
.dmypy.json
.idea
__pycache__
build
Expand Down
25 changes: 16 additions & 9 deletions colour_datasets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,26 @@
__change_version__ = "1"
__version__ = ".".join(
(__major_version__, __minor_version__, __change_version__)
) # yapf: disable
)

try:
version = subprocess.check_output( # nosec
["git", "describe"],
cwd=os.path.dirname(__file__),
stderr=subprocess.STDOUT,
).strip()
version = version.decode("utf-8")
_version = (
subprocess.check_output( # nosec
["git", "describe"],
cwd=os.path.dirname(__file__),
stderr=subprocess.STDOUT,
)
.strip()
.decode("utf-8")
)
except Exception:
version = __version__
_version = __version__

colour.utilities.ANCILLARY_COLOUR_SCIENCE_PACKAGES["colour-datasets"] = version
colour.utilities.ANCILLARY_COLOUR_SCIENCE_PACKAGES[
"colour-datasets"
] = _version

del _version

# TODO: Remove legacy printing support when deemed appropriate.
try:
Expand Down
4 changes: 2 additions & 2 deletions colour_datasets/examples/examples_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

message_box(
"Listing the available datasets.\n\n"
"Note: A ticked checkbox means that the particular dataset "
"has been synced locally."
"Note: A ticked checkbox means that the particular dataset has been "
"synced locally."
)
print(colour_datasets.datasets())

Expand Down
17 changes: 8 additions & 9 deletions colour_datasets/loaders/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from __future__ import annotations

import sys

from colour.hints import Any, Boolean, Integer, Union
from colour.utilities import CaseInsensitiveMapping, warning

from colour_datasets.records import datasets
Expand Down Expand Up @@ -75,7 +78,7 @@
"build_Zhao2009",
]

DATASET_LOADERS = CaseInsensitiveMapping(
DATASET_LOADERS: CaseInsensitiveMapping = CaseInsensitiveMapping(
{
DatasetLoader_Asano2015.ID: build_Asano2015,
DatasetLoader_Brendel2020.ID: build_Brendel2020,
Expand All @@ -94,8 +97,6 @@
)
DATASET_LOADERS.__doc__ = """
Dataset loaders ids and callables.
DATASET_LOADERS : CaseInsensitiveMapping
"""

from .kuopio import DATASET_LOADERS_KUOPIO_UNIVERSITY # noqa
Expand All @@ -117,30 +118,28 @@

del _module, _export

_HAS_TITLE_KEYS = False
_HAS_TITLE_KEYS: Boolean = False
"""
Whether the :attr:`colour_datasets.loaders.DATASET_LOADERS` attribute has
been updated with dataset titles. This variable is used in the one time
initialisation step that ensures that datasets can also be loaded using their
titles.
_HAS_TITLE_KEYS : bool
"""


def load(dataset):
def load(dataset: Union[Integer, str]) -> Any:
"""
Loads given dataset: The dataset is pulled locally, i.e. synced if required
and then its data is loaded.
Parameters
----------
dataset : str or int
dataset
Dataset id, i.e. the *Zenodo* record number or title.
Returns
-------
object
:class:`object`
Dataset data.
Examples
Expand Down
54 changes: 21 additions & 33 deletions colour_datasets/loaders/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
- :class:`colour_datasets.loaders.AbstractDatasetLoader`
"""

from __future__ import annotations

from abc import ABC, abstractmethod

from colour.hints import Any, Optional

from colour_datasets.records import Record

__author__ = "Colour Developers"
__copyright__ = "Copyright (C) 2019-2021 - Colour Developers"
__license__ = "New BSD License - https://opensource.org/licenses/BSD-3-Clause"
Expand Down Expand Up @@ -48,92 +54,74 @@ class AbstractDatasetLoader(ABC):
Parameters
----------
record : Record
record
Dataset record.
"""

ID = None
ID: str = "Undefined"
"""
Dataset record id, i.e. the *Zenodo* record number.
ID : str
"""

def __init__(self, record):
self._record = record
self._content = None
def __init__(self, record: Record):
self._record: Record = record
self._content: Optional[Any] = None

@property
def record(self):
def record(self) -> Record:
"""
Getter and setter property for the dataset record.
Parameters
----------
value : Record
Value to set the dataset record with.
Getter property for the dataset record.
Returns
-------
str
:class:`colour_datasets.Record`
Dataset record.
"""

return self._record

@property
def id(self):
def id(self) -> str:
"""
Getter and setter property for the dataset id.
Parameters
----------
value : str
Value to set the dataset id with.
Returns
-------
str
:class:`str`
Dataset id.
"""

return self.__class__.ID

@property
def content(self):
def content(self) -> Any:
"""
Getter and setter property for the dataset content.
Parameters
----------
value : object
Value to set the dataset content with.
Returns
-------
str
:class:`object`
Dataset content.
"""

return self._content

@abstractmethod
def load(self):
def load(self) -> Any:
"""
Syncs, parses, converts and returns the dataset content as a *Python*
object.
Returns
-------
object
:class:`object`
Dataset content as a *Python* object.
Notes
-----
- Sub-classes are required to call
:meth:`colour_datasets.loaders.AbstractDatasetLoader.sync` method
when they implement it, e.g.
``super(MyDatasetLoader, self).sync()``.
when they implement it, e.g. ``super().sync()``.
"""

pass
Expand Down
Loading

0 comments on commit 43f8040

Please sign in to comment.