# 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] = None
env = Env.load()
env.A
env.C
env.B
```
# Examples
## dataclass example
```python
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
```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] = 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
```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] = 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)
```
Raw data
{
"_id": null,
"home_page": "",
"name": "cattrs-env",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "cattrs,envvar,environment variables,attrs,dataclasses",
"author": "",
"author_email": "Henry Jones <henryivesjones@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/c7/f2/5cc47839a8dc804da9eebd80b14be66f2f0dfb7ee03a78d6f129066e75c6/cattrs-env-1.0.4.tar.gz",
"platform": null,
"description": "# cattrs-env\n![Tests + Linting](https://github.com/henryivesjones/cattrs-env/actions/workflows/checks.yml/badge.svg?branch=main&event=push)\n![pypi](https://img.shields.io/pypi/v/cattrs-env)\n![License](https://img.shields.io/pypi/l/cattrs-env)\n![Downloads](https://img.shields.io/pypi/dm/cattrs-env)\n\n\n`cattrs-env` is an Environment Variable parser/validator which utilizes the [`cattrs`](https://github.com/python-attrs/cattrs) library.\n\n`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.\n\nBecause `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).\n\n```python\nfrom dataclasses import dataclass\nfrom typing import List, Optional\nfrom cattrs_env import CattrsEnv\n\n@dataclass\nclass Env(CattrsEnv):\n A: int\n C: List[str]\n B: Optional[float] = None\n\nenv = Env.load()\n\nenv.A\nenv.C\nenv.B\n```\n\n# Examples\n\n## dataclass example\n```python\nimport os\nfrom dataclasses import dataclass\nfrom typing import List, Optional\n\nfrom cattrs_env import CattrsEnv\n\n\n@dataclass\nclass Env(CattrsEnv):\n A: int\n C: List[str]\n B: Optional[float] = None\n\n\nif __name__ == \"__main__\":\n os.environ.update(\n {\n \"A\": \"99\",\n \"B\": \"9.9\",\n \"C\": '[\"a\",\"b\",\"c\", 1]',\n }\n )\n env = Env.load()\n print(env)\n```\n\n## attrs example\n```python\nimport os\nfrom typing import List, Optional\nimport attrs\nfrom cattrs_env import CattrsEnv\n\n\n@attrs.define\nclass Config:\n E: str\n F: Optional[int] = None\n\n\n@attrs.define\nclass Env(CattrsEnv):\n A: int\n C: List[str]\n D: Config\n B: Optional[float] = None\n\n\nif __name__ == \"__main__\":\n os.environ.update(\n {\n \"A\": \"99\",\n \"B\": \"9.9\",\n \"C\": '[\"a\",\"b\",\"c\", 1]',\n \"D\": \"\"\"{'E':\"abcdef\"}\"\"\",\n }\n )\n env = Env.load()\n```\n## python-dotenv example\n```python\nfrom dataclasses import dataclass\nfrom typing import List, Optional\nimport dotenv\nfrom cattrs_env import CattrsEnv\n\n\n@dataclass\nclass Env(CattrsEnv):\n A: int\n C: List[str]\n B: Optional[float] = None\n\n\nENV_FILE_CONTENTS = \"\"\"\nA=1\nB=99.9\nC=\"['foo', 'bar']\"\n\"\"\"\n\nif __name__ == \"__main__\":\n with open(\".env\", \"w\") as env_file:\n env_file.write(ENV_FILE_CONTENTS)\n dotenv.load_dotenv()\n env = Env.load()\n print(env)\n\n```\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A tool for parsing and validating env vars using cattrs",
"version": "1.0.4",
"project_urls": {
"Homepage": "https://github.com/henryivesjones/cattrs-env"
},
"split_keywords": [
"cattrs",
"envvar",
"environment variables",
"attrs",
"dataclasses"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "03bdeb39545f271b32a111b51725995fccdf6140e033fab0a311bcf1ced345e3",
"md5": "7aee921f14b8838d539ea29d370cfef5",
"sha256": "5084d5eae27a6178fec0d7d4c3921a7232d2b3a83d3f046e1ba6917b5ccd60d3"
},
"downloads": -1,
"filename": "cattrs_env-1.0.4-py3-none-any.whl",
"has_sig": false,
"md5_digest": "7aee921f14b8838d539ea29d370cfef5",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 3917,
"upload_time": "2023-08-25T15:32:54",
"upload_time_iso_8601": "2023-08-25T15:32:54.071353Z",
"url": "https://files.pythonhosted.org/packages/03/bd/eb39545f271b32a111b51725995fccdf6140e033fab0a311bcf1ced345e3/cattrs_env-1.0.4-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "c7f25cc47839a8dc804da9eebd80b14be66f2f0dfb7ee03a78d6f129066e75c6",
"md5": "f5b23961f7adaca83c94d9851abf0f4e",
"sha256": "518106ae678d697e759a03db630296232e6134fc71aaa5ba6932668c5e722c3d"
},
"downloads": -1,
"filename": "cattrs-env-1.0.4.tar.gz",
"has_sig": false,
"md5_digest": "f5b23961f7adaca83c94d9851abf0f4e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 4005,
"upload_time": "2023-08-25T15:32:55",
"upload_time_iso_8601": "2023-08-25T15:32:55.816825Z",
"url": "https://files.pythonhosted.org/packages/c7/f2/5cc47839a8dc804da9eebd80b14be66f2f0dfb7ee03a78d6f129066e75c6/cattrs-env-1.0.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-08-25 15:32:55",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "henryivesjones",
"github_project": "cattrs-env",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "cattrs-env"
}