Table Types¶
tabletypes package.
The tabletypes package provides the tabler.tabletypes.BaseTableType
class which can be subclassed to provided open and write methods to
tabler.Table
.
They can be customised by providing paramaters to the __init__ method.
Also provides subclasses of table.tabletypes.BaseTableType
for
common file types.
tabler.tabletypes.CSV
Open and write .csv files.tabler.tabletypes.CSVURL
Open .csv over HTTP.tabler.tabletypes.HTML
: Save table as .html file.tabler.tabletypes.ODS
: Open and save Open Document- Spreadsheed (.ods) files.
tabler.tabletypes.XLSX
: Open and save Microsoft Excel (.xlsx) files.
Basic Usage:
from tabler import Table
from tabler.tabletypes import CSV
table = Table('path/to/open.csv', table_type=CSV())
table.save('path/to/save.csv', table_type=CSV())
Usage with paramaters:
from tabler import Table
from tabler.tabletypes import CSV
table = Table('path/to/open.csv', table_type=CSV(
extension='.txt', delimiter='\t', encoding='latin'))
table.save('path/to/save.csv', table_type=CSV(
extension='.txt', delimiter='\t', encoding='latin'))
Alternate Usage:
from tabler import Table
from tabler.tabletypes import CSV
csv = CSV(delimiter='\t', delimiter='\t', encoding='latin')
table = Table('path/to/open.csv', table_type=csv)
table.save('path/to/save.csv', table_type=csv)
-
class
tabler.tabletypes.
BaseTableType
(extension: str, verbose: Optional[bool] = None)¶ Base class for Table Types.
Table types are used to provide methods to
tabler.Table
to load data from files and write to files of different types.Subclasses should implement
tabler.tabletypes.BaseTableType.write()
and / ortabler.tabletypes.BaseTableType.open()
methods.Subclasses can also implement an extensions property. This is a list of file extensions matching the Table Type. It will then be used automatically when opening or writing files with a path ending in that extension if no table type is specified.
Example:
extensions = ['.csv', '.txt']
-
classmethod
get_by_extension
(extension: str) → Any¶ Get apropriate subclass of BaseTableType for file extension.
Uses
tabler.tabletypes.basetabletype.all_subclasses()
to check all subclasses of ‘tabler.tabletypes.BaseTableType’ for a matching extension in it’s extensions property.Parameters: extension (str) – File extension for which to file TableType. Raises: tabler.exceptions.ExtensionNotRecognised – If no BaseTableType subclass matching extension is found.
-
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_row
(row: List[Any]) → List[Any]¶ Return a row of parsed values.
-
parse_row_data
(rows: List[List[Any]]) → Tuple[List[str], List[List[Any]]]¶ Return header and rows.
-
parse_value
(value: Any) → Any¶ Return None if the value is empty, otherwise return str(value).
-
prepare_row
(row: List[Union[str, int, float, None]]) → List[Union[str, int, float, None]]¶ Remove excess empty values.
-
prepare_rows
(header: List[Any], rows: List[List[Any]]) → List[List[Any]]¶ Prepare rows for writing.
-
prepare_value
(value: Any) → Any¶ Prepare a value for writing.
-
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.
-
classmethod
-
class
tabler.tabletypes.
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.tabletypes.
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.tabletypes.
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.tabletypes.
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.tabletypes.
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.tabletypes.BaseTableType
¶
-
class
tabler.tabletypes.
BaseTableType
(extension: str, verbose: Optional[bool] = None) Base class for Table Types.
Table types are used to provide methods to
tabler.Table
to load data from files and write to files of different types.Subclasses should implement
tabler.tabletypes.BaseTableType.write()
and / ortabler.tabletypes.BaseTableType.open()
methods.Subclasses can also implement an extensions property. This is a list of file extensions matching the Table Type. It will then be used automatically when opening or writing files with a path ending in that extension if no table type is specified.
Example:
extensions = ['.csv', '.txt']
-
classmethod
get_by_extension
(extension: str) → Any Get apropriate subclass of BaseTableType for file extension.
Uses
tabler.tabletypes.basetabletype.all_subclasses()
to check all subclasses of ‘tabler.tabletypes.BaseTableType’ for a matching extension in it’s extensions property.Parameters: extension (str) – File extension for which to file TableType. Raises: tabler.exceptions.ExtensionNotRecognised – If no BaseTableType subclass matching extension is found.
-
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_row
(row: List[Any]) → List[Any] Return a row of parsed values.
-
parse_row_data
(rows: List[List[Any]]) → Tuple[List[str], List[List[Any]]] Return header and rows.
-
parse_value
(value: Any) → Any Return None if the value is empty, otherwise return str(value).
-
prepare_row
(row: List[Union[str, int, float, None]]) → List[Union[str, int, float, None]] Remove excess empty values.
-
prepare_rows
(header: List[Any], rows: List[List[Any]]) → List[List[Any]] Prepare rows for writing.
-
prepare_value
(value: Any) → Any Prepare a value for writing.
-
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.
-
classmethod
tabler.tabletypes.CSV
¶
-
class
tabler.tabletypes.
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.
tabler.tabletypes.CSVURL
¶
-
class
tabler.tabletypes.
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.
-
tabler.tabletypes.ODS
¶
-
class
tabler.tabletypes.
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.
tabler.tabletypes.XLSX
¶
-
class
tabler.tabletypes.
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.tabletypes.HTML
¶
-
class
tabler.tabletypes.
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.