This class implements a set backed by an sqlite database.
from sqlite_object import SqliteSet
Be careful what kind of items you put into the set and how they are serialized in JSON. If you are inserting items into the set that do not serialize deterministically (i.e. unordered containers like set and map), their behavior in the set may be unpredictable (i.e. there could be multiple differently-serialized copies of your objects in the SqliteSet). If you need to insert containers like this into the set, you should define a different coder and decoder.
Iterating: SqliteList objects can be iterated over just like a normal set. Iterating over the set will lock the set so nothing else can use it.
len() works as normal, returning the size of the set.
Membership testing: Membership testing will be slow for large sets with indexing turned off (indexing is turned on by default)
Subset testing: Test if this set is a subset of another set
Proper subset testing: Test if this set is a proper subset of another set (i.e. set <= other_set && set != other_set)
Superset testing: Test if this set is a superset of another set
Proper superset testing: Test if this set is a proper superset of another set (i.e. set >= other_set && set != other_set)
Set equality: Test equality of this set to another set
Create an sql-backed set.
By default, this will create a new sqlite database with a random filename in the current working directory.
Parameters: |
|
---|
Add an item to the set. If the item is already in the set, nothing happens
Parameters: | item – The item to add to the set. |
---|
Remove an item from the set, if the item does not exist in the set, raise a KeyError
Parameters: | item – The item to remove from the set. |
---|
Remove an item from the set, if the item does not exist in the set, nothing happens.
Parameters: | item – The item to remove from the set. |
---|
Remove an item from the set and return it. If the set is empty, raise a KeyError
Test if this set is disjoint from other (i.e. if the intersection of this set and other is an empty set).
Parameters: | other – Another set to test. |
---|
Test if this set is a subset of other. This is also accessible through the <= operator, i.e. set <= other
Parameters: | other – Another set to test. |
---|
Test if this set is a superset of other. This is also accessible through the >= operator, i.e. set >= other
Parameters: | other – Another set to test. |
---|
Add each item from iterable to the set.
Parameters: | iterable – An iterable of items to add to the set. |
---|
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 set 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.