Database#

The Database module provides classes for managing the context storage of a dialog system. The module can be used to store information such as the current state of the conversation and other data. This module includes the intermediate class (DBContextStorage) is a class that developers can inherit from in order to create their own context storage solutions. This class implements the basic functionality and can be extended to add additional features as needed.

class DBContextStorage(path)[source]#

Bases: ABC

An abstract interface for chatsky DB context storages. It includes the most essential methods of the python dict class. Can not be instantiated.

Parameters:

path (str) – Parameter path should be set with the URI of the database. It includes a prefix and the required connection credentials. Example: postgresql+asyncpg://user:password@host:port/database In the case of classes that save data to hard drive instead of external databases you need to specify the location of the file, like you do in sqlite. Keep in mind that in Windows you will have to use double backslashes ‘\’ instead of forward slashes ‘/’ when defining the file path.

full_path#

Full path to access the context storage, as it was provided by user.

path#

full_path without a prefix defining db used

_lock#

Threading for methods that require single thread access.

abstract async get_item_async(key)[source]#

Asynchronous method for accessing stored Context.

Parameters:

key (Hashable) – Hashable key used to store Context instance.

Return type:

Context

Returns:

The stored context, associated with the given key.

abstract async set_item_async(key, value)[source]#

Asynchronous method for storing Context.

Parameters:
  • key (Hashable) – Hashable key used to store Context instance.

  • value (Context) – Context to store.

abstract async del_item_async(key)[source]#

Asynchronous method for removing stored Context.

Parameters:

key (Hashable) – Hashable key used to identify Context instance for deletion.

abstract async contains_async(key)[source]#

Asynchronous method for finding whether any Context is stored with given key.

Parameters:

key (Hashable) – Hashable key used to check if Context instance is stored.

Return type:

bool

Returns:

True if there is Context accessible by given key, False otherwise.

abstract async len_async()[source]#

Asynchronous method for retrieving number of stored Contexts.

Return type:

int

Returns:

The number of stored Contexts.

get(key, default=None)[source]#

Synchronous method for accessing stored Context, returning default if no Context is stored with the given key.

Parameters:
  • key (Hashable) – Hashable key used to store Context instance.

  • default (Optional[Context]) – Optional default value to be returned if no Context is found.

Return type:

Context

Returns:

The stored context, associated with the given key or default value.

async get_async(key, default=None)[source]#

Asynchronous method for accessing stored Context, returning default if no Context is stored with the given key.

Parameters:
  • key (Hashable) – Hashable key used to store Context instance.

  • default (Optional[Context]) – Optional default value to be returned if no Context is found.

Return type:

Context

Returns:

The stored context, associated with the given key or default value.

clear()[source]#

Synchronous method for clearing context storage, removing all the stored Contexts.

abstract async clear_async()[source]#

Asynchronous method for clearing context storage, removing all the stored Contexts.

threadsafe_method(func)[source]#

A decorator that makes sure methods of an object instance are threadsafe.

context_storage_factory(path, **kwargs)[source]#

Use context_storage_factory to lazy import context storage types and instantiate them. The function takes a database connection URI or its equivalent. It should be prefixed with database name, followed by the symbol triplet ‘://’.

Then, you should list the connection parameters like this: user:password@host:port/database The whole URI will then look like this:

  • shelve://path_to_the_file/file_name

  • json://path_to_the_file/file_name

  • pickle://path_to_the_file/file_name

  • sqlite+aiosqlite://path_to_the_file/file_name

  • redis://:pass@localhost:6378/0

  • mongodb://admin:pass@localhost:27016/admin

  • mysql+asyncmy://root:pass@localhost:3306/test

  • postgresql+asyncpg://postgres:pass@localhost:5430/test

  • grpc://localhost:2134/local

  • grpcs://localhost:2134/local

For context storages that write to local files, the function expects a file path instead of connection params: json://file.json When using sqlite backend your prefix should contain three slashes if you use Windows, or four in other cases: sqlite:////file.db If you want to use additional parameters in class constructors, you can pass them to this function as kwargs.

Parameters:

path (str) – Path to the file.

Return type:

DBContextStorage