Skip to main content

Generic Python utilities — KEV environment manager, XDG directories, DX-first terminal logger, number/percentage/currency formatting. Python 3.12+.

Project description

adrian-utils

Generic Python utilities — KEV environment manager, XDG directories, DX-first terminal logger, number/percentage/currency formatting. Python 3.12+.

Install

pip install adrian-utils
# or with uv
uv add adrian-utils

Install (local dev)

uv add -e .

Usage

from py_utils import log, kev, xdg, usd, percentage

# KEV - Redis-style env vars
api_key = kev.must_get("API_KEY")      # Raises if not found
api_key = kev.get("API_KEY", "dev")    # With default
port = kev.int("PORT", 8080)           # Type conversion
kev.set("DEBUG", "true")               # Memory only (fast)
kev.get("os:PATH")                     # Direct from OS
kev.get(".env:SECRET")                 # Direct from .env

# XDG directories
config_file = xdg.config / "myapp" / "config.toml"
data_file = xdg.data / "myapp" / "database.db"
state_dir = xdg.state / "notify"
cache_dir = xdg.cache / "myapp"

# Narration
log.info("Connected to postgres:5432")

# Task with steps
with log.task("Build assets"):
    log.step("Transpiling")
    log.step("Bundling")

# Status
log.success("Deployed")
log.warn_once("Flag --fast is deprecated")

# Progress
with log.task("Sync"):
    p = log.progress(total=3, title="Uploading")
    p.tick(); p.tick(); p.tick(); p.done(success=True)

# Formatting
usd(1234.56)                  # "+$1,234.56" (color and + by default)
usd(1234.56, signed=False)    # "$1,234.56" (no leading +)
percentage(15.234)            # "+15.2%"
percentage(15.234, signed=False)  # "15.2%"

KEV

Redis-style KV store for environment variables. Searches memory → os.environ.env files, caches results.

from py_utils import kev

# Basics
api_key = kev.must_get("API_KEY")       # Raises if not found
api_key = kev.get("API_KEY", "dev")     # With default
port = kev.int("PORT", 8080)            # Type conversion
debug = kev.bool("DEBUG", False)        # true/1/yes/on → True
rate = kev.float("RATE", 0.5)           # Float conversion
kev.set("APP_NAME", "myapp")            # Memory only (fast)

# Namespaced access (skip the search chain)
kev.get("os:PATH")                      # ONLY from OS
kev.get(".env:SECRET")                  # ONLY from .env file
kev.set("os:DEBUG", "true")             # Write directly to OS
kev.set(".env:API_KEY", "secret")       # Update .env file

# Source tracking
value, source = kev.get_with_source("API_KEY")  # ("secret", ".env")
kev.source_of("API_KEY")                        # ".env" or "os" or "default"

# Customize search order
kev.source.remove("os")                 # Ignore OS env (perfect for tests!)
kev.source.add(".env.local")            # Add more fallbacks
kev.source.set(".env.test")             # Replace entirely

# Pattern matching
kev.keys("API_*")                       # Find all API_ keys
kev.has("API_KEY")                      # Check if configured
kev.clear("TEMP_*")                     # Clear from memory

# Debug mode — shows the full lookup chain
kev.debug = True
kev.get("DATABASE_URL")                 # Prints each source checked

XDG

XDG Base Directory paths — reads env vars set by xdg-dirs, falls back to spec defaults:

from py_utils import xdg

xdg.config / "myapp" / "config.toml"    # ~/.config/myapp/config.toml
xdg.data / "myapp" / "data.db"          # ~/.local/share/myapp/data.db
xdg.state / "notify"                    # ~/.local/state/notify
xdg.cache / "myapp"                     # ~/.cache/myapp
xdg.runtime / "myapp"                   # $XDG_RUNTIME_DIR/myapp

Colors automatically disable when stdout is not a TTY. To override:

from py_utils import set_color_enabled

set_color_enabled(True)   # force color on
set_color_enabled(False)  # force color off
set_color_enabled(None)   # return to auto detection

Run the bundled demo:

# After `uv add -e .`
uv run python example_usage.py

Linting / Formatting

uv run ruff check src
uv run ruff format src

No offensive module

Unlike go-utils and ts-utils, py-utils has no offensive programming module. Python's exception model is already offensive by default — functions raise with full stack traces, uncaught exceptions crash the process, and assert is a language keyword. Go and TS need offensive primitives because their error patterns ((val, err) tuples, try/catch, process.exit) encourage silent failure. Python doesn't have this problem.

Part of the utils suite by Adrian Galilea: go-utils, ts-utils, py-utils.

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

adrian_utils-0.2.0.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

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

adrian_utils-0.2.0-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file adrian_utils-0.2.0.tar.gz.

File metadata

  • Download URL: adrian_utils-0.2.0.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for adrian_utils-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a4b097819afc23eded76b7501c668cb2f173be42ce3fef040f650b0058b584cf
MD5 b3a3a2d69c9af882c012b23702becf2a
BLAKE2b-256 52794f1ecee9c75f988ef29be30d9bf92df8d4d8bf00874998cecb8349b011b4

See more details on using hashes here.

Provenance

The following attestation bundles were made for adrian_utils-0.2.0.tar.gz:

Publisher: release.yml on adriangalilea/py-utils

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file adrian_utils-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: adrian_utils-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for adrian_utils-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8c2caf891b769ecf18590332171335ec69e8b82012f54a0d06d7a590fe9ad848
MD5 e4025b533bd05d7313c37d8c8729689e
BLAKE2b-256 5ef4e68549934878655387a69998caf620e7255ef416ee147c7b28847326eb60

See more details on using hashes here.

Provenance

The following attestation bundles were made for adrian_utils-0.2.0-py3-none-any.whl:

Publisher: release.yml on adriangalilea/py-utils

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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