Skip to content

pandas_openscm.db.backends#

Available back-ends

This is just a shortcut/convenience module

Classes:

Name Description
DataBackendOptions

A collection of data back-end options

IndexBackendOptions

A collection of index back-end options

Attributes:

Name Type Description
DATA_BACKENDS

Inbuilt data back-ends

INDEX_BACKENDS

Inbuilt index back-ends

DATA_BACKENDS module-attribute #

DATA_BACKENDS = DataBackendOptions(
    cast(
        tuple[tuple[str, type[OpenSCMDBDataBackend]], ...],
        (
            ("csv", CSVDataBackend),
            ("feather", FeatherDataBackend),
            ("in_memory", InMemoryDataBackend),
            ("netCDF", netCDFDataBackend),
        ),
    )
)

Inbuilt data back-ends

INDEX_BACKENDS module-attribute #

INDEX_BACKENDS = IndexBackendOptions(
    cast(
        tuple[tuple[str, type[OpenSCMDBIndexBackend]], ...],
        (
            ("csv", CSVIndexBackend),
            ("feather", FeatherIndexBackend),
            ("in_memory", InMemoryIndexBackend),
            ("netCDF", netCDFIndexBackend),
        ),
    )
)

Inbuilt index back-ends

DataBackendOptions #

A collection of data back-end options

Methods:

Name Description
get_instance

Get an instance of one of the options

guess_backend

Guess backend from a file name

Attributes:

Name Type Description
options tuple[tuple[str, type[OpenSCMDBDataBackend]], ...]

Options

Source code in src/pandas_openscm/db/backends.py
@frozen
class DataBackendOptions:
    """A collection of data back-end options"""

    options: tuple[  # type hint doesn't work properly, but ok
        tuple[str, type[OpenSCMDBDataBackend]], ...
    ]
    """
    Options

    The first element of each option is the option's short name.
    The second element is the class that matches that option.
    """

    def get_instance(self, option: str) -> OpenSCMDBDataBackend:
        """
        Get an instance of one of the options

        Parameters
        ----------
        option
            Option for which to get a data back-end instance

        Returns
        -------
        :
            Initialised instance

        Raises
        ------
        KeyError
            The option is not supported
        """
        for short_name, option_cls in self.options:
            if short_name == option:
                return option_cls()

        msg = (
            f"{option=} is not supported. "
            f"Available options: {tuple(v[1] for v in self.options)}"
        )
        raise KeyError(msg)

    def guess_backend(self, data_file_name: str) -> OpenSCMDBDataBackend:
        """
        Guess backend from a file name

        Parameters
        ----------
        data_file_name
            Name of the data file from which to guess the backend

        Returns
        -------
        :
            Guessed backend

        Raises
        ------
        ValueError
            The backend could not be guessed from `data_file_name`
        """
        ext = Path(data_file_name).suffix
        for _, option_cls in self.options:
            option = option_cls()
            if ext == option.ext:
                return option

        known_options_and_extensions = [(v[0], v[1]().ext) for v in self.options]
        msg = (
            f"Could not guess backend from {data_file_name=!r}. "
            "The file's extension does not match any of the available options: "
            f"{known_options_and_extensions=}"
        )
        raise ValueError(msg)

options instance-attribute #

Options

The first element of each option is the option's short name. The second element is the class that matches that option.

get_instance #

get_instance(option: str) -> OpenSCMDBDataBackend

Get an instance of one of the options

Parameters:

Name Type Description Default
option str

Option for which to get a data back-end instance

required

Returns:

Type Description
OpenSCMDBDataBackend

Initialised instance

Raises:

Type Description
KeyError

The option is not supported

Source code in src/pandas_openscm/db/backends.py
def get_instance(self, option: str) -> OpenSCMDBDataBackend:
    """
    Get an instance of one of the options

    Parameters
    ----------
    option
        Option for which to get a data back-end instance

    Returns
    -------
    :
        Initialised instance

    Raises
    ------
    KeyError
        The option is not supported
    """
    for short_name, option_cls in self.options:
        if short_name == option:
            return option_cls()

    msg = (
        f"{option=} is not supported. "
        f"Available options: {tuple(v[1] for v in self.options)}"
    )
    raise KeyError(msg)

guess_backend #

guess_backend(data_file_name: str) -> OpenSCMDBDataBackend

Guess backend from a file name

Parameters:

Name Type Description Default
data_file_name str

Name of the data file from which to guess the backend

required

Returns:

Type Description
OpenSCMDBDataBackend

Guessed backend

Raises:

Type Description
ValueError

The backend could not be guessed from data_file_name

Source code in src/pandas_openscm/db/backends.py
def guess_backend(self, data_file_name: str) -> OpenSCMDBDataBackend:
    """
    Guess backend from a file name

    Parameters
    ----------
    data_file_name
        Name of the data file from which to guess the backend

    Returns
    -------
    :
        Guessed backend

    Raises
    ------
    ValueError
        The backend could not be guessed from `data_file_name`
    """
    ext = Path(data_file_name).suffix
    for _, option_cls in self.options:
        option = option_cls()
        if ext == option.ext:
            return option

    known_options_and_extensions = [(v[0], v[1]().ext) for v in self.options]
    msg = (
        f"Could not guess backend from {data_file_name=!r}. "
        "The file's extension does not match any of the available options: "
        f"{known_options_and_extensions=}"
    )
    raise ValueError(msg)

IndexBackendOptions #

A collection of index back-end options

Methods:

Name Description
get_instance

Get an instance of one of the options

guess_backend

Guess backend from a file name

Attributes:

Name Type Description
options tuple[tuple[str, type[OpenSCMDBIndexBackend]], ...]

Options

Source code in src/pandas_openscm/db/backends.py
@frozen
class IndexBackendOptions:
    """A collection of index back-end options"""

    options: tuple[tuple[str, type[OpenSCMDBIndexBackend]], ...]
    """
    Options

    The first element of each option is the option's short name.
    The second element is the class that matches that option.
    """

    def get_instance(self, option: str) -> OpenSCMDBIndexBackend:
        """
        Get an instance of one of the options

        Parameters
        ----------
        option
            Option for which to get a index back-end instance

        Returns
        -------
        :
            Initialised instance

        Raises
        ------
        KeyError
            The option is not supported
        """
        for short_name, option_cls in self.options:
            if short_name == option:
                return option_cls()

        msg = (
            f"{option=} is not supported. "
            f"Available options: {tuple(v[1] for v in self.options)}"
        )
        raise KeyError(msg)

    def guess_backend(self, index_file_name: str) -> OpenSCMDBIndexBackend:
        """
        Guess backend from a file name

        Parameters
        ----------
        index_file_name
            Name of the index file from which to guess the backend

        Returns
        -------
        :
            Guessed backend

        Raises
        ------
        ValueError
            The backend could not be guessed from `index_file_name`
        """
        ext = Path(index_file_name).suffix
        for _, option_cls in self.options:
            option = option_cls()
            if ext == option.ext:
                return option

        known_options_and_extensions = [(v[0], v[1]().ext) for v in self.options]
        msg = (
            f"Could not guess backend from {index_file_name=!r}. "
            "The file's extension does not match any of the available options: "
            f"{known_options_and_extensions=}"
        )
        raise ValueError(msg)

options instance-attribute #

Options

The first element of each option is the option's short name. The second element is the class that matches that option.

get_instance #

get_instance(option: str) -> OpenSCMDBIndexBackend

Get an instance of one of the options

Parameters:

Name Type Description Default
option str

Option for which to get a index back-end instance

required

Returns:

Type Description
OpenSCMDBIndexBackend

Initialised instance

Raises:

Type Description
KeyError

The option is not supported

Source code in src/pandas_openscm/db/backends.py
def get_instance(self, option: str) -> OpenSCMDBIndexBackend:
    """
    Get an instance of one of the options

    Parameters
    ----------
    option
        Option for which to get a index back-end instance

    Returns
    -------
    :
        Initialised instance

    Raises
    ------
    KeyError
        The option is not supported
    """
    for short_name, option_cls in self.options:
        if short_name == option:
            return option_cls()

    msg = (
        f"{option=} is not supported. "
        f"Available options: {tuple(v[1] for v in self.options)}"
    )
    raise KeyError(msg)

guess_backend #

guess_backend(
    index_file_name: str,
) -> OpenSCMDBIndexBackend

Guess backend from a file name

Parameters:

Name Type Description Default
index_file_name str

Name of the index file from which to guess the backend

required

Returns:

Type Description
OpenSCMDBIndexBackend

Guessed backend

Raises:

Type Description
ValueError

The backend could not be guessed from index_file_name

Source code in src/pandas_openscm/db/backends.py
def guess_backend(self, index_file_name: str) -> OpenSCMDBIndexBackend:
    """
    Guess backend from a file name

    Parameters
    ----------
    index_file_name
        Name of the index file from which to guess the backend

    Returns
    -------
    :
        Guessed backend

    Raises
    ------
    ValueError
        The backend could not be guessed from `index_file_name`
    """
    ext = Path(index_file_name).suffix
    for _, option_cls in self.options:
        option = option_cls()
        if ext == option.ext:
            return option

    known_options_and_extensions = [(v[0], v[1]().ext) for v in self.options]
    msg = (
        f"Could not guess backend from {index_file_name=!r}. "
        "The file's extension does not match any of the available options: "
        f"{known_options_and_extensions=}"
    )
    raise ValueError(msg)