Package csb :: Package io :: Module tsv |
[frames] | no frames]

# Module tsv

source code

Read, query and update textual tables via flexible SQL interface.

Tables can be created and populated with data from scratch, built from TSV files, 2D lists or other tables. Once the data is loaded in memory, each storage operation on the table object is delegated via bridge to an SQL storage backend (by default this is SQLite). However the table uses the backend only as a temp storage to ensure maximum portability of the data. Tables can be stored persistently as text (TSV) files and then loaded back in memory when needed.

These Tables can be queried and updated in a vast number of ways; each query returns a new Table:

1. Using slice expressions. The general form of a slice expression is `[rows, columns]`, where `rows` can be:
   - a row index, 0-based, e.g. `5`
   - a tuple of row indices, e.g. `(1, 3, 6)`
   - a standard Python slice, e.g. `1:3` or `:5` or `:`
   - omitted (means: all rows)

   and `columns` can be:

   - a column index, 0-based, e.g. `5`
   - a tuple of columns indices, 0-based
   - a column name, e.g. `'TmScore'`
   - a tuple of column names, e.g. `('ID', 'TmScore')`
   - a standard Python slice using column indices
   - a slice using column names, e.g. `'ID':'TM'` or `:'TM'` or `:`
   - omitted (means: all columns)
2. Using query expressions, for example:

       >>> table.where('ID').between(1, 5).select('TmScore', 'RMSD')
       Table ('TmScore', 'RMSD')

       >>> table.where('ID').between(1, 5).update('RMSD', 0.2)
       Table (the same table)

3. With SQL queries:

       >>> t.query(r'''SELECT  ColumnB * ColumnA AS ComputedValue
                       FROM    {0.name}
                       WHERE   ColumnC IN ({1}, {1})'''.format(t, Predicate.PH),
                   [12, 55])
       iterable

The data contained in a Table can be extracted in several ways:

- if you need a single (scalar) value -- with the `table[row, column]` indexing expression or with the dedicated `table.scalar(row, column)` method.
- by treating the table as an iterator; each cycle will then yield a DataRow object
- with text (TSV) serialization: simply call `table.dump(file)`.

See Table for full API details.

Classes
| Between
| ColumnInfo Holder object for column metadata.
| DataRow Represents a table data row.
| Equals
| Expression Metadata container: represents a table select or update expression.
| Greater
| GreaterOrEquals
| In
| InvalidColumnError
| Lower
| LowerOrEquals
| NotEquals
| NotIn
| Operator
| Predicate
| RepositoryImp Abstract SQL backend interface.
| SQLiteRepository SQLite-based concrete repository implementor.
| Step
| Table Build and query a TSV Table.
| UnsupportedTypeError
| Where