gavo.rsc.table module

Tables, base and in memory.

Basically, a table consists of a list of dictionaries (the rows) and a table definition (resdef.TableDef).

You should, in general, not construct the tables directly but use the tables.TableForDef factory. The reason is that some classes ignore certain aspects of TableDefs (indices, uniqueForceness) or may not be what TableDef requires at all (onDisk). Arguably there should be different TableDefs for all these aspects, but then I’d have a plethora of TableDef elements, which I think is worse than a factory function.

class gavo.rsc.table.BaseTable(tableDef, **kwargs)[source]

Bases: gavo.base.meta.MetaMixin, gavo.rsc.common.ParamMixin

is a container for row data.

Tables consist of rows, where each row maps column names to their value for that row. The rows are accessible at least by iterating over a table.

Tables get constructed with a tableDef and keyword arguments. For convenience, tables must accept any keyword argument and only pluck those out it wants.

Here’s a list of keywords used by BaseTables or known subclasses:

  • validateRows – have rows be validated by the tableDef before addition (all Tables)

  • rows – a list of rows the table has at start (InMemoryTables; DbTables will raise an error on these).

  • connection – a database connection to use for accessing DbTables.

  • votCasts – a dictionary mapping column names to dictionaries overriding keys of valuemappers.AnnontatedColumn.

  • params – a dictionary mapping param keys to values, where python values and literals allowed.

You can add rows using the addRow method. For bulk additions, however, it may be much more efficient to call getFeeder (though for in-memory tables, there is no advantage).

Initial Metadata is populated from the tableDef.

Tables have to implement the following methods:

  • __iter__

  • __len__

  • __getitem__(n) – returns the n-th row or raises an IndexError

  • ``removeRow(row) removes a row from the table or raises an IndexError if the row does not exist. This is a slow, O(n) operation.

  • addRow(row) – appends new data to the table

  • getRow(*args) – returns a row by the primary key. If no primary key is defined, a ValueError is raised, if the key is not present, a KeyError. An atomic primary key is accessed through its value, for compound primary keys a tuple must be passed.

  • getFeeder(**kwargs) -> feeder object – returns an object with add and exit methods. See feeder above.

  • importFinished(nAffected) -> None – called when a feeder exits successfully

  • importFailed(*excInfo) -> boolean – called when feeding has failed; when returning True, the exception that has caused the failure is not propagated.

  • close() -> may be called by clients to signify the table will no longer be used and resources should be cleared (e.g., for DBTables with private connections).

Tables also have several attributes:

  • tableDef – the TableDef that describes the table.

  • newlyCreated – true unless a pre-existing database table was kept (e.g., in updating DDs).

addRow(*args, **kwargs)
getFeeder(*args, **kwargs)
getRow(*args, **kwargs)
removeRow(*args, **kwargs)

raises a ValidationError if any required parameters of this tables are None.

class gavo.rsc.table.ColumnStat[source]

Bases: object

Column statistics as exposed by Limits.

These have min, max, and values attributes, all of which can be None. Otherwise, min and max are values of the column type, values is a set of those.

exception gavo.rsc.table.Error(msg='', hint=None)[source]

Bases: gavo.utils.excs.Error

class gavo.rsc.table.InMemoryIndexedTable(tableDef, **kwargs)[source]

Bases: gavo.rsc.table.InMemoryTable

is an InMemoryTable for a TableDef with a primary key.

class gavo.rsc.table.InMemoryTable(tableDef, **kwargs)[source]

Bases: gavo.rsc.table.BaseTable

is a table kept in memory.

This table only keeps an index for the primary key. All other indices are ignored.


returns a limits instance for this table.

This is a characterisation of the ranges of things in this table, pretty much as what dachs info does; if you fix things here, you probably want to fix things there, too.

class gavo.rsc.table.Limits(rows, minmaxColumns, enumColumns)[source]

Bases: dict

Column statistics (min/max, values) for an in-memory table.

These are constructed with the rows attribute and a list each for columns for which you want min/max and the values present. Note that None in min/max indicates no non-None values were found. An empty set in values indicates that all values were None.

This then exposes a dictionary interface

class gavo.rsc.table.UniqueForcedTable(tableDef, **kwargs)[source]

Bases: gavo.rsc.table.InMemoryIndexedTable

is an InMemoryTable with an enforced policy on duplicate primary keys.

See resdef.TableDef for a discussion of the policies.