Skip to main content

Handle sqlalchemy connection params in a config file.

Project description

Getting Started with sqlalchemy-config

version: 0.4.7b0

The sqlalchemy-config package is a toolkit to handle database configuration parameters that are stored in a config file. This can locate, read, and extract the config parameters and create a sessionmaker object from them that is bound to the engine object.

There is online documentation for sqlalchemy-config.

Installation instructions

sqlalchemy-config can be installed via pypi or conda.

Installation using pip

pip install sqlalchemy-config

Installation using conda

I maintain a conda package in my personal conda channel. To install this please run:

conda install -c cfin -c conda-forge sqlalchemy-config

Usage TL;DR

sqlalchemy-config is designed to handle database connection inputs coming from different sources. For example: a URL or SQLite file path on the command line, a config file path from a command-line argument, an environment variable, or a default file in the home directory.

The config files used in the example below have a similar structure to this see SQLAlchemy engine from config:

[mydb_mysql]
# Make sure this is only readable by you
# Make sure password is URL encoded, see: sqlalchemy_config.encode_password
db.url = mysql+pymysql://user:password@127.0.0.1:3306/db_cfg_arg
>>> import sqlalchemy_config as sqlc
>>> import os

>>> # Input from the command line (SQLite file path)
>>> dbarg = "mydb.db"

>>> # Prefix used for SQLAlchemy parameters in config sections
>>> conn_prefix = "db."

>>> # This represents a default location for config files containing parameters
>>> config_file = os.path.join(os.environ['HOME'], "mydbs_def.cnf")

>>> # dbarg is treated as SQLite file path
>>> sqlc.get_new_sessionmaker(dbarg, conn_prefix=conn_prefix, config_arg=config_file)
>>> sessionmaker(class_='Session', bind=Engine(sqlite:///mydb.db), autoflush=True, autocommit=False, expire_on_commit=True)

>>> # dbarg can also be a fully formed SQLAlchemy URL
>>> dbarg = "mysql+pymysql://user:password@127.0.0.1:3306/db_url_arg"
>>> sqlc.get_new_sessionmaker(dbarg)
>>> sessionmaker(class_='Session', bind=Engine(mysql+pymysql://user:***@127.0.0.1:3306/db_url_arg), autoflush=True, autocommit=False, expire_on_commit=True)

See the API documentation for lower level functions.

Development

To set up a development environment, install the package with development dependencies:

pip install -e ".[dev]"

Run the test suite:

pytest tests/ -v

Run tests with coverage reporting:

pytest --cov=sqlalchemy_config --cov-report=term-missing tests/

Verify the coverage threshold (80%) is met:

pytest --cov=sqlalchemy_config --cov-fail-under=80 tests/

Lint and format code before committing:

ruff check --fix . && ruff format .

Change log

See CHANGELOG.md for the full change log.

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

sqlalchemy_config-0.4.7b0.tar.gz (54.0 kB view details)

Uploaded Source

Built Distribution

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

sqlalchemy_config-0.4.7b0-py3-none-any.whl (54.7 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy_config-0.4.7b0.tar.gz.

File metadata

  • Download URL: sqlalchemy_config-0.4.7b0.tar.gz
  • Upload date:
  • Size: 54.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for sqlalchemy_config-0.4.7b0.tar.gz
Algorithm Hash digest
SHA256 0fd497a8663753922025ef821e0a4fcbe4e922371dc376cb49855c8ae2b10c36
MD5 b9ef9217b469b2e5b54809c38ce5ba5c
BLAKE2b-256 dc35e7e24ad021d6ce4d6d9ba1efd4d026ae50d833e47941f5d9a83df19a2091

See more details on using hashes here.

File details

Details for the file sqlalchemy_config-0.4.7b0-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_config-0.4.7b0-py3-none-any.whl
Algorithm Hash digest
SHA256 dfa2533c6fcdace3875965dd1082358fee686f3f2fa7b59f95ddc388fed23c46
MD5 aebcbc98b283943caec3f28bceeb2e7f
BLAKE2b-256 a1ff83c8335e7b484aae0052a7ee53f2e0c101f83c544ab24df4404e0f20a5db

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