mlrl.testbed.experiments.table module

Author Michael Rapp (michael.rapp.ml@gmail.com)

Provides classes for representing tables.

class mlrl.testbed.experiments.table.Alignment(*values)

Bases: Enum

All possible alignments of a table column.

AUTO = None
CENTER = 'center'
LEFT = 'left'
RIGHT = 'right'
class mlrl.testbed.experiments.table.Column

Bases: Iterable[Any | None], ABC

Provides access to a single column of a table.

abstract property alignment: Alignment | None

The alignment of the column.

abstract property header: Any | None

The header of the column.

abstract property num_rows: int

The number of rows in the table.

abstractmethod set_header(value: Any | None)

Sets the header of the column to a given value.

Parameters:

value – The value to be set

class mlrl.testbed.experiments.table.ColumnWiseTable

Bases: Table

A table to which new columns can be added one by one.

class Column(table: ColumnWiseTable, column_index: int)

Bases: Column

Provides access to a single column of a ColumnWiseTable.

property alignment: Alignment | None

The alignment of the column.

property header: Any | None

The header of the column.

property num_rows: int

The number of rows in the table.

set_header(value: Any | None)

Sets the header of the column to a given value.

Parameters:

value – The value to be set

class HeaderRow(table: ColumnWiseTable)

Bases: HeaderRow

Provides access to the headers of a ColumnWiseTable.

property num_columns: int

The number of columns in the table.

class Row(table: ColumnWiseTable, row_index: int)

Bases: Row

Provides access to a single row of a ColumnWiseTable.

property num_columns: int

The number of columns in the table.

add_column(*values: Any | None, header: Any | None = None, alignment: Alignment | None = None, position: int = -1) ColumnWiseTable

Adds a new column to a table at a specific position.

Parameters:
  • values – The values in the column

  • header – The header of the column or None

  • alignment – The alignment of the column or None

  • position – The position, the column should be added at, or -1, if the column should be added at the end

Returns:

The modified table

property alignments: list[Alignment | None] | None

The individual alignments of the columns in the table or None, if no alignments have been specified.

property columns: Generator[Column, None, None]

A generator that provides access to the individual columns in the table.

property header_row: HeaderRow | None

The header row of the table or None, if the table does not have any headers.

property num_columns: int

The number of columns in the table.

property num_rows: int

The number of rows in the table.

property rows: Generator[Row, None, None]

A generator that provides access to the individual rows in the table.

slice(*column_indices: int) ColumnWiseTable

Creates and returns a copy of this table that contains the columns with specific indices in the given order.

Parameters:

column_indices – The indices of the columns to be sliced

Returns:

The table that has been created

sort_by_headers() ColumnWiseTable

Sorts the columns in the table by their headers.

Returns:

The sorted table

to_column_wise_table() ColumnWiseTable

Creates and returns a ColumnWiseTable from this table.

Returns:

The ColumnWiseTable that has been created

to_row_wise_table() RowWiseTable

Creates and returns a RowWiseTable from this table.

Returns:

The RowWiseTable that has been created

class mlrl.testbed.experiments.table.HeaderRow

Bases: Iterable[Any | None], ABC

Provides access to the headers of a table.

abstract property num_columns: int

The number of columns in the table.

class mlrl.testbed.experiments.table.Row

Bases: Iterable[Any | None], ABC

Provides access to a single row of a table.

abstract property num_columns: int

The number of columns in the table.

class mlrl.testbed.experiments.table.RowWiseTable(*headers: Any | None, alignments: list[Alignment | None] | None = None)

Bases: Table

A table to which new rows can be added one by one.

class Column(table: RowWiseTable, column_index: int)

Bases: Column

Provides access to a single column of a RowWiseTable.

property alignment: Alignment | None

The alignment of the column.

property header: Any | None

The header of the column.

property num_rows: int

The number of rows in the table.

set_header(value: Any | None)

Sets the header of the column to a given value.

Parameters:

value – The value to be set

class HeaderRow(table: RowWiseTable)

Bases: HeaderRow

Provides access to the headers of a RowWiseTable.

property num_columns: int

The number of columns in the table.

class Row(table: RowWiseTable, row_index: int)

Bases: Row

Provides access to a single row of a RowWiseTable.

property num_columns: int

The number of columns in the table.

add_row(*values: Any | None, position: int = -1)

Adds a new row at a specific position of the table.

Parameters:
  • values – The values in the row

  • position – The position where the row should be added or -1, if the row should be added at the end

Returns:

The modified table

static aggregate(*tables: Table) RowWiseTable

Creates and returns a RowWiseTable that contains all rows of the given tables. The given tables must all have the same headers.

Parameters:

tables – The tables to aggregate

Returns:

A RowWiseTable that contains all rows of the given tables

property alignments: list[Alignment | None] | None

The individual alignments of the columns in the table or None, if no alignments have been specified.

property columns: Generator[Column, None, None]

A generator that provides access to the individual columns in the table.

copy() RowWiseTable

Creates and returns a copy of this table.

Returns:

The copy that has been created

property header_row: HeaderRow | None

The header row of the table or None, if the table does not have any headers.

property num_columns: int

The number of columns in the table.

property num_rows: int

The number of rows in the table.

property rows: Generator[Row, None, None]

A generator that provides access to the individual rows in the table.

sort_by_columns(column_index: int, *additional_column_indices: int, descending: bool = False) RowWiseTable

Sorts the rows in the table by the values in one or several columns.

Parameters:
  • column_index – The index of the column to sort by

  • additional_column_indices – Additional indices of columns to sort by

  • descending – True, if the rows should be sorted in descending order, False, if they should be sorted in ascending order

Returns:

The sorted table

to_column_wise_table() ColumnWiseTable

Creates and returns a ColumnWiseTable from this table.

Returns:

The ColumnWiseTable that has been created

to_row_wise_table() RowWiseTable

Creates and returns a RowWiseTable from this table.

Returns:

The RowWiseTable that has been created

class mlrl.testbed.experiments.table.Table

Bases: ABC

An abstract base class for all tables.

class Format(*values)

Bases: StrEnum

All formats that can be used for formatting tables.

OUTLINE = 'simple_outline'
PLAIN = 'plain'
SIMPLE = 'simple'
abstract property alignments: list[Alignment | None] | None

The individual alignments of the columns in the table or None, if no alignments have been specified.

abstract property columns: Generator[Column, None, None]

A generator that provides access to the individual columns in the table.

format(auto_rotate: bool = True, table_format: Format | None = None, separator_indices: list[int] | None = None) str

Creates and returns a textual representation of the table.

Parameters:
  • auto_rotate – True, if tables with a single row should automatically be rotated for legibility, False otherwise

  • table_format – The format that should be used for formatting the table or None, if the default should be used

  • separator_indices – A list that contains the indices of the row at which a separator should be inserted, or None if no separators should be inserted

Returns:

The textual representation that has been created

property has_headers: bool

True, if the table has any headers, False otherwise.

abstract property header_row: HeaderRow | None

The header row of the table or None, if the table does not have any headers.

property num_cells: int

The number of cells in the table.

abstract property num_columns: int

The number of columns in the table.

abstract property num_rows: int

The number of rows in the table.

abstract property rows: Generator[Row, None, None]

A generator that provides access to the individual rows in the table.

abstractmethod to_column_wise_table() ColumnWiseTable

Creates and returns a ColumnWiseTable from this table.

Returns:

The ColumnWiseTable that has been created

abstractmethod to_row_wise_table() RowWiseTable

Creates and returns a RowWiseTable from this table.

Returns:

The RowWiseTable that has been created