fluentfs.common.table.Table

class fluentfs.common.table.Table(cols: Union[Sequence[str], Mapping[str, Sequence[Any]]])

Bases: object

__init__(cols: Union[Sequence[str], Mapping[str, Sequence[Any]]]) None

Initialize an empty table with the given column names.

Parameters:

cols – A sequence of column names. Each column name should be a string. Note that the order of the column names matters since columns can be accessed by index.

Methods

__init__(cols)

Initialize an empty table with the given column names.

add_row(row)

Add a row to the table.

add_rows(rows)

col(col_id)

Get (the values of) a column by its name or index.

col_name(idx)

Get the name of a column from its index.

row(idx[, return_mapping])

Get a row.

value(row_idx, col_id)

Get a value from the table by row and column.

Attributes

col_names

Dict[str, str] The names of the table columns.

n_cols

The number of columns.

n_rows

The number of rows.

add_row(row: Union[Mapping[str, Any], Sequence[Any]]) None

Add a row to the table.

>>> table = Table(["Country", "Capital", "Language"])
>>> table.add_row(["Germany", "Berlin", "German"])
>>> table.add_row({"Language": "French", "Capital": "Paris", "Country": "France"})
>>> table
| Country  | Capital  | Language |
| ________ | ________ | _________|
| Germany  | Berlin   | German   |
| ________ | ________ | _________|
| France   | Paris    | French   |
Parameters:

row – The row. It must be either a sequence of values or a mapping of column names to values. If the row is a sequence of values, each value will be inserted into the column with the corresponding index.

col(col_id: Union[str, int]) List[Any]

Get (the values of) a column by its name or index.

>>> table = Table({
...     "Country": ["Germany", "France"],
...     "Capital": ["Berlin", "Paris"],
...     "Language": ["German", "French"]
... })
>>> table.col(0)
['Germany', 'France']
>>> table.col("Country")
['Germany', 'France']
>>> table.col(1)
['Berlin', 'Paris']
>>> table.col("Capital")
['Berlin', 'Paris']
>>> table.col(2)
['German', 'French']
>>> table.col("Language")
['German', 'French']
Parameters:

col_id – The column identifier. If this is a string, then col_id is assumed to be the name of the column. If this is an integer, then col_id is assumed to be the index of the column.

Returns:

A list containing the values of the column.

col_name(idx: int) str

Get the name of a column from its index.

>>> table = Table({
...     "Country": ["Germany", "France"],
...     "Capital": ["Berlin", "Paris"],
...     "Language": ["German", "French"]
... })
>>> table.col_name(0)
'Country'
>>> table.col_name(1)
'Capital'
>>> table.col_name(2)
'Language'
Parameters:

idx – The column index.

Returns:

The column name.

property col_names: List[str]

Dict[str, str] The names of the table columns.

Note that the names are returned in the order of the columns.

>>> table = Table({
...     "Country": ["Germany", "France"],
...     "Capital": ["Berlin", "Paris"],
...     "Language": ["German", "French"]
... })
>>> table.col_names
['Country', 'Capital', 'Language']
property n_cols: int

The number of columns.

>>> table = Table({
...     "Country": ["Germany", "France"],
...     "Capital": ["Berlin", "Paris"],
...     "Language": ["German", "French"]
... })
>>> table.n_cols
3
property n_rows: int

The number of rows.

>>> table = Table({
...     "Country": ["Germany", "France"],
...     "Capital": ["Berlin", "Paris"],
...     "Language": ["German", "French"]
... })
>>> table.n_rows
2
row(idx: int, return_mapping: bool = False) Union[List[str], Dict[str, Any]]

Get a row.

Note that values of the row will be ordered according to the order of the columns.

>>> table = Table({
...     "Country": ["Germany", "France"],
...     "Capital": ["Berlin", "Paris"],
...     "Language": ["German", "French"]
... })
>>> table.row(idx=0)
['Germany', 'Berlin', 'German']
>>> table.row(idx=1)
['France', 'Paris', 'French']
>>> table.row(idx=0, return_mapping=True)
{'Country': 'Germany', 'Capital': 'Berlin', 'Language': 'German'}
>>> table.row(idx=1, return_mapping=True)
{'Country': 'France', 'Capital': 'Paris', 'Language': 'French'}
Parameters:
  • idx – The row index.

  • return_mapping – If this is True, return a list of row values. If this is False, return a dictionary mapping column values to row values. Note that the dictionary is ordered according to the order of the columns.

Returns:

A list containing the row values.

value(row_idx: int, col_id: Union[str, int]) Any

Get a value from the table by row and column.

>>> table = Table({
...     "Country": ["Germany", "France"],
...     "Capital": ["Berlin", "Paris"],
...     "Language": ["German", "French"]
... })
>>> table.value(row_idx=1, col_id=2)
'French'
>>> table.value(row_idx=1, col_id="Language")
'French'
Parameters:
  • row_idx – The index of the row.

  • col_id – The column identifier. If this is a string, then col_id is assumed to be the name of the column. If this is an integer, then col_id is assumed to be the index of the column.

Returns:

The value residing at row with index row_idx and column with identifier col_id.