Giter Site home page Giter Site logo

exenenv's Introduction

ExenENV

Environment variables verifier and type converter.

Installation

Library is available for installation from PyPI

$ pip install exenenv

Basic Usage

import os
from exenenv import EnvironmentProfile

os.environ["REQUIRED_VARIABLE"] = "20"  # assume it's set to this


class Environment(EnvironmentProfile):
    REQUIRED_VARIABLE: int
    DEFAULT_VALUE_VARIABLE: float = 30.0


env = Environment()
env.load()

print(f"{env.REQUIRED_VARIABLE=}\n{env.DEFAULT_VALUE_VARIABLE=}")
env.REQUIRED_VARIABLE=20
env.DEFAULT_VALUE_VARIABLE=30.0

Using EnvVars

import os
from exenenv import EnvironmentProfile, EnvVar

os.environ.update({
    "REQUIRED_VAR": "10",
    "ALT_NAME_VAR": "40",
    "CONVERTER_VAR": "gamer,coder,python"
})  # assume our environment is this


class Environment(EnvironmentProfile):
    REQUIRED_VAR: int
    DEFAULT_VALUE_VAR: str = EnvVar(default=20)
    OTHER_VAR: int = EnvVar(env_name="ALT_NAME_VAR")
    CONVERTER_VAR: list[str] = EnvVar(converter=lambda x: x.split(","))


env = Environment()
env.load()

print(f"""\
{env.REQUIRED_VAR=}
{env.DEFAULT_VALUE_VAR=}
{env.OTHER_VAR=}
{env.CONVERTER_VAR=}
""")
env.REQUIRED_VAR=10
env.DEFAULT_VALUE_VAR=20
env.OTHER_VAR=40
env.CONVERTER_VAR=['gamer', 'coder', 'python']

Union Typehints

Since v1.2, library supports converting to one of provided types.

import os
from exenenv import EnvironmentProfile

os.environ.update({
    "UNION_VAR": "union"
})


class Environment(EnvironmentProfile):
    UNION_VAR: int | str
    OPTIONAL_VAR: float | None = None


env = Environment()
env.load()

print(f"{env.UNION_VAR=}\n{env.OPTIONAL_VAR=}")
env.UNION_VAR='union'
env.OPTIONAL_VAR=None

In this case, converting to UNION_VAR to int, so library used next provided type. Default value for OPTIONAL_VAR still has to be declared explicitly.

exenenv's People

Contributors

exenifix avatar

Stargazers

 avatar

Watchers

 avatar

exenenv's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.