Skip to content

pandas_openscm.db.in_memory#

In-memory backend

Not very useful in practice, but helpful for testing

Classes:

Name Description
InMemoryDataBackend

In-memory data backend

InMemoryIndexBackend

In-memory index backend

InMemoryDataBackend #

In-memory data backend

Methods:

Name Description
load_data

Load a data file

save_data

Save data to disk

Attributes:

Name Type Description
data dict[str, DataFrame] | None

Data store

ext str

Extension to use with files saved by this backend.

preserves_index Literal[True]

Whether this backend preserves the index of data upon (de-)serialisation

Source code in src/pandas_openscm/db/in_memory.py
@define
class InMemoryDataBackend:
    """
    In-memory data backend
    """

    ext: str = ".in-mem"
    """
    Extension to use with files saved by this backend.
    """

    data: dict[str, pd.DataFrame] | None = None
    """
    Data store
    """

    @property
    def preserves_index(self) -> Literal[True]:
        """
        Whether this backend preserves the index of data upon (de-)serialisation
        """
        return True

    def load_data(self, data_file: Path) -> pd.DataFrame:
        """
        Load a data file

        Parameters
        ----------
        data_file
            File from which to load the data

        Returns
        -------
        :
            Loaded data
        """
        if self.data is None:
            raise TypeError

        return self.data[str(data_file)]

    def save_data(self, data: pd.DataFrame, data_file: Path) -> None:
        """
        Save data to disk

        Parameters
        ----------
        data
            Data to save

        data_file
            File in which to save the data
        """
        if self.data is None:
            self.data = {}

        self.data[str(data_file)] = data
        # Have to do this as, even though it's in-memory,
        # the layer above expects to have files to check, remove etc.
        data_file.touch()

data class-attribute instance-attribute #

data: dict[str, DataFrame] | None = None

Data store

ext class-attribute instance-attribute #

ext: str = '.in-mem'

Extension to use with files saved by this backend.

preserves_index property #

preserves_index: Literal[True]

Whether this backend preserves the index of data upon (de-)serialisation

load_data #

load_data(data_file: Path) -> DataFrame

Load a data file

Parameters:

Name Type Description Default
data_file Path

File from which to load the data

required

Returns:

Type Description
DataFrame

Loaded data

Source code in src/pandas_openscm/db/in_memory.py
def load_data(self, data_file: Path) -> pd.DataFrame:
    """
    Load a data file

    Parameters
    ----------
    data_file
        File from which to load the data

    Returns
    -------
    :
        Loaded data
    """
    if self.data is None:
        raise TypeError

    return self.data[str(data_file)]

save_data #

save_data(data: DataFrame, data_file: Path) -> None

Save data to disk

Parameters:

Name Type Description Default
data DataFrame

Data to save

required
data_file Path

File in which to save the data

required
Source code in src/pandas_openscm/db/in_memory.py
def save_data(self, data: pd.DataFrame, data_file: Path) -> None:
    """
    Save data to disk

    Parameters
    ----------
    data
        Data to save

    data_file
        File in which to save the data
    """
    if self.data is None:
        self.data = {}

    self.data[str(data_file)] = data
    # Have to do this as, even though it's in-memory,
    # the layer above expects to have files to check, remove etc.
    data_file.touch()

InMemoryIndexBackend #

In-memory index backend

Methods:

Name Description
load_file_map

Load the file map

load_index

Load the index

save_file_map

Save the file map to disk

save_index

Save the index to disk

Attributes:

Name Type Description
ext str

Extension to use with files saved by this backend.

file_map Series[Path] | None

File map store

index DataFrame | None

Index store

preserves_index Literal[True]

Whether this backend preserves the pd.MultiIndex upon (de-)serialisation

Source code in src/pandas_openscm/db/in_memory.py
@define
class InMemoryIndexBackend:
    """
    In-memory index backend
    """

    ext: str = ".in-mem"
    """
    Extension to use with files saved by this backend.
    """

    index: pd.DataFrame | None = None
    """Index store"""

    file_map: pd.Series[Path] | None = None  # type: ignore # pandas confused about what it supports
    """File map store"""

    @property
    def preserves_index(self) -> Literal[True]:
        """
        Whether this backend preserves the `pd.MultiIndex` upon (de-)serialisation
        """
        return True

    def load_file_map(self, file_map_file: Path) -> pd.DataFrame:
        """
        Load the file map

        Parameters
        ----------
        file_map_file
            File from which to load the file map

        Returns
        -------
        :
            Loaded file map
        """
        if self.file_map is None:
            raise TypeError

        return self.file_map.to_frame()

    def load_index(self, index_file: Path) -> pd.DataFrame:
        """
        Load the index

        Parameters
        ----------
        index_file
            File from which to load the index

        Returns
        -------
        :
            Loaded index
        """
        if self.index is None:
            raise TypeError

        return self.index

    def save_file_map(
        self,
        file_map: pd.Series[Path],  # type: ignore # pandas confused about what it supports
        file_map_file: Path,
    ) -> None:
        """
        Save the file map to disk

        Parameters
        ----------
        file_map
            File map to save

        file_map_file
            File in which to save the file map
        """
        self.file_map = file_map
        # Have to do this as, even though it's in-memory,
        # the layer above expects to have files to check
        file_map_file.touch()

    def save_index(
        self,
        index: pd.DataFrame,
        index_file: Path,
    ) -> None:
        """
        Save the index to disk

        Parameters
        ----------
        index
            Index to save

        index_file
            File in which to save the index
        """
        self.index = index
        # Have to do this as, even though it's in-memory,
        # the layer above expects to have files to check
        index_file.touch()

ext class-attribute instance-attribute #

ext: str = '.in-mem'

Extension to use with files saved by this backend.

file_map class-attribute instance-attribute #

file_map: Series[Path] | None = None

File map store

index class-attribute instance-attribute #

index: DataFrame | None = None

Index store

preserves_index property #

preserves_index: Literal[True]

Whether this backend preserves the pd.MultiIndex upon (de-)serialisation

load_file_map #

load_file_map(file_map_file: Path) -> DataFrame

Load the file map

Parameters:

Name Type Description Default
file_map_file Path

File from which to load the file map

required

Returns:

Type Description
DataFrame

Loaded file map

Source code in src/pandas_openscm/db/in_memory.py
def load_file_map(self, file_map_file: Path) -> pd.DataFrame:
    """
    Load the file map

    Parameters
    ----------
    file_map_file
        File from which to load the file map

    Returns
    -------
    :
        Loaded file map
    """
    if self.file_map is None:
        raise TypeError

    return self.file_map.to_frame()

load_index #

load_index(index_file: Path) -> DataFrame

Load the index

Parameters:

Name Type Description Default
index_file Path

File from which to load the index

required

Returns:

Type Description
DataFrame

Loaded index

Source code in src/pandas_openscm/db/in_memory.py
def load_index(self, index_file: Path) -> pd.DataFrame:
    """
    Load the index

    Parameters
    ----------
    index_file
        File from which to load the index

    Returns
    -------
    :
        Loaded index
    """
    if self.index is None:
        raise TypeError

    return self.index

save_file_map #

save_file_map(
    file_map: Series[Path], file_map_file: Path
) -> None

Save the file map to disk

Parameters:

Name Type Description Default
file_map Series[Path]

File map to save

required
file_map_file Path

File in which to save the file map

required
Source code in src/pandas_openscm/db/in_memory.py
def save_file_map(
    self,
    file_map: pd.Series[Path],  # type: ignore # pandas confused about what it supports
    file_map_file: Path,
) -> None:
    """
    Save the file map to disk

    Parameters
    ----------
    file_map
        File map to save

    file_map_file
        File in which to save the file map
    """
    self.file_map = file_map
    # Have to do this as, even though it's in-memory,
    # the layer above expects to have files to check
    file_map_file.touch()

save_index #

save_index(index: DataFrame, index_file: Path) -> None

Save the index to disk

Parameters:

Name Type Description Default
index DataFrame

Index to save

required
index_file Path

File in which to save the index

required
Source code in src/pandas_openscm/db/in_memory.py
def save_index(
    self,
    index: pd.DataFrame,
    index_file: Path,
) -> None:
    """
    Save the index to disk

    Parameters
    ----------
    index
        Index to save

    index_file
        File in which to save the index
    """
    self.index = index
    # Have to do this as, even though it's in-memory,
    # the layer above expects to have files to check
    index_file.touch()