Skip to main content

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.


>>> 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
>>> len(db)
>>> db.insert(d)
>>> d in db
>>> len(db)  # we have 4 unique tuples in the database.

There are various ways to retreive tuples from the database:

>>> p3 =  # not deterministic
>>> p3
Point(x=11, y=22)
>>> p3 =  # deterministic (since p3 is fully specified)
>>> p3 in db
>>> db.pop(p3)  # remove and return
Point(x=11, y=22)
>>> p3 in db
>>> 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
>>> 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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlitent-0.2.1.tar.gz (5.4 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page