Skip to main content

CaptainPlanet 🌍

Open-source package for loading environment variables easier and in a Pythonic way.

Installation

pip install captainplanet

Requires Python 3.10+.

Getting Started

>>> from captainplanet import EnvironmentVariable
>>>
>>> port = EnvironmentVariable("APP_PORT", int, default_value=8080)
>>> host = EnvironmentVariable("APP_HOST", str, default_value="localhost")
>>> debug = EnvironmentVariable("APP_DEBUG", bool, default_value=False)
>>>
>>> port.get()
8080
>>> host.get()
'localhost'
>>> debug.get()
False

When the environment variable is set, CaptainPlanet parses it to the specified type:

>>> import os
>>> os.environ["APP_PORT"] = "3000"
>>> port.get()
3000

You can also set and unset environment variables programmatically:

>>> debug.set(True)
>>> os.environ["APP_DEBUG"]
'True'
>>> debug.get()
True
>>> debug.unset()
>>> debug.get()
False  # falls back to default

Usage

Supported Types

Any callable that accepts a str and returns the desired type works:

from captainplanet import EnvironmentVariable

# Strings (no conversion needed)
app_name = EnvironmentVariable("APP_NAME", str)

# Integers
max_retries = EnvironmentVariable("MAX_RETRIES", int, default_value=3)

# Floats
timeout = EnvironmentVariable("TIMEOUT", float, default_value=30.0)

# Booleans (parses common string representations: "true", "1", "yes", "on" / "false", "0", "no", "off")
enable_logging = EnvironmentVariable("ENABLE_LOGGING", bool, default_value=True)

# Custom types; for example, comma-separated list of strings
def comma_separated_list(value: str) -> list[str]:
    return [item.strip() for item in value.split(",") if item.strip()]

urls = EnvironmentVariable("URLS", comma_separated_list, default_value=[])

Boolean Parsing

The bool type automatically handles common string representations (case-insensitive):

Truthy values Falsy values
"true" "false"
"1" "0"
"yes" "no"
"on" "off"

Checking if a Variable is Set

from captainplanet import EnvironmentVariable

api_key = EnvironmentVariable("API_KEY", str)

if api_key.is_set:
    key = api_key.get()
else:
    raise EnvironmentError("API_KEY is not set")

Error Handling

If the environment variable's value can't be converted to the requested type, a ValueError is raised:

>>> os.environ["COUNT"] = "not-a-number"
>>> count = EnvironmentVariable("COUNT", int, default_value=0)
>>> count.get()
ValueError: Failed to convert 'not-a-number' for COUNT

Obfuscated Mode

When obfuscated=True, the value is hidden in error messages and string representation for sensitive fields:

Development

[!IMPORTANT] We use uv for development, from environment setup to test and build/deploy.

See official uv documentation for more information.

Initial setup

If it's your first time working on this project, you can run the following command to set up your environment:

make init

[!TIP] We also provide a number of other make targets for convenience. Run make (or make help) to see what's available.

Code format

We use ruff for code formatting and linting. You can run the following command to check your code. A simple way to lint your code is to run the pre-commit hooks using the format target:

make format

Unit tests

We use pytest for unit tests, and all tests are in the tests directory.

To quickly run unit tests using the development environment, you can run

make test

To make sure that the package works as expected in all supported python versions, you can run the test-all target. Note that each python version will be tested in an isolated environment, and you can use the -j, --jobs option to run tests in parallel (each python version in its own process). For example:

make -j5 test-all

[!TIP] For the test-all target, both stdout and stderr from testing each python version are written to respective .test-{version}.log files.

Metadata

Release files for captainplanet 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for captainplanet 0.0.3
File Size Uploaded
captainplanet-0.0.3.tar.gz 46.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for captainplanet 0.0.3
File Interpreter ABI Platform
captainplanet-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 55.0 kB

Release files / captainplanet-0.0.3.tar.gz

Download URL captainplanet-0.0.3.tar.gz
Size 46.2 kB
Tags Source
SHA-256 checksum
How to use checksums
e7727fbfbc86e9069bc589a71071acd1a8ebb2c43f8a4d549f38f9ba2d7d7208
BLAKE2b-256 checksum
How to use checksums
a3d335d21b425e3a50892f0afaa6e97819ab638cb4ff0ee25272736f9c41eeae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / captainplanet-0.0.3-py3-none-any.whl

Download URL captainplanet-0.0.3-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
34f7ef0ddc0b933929402976f9bc0f90734e3ae77f2aa7a317ef19cfe9b02818
BLAKE2b-256 checksum
How to use checksums
28a789180b28dfd189b8068e42cf9addc6e79515379a084dff0ca5d92fbce81f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files

0.0.1

2 release files

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