The main class of TinyDB.
Gives access to the database, provides methods to insert/search/remove and getting tables.
See Table.__exit__()
Forward all unknown attribute calls to the underlying standard table.
Create a new instance of TinyDB.
All arguments and keyword arguments will be passed to the underlying storage class (default: JSONStorage).
Parameters: | storage – The class of the storage to use. Will be initialized with args and kwargs. |
---|
Get the total number of elements in the DB.
>>> len(db)
0
Purge all tables from the database. CANNOT BE REVERSED!
Get access to a specific table.
Creates a new table, if it hasn’t been created before, otherwise it returns the cached Table object.
Parameters: |
|
---|
alias of Table
Get the names of all tables in the database.
Returns: | a set of table names |
---|---|
Return type: | set[str] |
Represents a single TinyDB Table.
Allow the database to be used as a context manager.
Returns: | the table instance |
---|
Try to close the storage after being used as a context manager.
Get access to a table.
Parameters: |
|
---|
Get the total number of elements in the table.
Get all elements stored in the table.
Returns: | a list with all elements. |
---|---|
Return type: | list[Element] |
Try to close the storage after being used as a context manager.
Check wether the database contains an element matching a condition or an ID.
If eids is set, it checks if the db contains an element with one of the specified.
Parameters: |
|
---|
Count the elements matching a condition.
Parameters: | cond (Query) – the condition use |
---|
Get exactly one element specified by a query or and ID.
Returns None if the element doesn’t exist
Parameters: |
|
---|---|
Returns: | the element or None |
Return type: | Element | None |
Insert a new element into the table.
Parameters: | element – the element to insert |
---|---|
Returns: | the inserted element’s ID |
Insert multiple elements into the table.
Parameters: | elements – a list of elements to insert |
---|---|
Returns: | a list containing the inserted elements’ IDs |
Helper function for processing all elements specified by condition or IDs.
A repeating pattern in TinyDB is to run some code on all elements that match a condition or are specified by their ID. This is implemented in this function. The function passed as func has to be a callable. It’s first argument will be the data currently in the database. It’s second argument is the element ID of the currently processed element.
Parameters: |
|
---|
Purge the table by removing all elements.
Remove all matching elements.
Parameters: |
|
---|
Search for all elements matching a ‘where’ cond.
Parameters: | cond (Query) – the condition to check against |
---|---|
Returns: | list of matching elements |
Return type: | list[Element] |
Update all matching elements to have a given set of fields.
Parameters: |
|
---|
Provides methods to do tests on dict fields.
Any type of comparison will be called in this class. In addition, it is aliased to where to provide a more intuitive syntax.
When not using any comparison operation, this simply tests for existence of the given key.
Combines this query and another with logical and.
Example:
>>> (where('f1') == 5) & (where('f2') != 2)
('f1' == 5) and ('f2' != 2)
Return type: | QueryAnd |
---|
Run the test on the element.
Parameters: | element (dict) – The dict that we will run our tests against. |
---|
Test a dict value for equality.
>>> where('f1') == 42
'f1' == 42
Test a dict value for being greater than or equal to another value.
>>> where('f1') >= 42
'f1' >= 42
Test a dict value for being greater than another value.
>>> where('f1') > 42
'f1' > 42
Negates a query.
>>> ~(where('f1') >= 42)
not ('f1' >= 42)
Return type: | tinydb.queries.QueryNot |
---|
Test a dict value for being lower than or equal to another value.
>>> where('f1') <= 42
'f1' <= 42
Test a dict value for being lower than another value.
>>> where('f1') < 42
'f1' < 42
Test a dict value for inequality.
>>> where('f1') != 42
'f1' != 42
Combines this query and another with logical or.
Example:
>>> (where('f1') == 5) | (where('f2') != 2)
('f1' == 5) or ('f2' != 2)
Return type: | QueryOr |
---|
Checks if a condition is met by any element in a list, where a condition can also be a sequence (e.g. list).
>>> where('f1').all(where('f2') == 1)
'f1' all have 'f2' == 1
Matches:
{'f1': [{'f2': 1}, {'f2': 1}]}
>>> where('f1').all([{'f2': 1}, {'f3': 2}])
'f1' all have [{'f2': 1}, {'f3': 2}]
Matches:
{'f1': [{'f2': 1}, {'f3': 2}]}
{'f1': [{'f2': 1}, {'f3': 2}, {'f4': 3}]}
Parameters: | cond – The condition to check |
---|---|
Return type: | tinydb.queries.Query |
Checks if a condition is met by any element in a list, where a condition can also be a sequence (e.g. list).
>>> where('f1').any(where('f2') == 1)
'f1' has any 'f2' == 1
Matches:
{'f1': [{'f2': 1}, {'f2': 0}]}
>>> where('f1').any([1, 2, 3])
'f1' has any [1, 2, 3]
Matches:
{'f1': [1, 2]}
{'f1': [3, 4, 5]}
Parameters: | cond – The condition to check |
---|---|
Return type: | tinydb.queries.Query |
Run a regex test against a dict value (only substring has to match).
>>> where('f1').contains(r'\d+')
'f1' ~= \d+
Parameters: | regex – The regular expression to pass to re.search |
---|---|
Return type: | QueryRegex |
Run test on a nested dict.
>>> where('x').has('y') == 2
has 'x' => ('y' == 2)
Matches:
{'x': {'y': 2}}
Parameters: | key – the key to search for in the nested dict |
---|---|
Return type: | QueryHas |
Run a regex test against a dict value (whole string has to match).
>>> where('f1').matches(r'^\w+$')
'f1' ~= ^\w+$
Parameters: | regex – The regular expression to pass to re.match |
---|---|
Return type: | QueryRegex |
Run a user-defined test function against a dict value.
>>> def test_func(val):
... return val == 42
...
>>> where('f1').test(test_func)
'f1'.test(<function test_func at 0xXXXXXXXX>)
Parameters: | func – The function to run. Has to accept one parameter and return a boolean. |
---|---|
Return type: | QueryCustom |
Contains the base class for storages and implementations.
The abstract base class for all Storages.
A Storage (de)serializes the current state of the database and stores it in some place (memory, file on disk, ...).
Read the last stored state.
Write the current state of the database to the storage.
Optional: Close open file handles, etc.
Contains the base class for middlewares and implementations.
The base class for all Middlewares.
Middlewares hook into the read/write process of TinyDB allowing you to extend the behaviour by adding caching, logging, ...
If read() or write() are not overloaded, they will be forwarded directly to the storage instance.
Read the last stored state.
Write the current state of the database to the storage.
Optional: Close open file handles, etc.