Database

class crotal.db.Database(path, content=None)[source]

Database is the interface to the file 'db.json'.

Initalize a new database object.

Parameters
  • path -- Indicating the path of the file that this database will be written to when saving the database.
  • content -- Content of the database if needed to predefine.
Returns

None

classmethod from_file(path)[source]

Read the database from the path argument. :param path: The file path of the designated database file. :return: The database object.

get_table(table)[source]

Get a table object, same to self.__getitem__.

Parameters
table -- Name of the table acquired.
Returns
A table object.
get_item(table, key)[source]

Get the value directly from the database based on key and table name.

Parameters
  • table -- Name of the table acquired.
  • key -- Name of the key in the indicated table.
Returns

The corresponding value stored in the table.

set_item(table, key, value)[source]

Set the entry directly from the database based on key and table name.

Parameters
  • table -- Name of the table acquired.
  • key -- Name of the key in the indicated table.
Value value

Value to be set in the table.

Returns

The corresponding value stored in the table.

remove_item(table, key)[source]

Remove the entry directly from the database based on key and table name.

Parameters
  • table -- Name of the table acquired.
  • key -- Name of the key in the indicated table.
Value value

Value to be set in the table.

Returns

The corresponding value stored in the table.

dumps()[source]

Similar to json.dumps.

save()[source]

Write the content of the database to the file indicated by self._path in json format. :return:

class crotal.db.Table(table_name, content=None)[source]

Table is a wrapper of dictionary. The usage is the same as dict.