This class implements a list backed by an sqlite database.
from sqlite_object import SqliteList
Indexing: Read, overwrite, increment operations all work as with an ordinary python list.
Iterating: SqliteList objects can be iterated over just like a normal list. Iterating over the list will lock the list so nothing else can use it.
Slicing: SqliteLists can be sliced just like normal lists, except that slices return an iterator over the slice. Use caution when slicing in a multithreaded situation, the generator returned by a slice does not lock the list. For many applications, iterating over the whole list may be more efficient than slicing.
len() works as normal, returning the size of the list.
Create an sql-backed list.
By default, this will create a new sqlite database with a random filename in the current working directory.
Parameters: |
|
---|
Add an item to the end of the list
Parameters: | item – The item to add to the end of the list. |
---|
Add an item to the beginning of the list
Parameters: | item – The item to add to the beginning of the list |
---|
Remove the last item from the list and return it. If the list is empty, this will raise an IndexError
Remove the first item from the list and return it. If the list is empty, this will raise an IndexError
Add each item from iterable to the end of the list.
Parameters: | iterable – an iterable object containing items to be added to the list. |
---|
Write the entire set out to a file as a JSON list
Parameters: | file – A file object to write to |
---|
Write each item in the set to a file as JSON, one item per line.
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 set 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 list 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.