Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/henryivesjones/cattrs-env
A tool for parsing and validating env vars using cattrs
https://github.com/henryivesjones/cattrs-env
attrs cattrs dataclasses dotenv environment-variables mit-license python validation
Last synced: 25 days ago
JSON representation
A tool for parsing and validating env vars using cattrs
- Host: GitHub
- URL: https://github.com/henryivesjones/cattrs-env
- Owner: henryivesjones
- License: mit
- Created: 2023-08-16T16:40:11.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-08-25T15:36:31.000Z (about 1 year ago)
- Last Synced: 2024-10-03T03:49:34.225Z (about 1 month ago)
- Topics: attrs, cattrs, dataclasses, dotenv, environment-variables, mit-license, python, validation
- Language: Python
- Homepage: https://pypi.org/project/cattrs-env/
- Size: 9.77 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# cattrs-env
![Tests + Linting](https://github.com/henryivesjones/cattrs-env/actions/workflows/checks.yml/badge.svg?branch=main&event=push)
![pypi](https://img.shields.io/pypi/v/cattrs-env)
![License](https://img.shields.io/pypi/l/cattrs-env)
![Downloads](https://img.shields.io/pypi/dm/cattrs-env)`cattrs-env` is an Environment Variable parser/validator which utilizes the [`cattrs`](https://github.com/python-attrs/cattrs) library.
`cattrs-env` parses Environment Variables from `os.environ` and structures them into a [`cattrs` compatible dataclass](https://catt.rs/en/stable/structuring.html#simple-attrs-classes-and-dataclasses). 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`](https://github.com/theskumar/python-dotenv).
```python
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] = Noneenv = Env.load()
env.A
env.C
env.B
```# Examples
## dataclass example
```python
import os
from dataclasses import dataclass
from typing import List, Optionalfrom cattrs_env import CattrsEnv
@dataclass
class Env(CattrsEnv):
A: int
C: List[str]
B: Optional[float] = Noneif __name__ == "__main__":
os.environ.update(
{
"A": "99",
"B": "9.9",
"C": '["a","b","c", 1]',
}
)
env = Env.load()
print(env)
```## attrs example
```python
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] = Noneif __name__ == "__main__":
os.environ.update(
{
"A": "99",
"B": "9.9",
"C": '["a","b","c", 1]',
"D": """{'E':"abcdef"}""",
}
)
env = Env.load()
```
## python-dotenv example
```python
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] = NoneENV_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)```