Skip to main content

Small SQL helper utilities with optional external authsql.py config loading.

Project description

toolssql

Small SQL helper utilities (MySQL-focused) with flexible auth configuration.

Install

pip install toolssql

Auth config loading

This package will try to load CONNECTION_CONFIG and CONNECTION_CONFIG_DEFAULT from an authsql.py module/file in this order:

  1. AUTHSQL_MODULE env var (importable module path, e.g. myapp.authsql)
  2. Import authsql (works when your project root is on sys.path)
  3. AUTHSQL_PATH env var pointing directly to an authsql.py file (supports ~ and $HOME)
  4. Home directory fallbacks:
    • ~/authsql.py
    • ~/.authsql.py
    • ~/.config/authsql.py

Example config:

from typing import Dict, Any
CONNECTION_CONFIG: Dict[str, Any] = {
    'config1':
    {
        'host': 'localhost',
        'user': 'Analyst',
        'password': 'XXXXXXXXXXXXXXXXX',
        'database': 'Playground',
    },
    'config2':
    {
        'host': 'localhost',
        'user': 'Analyst2',
        'password': 'XXXXXXXXXXXXXXXXX',
    },
}

# default connection to be used, if nothing else is specified
CONNECTION_CONFIG_DEFAULT = "config1"

If none are found, it falls back to {} / None.

Important: Do not ship secrets inside this PyPI package. Keep authsql.py outside git.

Usage

from toolssql import MysqlConnect

db = MysqlConnect()  # uses loaded defaults if available
df = db.select_df("SELECT 1 AS one")
print(df)

Or inject config explicitly:

from toolssql import MysqlConnect

cfg = {
  "prod": {"host": "...", "user": "...", "password": "...", "database": "..."}
}
db = MysqlConnect(connection="prod", connection_config=cfg, connection_default="prod")

Retry on transient errors

MysqlFetch and MysqlInsertValues support automatic retries for transient MySQL errors (deadlock, lock timeout, server gone, lost connection).

By default, one retry is enabled (retry_on_error=1, i.e. 2 attempts total) with a 10 s base delay plus random jitter (actual sleep is between 5–10 s) to avoid thundering-herd collisions. By default, only deadlocks are retried; to retry other transient errors, set retry_errors. You can customise this:

from toolssql import MysqlFetch

# default: 1 retry (2 attempts total) with ~10 s jittered delay
sql = MysqlFetch("SELECT 1")
sql.runsql()

# disable retries
sql = MysqlFetch("SELECT 1", retry_on_error=0)

# up to 3 retries (4 attempts total) with 5 s delay
sql = MysqlFetch("SELECT 1", retry_on_error=3, retry_delay=5)

# retry all built-in transient errors
from toolssql.sql import RETRYABLE_ERRORS
sql = MysqlFetch("SELECT 1", retry_on_error=2, retry_delay=5, retry_errors=RETRYABLE_ERRORS)
from toolssql import MysqlInsertValues

ins = MysqlInsertValues(
    "my_insert",
    "INSERT INTO t (a, b) VALUES (%s, %s)",
    retry_on_error=2,   # up to 3 attempts total
    retry_delay=5,       # 5 s between retries
)

Non-retryable errors (e.g. syntax error, duplicate key) are never retried.

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

toolssql-0.1.5.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

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

toolssql-0.1.5-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file toolssql-0.1.5.tar.gz.

File metadata

  • Download URL: toolssql-0.1.5.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for toolssql-0.1.5.tar.gz
Algorithm Hash digest
SHA256 4226e9e5ea2a33283379bc2201cbeb0baa2a5a4af8df34a43ea0fbc40b068eb9
MD5 805bf2b6f361665e91b6922f16dbbb18
BLAKE2b-256 855586dd83f9f58b0f12a40fb6902ea8b398b1fe8a2840c51b01b0c7c37d146f

See more details on using hashes here.

File details

Details for the file toolssql-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: toolssql-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for toolssql-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 85456c9c19ded7fc91f1464f02c0344c081b42ae0d632ca77365ba23a7d1b3e7
MD5 b0dd05a7523b9f57785222423a87f058
BLAKE2b-256 0bb092c786fbc4563e4faf55624397a82c4bd0547c268df2b6b5af6743972d60

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