Package csb :: Package io :: Module tsv :: Class Table

[frames] | no frames]

# Class Table

source code

    object --+
             |
            Table

Build and query a TSV Table. See the documentation of csb.io.tsv for details and examples.

Instance Methods

__array__(self) | source code

__del__(self) | source code

__getitem__(self, i) | source code

__getstate__(self) | source code

__init__(self, definition, name=`'``TSV``'`, backend=<class 'csb.io.tsv.SQLiteRepository'>) x.__init__(...) initializes x; see help(type(x)) for signature | source code

__iter__(self) | source code

__len__(self) | source code

__setitem__(self, i, value) | source code

__setstate__(self, state) | source code

column(self, column) Returns: a list all values in the specified column | source code

dump(self, file) Dump the table in a file. | source code

insert(self, row) Insert a new row in the table. | source code

iterable (2D)

query(self, sql, params=None) Execute a native SQL query against the storage engine. | source code

scalar(self, row=None, column=None) Returns: a scalar value at the specified row and column. | source code

select(self, *columns) Returns: a new Table | source code

update(self, column, value) Update `column` for all rows in the table. | source code

where(self, column) | source code

Inherited from `object`: `__delattr__`, `__format__`, `__getattribute__`, `__hash__`, `__new__`, `__reduce__`, `__reduce_ex__`, `__repr__`, `__setattr__`, `__sizeof__`, `__str__`, `__subclasshook__`

Static Methods

Table

from_iterable(iterable, definition, name=`'``TSV``'`, backend=<class 'csb.io.tsv.SQLiteRepository'>) Table factory: build a Table from a 2D iterable/data reader. | source code

Table

from_table(table, data=False, name=`'``TSV``'`, backend=<class 'csb.io.tsv.SQLiteRepository'>) Table factory: build a Table with the definition of another Table. | source code

Table

from_tsv(tsv, definition=None, delimiter=`'``\t``'`, skip=0, name=`'``TSV``'`, backend=<class 'csb.io.tsv.SQLiteRepository'>) Table factory: build a Table from a TSV file. | source code

Class Variables
| HEADER = ``'``# @TSV` `'``
| NULL = ``'````'``

Properties
tuple | columns All column names
str | name Table name
| pk
Inherited from `object`: `__class__`

Method Details

### __init__(self, definition, name=`'``TSV``'`, backend=<class 'csb.io.tsv.SQLiteRepository'>)
(Constructor)

source code

x.__init__(...) initializes x; see help(type(x)) for signature

Parameters:
  - `definition` (str, tuple of ColumnInfo) - column definition string: `ColA:typeA colB:typeB ...`, where `ColN` is a column name and `typeN` is one of the base Python data types: str, int, long, float. Alternatively, the table definition may be specified directly as a list of metadata objects.
  - `name` (str) - name of the table on the SQL backend. Useful when you need to execute native SQL queries against the table.
  - `backend` (type (reference to a RepositoryImp subclass)) - table backend storage engine. This must be a proper RepositoryImp bridge implementor.
Raises:
  - `UnsupportedTypeError` - when an unsupported type is used in the table `definition`
  - `ValueError` - if the `definition` is not valid
Overrides: object.__init__
  (inherited documentation)

### column(self, column)

source code

Parameters:
  - `column` (str) - column to fetch
Returns:
  a list all values in the specified column

### dump(self, file)

source code

Dump the table in a file.

Parameters:
  - `file` (file (stream) or str (filename)) - destination stream or filename

### from_iterable(iterable, definition, name=`'``TSV``'`, backend=<class 'csb.io.tsv.SQLiteRepository'>)
Static Method

source code

Table factory: build a Table from a 2D iterable/data reader.

Parameters:
  - `iterable` (iterable (2D)) - data container
  - `definition` (str, tuple of ColumnInfo) - table column definition (see Table).
Returns: Table

### from_table(table, data=False, name=`'``TSV``'`, backend=<class 'csb.io.tsv.SQLiteRepository'>)
Static Method

source code

Table factory: build a Table with the definition of another Table.

Parameters:
  - `table` (Table) - template table
  - `data` (bool) - if True, also copy the data from the source `table`
Returns: Table

### from_tsv(tsv, definition=None, delimiter=`'``\t``'`, skip=0, name=`'``TSV``'`, backend=<class 'csb.io.tsv.SQLiteRepository'>)
Static Method

source code

Table factory: build a Table from a TSV file.

Parameters:
  - `tsv` (str) - TSV path and filename. This can be either a conventional TSV/CSV file, or a file created with `table.dump(tsv)`
  - `definition` (str, tuple of ColumnInfo) - table column definition (see Table). If defined, this parameter will determine the structure of the table. Otherwise, the table definition will be extracted from the TSV header. If the file contains no TSV header, this parameter is mandatory.
  - `delimiter` (str) - column separator used in the file
  - `skip` (int) - skip the first N number of rows (the header can still be extracted from those however)
Returns: Table
Raises:
  - `ValueError` - if neither a table `definition` is provided, nor the `tsv` file has a header line

### insert(self, row)

source code

Insert a new row in the table.

Parameters:
  - `row` (tuple) - a tuple of the appropriate length

### query(self, sql, params=None)

source code

Execute a native SQL query against the storage engine.

Parameters:
  - `sql` - SQL query text. May contain parameter binding placeholders (see Predicate.PH). The SQL dialect of the query depends on the SQL `backend` being used by the table.
Returns: iterable (2D)
  native data reader

### scalar(self, row=None, column=None)

source code

Parameters:
  - `row` (int) - row index; if not specified - take the first row
  - `column` (str) - column name; if not specified - take the first
Returns:
  a scalar value at the specified row and column.
Raises:
  - `IndexError` - when an invalid row is requested
  - `InvalidColumnError` - when an invalid column is requested

### select(self, *columns)

source code

Parameters:
  - `columns` (str, tuple of str) - column names; defaults to all columns
Returns:
  a new Table
Raises:
  - `InvalidColumnError` - when an invalid column is requested

### update(self, column, value)

source code

Update `column` for all rows in the table.

Parameters:
  - `column` (str) - column to update (name)
  - `value` - new column value
Raises:
  - `InvalidColumnError` - when an invalid column is referenced

### where(self, column)

source code

Parameters:
  - `column` (str) - column name
Raises:
  - `InvalidColumnError` - when an invalid column is requested

Property Details

### columns

All column names

Get Method:
  unreachable.columns(self) - All column names
Type:
  tuple

### name

Table name

Get Method:
  unreachable.name(self) - Table name
Type:
  str

### pk

Get Method:
  unreachable.pk(self)