Skip to main content

Environment variables parser with types!

Project description

EnvReader

Environment variables parser with types! Yes!

Every time when you make new service you need some class to receive, validate and store environment variables.

With this package it’ll be easy and funny.

Just make a class with typed fields and... that’s it.

Requirements

Python 3.7 and above. There's no additional dependencies.

Installation

pip install envreader

Simple usage

from envreader import EnvReader


class MyEnv(EnvReader):
    PATH: str
    LIST: list
    NONE_EXIST: int = 1234  # Variable with default value


e = MyEnv()

print(e.PATH)
# /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

print(e.LIST)
# [1, 2, 3, 4]

print(e.NONE_EXIST)
# 1234

Use with transform functions

I don’t want to make a giant validation library like wonderful Pydantic. Thus EnvReader supports only simple types( bool, str, int, float, list, tuple and dict) by default. This is enough in most cases.

Transform functions allows using EnvReader for more complex cases.

from typing import List
from envreader import EnvReader, Field


class MyEnv(EnvReader):
    PATH: List[str] = Field(transform=lambda x: x.split(":"))


e = MyEnv()

print(e.PATH)
# ['/usr/local/bin', '/usr/bin', '/bin', '/usr/sbin', '/sbin']

Using static methods as a transform functions.

You may store all your helper functions inside the same class. But don’t forget to add @staticmethod decorator.

from typing import List
from envreader import EnvReader, Field


class MyEnv(EnvReader):
    @staticmethod
    def trans(x: str) -> List[str]:
        return x.split(':')

    PATH: List[str] = Field(transform=trans)


e = MyEnv()

print(e.PATH)
# ['/usr/local/bin', '/usr/bin', '/bin', '/usr/sbin', '/sbin']

Make your environment variables self-documented.

Documentation is in great demand for all good applications, right?

from envreader import EnvReader, Field


class MyEnv(EnvReader):
    PATH: str = Field("/sbin", description="Application path", example="/usr/bin:/bin:/usr/sbin:/sbin")


e = MyEnv()

print(e.help())
# PATH
#     Application path
#     Example: /usr/bin:/bin:/usr/sbin:/sbin
#     Default: /sbin

Error handling? Easy.

import sys
from envreader import EnvReader, EnvMissingError


class MyEnv(EnvReader):
    SOME_VAR: str


try:
    e = MyEnv()
except EnvMissingError as e:
    print(f"Missing required env var {e.field}")
    sys.exit(-1)
# Missing required env var SOME_VAR

Enjoy!

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

envreader-1.0.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

envreader-1.0.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file envreader-1.0.0.tar.gz.

File metadata

  • Download URL: envreader-1.0.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for envreader-1.0.0.tar.gz
Algorithm Hash digest
SHA256 dcf88657f906d167ebe3ee6b0adee8ec8139ad9aa1cdb98a55c052a96ee56374
MD5 bdae9f77c46f6f88615e790fabad86dc
BLAKE2b-256 21e0ff9777630b696179d8c0371e69524ae4bf74136166bced09ef24006fe6ac

See more details on using hashes here.

File details

Details for the file envreader-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: envreader-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for envreader-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3b2d57448bfb669a183344950b6f7bb7847fac62776e6ee93429bdfff8116af4
MD5 9ecd13d2e9bcefe22c070d85f57a223a
BLAKE2b-256 9d0855080a4dceed8a5e59724908f5b53a4020a5622ec43d216e20c88cf95cad

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page