I started a newsletter!
You can read more about it here.

Nik Kantar

Wednesday, February 24, 2021

Introducing Parsenvy

I made an environment variable parsing library for Python, and forgot to tell you all about it!

I’m laughing as I type this, since this is the third post about Parsenvy I’ve written in the last six weeks. First I wrote a draft of an introduction much like this one, long overdue. Throughout the process, I started questioning the point of the library in the first place, especially as I don’t really use it much myself any more, so I wrote one wondering about its future.

I didn’t get around to finishing up either one of those before PyCascades, where a few people ended up sprinting on it (check out the sweet docs!), which got me to finish up v3. So now you get this third version.

I’m actually mildly shocked I never wrote this post, since Parsenvy has been around for a while—the first commit landed on March 16, 2017, and the first release was installable on July 31, 2017.

What is Parsenvy anyway?

It’s a Python library for reading environment variables and typecasting them into some of the built-in types—namely the “simple” ones: numerics, sequences, and mappings. The various tools in the standard library’s os module always return strings (unless a default of a different type is specified), so if you need an integer, you have to do something like this:

from os import getenv

If you omit the int() call, you’ll get a string—e.g., "7"—instead of integer—e.g., 7—from the environment, if the variable is defined. This is hardly groundbreaking, but it can read a bit clumsy if you need a lot of it.

With Parsenvy it reads a little better:

import parsenvy

But it improves if there’s a block of these:

from os import getenv
DEBUG = bool(getenv("DEBUG", False))

# versus

import parsenvy
DEBUG = parsenvy.bool("DEBUG", False)
HOSTNAME = parsenvy.str("HOSTNAME")

What does Parsenvy support?

Parsenvy supports some of the most commonly used built-in types:

Here’s the example section of the README:

>>> import parsenvy

>>> parsenvy.bool("DEBUG_ENABLED")

>>> parsenvy.int("POSTS_PER_PAGE")

>>> parsenvy.float("EXCHANGE_RATE")

# INVALID_USERNAMES=admin,superuser,user,webmaster
>>> parsenvy.list("INVALID_USERNAMES")
["admin", "superuser", "user", "webmaster"]

# SAMPLE_GREETING=Hello,world!
>>> parsenvy.tuple("SAMPLE_GREETING")
("Hello", "world!")

# ALLOWED_CATEGORIES=python,vim,git
>>> parsenvy.set("ALLOWED_CATEGORIES")
{"python", "vim", "git"}

>>> parsenvy.str("DB_PREFIX")

What about other types?

Short answer: some combination of “I haven’t needed them yet”, “couldn’t come up with good syntax”, and “just plain forgot about them”.

There’s an issue to add support for range already, and bytes, bytearray, dict, memoryview, and frozenset are under consideration in another. There’s also the possibility of adding support for shell data structures, but that’s enough of a change to warrant a new major version, and I’m in no rush.

Feedback is welcome on any of the above issues, any other ones, or even new ones. After all, it’s a community project now. ;)