namedtuples inside set-like sqlite databases
Project description
sqlitent provides a set-like interface on top of SQLite. Values can be None, int, float, str, and bytes. Tuple types are distinguished by name (case sensitive!) and number of fields. Documentation.
sqlitent was inspired by sqlitedict and kv.
Example
>>> from collections import namedtuple >>> from sqlitent import sqlitent, fuzzy >>> db = sqlitent('database.sqlite', autocommit=True) >>> >>> Point = namedtuple('Point', ['x', 'y']) >>> p1 = Point(11, y=22) >>> p1 Point(x=11, y=22) >>> p2 = p._replace(x=33) Point(x=33, y=22) >>> Car = namedtuple('Car', [ ... 'brand', ... 'model', ... 'configuration', ... 'hp', ... ]) >>> c = Car('Audi', 'A1', 'Sport 1.8 TFSI S tronic', 192) >>> c Car(brand='Audi', model='A1', configuration='Sport 1.8 TFSI S tronic', hp=192) >>> db.register(Point, Car) # necessary to read tuples
In addition to the methods which the set type provides, there are convenience methods (insert, delete) that handle multiple tuples at once.
>>> db.add(p1) >>> db.remove(p1) >>> db.insert(p1, p2) >>> db.delete(p1, p2) >>> db.insert([p1], [[p1], p2]) >>> db.insert(c) >>> db.insert([c, p1]) # tuples may be of different types
Of course, sqlitent also supports membership checking and counting:
>>> d = c._replace(configuration='1.6 TDI S tronic', hp=116) >>> d in db False >>> len(db) 3 >>> db.insert(d) >>> d in db True >>> len(db) # ...now we have 4 unique tuples in the database. 4
There are various ways to retreive tuples from the database:
>>> p3 = db.one(Point) # not deterministic >>> p3 Point(x=11, y=22) >>> p3 = db.one(p3) # deterministic (since p3 is fully specified) >>> p3 in db True >>> db.pop(p3) # remove and return Point(x=11, y=22) >>> p3 in db False >>> db.pop(Point) # not deterministic Point(x=33, y=22) >>> db.pop(Point) # returns None since there are no more Point tuples >>>
Obviously there are also functions to retrieve or pop multiple values out of the database. sqlitent also supports fuzzy matching on text fields and filter functions:
>>> cs = db.many(Car) # get all cars >>> cs <generator object _sqlitent_iter at 0x10f39bb48> >>> list(cs) [Car(brand='Audi', model='A1', configuration='Sport 1.8 TFSI S tronic', hp=192), Car(brand='Audi', model='A1', configuration='1.6 TDI S tronic', hp=116)] >>> d = list(db.popmany(Car, configuration=fuzzy('%TDI%'))) >>> any(x in db for x in d) # we removed all TDI cars from the database False >>> list(db.many(Car, hp=lambda v: v > 150)) [Car(brand='Audi', model='A1', configuration='Sport 1.8 TFSI S tronic', hp=192)] >>> list(db.many(Car, brand='Kia')) []
This library is MIT licensed.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file sqlitent-0.2.1.tar.gz
.
File metadata
- Download URL: sqlitent-0.2.1.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d1bac70edda04f90efb2b40ed9bdb4a0e9a63a0bec2e42fb2ced72e9c77fe733 |
|
MD5 | 98db73befde3fee3a4409e77f1641ec9 |
|
BLAKE2b-256 | 5fc6b7988f1478e6b403c839abaa37edb5a13c1974fcbc3a673b3f9f9b34b1f8 |