Skip to main content

Python custom implementation of unit of work pattern

Project description

pyUoW

Unit of Work for Python — composable units, transactional work managers, and a domain model toolkit.

📖 Read the docs  ·  Quickstart  ·  PyPI

Downloads Build Codecov Ruff Python versions License


What is PyUoW?

PyUoW is a Python implementation of the Unit of Work behavioural pattern. It gives you small, typed building blocks for business logic that compose into a flow and run inside a Work Manager that handles transactions, batching, and domain events.

The result: business logic that reads top-to-bottom, separates orchestration from execution, and stays testable as it grows.

Highlights

  • Composable units — write each step as a ConditionalUnit / RunUnit / FinalUnit, chain them with >>, validate the chain at build time.
  • Transactional out of the box — a SQLAlchemy adapter ships in pyuow.contrib.sqlalchemy (sync + async), with nested-transaction support.
  • Domain-firstEntity, AuditedEntity, SoftDeletableEntity, VersionedEntity, and an event-emitting Model base, all immutable dataclasses.
  • Sync and async parity — every primitive has an aio/ twin; pick one per flow.
  • Strict types — passes mypy --strict; runs on Python 3.10 through 3.14.

Install

pip install pyuow                # core
pip install "pyuow[sqlalchemy]"  # with SQLAlchemy integration

Python ≥ 3.10.

At a glance

from dataclasses import dataclass

from pyuow import (
    BaseContext, BaseParams, ConditionalUnit, ErrorUnit,
    FinalUnit, Result, RunUnit,
)
from pyuow.work.noop import NoOpWorkManager


@dataclass(frozen=True)
class Greeting(BaseParams):
    name: str


@dataclass
class Ctx(BaseContext[Greeting]):
    params: Greeting


class IsNamePresent(ConditionalUnit[Ctx, str]):
    def condition(self, ctx: Ctx) -> bool:
        return bool(ctx.params.name)


class Greet(RunUnit[Ctx, str]):
    def run(self, ctx: Ctx) -> None:
        print(f"Hello, {ctx.params.name}!")


class Done(FinalUnit[Ctx, str]):
    def finish(self, ctx: Ctx) -> Result[str]:
        return Result.ok("greeted")


flow = (
    IsNamePresent(on_failure=ErrorUnit(exc=ValueError("name required")))
    >> Greet()
    >> Done()
).build()

result = NoOpWorkManager().by(flow).do_with(Ctx(params=Greeting(name="Alice")))
assert result.get() == "greeted"

What's in the box

Concept What it gives you
Units & Flow ConditionalUnit / RunUnit / FinalUnit / ErrorUnit, chained with >>
Result ok / error / empty plus .map, .and_then, .unwrap_or
Context Mutable, immutable, and domain-aware context bases
Work Manager NoOp, transactional, and domain-transactional managers
Domain Model Entity, AuditedEntity, Model, Batch, events, typed exceptions
DataPoints Typed producer / consumer contracts between units
SQLAlchemy Ready-made repositories, table mixins, and transaction manager
Async A sync and an aio/ twin for every primitive

Contributing

PRs welcome — see CONTRIBUTING.md for the dev setup, or run:

poetry install --with docs
make tests        # pytest + coverage
make fmt          # ruff + mypy
make docs-serve   # live preview at http://127.0.0.1:8000

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyuow-0.9.0.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyuow-0.9.0-py3-none-any.whl (43.3 kB view details)

Uploaded Python 3

File details

Details for the file pyuow-0.9.0.tar.gz.

File metadata

  • Download URL: pyuow-0.9.0.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.14.5 Linux/6.17.0-1015-azure

File hashes

Hashes for pyuow-0.9.0.tar.gz
Algorithm Hash digest
SHA256 9e25711d2f321378ca021028343e061ed2c41a61849916f4b28dfa3af8bbbbc5
MD5 3460bf35ba26b6ae6f3934f20da7d342
BLAKE2b-256 32a22c03cb3186519edfc983f56711d7c4d77ba04b182d1b99872804251301fb

See more details on using hashes here.

File details

Details for the file pyuow-0.9.0-py3-none-any.whl.

File metadata

  • Download URL: pyuow-0.9.0-py3-none-any.whl
  • Upload date:
  • Size: 43.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.14.5 Linux/6.17.0-1015-azure

File hashes

Hashes for pyuow-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a3dea60f4827c5f5bc29a6c7e5475ded1c2c2dbd3e1ed64df03673c5dde32596
MD5 f4cd2cd45e4c2e8b5361fe6ea6fb1a4a
BLAKE2b-256 9b28732cfef41eb197b9e4167b9dbed1eb747924a03c633cb167829aa07f5fea

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page