tableread package

Tableread package to read a text file table into a Python object.

class tableread.BaseRSTDataObject[source]

Bases: object

Base Class for RST Table Data handling.

column_default_separator = '='
column_divider_char = ' '
comment_char = '#'
data_format: Any = None
header_divider = '='
header_markers = {'!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~'}
exception tableread.FileParsingException[source]

Bases: Exception

Exception for parsing failures.

exception tableread.InvalidFileException[source]

Bases: Exception

Exception for improperly formatted files.

class tableread.SimpleRSTReader(rst_source)[source]

Bases: tableread.BaseRSTDataObject

Represent all tables found in a RST file.

Determine from where to parse RST content and then parse it.

Parameters

rst_source (str) – The source of the RST content to parse. This can either be a file path with a .rst extension, or a string containing the RST content.

data_format

alias of collections.OrderedDict

property first

Return the first table found in the document.

property tables

Get the list of table names found in the document.

class tableread.SimpleRSTTable(divider_row, header, rows)[source]

Bases: tableread.BaseRSTDataObject

Represent a single table from a RST file.

Build a table from the text string parts.

Parameters
  • divider_row (str) – the row above or below the table headers, consisting solely of “=” and spaces, that delineates the column boundaries.

  • header (str) – the column header row

  • rows (List[str]) – the rows within the table containing data

data_format

alias of builtins.list

exclude_by(**kwargs)[source]

Filter data to exclude items matching conditions.

Given a set of key/value filters, returns a new TableRead object without the matching data, that can be iterated over. Kwarg values may be a simple value (str, int) or a function that returns a boolean.

Note: When filtering both keys and values are not case sensitive.

classmethod from_data(data)[source]

Given data, build a SimpleRSTTable object.

get_fields(*fields)[source]

Get only specified fields from data.

Given a set of fields, returns a list of those field values from each entry. A single field will return a list of values, Multiple fields will return a list of tuples of values.

matches_all(**kwargs)[source]

Filter data for a positive match to conditions.

Given a set of key/value filters, returns a new TableRead object with the filtered data, that can be iterated over. Kwarg values may be a simple value (str, int) or a function that returns a boolean.

Note: When filtering both keys and values are not case sensitive.

tableread.get_specific_attr_matcher(key, value)[source]

Check if a given attribute value matches the expected value.

Parameters
  • key (str) – the name of the attribute to check

  • value (str) – the expected string value

Returns

a checker that will accept an object and return True if the attribute value matches, or False if not.

Return type

function

tableread.safe_list_index(a_list, index_value, default=None)[source]

Return the value at the given index, or a default if index does not exist.

Parameters
  • a_list (list) – the list to be indexed

  • index_value (int) – the desired index position from the list

  • default (Optional[Any]) – the default value to return if the given index does not exist

Returns

the value at the list index position or the default

Return type

any

Submodules

tableread.writer module

Tableread module to write a text file from a Python object.

class tableread.writer.SimpleRSTTableWriteable(title, row_data)[source]

Bases: object

Single rST table object to be written.

property divider

Format divider row as a spaced string.

divider_char = '='
property headers

Headers for the table, formatted as a spaced string.

title_marker = '~'
write_table(writer)[source]

Write table out to file using the provided writer.

Parameters

writer (TextIOBase) – file-like object to be written to

class tableread.writer.SimpleRSTWriter(file_path, *tables)[source]

Bases: object

Write a .rst file from a list of tables.

Accept a list of table information and write to file.

Parameters
  • file_path (str) – the path to write the output file

  • tables (Tuple[str, List[dict]]) – Each a tuple of table title (str) and list of row dicts

write_tables()[source]

Write provided tables out to .rst file.