Skip to main content

DRY Foundation

DRY Foundation logo

Tools and components for building customized and consistent small-scale Flask applications.

Why DRY?

Don't Repeat Yourself. Why write and maintain multiple copies of application infrastructure if it's going to be used and reused?

This package is designed to be a lightweight foundation a Flask application: it performs some of the database setup operations, coordinates database management for robust test suites, and also provides some utilities for enforcing authorization criteria for database access, among other things.

Here are some of the features explained in greater detail.

Application Factories and Command Line Interfaces

The Flask command line interface (CLI) provides a lot of functionality via its Click backend. However, users (e.g., me) may wish to launch an application from the command line depending on a certain configuration. The Flask documentation shows an example of how to structure an application to leverage multiple configurations via objects, but leaves it up to a user to implement the mechanics of passing this configuration to the application via environment variable or some other script.

This package takes a different approach, and provides tools to customize the CLI so that configurations can be loaded dynamically at runtime. Configurations are predefined (for production, development, and testing), and can be loaded by specifying a launch mode:

  • Production Mode: Launch an app for production use, drawing from a production-style configuration.
  • Local Mode: Launch an app for local use, using the Flask development server but still relying on a production-style configuration.
  • Development Mode: Launch an app for development, with default debugging features and using a development style configuration.

A testing configuration is also built-in to provide test-specific configuration settings.

Database Setup

One element of an application foundation is the testing infrastructure. Flask gives some guidance in the current edition of their documentation, and it suggests creating a temporary SQLite database for each test. This is perfectly fine for simple applications, but adds substantial overhead (and test time) to applications with many tests, especially for those where the database contains some preloaded information. Instead, it would be preferable to create and use one temporary database instance for the majority of tests that only require read-access to the database, and then only create additional test databases for the tests that modify the state of the database (e.g., SQLAlchemy transactions).1

DRY Foundation provides the infrastructure to create databases in both of these cases: a "persistent" database for read-only tests, and "ephemeral" databases for each transaction). The persistent test database is used by default, but a transaction_lifetime decorator can be used to indicate that a given test should use an ephemeral copy.

Repositories and Authorization

The common operations are easy to do using SQLAlchemy, but rather than rolling new functions for each new query in every application, this tool provides a set of repositories that are designed to abstract away a bunch of the tedious details. Also, my experience is that enforcing authorization constraints when manipulating sophisticated table relationships can be tricky, and so this tool and its repositories provide an interface for managing those authorizations consistently.

Installation

DRY Foundation is registered on the Python Package Index (PyPI) for easy installation. To install the package, simply run

pip install dry-foundation

The package requires a recent version of Python (3.10+).

Development dependencies include uv and just.

Usage

The app has lots of tools, but the basics can be used just as drop-in replacements for the standard Flask object and application factory.

# DRY Application:  `dry_application/__init__.py`

from dry_foundation import DryFlask, Factory


@Factory
def create_app(config=None):
    app = DryFlask(__name__, app_name="DRY Application")
    app.configure(config)
    # Anything else performed by the application factory...
    return app

For a more complete, up-to-date API reference, build and read the docs:

# Using `just`: https://just.systems/
just docs
# open `docs/build/html/index.html` in your browser

License

This project is licensed under the GNU General Public License, Version 3. It is fully open-source, and while you are more than welcome to fork, add, modify, etc. it is required that you keep any distributed changes and additions open-source.

Changes

Changes between versions are tracked in the changelog.

  1. Yes, you could also just not create databases and mock the database return functions, but my experience has been that it is even more tedious to maintain a comprehensive set of mocked data to test various intricacies and edge cases while simultaneously ensuring that the mocks behave similarly enough to SQLAlchemy objects. ↩

Metadata

Release files for dry-foundation 2.2.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 dry-foundation 2.2.0
File Size Uploaded Provenance Actions
dry_foundation-2.2.0.tar.gz 158.5 kB Without provenance Details for dry_foundation-2.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dry-foundation 2.2.0
File Interpreter ABI Platform Actions
dry_foundation-2.2.0-py3-none-any.whl Python 3 none any Details for dry_foundation-2.2.0-py3-none-any.whl

Total release size: 219.4 kB

Release files / dry_foundation-2.2.0.tar.gz

Download URL dry_foundation-2.2.0.tar.gz
Size 158.5 kB
Tags Source
SHA-256 checksum
How to use checksums
e12c3d1f91f8fa9a6db3b57f2a80b11e3dbf297444ec5cb3993ab5236843eed3
BLAKE2b-256 checksum
How to use checksums
1011d313e0175b082d1eb1d31e5223dc4e806ea500a6c7806bbc49946fd50c07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / dry_foundation-2.2.0-py3-none-any.whl

Download URL dry_foundation-2.2.0-py3-none-any.whl
Size 61.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
90cd3ff2c25cffcf27f4ff151e81b5701c8b37866a424af2d1e307631bdd3af8
BLAKE2b-256 checksum
How to use checksums
8964f678c3cdd17cc6c7ef79d9d9f9473894a71c97d1014a1ab853f5cfc237d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for dry-foundation 2.2.0

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