Skip to main content

lica-sqa

Introduction

lica-sqa is a lightweight SQLAlchemy utility layer for day-to-day LICA Python applications. It defines minimal synchronous and asynchronous database interfaces, provides shared declarative-model metadata, and includes adapters that create SQLAlchemy engines and session factories from a configuration value.

The package supports conventional SQLAlchemy ORM models, asynchronous models based on AsyncAttrs, consistent database-object naming conventions, and a small helper for creating and dropping SQL views. Application code depends only on the Database or AsyncDatabase protocol, while the concrete database behaviour is supplied by an adapter.

Installation

Install lica-sqa directly from the lica-ng Git repository:

pip install lica-sqa

or add it to your pyproject.toml:

[project]
dependencies = [
    "lica-sqa",
]

The included adapters require SQLAlchemy and python-decouple:

[project]
dependencies = [
    "lica-sqa",
    "sqlalchemy",
    "python-decouple",
]

For asynchronous applications using SQLite, add an async driver such as aiosqlite:

[project]
dependencies = [
    "lica-sqa",
    "sqlalchemy",
    "python-decouple",
    "aiosqlite",
]

Interfaces

lica-sqa defines its database contracts using typing.Protocol. Adapters do not need to inherit from these protocols explicitly; they only need to provide the required methods.

Synchronous database

from contextlib import AbstractContextManager
from typing import Protocol

from sqlalchemy.orm import Session


class Database(Protocol):
    """Application software uses this interface as the dependency."""

    def session(self) -> AbstractContextManager[Session]: ...

Asynchronous database

from contextlib import AbstractAsyncContextManager
from typing import Protocol

from sqlalchemy.ext.asyncio import AsyncSession


class AsyncDatabase(Protocol):
    """Application software uses this interface as the dependency."""

    def session(self) -> AbstractAsyncContextManager[AsyncSession]: ...

Adapters

Adapter Module Extra Description
DcplDatabase lica_sqa.adapters.decouple sqlalchemy, python-decouple Creates a synchronous SQLAlchemy engine and session factory from a python-decouple configuration value.
AsyncDcplDatabase lica_sqa.adapters.asyncio.decouple sqlalchemy, python-decouple Creates an asynchronous SQLAlchemy engine and session factory from a python-decouple configuration value.

Both adapters accept the name of the environment variable or configuration key containing the database URL:

DcplDatabase(env_var: str = "DATABASE_URL") -> None
AsyncDcplDatabase(env_var: str = "DATABASE_URL") -> None

Call init() before using the adapter. It reads the database URL, creates the engine with pool_pre_ping=True, and creates a session factory with autoflush=False and expire_on_commit=False.

The expected configuration value is a standard SQLAlchemy database URL, for example:

DATABASE_URL=sqlite:///app.db

For an asynchronous SQLite database:

DATABASE_URL=sqlite+aiosqlite:///app.db

Session usage

The session() method returns a context manager that yields a SQLAlchemy session and closes it automatically on exit.

with database.session() as session:
    session.add(obj)
    session.commit()

For asynchronous adapters:

async with database.session() as session:
    session.add(obj)
    await session.commit()

Models and metadata

lica-sqa provides shared SQLAlchemy MetaData objects with a consistent naming convention for indexes, unique constraints, check constraints, foreign keys, and primary keys.

Synchronous models

Derive application models from Model:

from sqlalchemy.orm import Mapped, mapped_column

from lica_sqa import Model


class User(Model):
    __tablename__ = "user"

    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str]

Asynchronous models

Derive asynchronous application models from AsyncModel:

from sqlalchemy.orm import Mapped, mapped_column

from lica_sqa.metadata.asyncio import AsyncModel


class User(AsyncModel):
    __tablename__ = "user"

    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str]

AsyncModel combines SQLAlchemy’s AsyncAttrs and DeclarativeBase, enabling lazy attribute loading in asynchronous applications.

SQL views

The view() helper creates a SQLAlchemy table-like object associated with a SQL view. It registers CREATE VIEW and DROP VIEW events on the supplied MetaData, creating the view only if it does not already exist and dropping it only if it exists.

from sqlalchemy import select

from lica_sqa import metadata, view
from myapp.models import User

user_view = view(
    "user_names",
    metadata,
    select(User.id, User.name),
)

The helper is based on the SQLAlchemy views recipe and has been updated for SQLAlchemy 2.0.

Logging

Use sqa_logging() to configure the verbosity of SQLAlchemy engine logging:

from lica_sqa import sqa_logging

sqa_logging(debug=True)

When debug=True, the sqlalchemy.engine and aiosqlite loggers are set to INFO. Otherwise, sqlalchemy.engine is set to WARNING.

Usage example, with lica-cli package

import sys
from argparse import ArgumentParser, Namespace

from lica_cli import parse_args, run_cli
from lica_sqa import Database
from lica_sqa.adapters.decouple import DcplDatabase

from myapp.models import User
from myprog import __version__

DESCRIPTION = "My program short description"


def add_args(parser: ArgumentParser) -> None:
    """Add extra CLI arguments here"""
    pass


def cli_main(args: Namespace, database: Database) -> None:
    with database.session() as session:
        session.add(User(name="Alice"))
        session.commit()


def main():
    args = parse_args(add_args, __name__, __version__, DESCRIPTION)

    database: Database = DcplDatabase()
    database.init()

    return_code = run_cli(cli_main, args, __name__, __version__, database=database)
    sys.exit(return_code)


if __name__ == "__main__":
    main()

Metadata

Release files for lica-sqa 0.2.3

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

Source distribution (sdist)

Source distribution for lica-sqa 0.2.3
File Size Uploaded Provenance Actions
lica_sqa-0.2.3.tar.gz 7.0 kB Without provenance Details for lica_sqa-0.2.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for lica-sqa 0.2.3
File Interpreter ABI Platform Actions
lica_sqa-0.2.3-py3-none-any.whl Python 3 none any Details for lica_sqa-0.2.3-py3-none-any.whl

Total release size: 17.6 kB

Release files / lica_sqa-0.2.3.tar.gz

Download URL lica_sqa-0.2.3.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
3d32953bc3b3da56244b224f5f3ea4b7fef6df6fbfdb374207a308255bd4b5ce
BLAKE2b-256 checksum
How to use checksums
7b9fb27322807c03c84a17c34a9e2bc58bd81230b83c4ef08cbfbcadb497f71f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","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":null}

Release files / lica_sqa-0.2.3-py3-none-any.whl

Download URL lica_sqa-0.2.3-py3-none-any.whl
Size 10.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2b043580d3af3dbadccc34029c3ad891dafe20e9c86b5a1d17e46fa88276ad36
BLAKE2b-256 checksum
How to use checksums
00df0e4d150abbd29385b953d2b71fcedf03d714f530b42fbadc8c86c6315ce1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","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":null}

Security for lica-sqa 0.2.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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