Tabler

Tabler package.

The tabler package provides the tabler.Table class for simple and intutive accessing, manipulation and writing of tablulated data.

Basic Usage:

>>> from tabler import Table
>>> table = Table('somefile.csv')
>>> table.open('Path/To/Input_File.csv')
>>> table[0]['Price']
'29.99'
>>> table[0]['Price'] = 15.50
>>> table[0]['Price']
'15.5'
>>> table.write('Path/To/Output_File')
Writen 3 lines to file Path/To/Output_File.csv
class tabler.Table(filepath: Optional[str] = None, table_type: Optional[tabler.tabletypes.basetabletype.BaseTableType] = None, header: Optional[Collection[str]] = None, data: Optional[Iterable[T_co]] = None)

A wrapper object for tabulated data.

Allows access to and manipulation of tablulated data. This data can be input directly or loaded from a file. Data can also be writen data to a file. Table rows are encapsulated with the tabler.tablerow.TableRow class.

Different filetypes can be read and written by providing a subclass of tabler.tabletypes.BaseTableType which implements the open and write methods.

A filename can be provided to open an existing file. An apropriate tabler.tabletypes.BaseTableType object can be provided to specify how the file will be opened. If this is not specified one will be selected based on the file extension in the filename using default parameters.

Alternatively header and data can be specified to populate the table directly.

Parameters:
  • table_type (tabler.tabletypes.BaseTableType) – Table Type to use to open a file referenced by filetype.
  • filepath (str) – Path to file to be opened.
  • header (list) – List of column headers to be used if not loaded from file.
  • data (list(list(str, int or float))) – Two dimensional list. Each list will form a row of cell data.
Raises:

ValueError – If filepath is None or both header and data are None.

append(row: Union[Iterable[T_co], tabler.tablerow.TableRow]) → None

Add new row to table.

Parameters:row (list or tabler.tablerow.TableRow.) – Data for new row.
copy() → tabler.table.Table

Return duplicate Table object.

empty() → None

Clear all data.

get_column(column: Union[int, str]) → List[T]

Return all values in a column.

Parameters:column (str or int.) – Name or index of to be returned.
Return type:list
is_empty() → bool

Return True if the table conatins no data, otherwise return False.

Return type:bool
load(header: Collection[T_co], data: Iterable[Union[Iterable[T_co], tabler.tablerow.TableRow]]) → None

Populate table with header and data.

Parameters:
  • header (list) – Names of column headers.
  • data (list(list(str, int or float))) – Rows of data. Each row must be a list of cell values
print_r() → None

Print table data in a readable format.

remove_column(column: str) → None

Remove a specified column from the Table.

Parameters:column (str or int.) – Name or index of to be removed.
sort(sort_key: str, asc: bool = True) → None

Sort table by column.

Parameters:
  • sort_key (str or int) – Column header or index of column to sort by.
  • asc (bool) – If True Table will be sorted in ascending order. Otherwise order will be descending. (Default: True)
sorted(sort_key: str, asc: bool = True) → tabler.table.Table

Return a sorted duplicate of the Table.

Parameters:
  • sort_key (str or int) – Column header or index of column to sort by.
  • asc (bool) – If True Table will be sorted in ascending order. Otherwise order will be descending. (Default: True)
Return type:

tabler.Table.

split_by_row_count(row_count: int) → List[tabler.table.Table]

Split table by row count.

Create multiple tabler.Table instances each with a subset of this one’s data.

Parameters:row_count (int) – Number of rows in each Table.
Return type:list(tabler.Table).
write(filepath: Union[str, pathlib.Path], table_type: Optional[tabler.tabletypes.basetabletype.BaseTableType] = None) → None

Create file from table.

Parameters:
  • table_type (tabler.BaseTableType) – Table Type to use to save the file.
  • filepath (str) – Path at which the file will be saved.
class tabler.CSV(encoding: str = 'utf-8', delimiter: str = ', ', extension: str = '.csv', verbose: Optional[bool] = None)

Table Type for comma separated value (.csv) files.

Parameters:
  • encoding (str) – Encoding of file. Default: utf8.
  • delimiter (str) – Delimiter used by file. Default , (Comma).
  • extension (str) – Extension of file to save. Default .csv.
  • verbose (bool or None.) – If True print status messages. If None use tabler.tabletype.BaseTableType.verbose.
open_path(path: str) → Tuple[List[str], List[List[Any]]]

Return header and rows from file.

Parameters:path (str, pathlib.Path or compatible.) – Path to file to be opened.
parse_value(value: Any) → Any

Return None if the value is empty, otherwise return str(value).

write(table: Table, path: Union[str, Path]) → None

Save data from tabler.Table to file.

:param table:”Table” to save. :type table: tabler.Table :param path: Path to file to be opened. :type path: str, pathlib.Path or compatible.

class tabler.CSVURL(encoding: str = 'utf-8', delimiter: str = ', ', extension: str = '.csv', verbose: Optional[bool] = None)

Table type for opening .csv files over HTTP.

open_path(path: str) → Tuple[List[str], List[List[Any]]]

Return header and rows from file.

Parameters:path (str) – URL of file to be opened.
write(table: Table, path: Union[str, Path]) → None

Save data from tabler.Table to file.

:param table:”Table” to save. :type table: tabler.Table :param path: Path to file to be opened. :type path: str, pathlib.Path or compatible.

class tabler.HTML(use_header: bool = True, encoding: str = 'utf8', extension: str = '.html', verbose: bool = True)

Table Type for comma separated value (.csv) files.

Parameters:
  • use_header (bool) – If True file will include column headers. Default(True)
  • encoding (str) – Encoding of file. Default: utf8.
  • extension (str) – Extension of file to save. Default .html.
  • verbose (bool or None.) – If True print status messages. If None use tabler.tabletype.BaseTableType.verbose.
write(table: Table, path: Union[str, Path]) → None

Save data from tabler.Table to file.

:param table:”Table” to save. :type table: tabler.Table :param path: Path to file to be opened. :type path: str, pathlib.Path or compatible.

class tabler.ODS(sheet: int = 0, extension: str = '.ods', verbose: bool = True)

Table Type for Open Document Format (.ods) files.

Parameters:
  • extension (str) – Extension of file to save. Default .ods.
  • verbose (bool or None.) – If True print status messages. If None use tabler.tabletype.BaseTableType.verbose.
open_path(path: Union[str, Path]) → Tuple[List[str], List[List[Any]]]

Return header and rows from file.

Parameters:path (str, pathlib.Path or compatible.) – Path to file to be opened.
write(table: Table, path: Union[str, Path]) → None

Save data from tabler.Table to file.

:param table:”Table” to save. :type table: tabler.Table :param path: Path to file to be opened. :type path: str, pathlib.Path or compatible.

class tabler.XLSX(extension: str = '.xlsx', verbose: bool = True)

Table Type for Microsft Excel (.xlsx) files.

Parameters:
  • extension (str) – Extension of file to save. Default .xlsx.
  • verbose (bool or None.) – If True print status messages. If None use tabler.tabletype.BaseTableType.verbose.
open_path(path: Union[str, Path]) → Tuple[List[str], List[List[Any]]]

Return header and rows from file.

Parameters:path (str, pathlib.Path or compatible.) – Path to file to be opened.
write(table: Table, path: Union[str, Path]) → None

Save data from tabler.Table to file.

:param table:”Table” to save. :type table: tabler.Table :param path: Path to file to be opened. :type path: str, pathlib.Path or compatible.

tabler.Table

class tabler.Table(filepath: Optional[str] = None, table_type: Optional[tabler.tabletypes.basetabletype.BaseTableType] = None, header: Optional[Collection[str]] = None, data: Optional[Iterable[T_co]] = None)

A wrapper object for tabulated data.

Allows access to and manipulation of tablulated data. This data can be input directly or loaded from a file. Data can also be writen data to a file. Table rows are encapsulated with the tabler.tablerow.TableRow class.

Different filetypes can be read and written by providing a subclass of tabler.tabletypes.BaseTableType which implements the open and write methods.

A filename can be provided to open an existing file. An apropriate tabler.tabletypes.BaseTableType object can be provided to specify how the file will be opened. If this is not specified one will be selected based on the file extension in the filename using default parameters.

Alternatively header and data can be specified to populate the table directly.

Parameters:
  • table_type (tabler.tabletypes.BaseTableType) – Table Type to use to open a file referenced by filetype.
  • filepath (str) – Path to file to be opened.
  • header (list) – List of column headers to be used if not loaded from file.
  • data (list(list(str, int or float))) – Two dimensional list. Each list will form a row of cell data.
Raises:

ValueError – If filepath is None or both header and data are None.

append(row: Union[Iterable[T_co], tabler.tablerow.TableRow]) → None

Add new row to table.

Parameters:row (list or tabler.tablerow.TableRow.) – Data for new row.
copy() → tabler.table.Table

Return duplicate Table object.

empty() → None

Clear all data.

get_column(column: Union[int, str]) → List[T]

Return all values in a column.

Parameters:column (str or int.) – Name or index of to be returned.
Return type:list
is_empty() → bool

Return True if the table conatins no data, otherwise return False.

Return type:bool
load(header: Collection[T_co], data: Iterable[Union[Iterable[T_co], tabler.tablerow.TableRow]]) → None

Populate table with header and data.

Parameters:
  • header (list) – Names of column headers.
  • data (list(list(str, int or float))) – Rows of data. Each row must be a list of cell values
print_r() → None

Print table data in a readable format.

remove_column(column: str) → None

Remove a specified column from the Table.

Parameters:column (str or int.) – Name or index of to be removed.
sort(sort_key: str, asc: bool = True) → None

Sort table by column.

Parameters:
  • sort_key (str or int) – Column header or index of column to sort by.
  • asc (bool) – If True Table will be sorted in ascending order. Otherwise order will be descending. (Default: True)
sorted(sort_key: str, asc: bool = True) → tabler.table.Table

Return a sorted duplicate of the Table.

Parameters:
  • sort_key (str or int) – Column header or index of column to sort by.
  • asc (bool) – If True Table will be sorted in ascending order. Otherwise order will be descending. (Default: True)
Return type:

tabler.Table.

split_by_row_count(row_count: int) → List[tabler.table.Table]

Split table by row count.

Create multiple tabler.Table instances each with a subset of this one’s data.

Parameters:row_count (int) – Number of rows in each Table.
Return type:list(tabler.Table).
write(filepath: Union[str, pathlib.Path], table_type: Optional[tabler.tabletypes.basetabletype.BaseTableType] = None) → None

Create file from table.

Parameters:
  • table_type (tabler.BaseTableType) – Table Type to use to save the file.
  • filepath (str) – Path at which the file will be saved.

tabler.tablerow.TableRow

class tabler.tablerow.TableRow(row: List[List[Any]], header: Iterable[str])

Provide methods for rows in tabler.Table instances.

copy() → tabler.tablerow.TableRow

Return duplicate tabler.tablerow.TableRow object.

Return type:tabler.tablerow.TableRow.
remove_column(column: str) → None

Remove the passed column.

Parameters:column (str) – Header for column to be removed.
Raises:ValueError: If column is not a valid column header.