env-var-1.0.1
توضیحات
simple utility to parse and validate environment variables
| ویژگی | مقدار |
|---|---|
| سیستم عامل | - |
| نام فایل | env-var-1.0.1 |
| نام | env-var |
| نسخه کتابخانه | 1.0.1 |
| نگهدارنده | [] |
| ایمیل نگهدارنده | [] |
| نویسنده | Sebastian Banaszkiewicz |
| ایمیل نویسنده | banaszkiewicz.sebastian@gmail.com |
| آدرس صفحه اصلی | https://github.com/seba-ban/env-var |
| آدرس اینترنتی | https://pypi.org/project/env-var/ |
| مجوز | MIT |
# env_var
A simple utility for working with environment variables. The main goal was to provide proper type check.
## Installation
```bash
pip install env-var
```
## Usage
```python
from env_var import env
minio_port = env('MINIO_PORT').as_port_number().default(9000).required() # port type hint is `int`
minio_host = env('MINIO_HOST').as_hostname().optional() # minio_host type hint is `str | None`
minio_secure = env('MINIO_SECURE').as_bool().required() # minio_host type hint is `bool`
```
Setting `default` will result in always returning a value, so it makes little sense to use it with `optional`.
It might be useful to gather all environment variables that are used in an application to a separate file.
```python
from enum import Enum
from env_var import env
class SomeImportantOption(Enum):
option_a = "a"
option_b = "b"
option_c = "c"
PG_HOST = env('PG_HOST').as_hostname().required()
PG_PORT = env('PG_PORT').as_port_number().default(5432).required()
PG_DB = env('PG_DB').as_string().required()
PG_USER = env('PG_USER').as_string().required()
PG_PASSWORD = env('PG_PASSWORD').as_string().required()
IMPORTANT_OPTION = env('IMPORTANT_OPTION').as_enum(SomeImportantOption).required()
```
Sometimes it might happen that some variables will be required only in specific circumstances, in such cases calling `required` can be postponed until the variable is actually needed.
```python
UPDATE_URL = env('UPDATE_URL').as_url()
"""required only when some condition is met"""
# elsewhere in the code
def send_update(status: str):
url = UPDATE_URL.required()
requests.post(url, dict(status=status))
```
It's also possible to pass custom transformers/validators.
```python
@dataclass
class MyOwnClass:
initial: str
initial_class = env("INITIAL").custom_transformer(MyOwnClass).required() # intial_class is of type MyOwnClass
```
نیازمندی
| مقدار | نام |
|---|---|
| >=0.1.4,<0.2.0 | rfc3339-validator |
| >=20.11.0,<21.0.0 | isoduration |
| >=2.8.2,<3.0.0 | python-dateutil |
| >=0.18.2,<0.19.0 | validators |
| >=0.1.1,<0.2.0 | rfc3986-validator |
زبان مورد نیاز
| مقدار | نام |
|---|---|
| >=3.8,<4.0 | Python |
نحوه نصب
نصب پکیج whl env-var-1.0.1:
pip install env-var-1.0.1.whl
نصب پکیج tar.gz env-var-1.0.1:
pip install env-var-1.0.1.tar.gz