TableSaver#

class petpal.io.table.TableSaver(saver: collections.abc.Callable[[pandas.DataFrame, str], None] | None = None)#

Class for saving Pandas Database objects as CSV or TSV files based on a provided path.

  • Default behavior writes atomically (write temp file + os.replace) to avoid partial files.

  • Accepts an injectable writer callable for testing or alternative persistence backends.

Example

import pandas as pd
from petpal.io.table import TableSaver

table_saver = TableSaver()
my_data = pd.DataFrame(data={'time': [0, 1, 2], 'value': [1, 4, 9]})

# when file extension is .csv, uses commas to separate values
table_saver.save(my_data, 'table.csv')

# when file extension is .tsv or .txt, uses tabs to separate values
table_saver.save(my_data, 'table.txt')
Variables:

_saver – Injectable tabular data saving function that saves a dataframe to a file.

_atomic_save(df: pandas.DataFrame, path: str)#

Saves the data from a Pandas DataFrame object as a tabular file, such as CSV or TSV.

Parameters:
  • df (pd.DataFrame) – Pandas DataFrame with data to be saved.

  • path (str) – Path to file where data is saved.

save(df: pandas.DataFrame, path: str) None#

API that applies the table saving function assigned to self._saver.

Parameters:
  • df (pd.DataFrame) – Pandas DataFrame with data to be saved.

  • path (str) – Path to file where data is saved.