# Classy Env
![Tests](https://github.com/mateusz-meksula/classy-env/actions/workflows/tests.yaml/badge.svg) [![](https://img.shields.io/badge/python-3.11+-blue.svg)](https://www.python.org/downloads/) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
## About
Classy Env is a lightweight Python package for managing environment variables in an OOP way.
## Requirements
This package requires Python 3.11 or higher.
## Installation
Classy Env is available on PyPI and can be installed by running the following command:
```shell
pip install classy-env
```
## Usage
Create a `ClassyEnv` subclass and declare its attributes using the `EnvVar` function:
```python
from classyenv import ClassyEnv, EnvVar
class Settings(ClassyEnv):
database_user = EnvVar("DB_USER")
database_password = EnvVar("DB_PASSWORD")
api_secret_key = EnvVar("SECRET_KEY")
```
The `EnvVar` function takes the name of the environment variable as an argument.
The Value of that variable will be assigned to the corresponding attribute.
Then, create an object of your subclass and access the environment variables
through the object's attributes:
```python
settings = Settings()
database_connection = connect(
user=settings.database_user,
password=settings.database_password,
)
```
At the object's creation, Classy Env will check if the environment variables
provided to the `EnvVar` functions are defined. If not, an exception will be raised.
### Runtime validation
> added in version 1.1.0
The validation mentioned above can also be triggered at class creation using the `runtime_check` class argument:
```python
from classyenv import ClassyEnv, EnvVar
class Settings(ClassyEnv, runtime_check=True):
database_user = EnvVar("DB_USER")
database_password = EnvVar("DB_PASSWORD")
api_secret_key = EnvVar("SECRET_KEY")
```
### Converters
> added in version 1.2.0
The `EnvVar` function accepts an optional `converter` argument.
Converter must be a callable that accepts a string value.
If provided, converter will be called with the environment variable value when the attribute is being accessed, and the returned by converter value will be returned as attribute value:
```python
from classyenv import ClassyEnv, EnvVar
class Settings(ClassyEnv):
database_port: int = EnvVar("DB_PORT", converter=int)
settings = Settings()
assert isinstance(settings.database_port, int)
```
### Defaults
> added in version 1.3.0
The `EnvVar` function accepts an optional `default` argument, which allows for specifying a default value, when corresponding environment variable is not defined:
```python
import os
from classyenv import ClassyEnv, EnvVar
if "DB_PORT" in os.environ:
os.environ.pop("DB_PORT")
class Settings(ClassyEnv):
database_port = EnvVar("DB_PORT", default=3306)
settings = Settings()
assert settings.database_port == 3306
```
### Mutating the `ClassyEnv` instances and subclasses
At this moment, mutating instances of the `ClassyEnv` class is not supported:
```python
settings.database_user = "Roe_Jogan123" # this will raise an exception
```
Similarly, mutating the class attributes of the `ClassyEnv` subclasses is not supported:
```python
Settings.database_user = "Roe_Jogan123" # this will raise an exception
```
## License
This project is licensed under the terms of the MIT license.
Raw data
{
"_id": null,
"home_page": "https://github.com/mateusz-meksula/classy-env/",
"name": "classy-env",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.11",
"maintainer_email": null,
"keywords": null,
"author": "Mateusz Meksu\u0142a",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/6c/cd/1aa0020ff8c85108c505bfb0522402e91d67c03398b1349465295a6d1121/classy_env-1.3.1.tar.gz",
"platform": null,
"description": "# Classy Env\n\n![Tests](https://github.com/mateusz-meksula/classy-env/actions/workflows/tests.yaml/badge.svg) [![](https://img.shields.io/badge/python-3.11+-blue.svg)](https://www.python.org/downloads/) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\n## About\n\nClassy Env is a lightweight Python package for managing environment variables in an OOP way.\n\n## Requirements\n\nThis package requires Python 3.11 or higher.\n\n## Installation\n\nClassy Env is available on PyPI and can be installed by running the following command:\n\n```shell\npip install classy-env\n```\n\n## Usage\n\nCreate a `ClassyEnv` subclass and declare its attributes using the `EnvVar` function:\n\n```python\nfrom classyenv import ClassyEnv, EnvVar\n\nclass Settings(ClassyEnv):\n database_user = EnvVar(\"DB_USER\")\n database_password = EnvVar(\"DB_PASSWORD\")\n api_secret_key = EnvVar(\"SECRET_KEY\")\n```\n\nThe `EnvVar` function takes the name of the environment variable as an argument.\nThe Value of that variable will be assigned to the corresponding attribute.\n\nThen, create an object of your subclass and access the environment variables\nthrough the object's attributes:\n\n```python\nsettings = Settings()\n\ndatabase_connection = connect(\n user=settings.database_user,\n password=settings.database_password,\n)\n```\n\nAt the object's creation, Classy Env will check if the environment variables\nprovided to the `EnvVar` functions are defined. If not, an exception will be raised.\n\n### Runtime validation\n\n> added in version 1.1.0\n\nThe validation mentioned above can also be triggered at class creation using the `runtime_check` class argument:\n\n```python\nfrom classyenv import ClassyEnv, EnvVar\n\nclass Settings(ClassyEnv, runtime_check=True):\n database_user = EnvVar(\"DB_USER\")\n database_password = EnvVar(\"DB_PASSWORD\")\n api_secret_key = EnvVar(\"SECRET_KEY\")\n```\n\n### Converters\n\n> added in version 1.2.0\n\nThe `EnvVar` function accepts an optional `converter` argument.\nConverter must be a callable that accepts a string value.\n\nIf provided, converter will be called with the environment variable value when the attribute is being accessed, and the returned by converter value will be returned as attribute value:\n\n```python\nfrom classyenv import ClassyEnv, EnvVar\n\nclass Settings(ClassyEnv):\n database_port: int = EnvVar(\"DB_PORT\", converter=int)\n\n\nsettings = Settings()\nassert isinstance(settings.database_port, int)\n```\n\n### Defaults\n\n> added in version 1.3.0\n\nThe `EnvVar` function accepts an optional `default` argument, which allows for specifying a default value, when corresponding environment variable is not defined:\n\n```python\nimport os\nfrom classyenv import ClassyEnv, EnvVar\n\nif \"DB_PORT\" in os.environ:\n os.environ.pop(\"DB_PORT\")\n\nclass Settings(ClassyEnv):\n database_port = EnvVar(\"DB_PORT\", default=3306)\n\n\nsettings = Settings()\nassert settings.database_port == 3306\n```\n\n### Mutating the `ClassyEnv` instances and subclasses\n\nAt this moment, mutating instances of the `ClassyEnv` class is not supported:\n\n```python\nsettings.database_user = \"Roe_Jogan123\" # this will raise an exception\n```\n\nSimilarly, mutating the class attributes of the `ClassyEnv` subclasses is not supported:\n\n```python\nSettings.database_user = \"Roe_Jogan123\" # this will raise an exception\n```\n\n## License\n\nThis project is licensed under the terms of the MIT license.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Classy Env is a lightweight python package for managing environment variables in OOP way.",
"version": "1.3.1",
"project_urls": {
"Homepage": "https://github.com/mateusz-meksula/classy-env/"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "d73f0d2acfd4a588b1ff8094fb50f0c0b5b459c62df119e3c5737134cf5c8686",
"md5": "5c74751503e647b5e71e733fcbe3325a",
"sha256": "8d517be7e6932f9f334879522a69f64329274783d193dab43947771658d5a66d"
},
"downloads": -1,
"filename": "classy_env-1.3.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "5c74751503e647b5e71e733fcbe3325a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.11",
"size": 6159,
"upload_time": "2024-07-21T05:45:08",
"upload_time_iso_8601": "2024-07-21T05:45:08.292737Z",
"url": "https://files.pythonhosted.org/packages/d7/3f/0d2acfd4a588b1ff8094fb50f0c0b5b459c62df119e3c5737134cf5c8686/classy_env-1.3.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "6ccd1aa0020ff8c85108c505bfb0522402e91d67c03398b1349465295a6d1121",
"md5": "74eadba9578d8d7dfe8380813af0b882",
"sha256": "1d4afa8227c915b7acad2de0851c00d4cee466c72342927b67236bca5977cd29"
},
"downloads": -1,
"filename": "classy_env-1.3.1.tar.gz",
"has_sig": false,
"md5_digest": "74eadba9578d8d7dfe8380813af0b882",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.11",
"size": 6652,
"upload_time": "2024-07-21T05:45:12",
"upload_time_iso_8601": "2024-07-21T05:45:12.201739Z",
"url": "https://files.pythonhosted.org/packages/6c/cd/1aa0020ff8c85108c505bfb0522402e91d67c03398b1349465295a6d1121/classy_env-1.3.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-21 05:45:12",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "mateusz-meksula",
"github_project": "classy-env",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "classy-env"
}