This class implements a dict backed by an sqlite database.
from sqlite_object import SqliteDict
Be careful what kind of items you use as keys and how they are serialized in JSON. If you are using keys that do not serialize deterministically (i.e. unordered containers like dict and map), their behavior in the dict may be unpredictable (i.e. there could be multiple differently-serialized copies of your keys in the SqliteDict). If you need to use keys like this, you should define a different coder and decoder.
Iterating: Iterate through the keys of the dict.
len() works as normal, returning the size of the dict.
Membership testing: Test if a key is present in the dict Membership testing will be slow for large dicts with indexing turned off (indexing is turned on by default)
Idexing: SqliteDicts are indexed just like regular python dicts.
Deletion: Remove key from dict
Create an sql-backed dict.
By default, this will create a new sqlite database with a random filename in the current working directory.
Parameters: |
|
---|
Remove all items from the dict
Retrieve an item from the dict, returning ‘default’ if the key is not present in the dict.
Parameters: |
|
---|
Remove an item from the dict and return it, returning ‘default’ if the key is not present in the dict.
Parameters: |
|
---|
Remove and return a (key, value) tuple from the dict, or rase a KeyError if the dict is empty
If key is in the dictionary, return its value. If not, insert key with a value of default and return default. default defaults to None.
Parameters: |
|
---|
If other is a list of 2-tupes, treat the the 2-tuples as (key, value) pairs and add them to the dict.
If other is a dict (if it has an “items” function that iterates of 2-tuples), add each item in other to the dict.
Parameters: | other – A dictionary or list of key-value pairs to add to the dict. |
---|
Return an iterator over the items ( 2-tuple (key, value) pairs) in the dictionary.
items() may not lock the underlying database, so if modifications to the DB are made during iteration, the behavior of items will be unpredictable
Return an iterator over the keys in the dictionary.
keys() may not lock the underlying database, so if modifications to the DB are made during iteration, the behavior of items will be unpredictable
Return an iterator over the values in the dictionary.
values() may not lock the underlying database, so if modifications to the DB are made during iteration, the behavior of items will be unpredictable
Write the dictionary as JSON to a file.
Parameters: | file – a file object to write to. |
---|
Write the dictionary to a file, one item per line, keys and values separated by a tab.
Parameters: |
|
---|
Explicitly close the database, deleting the database file if persist=False
You do not need to call close on SqliteObjects, close will be called automatically when the object is cleaned up
Explicitly commit any unsaved changes to disk. If commit_every is dict to 0 or 1, (the default), this is unnessecary since all writes are automatically committed immediately.
Return the name of the underlying database file.
SqliteList uses python multithreading RLock to make the dict somewhat threadsafe, but the underlying python sqlite3 library is not itself threadsafe, so your mileage may vary.
If you want to share an SqliteList between threads, it would be safer to create a new SqliteList object in each thread and use the same filename for each SqliteList. sqlite itself uses filesystem locks to ensure database integrity so this type of use would be just fine.
If you are using a SqliteList between multiple threads, some operations may be unpredictable (iteration, read-modify-write actions, etc), so use good judgement and put locks around your code.