Skip to main content

fort

The Python database micropackage


fort is a thin database wrapper for programmers who love to write SQL. Use it when SQLAlchemy is too much.

Usage

Start by initializing an object for your database, providing a connection string:

import fort

db = fort.PostgresDatabase('postgres://user:password@host/database')

Each of fort's database classes provides a small set of methods that makes working with SQL simple. You can immediately begin making queries to the database:

import uuid

db.u('CREATE TABLE widgets (id uuid PRIMARY KEY, name text)')

my_id = uuid.uuid4()
db.u('INSERT INTO widgets (id, name) VALUES (%(id)s, %(name)s)', {'id': my_id, 'name': 'Thingy'})

for row in db.q('SELECT id, name FROM widgets'):
    print(row['id'], row['name'])

my_widget = db.q_one('SELECT id, name FROM widgets WHERE id = %(id)s', {'id': my_id})
print(my_widget['name'])

my_widget_name = db.q_val('SELECT name FROM widgets WHERE id = %(id)s', {'id': my_id})
print(my_id, my_widget_name)

Using one of fort's database classes directly is fine, but it is better to consolidate your SQL statements by subclassing one of fort's classes and adding your own methods:

class MyDatabase(fort.PostgresDatabase):

    def migrate(self):
        self.u('CREATE TABLE widgets (id uuid PRIMARY KEY, name text)')

    def add_widget(self, widget_name: str) -> uuid.UUID:
        new_id = uuid.uuid4()
        sql = 'INSERT INTO widgets (id, name) VALUES (%(id)s, %(name)s)'
        params = {'id': new_id, 'name': widget_name}
        self.u(sql, params)
        return new_id

    def list_widgets(self) -> List[Dict]:
        return self.q('SELECT id, name FROM widgets')

    def get_widget(self, widget_id: uuid.UUID) -> Optional[Dict]:
        sql = 'SELECT id, name FROM widgets WHERE id = %(id)s'
        return self.q_one(sql, {'id': widget_id})

    def get_widget_name(self, widget_id: uuid.UUID) -> Optional[str]:
        sql = 'SELECT name FROM widgets WHERE id = %(id)s'
        return self.q_val(sql, {'id': widget_id})

db = MyDatabase('postgres://user:password@host/database')
db.migrate()

my_id = db.add_widget('Thingy')

for widget in db.list_widgets():
    print(widget['id'], widget['name'])

my_widget = db.get_widget(my_id)
print(my_widget['id'], my_widget['name'])

my_widget_name = db.get_widget_name(my_id)
print(my_id, my_widget_name)

Database class methods

The following methods come with every fort database class:

def u(self, sql: str, params: Dict = None) -> int: ...
    """
    Execute a statement and return the number of rows affected.
    Use this method for CREATE, INSERT, and UPDATE statements.
    """

def q(self, sql: str, params: Dict = None) -> List[Dict]: ...
    """
    Execute a statement and return all results.
    Use this method for SELECT statements.
    """

def q_one(self, sql: str, params: Dict = None) -> Optional[Dict]: ...
    """
    Execute a statement and return the first result.
    If there are no results, return `None`.
    """

def q_val(self, sql: str, params: Dict = None) -> Any: ...
    """
    Execute a statement and return the value in the first column of the first result.
    If there are no results, return `None`.
    """

Each fort database class instance also has a logger at self.log:

    db = MyDatabase('postgres://user:password@host/database')
    db.log.info('Hello from my database class instance!')

Notes on specific database classes

PostgresDatabase

Use PostgresDatabase to connect to a PostgreSQL database. Use pyformat paramstyle for all your statements. Your connection string will be passed directly to psycopg2.connect().

You are still responsible for installing psycopg2.

SQLiteDatabase

Use SQLiteDatabase to connect to an SQLite database. Use named paramstyle for all your statements. Your connection string will be passed directly to sqlite3.connect().

Release files for fort 2026.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fort 2026.0
File Size Uploaded
fort-2026.0.tar.gz 3.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fort 2026.0
File Interpreter ABI Platform
fort-2026.0-py3-none-any.whl Python 3 none any Details

Total release size: 7.8 kB

Release files / fort-2026.0.tar.gz

Download URL fort-2026.0.tar.gz
Size 3.4 kB
Tags Source
SHA-256 checksum
How to use checksums
d30fa2669ccd4691fa490a809db840fd22be0083d71a24a2243945e799b2ef4d
BLAKE2b-256 checksum
How to use checksums
85ad657f3c60aff8010d39381527d5b09147823b91b576ee3b423fa85d695192
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / fort-2026.0-py3-none-any.whl

Download URL fort-2026.0-py3-none-any.whl
Size 4.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0479f0ab071a180fe4e8bb4b59a8c132b75ba33c556908720f40767b6929410e
BLAKE2b-256 checksum
How to use checksums
82137bb8ec16b5f98341458c34d1909a36239450ff3876741d1700b9b4edd88c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

2026.0 This release

2 release files

2025.1

2 release files

2025.0

2 release files

1.2.0

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.0.1

1 release file

1.0.0

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page