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:

1from dataclasses import dataclass
2from wurm import Table, Unique
3
4@dataclass
5class NamedPoint(Table):
6    x: int
7    y: int
8    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.

 9from wurm import setup_connection
10import sqlite3
11
12setup_connection(sqlite3.connect(':memory:'))

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

13basecamp = NamedPoint(x=1, y=2, name='Basecamp')
14
15print(basecamp)
16
17basecamp.insert()
18
19print(basecamp.rowid)
20
21NamedPoint(x=10, y=-7, name='Goal').insert()
22
23print(list(NamedPoint))
24print(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