nannyml.io.db.database_writer module

class nannyml.io.db.database_writer.DatabaseWriter(connection_string: str, connection_options: Optional[Dict[str, Any]] = None, model_name: Optional[str] = None)[source]

Bases: Writer

A Writer implementation that writes a Result into a database table.

The Result class is transformed into a list of DbMetric objects by an appropriate Mapper instance. These DbMetrics are written into a database table, specific to the Result class.

This supports any database that is compatible with SQLAlchemy.

Creates a new DatabaseWriter instance.

Parameters:
  • connection_string (str) – The connection string that configures the connection to the database. Might contain user credentials as well.

  • connection_options (Dict[str, Any], default=None) – Additional options passed along to the underlying SQLAlchemy engine.

  • model_name (str, default=None) – An optional name for the model being monitored. When given this will cause a record to be created in the models table and having each DbMetric link to that one. This allows easy filtering and dropdown population in data visualization tools in case of multiple models exporting into the same database structure.

Examples

>>> # write to local in-memory database
>>> sqlite_writer = DatabaseWriter(connection_string='sqlite:///', model_name='car_loan_prediction')
>>> sqlite_writer.write(result)
>>> postgres_writer = DatabaseWriter(
...  connection_string='postgresql://postgres:mysecretpassword@localhost:5432/postgres',
...  model_name='car_loan_prediction'
... )
>>> postgres_writer.write(result)