Getting started

Installation

wurm is distributed on PyPI as a universal wheel and is available on Linux/macOS and Windows and supports Python 3.7+.

$ pip install wurm

First steps

To get started with Wurm, let’s first create a table:

1
2
3
4
5
6
7
8
from dataclasses import dataclass
from wurm import Table, Unique

@dataclass
class NamedPoint(Table):
    x: int
    y: int
    name: Unique[str]

Alright, so this tells Wurm what a NamedPoint is, that it has two regular fields named x and y which should both be integers, and a field named name which has a Unique constraint and should be a string.

To anything interesting with it, we should connect to a database, though. SQL databases are usually stored in a file, but if we pass ':memory:' as the filename, sqlite creates a temporary database in RAM, which is useful for quick tests and trying things out.

 9
10
11
12
from wurm import setup_connection
import sqlite3

setup_connection(sqlite3.connect(':memory:'))

Now, we can create objects, insert them in the database, and try some simple queries:

13
14
15
16
17
18
19
20
21
22
23
24
basecamp = NamedPoint(x=1, y=2, name='Basecamp')

print(basecamp)

basecamp.insert()

print(basecamp.rowid)

NamedPoint(x=10, y=-7, name='Goal').insert()

print(list(NamedPoint))
print(NamedPoint.query(x=10).one())

Which produces the following output:

NamedPoint(x=1, y=2, name='Basecamp')
1
[NamedPoint(x=1, y=2, name='Basecamp'), NamedPoint(x=10, y=-7, name='Goal')]
NamedPoint(x=10, y=-7, name='Goal')

TODO: explain commit, delete, queries /w comparators, show errors from Unique constraint violations and other errors