:mod:`database` -- btree database ================================= .. module:: database :synopsis: provides a simplified interface to a database backed by a ``btree`` module. Classes ------- .. class:: DataBase(filename='/var/upy.db') Opens (or creates if does not exist) a database from a file. If the ``btree`` module does not exist, or the initialization fails, a fallback database (backed by a dictionary) is created. .. method:: close() Closes the database and frees all the resources. When the database is not needed any more, do not forget to call this method to prevent data corruption. No further calls are possible after the invocation of this method. .. method:: __contains__(key): Check whether the ``key`` is in the database. .. method:: clean(): Remove all entries from the database. .. method:: add(key, value): Add the key-value pair to the database. The value must be a bytes-like object. .. method:: remove(key): Removes the key from the database. Returns ``True`` if the key was present in the database and has been removed. Otherwise, returns ``False``. .. method:: get(key) Return the value associated with the key. .. method:: pop(key) The combination of ``get(key)`` with the following ``remove(key)``. .. method:: get_str(key) Return the value for the ``key`` as a string. .. method:: contains(key) The same as ``key in self`` .. method:: empty() Checks if the database is empty.