4.7. The Cols class

This class is used as an accessor to the table columns following the natural name convention, so that you can access the different columns because there exists one attribute with the name of the columns for each associated column, which can be a Column instance (non-nested column) or another Cols instance (nested column).

Columns under a Cols accessor can be accessed as attributes of it. For instance, if table.cols is a Cols instance with a column named col1 under it, the later can be accessed as table.cols.col1. If col1 is nested and contains a col2 column, this can be accessed as table.cols.col1.col2 and so on and so forth.

4.7.1. Cols instance variables

_v_colnames

A list of the names of the columns (or nested columns) hanging directly from this Cols instance. The order of the names matches the order of their respective columns in the containing table.

_v_colpathnames

A list of the complete pathnames of the columns hanging directly from this Cols instance. If the table does not contain nested columns, this is exactly the same as _v_colnames attribute.

_v_table

The parent Table instance.

_v_desc

The associated Description 4.9 instance.

4.7.2. Cols methods

4.7.2.1. _f_col(colname)

Return a handler to the colname column. If colname is a nested column, a Cols instance is returned. If colname is a non-nested column a Column object is returned instead.

4.7.2.2. __getitem__(key)

Get a row or a range of rows from the Cols accessor.

If the key argument is an integer, the corresponding Cols row is returned as a tables.nestedrecords.NestedRecord object. If key is a slice, the range of rows determined by it is returned as a tables.nestedrecords.NestedRecArray object.

Using a string as key to get a column is supported but deprecated. Please use the col() (see 4.6.2) method.

Example of use:


record = table.cols[4]  # equivalent to table[4]
recarray = table.cols.Info[4:1000:2]
	      

Those statements are equivalent to:


nrecord = table.read(start=4)[0]
nrecarray = table.read(start=4, stop=1000, step=2).field('Info')
	      

Here you can see how a mix of natural naming, indexing and slicing can be used as shorthands for the read() (see 4.6.2) method.

4.7.2.3. __setitem__(key)

Set a row or a range of rows to the Cols accessor.

If the key argument is an integer, the corresponding Cols row is set to the value object. If key is a slice, the range of rows determined by it is set to the value object.

Example of use:


table.cols[4] = record
table.cols.Info[4:1000:2] = recarray

	      

Those statements are equivalent to:


table.modifyRows(4, rows=record)
table.modifyColumn(4, 1000, 2, colname='Info', column=recarray)

	      

Here you can see how a mix of natural naming, indexing and slicing can be used as shorthands for the modifyRows() and modifyColumn() (see 4.6.2 and 4.6.2) methods.