tkp.db.database – Database connection interface

class tkp.db.database.DBExceptions(engine)[source]

This provides an engine-agnostic wrapper around the exceptions that can the thrown by the database layer: we can refer to eg DBExcetions(engine).Error rather than <engine specific module>.Error.

We handle both the PEP-0249 exceptions as provided by the DB engine, and add our own as necessary.

class tkp.db.database.Database(**kwargs)[source]

An object representing a database connection.

close()[source]

close the connection if open

connect()[source]

connect to the configured database

connection

The database connection, will be created if it doesn’t exists.

This is a property to be backwards compatible with the rest of TKP.

Returns:a database connection
vacuum(table)[source]

Force a vacuum on a table, which removes dead rows. (Postgres only)

Normally the auto vacuum process does this for you, but in some cases (for example when the table receives many insert and deletes) manual vacuuming is necessary for performance reasons.

Parameters:table – name of the table in the database you want to vacuum
tkp.db.database.sanitize_db_inputs(params)[source]

Replace values in params with alternatives suitable for database insertion.

That includes:

  • Convert numpy.floating types into Python floats;
  • Convert infs into the string “Infinity”.
Parameters:params (dict/list/tuple) – (Potentially) dirty database inputs
Returns:cleaned – Sanitized database inputs
Return type:dict/list/tuple