Python datasets: dataset databases for lazy people! Bumped into this project that provides a simple abstraction layer removes most direct SQL and an export mechanism to export data in JSON or CSV formats.

Installing datasets via pip would be straight forward : pip install dataset

Why datasets? Say if you have opted to use sqlite, the below is how normal operations would look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import sqlite3

# open connection and get a cursor
conn = sqlite3.connect(':memory:')
c = conn.cursor()

# create schema for a new table
c.execute('CREATE TABLE IF NOT EXISTS sometable (name, age INTEGER)')
conn.commit()

# insert a new row
c.execute('INSERT INTO sometable values (?, ?) ', ('John Doe', 37))
conn.commit()

# extend schema during runtime
c.execute('ALTER TABLE sometable ADD COLUMN gender TEXT')
conn.commit()

# add another row
c.execute('INSERT INTO sometable values (?, ?, ?) ', ('Jane Doe', 34, 'female'))
conn.commit()

# get a single row
c.execute('SELECT name, age FROM sometable WHERE name = ?', ('John Doe', ))
row = list(c)[0]
john = dict(name=row[0], age=row[1])

Now the same with datasets can be reduced to:

1
2
3
4
5
6
import dataset
db = dataset.connect('sqlite:///:memory:')
table = db['sometable']
table.insert(dict(name='John Doe', age=37))
table.insert(dict(name='Jane Doe', age=34, gender='female'))
john = table.find_one(name='John Doe')

Isn't that sweet?

P.S: The ruby world also has something similar, do paw at Sequel::Dataset