Skip to main content

Environment variable helpers

Project description

Roskarl

Is a tiny module for environment variables

Requires

Python 3.11.0+

How to install

uv pip install roskarl

Example usage

from roskarl import (
    env_var,
    env_var_bool,
    env_var_cron,
    env_var_interval_expression,
    env_var_interval_expression_extended,
    env_var_float,
    env_var_int,
    env_var_iso8601_datetime,
    env_var_list,
    env_var_rfc3339_datetime,
    env_var_tz,
    env_var_dsn,
    DSN
)

All functions return None if the variable is not set. An optional default parameter can be provided to return a fallback value instead.

Pass required=True to raise instead of returning None — type checkers will narrow the return type accordingly (e.g. str instead of str | None), so no assert is needed at callsites.

str (returns str)

value = env_var(name="STR_VAR", default="fallback")

bool (returns bool — accepts true or false (case insensitive))

value = env_var_bool(name="BOOL_VAR")

tz (returns str if value is a valid IANA timezone (e.g. Europe/Stockholm))

value = env_var_tz(name="TZ_VAR")

list (returns list[str] if value is splittable by separator)

value = env_var_list(name="LIST_VAR", separator="|")

int (returns int if value is numeric)

value = env_var_int(name="INT_VAR")

float (returns float if value is a float)

value = env_var_float(name="FLOAT_VAR")

cron (returns str if value is a valid cron expression)

value = env_var_cron(name="CRON_VAR")

interval expression (returns str if value is a valid offset-free 5-field cron expression)

value = env_var_interval_expression(name="INTERVAL_VAR")

interval expression extended (returns str if value is a valid offset-free 6-field cron expression)

value = env_var_interval_expression_extended(name="INTERVAL_EXTENDED_VAR")

datetime (ISO8601) (returns datetime if value is a valid ISO8601 datetime string — timezone is optional)

value = env_var_iso8601_datetime(name="DATETIME_VAR")

datetime (RFC3339) (returns datetime if value is a valid RFC3339 datetime string — timezone is required)

value = env_var_rfc3339_datetime(name="DATETIME_VAR")

DSN

Note: Special characters in passwords must be URL-encoded.

from urllib.parse import quote
password = 'My$ecret!Pass@2024'
encoded = quote(password, safe='')
print(encoded)  # My%24ecret%21Pass%402024 <--- use this
value = env_var_dsn(name="DSN_VAR")

returns DSN object if value is a valid DSN string, formatted as:

postgresql://username:password@hostname:5432/database_name

The DSN object exposes the following attributes:

Attribute Type Example
scheme str postgresql
host str hostname
port int 5432
username str username
password str password
database str database_name

Testing

Tests use pytest. Run the full suite:

pytest tests/

Release

See RELEASE.md for instructions on how to publish a new version.

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

roskarl-3.1.25.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

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

roskarl-3.1.25-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file roskarl-3.1.25.tar.gz.

File metadata

  • Download URL: roskarl-3.1.25.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for roskarl-3.1.25.tar.gz
Algorithm Hash digest
SHA256 f6af1eab664d079caeac80fe3ce756a4ceba9189dfcd58173603f4d2637f0a51
MD5 2f5caa330c6dd55e9d252b77e013b242
BLAKE2b-256 ff37d8e0af5ada993315c5c2e8fda29d183032e131d3f2ae005a245751e5cf8d

See more details on using hashes here.

File details

Details for the file roskarl-3.1.25-py3-none-any.whl.

File metadata

  • Download URL: roskarl-3.1.25-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for roskarl-3.1.25-py3-none-any.whl
Algorithm Hash digest
SHA256 84ca31c8aa38ec7fec12071007390e92dfdce55ff5e9fb1d24ebe381a0279e35
MD5 2957e3aef30705e1df81d7505c2a7c62
BLAKE2b-256 782d7aff2138b7d03618492415d7f3d4748d3a039924aa09ff0bf9691069679f

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