database module

class database.Database(*args: dict, index: Optional[str] = None)

Bases: List[dict]

Class that works as a list of dictionaries with attributes for each key

Database class which inherits the properties of a list of dictionaries. It also has two extra methods and an attribute per key of the internal dictionaries. Each key attribute is a list of the values of those keys. The add_entry method is a wrapper for the append method that also appends the key values to the attributes. The search method returns the Database with only the dictionaries whose key values match the requested key values.

add_entry(entry: dict, **kwargs) dict

Wrapper for the list append method that adds contents to attributes

Wrapper for the append method with the added effect of appending to the attributes and returning the added dictionary for validation. Key word arguments may be added to add a key to the dictionary before appending it. This will be both appended and returned. Any data added that contains an index key that is already in the database will overwrite the previous database item. If the item to be overwritten is a list, the default behavior is to append to that list instead of deleting it.

Parameters
  • entry (dict) – Dictionary to be appended to the database

  • kwargs (dict) – Optional key word arguments to append to each dictionary

Returns

The appended dictionary

Return type

dict

search(get: str = 'latest', **kwargs) Union[dict, List[dict]]

Method to return a subset of the database based on a key word value

Search method for the database class. Takes a search ‘mode’ in the get parameter which can be either ‘first’, ‘all’, or ‘latest’ (default). It also takes at least one more key word argument to use as a search term. The key is the matching key in the database and the value matches the value of that key.

Parameters
  • get (str) – Determines the search mode (first, all, latest)

  • kwargs (dict) – Key value pairs to search the database for

Returns

Database subset that matches the search terms

Return type

Database