A tool for parsing and validating env vars using cattrs
Project description
cattrs-env
cattrs-env
is an Environment Variable parser/validator which utilizes the cattrs
library.
cattrs-env
parses Environment Variables from os.environ
and structures them into a cattrs
compatible dataclass. Providing you with easy and type safe environment variables in your project.
Because cattrs-env
gets the Environment Variables from os.environ
, it is fully compatible with any Environment Variable loading library such as python-dotenv
.
from dataclasses import dataclass
from typing import List, Optional
from cattrs_env import CattrsEnv
@dataclass
class Env(CattrsEnv):
A: int
C: List[str]
B: Optional[float] = None
env = Env.load()
env.A
env.C
env.B
Examples
dataclass example
import os
from dataclasses import dataclass
from typing import List, Optional
from cattrs_env import CattrsEnv
@dataclass
class Env(CattrsEnv):
A: int
C: List[str]
B: Optional[float] = None
if __name__ == "__main__":
os.environ.update(
{
"A": "99",
"B": "9.9",
"C": '["a","b","c", 1]',
}
)
env = Env.load()
print(env)
attrs example
import os
from typing import List, Optional
import attrs
from cattrs_env import CattrsEnv
@attrs.define
class Config:
E: str
F: Optional[int] = None
@attrs.define
class Env(CattrsEnv):
A: int
C: List[str]
D: Config
B: Optional[float] = None
if __name__ == "__main__":
os.environ.update(
{
"A": "99",
"B": "9.9",
"C": '["a","b","c", 1]',
"D": """{'E':"abcdef"}""",
}
)
env = Env.load()
python-dotenv example
from dataclasses import dataclass
from typing import List, Optional
import dotenv
from cattrs_env import CattrsEnv
@dataclass
class Env(CattrsEnv):
A: int
C: List[str]
B: Optional[float] = None
ENV_FILE_CONTENTS = """
A=1
B=99.9
C="['foo', 'bar']"
"""
if __name__ == "__main__":
with open(".env", "w") as env_file:
env_file.write(ENV_FILE_CONTENTS)
dotenv.load_dotenv()
env = Env.load()
print(env)
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
cattrs-env-1.0.4.tar.gz
(4.0 kB
view details)
Built Distribution
File details
Details for the file cattrs-env-1.0.4.tar.gz
.
File metadata
- Download URL: cattrs-env-1.0.4.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 518106ae678d697e759a03db630296232e6134fc71aaa5ba6932668c5e722c3d |
|
MD5 | f5b23961f7adaca83c94d9851abf0f4e |
|
BLAKE2b-256 | c7f25cc47839a8dc804da9eebd80b14be66f2f0dfb7ee03a78d6f129066e75c6 |
File details
Details for the file cattrs_env-1.0.4-py3-none-any.whl
.
File metadata
- Download URL: cattrs_env-1.0.4-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5084d5eae27a6178fec0d7d4c3921a7232d2b3a83d3f046e1ba6917b5ccd60d3 |
|
MD5 | 7aee921f14b8838d539ea29d370cfef5 |
|
BLAKE2b-256 | 03bdeb39545f271b32a111b51725995fccdf6140e033fab0a311bcf1ced345e3 |